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() vexe := pref.vexe_path()
vroot := os.dir(vexe) vroot := os.dir(vexe)
set_vroot_folder(vroot) 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_exe := path_of_executable(os.real_path('$vroot/cmd/tools/$tool_name'))
tool_source := os.real_path('$vroot/cmd/tools/${tool_name}.v') tool_source := os.real_path('$vroot/cmd/tools/${tool_name}.v')
tool_command := '"$tool_exe" $tool_args' 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 vexe : $vroot')
eprintln('launch_tool vroot : $vroot') eprintln('launch_tool vroot : $vroot')
eprintln('launch_tool tool_args : $tool_args') eprintln('launch_tool tool_args : $tool_args')
@ -57,7 +57,7 @@ fn launch_tool(verbosity pref.VerboseLevel, tool_name string) {
should_compile = true should_compile = true
} }
} }
if verbosity.is_higher_or_equal(.level_two) { if is_verbose {
eprintln('launch_tool should_compile: $should_compile') 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 += '-d vfmt '
} }
compilation_command += '"$tool_source"' compilation_command += '"$tool_source"'
if verbosity.is_higher_or_equal(.level_three) { if is_verbose {
eprintln('Compiling $tool_name with: "$compilation_command"') eprintln('Compiling $tool_name with: "$compilation_command"')
} }
tool_compilation := os.exec(compilation_command) or { panic(err) } 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) 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 ...') eprintln('launch_tool running tool command: $tool_command ...')
} }

View File

@ -8,6 +8,7 @@ import (
internal.flag internal.flag
internal.help internal.help
os os
os.cmdline
v.table v.table
v.doc v.doc
v.pref v.pref
@ -25,52 +26,48 @@ const (
) )
fn main() { 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{} prefs := flag.MainCmdPreferences{}
values := flag.parse_main_cmd(os.args, parse_flags, prefs) or { values := flag.parse_main_cmd(os.args, parse_flags, prefs) or {
println('V Error: An error has occurred while parsing flags: ') println('V Error: An error has occurred while parsing flags: ')
println(err) println(err)
exit(1) exit(1)
} }
if prefs.verbosity.is_higher_or_equal(.level_two) { if prefs2.is_verbose {
println(util.full_v_version()) println(util.full_v_version())
} }
if prefs.verbosity.is_higher_or_equal(.level_three) { if prefs2.is_verbose {
println('Parsed preferences: ') println('Parsed preferences: ')
//println(prefs) // QTODO //println(prefs) // QTODO
println('Remaining: $values') 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 // Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below. // 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 { if values.len == 0 && prefs.action == .help {
invoke_help_and_exit(values) invoke_help_and_exit(values)
} }
if values.len == 0 || values[0] == '-' || values[0] == 'repl' { command := if values.len > 0 { values[0] } else { '' }
// 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]
if command in simple_cmd { if command in simple_cmd {
// External tools // External tools
launch_tool(prefs.verbosity, 'v' + command) launch_tool(prefs2.is_verbose, 'v' + command)
return return
} }
match command { match command {
'create', 'init' { 'create', 'init' {
launch_tool(prefs.verbosity, 'vcreate') launch_tool(prefs2.is_verbose, 'vcreate')
return return
} }
'translate' { 'translate' {
@ -78,7 +75,7 @@ fn main() {
return return
} }
'search', 'install', 'update', 'remove' { 'search', 'install', 'update', 'remove' {
launch_tool(prefs.verbosity, 'vpm') launch_tool(prefs2.is_verbose, 'vpm')
return return
} }
'get' { 'get' {
@ -105,11 +102,6 @@ fn main() {
invoke_help_and_exit(values) invoke_help_and_exit(values)
return return
} }
'version' {
disallow_unknown_flags(prefs)
print_version_and_exit()
return
}
else {} else {}
} }
if command == 'run' || command == 'build' || command.ends_with('.v') || os.exists(command) { if command == 'run' || command == 'build' || command.ends_with('.v') || os.exists(command) {
@ -121,9 +113,16 @@ fn main() {
exit(1) exit(1)
} }
fn print_version_and_exit() { fn parse_args(args []string) &pref.Preferences{
println(util.full_v_version()) mut res := &pref.Preferences{}
exit(0) 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) { 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) { pub fn (c mut Checker) error(s string, pos token.Position) {
c.nr_errors++ c.nr_errors++
//if c.pref.is_verbose {
print_backtrace() print_backtrace()
//}
mut path := c.file.path mut path := c.file.path
// Get relative path // Get relative path
workdir := os.getwd() + os.path_separator workdir := os.getwd() + os.path_separator

View File

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

View File

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