parser: match must be exhaustive; cmd/v: one verbosity level, simpler version

pull/4202/head
Alexander Medvednikov 2020-04-02 16:51:16 +02:00
parent f087e819d7
commit 40fd924b15
5 changed files with 45 additions and 38 deletions

View File

@ -17,7 +17,7 @@ fn set_vroot_folder(vroot_path string) {
}
fn launch_tool(verbosity pref.VerboseLevel, tool_name string) {
fn launch_tool(is_verbose bool, tool_name string) {
vexe := pref.vexe_path()
vroot := os.dir(vexe)
set_vroot_folder(vroot)
@ -26,7 +26,7 @@ fn launch_tool(verbosity pref.VerboseLevel, tool_name string) {
tool_exe := path_of_executable(os.real_path('$vroot/cmd/tools/$tool_name'))
tool_source := os.real_path('$vroot/cmd/tools/${tool_name}.v')
tool_command := '"$tool_exe" $tool_args'
if verbosity.is_higher_or_equal(.level_two) {
if is_verbose {
eprintln('launch_tool vexe : $vroot')
eprintln('launch_tool vroot : $vroot')
eprintln('launch_tool tool_args : $tool_args')
@ -57,7 +57,7 @@ fn launch_tool(verbosity pref.VerboseLevel, tool_name string) {
should_compile = true
}
}
if verbosity.is_higher_or_equal(.level_two) {
if is_verbose {
eprintln('launch_tool should_compile: $should_compile')
}
@ -68,7 +68,7 @@ fn launch_tool(verbosity pref.VerboseLevel, tool_name string) {
compilation_command += '-d vfmt '
}
compilation_command += '"$tool_source"'
if verbosity.is_higher_or_equal(.level_three) {
if is_verbose {
eprintln('Compiling $tool_name with: "$compilation_command"')
}
tool_compilation := os.exec(compilation_command) or { panic(err) }
@ -76,7 +76,7 @@ fn launch_tool(verbosity pref.VerboseLevel, tool_name string) {
panic('V tool "$tool_source" could not be compiled\n' + tool_compilation.output)
}
}
if verbosity.is_higher_or_equal(.level_three) {
if is_verbose {
eprintln('launch_tool running tool command: $tool_command ...')
}

View File

@ -8,6 +8,7 @@ import (
internal.flag
internal.help
os
os.cmdline
v.table
v.doc
v.pref
@ -25,52 +26,48 @@ const (
)
fn main() {
args := os.args[1..]
//args = 123
if args.len == 0 || args[0] in ['-', 'repl'] {
// Running `./v` without args launches repl
println('For usage information, quit V REPL using `exit` and use `v help`')
launch_tool(false, 'vrepl')
return
}
if args.len > 0 && (args[0] in ['version', '-V', '-version', '--version'] || (args[0] == '-v' && args.len == 1) ) {
// `-v` flag is for setting verbosity, but without any args it prints the version, like Clang
println(util.full_v_version())
return
}
prefs2 := parse_args(args)
prefs := flag.MainCmdPreferences{}
values := flag.parse_main_cmd(os.args, parse_flags, prefs) or {
println('V Error: An error has occurred while parsing flags: ')
println(err)
exit(1)
}
if prefs.verbosity.is_higher_or_equal(.level_two) {
if prefs2.is_verbose {
println(util.full_v_version())
}
if prefs.verbosity.is_higher_or_equal(.level_three) {
if prefs2.is_verbose {
println('Parsed preferences: ')
//println(prefs) // QTODO
println('Remaining: $values')
}
// Do a quick check for `v -v`. Too much error has been made this way.
if prefs.verbosity == .level_one && values.len == 0 {
println("`v -v` now runs V with verbose mode set to level one which doesn't do anything.")
println('Did you mean `v -version` instead?')
exit(1)
}
// Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below.
if prefs.action == .version {
disallow_unknown_flags(prefs)
print_version_and_exit()
}
if values.len == 0 && prefs.action == .help {
invoke_help_and_exit(values)
}
if values.len == 0 || values[0] == '-' || values[0] == 'repl' {
// Check for REPL.
if values.len == 0 {
println('Running REPL as no arguments are provided.')
println('For usage information, quit V REPL using `exit` and use `v help`.')
}
launch_tool(prefs.verbosity, 'vrepl')
}
command := values[0]
command := if values.len > 0 { values[0] } else { '' }
if command in simple_cmd {
// External tools
launch_tool(prefs.verbosity, 'v' + command)
launch_tool(prefs2.is_verbose, 'v' + command)
return
}
match command {
'create', 'init' {
launch_tool(prefs.verbosity, 'vcreate')
launch_tool(prefs2.is_verbose, 'vcreate')
return
}
'translate' {
@ -78,7 +75,7 @@ fn main() {
return
}
'search', 'install', 'update', 'remove' {
launch_tool(prefs.verbosity, 'vpm')
launch_tool(prefs2.is_verbose, 'vpm')
return
}
'get' {
@ -105,11 +102,6 @@ fn main() {
invoke_help_and_exit(values)
return
}
'version' {
disallow_unknown_flags(prefs)
print_version_and_exit()
return
}
else {}
}
if command == 'run' || command == 'build' || command.ends_with('.v') || os.exists(command) {
@ -121,9 +113,16 @@ fn main() {
exit(1)
}
fn print_version_and_exit() {
println(util.full_v_version())
exit(0)
fn parse_args(args []string) &pref.Preferences{
mut res := &pref.Preferences{}
for i, arg in args {
match arg {
'-v' { res.is_verbose = true }
'-cg' { res.ccompiler = cmdline.option(args, '-cc', 'cc') }
else { }
}
}
return res
}
fn invoke_help_and_exit(remaining []string) {

View File

@ -1116,7 +1116,9 @@ pub fn (c mut Checker) map_init(node mut ast.MapInit) table.Type {
pub fn (c mut Checker) error(s string, pos token.Position) {
c.nr_errors++
print_backtrace()
//if c.pref.is_verbose {
print_backtrace()
//}
mut path := c.file.path
// Get relative path
workdir := os.getwd() + os.path_separator

View File

@ -1772,12 +1772,14 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
cond := p.expr(0)
p.check(.lcbr)
mut branches := []ast.MatchBranch
mut have_final_else := false
for {
mut exprs := []ast.Expr
branch_pos := p.tok.position()
p.open_scope()
// final else
if p.tok.kind == .key_else {
have_final_else = true
p.next()
}
// Sum type match
@ -1826,6 +1828,9 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
break
}
}
if !have_final_else {
p.error('match must be exhaustive')
}
p.check(.rcbr)
return ast.MatchExpr{
branches: branches

View File

@ -32,6 +32,7 @@ pub mut:
backend Backend
build_mode BuildMode
verbosity VerboseLevel
is_verbose bool
// nofmt bool // disable vfmt
is_test bool // `v test string_test.v`
is_script bool // single file mode (`v program.v`), main function can be skipped