From 9743b04fe65cefee6236aeae1d76196f1b99829a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 23 Oct 2019 00:56:25 +0300 Subject: [PATCH] update match statement --- vlib/compiler/compile_errors.v | 11 ++++++++++- vlib/compiler/module_header.v | 4 ++-- vlib/compiler/parser.v | 23 +++++++++-------------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/vlib/compiler/compile_errors.v b/vlib/compiler/compile_errors.v index 51fe9552e4..9c1ce53702 100644 --- a/vlib/compiler/compile_errors.v +++ b/vlib/compiler/compile_errors.v @@ -135,7 +135,7 @@ fn (s &Scanner) get_error_filepath() string { if s.should_print_relative_paths_on_error { workdir := os.getwd() + os.path_separator if s.file_path.starts_with(workdir) { - return s.file_path.replace( workdir, '') + return s.file_path.replace( workdir, '') } return s.file_path } @@ -269,3 +269,12 @@ fn (s mut Scanner) eat_single_newline(){ if s.text[ s.pos ] == `\n` { s.pos ++ return } if s.text[ s.pos ] == `\r` { s.pos ++ return } } + +const ( + match_arrow_warning = '=> is no longer needed in match statements, use\n' + +'match foo { + 1 { bar } + 2 { baz } + else { ... } +}' +) diff --git a/vlib/compiler/module_header.v b/vlib/compiler/module_header.v index f68896dd2b..e592579298 100644 --- a/vlib/compiler/module_header.v +++ b/vlib/compiler/module_header.v @@ -151,8 +151,8 @@ fn generate_vh(mod string) { continue } match tok.tok { - TokenKind.key_fn => { generate_fn(out, p.tokens, i) } - TokenKind.key_const => { generate_const(out, p.tokens, i) } + TokenKind.key_fn { generate_fn(out, p.tokens, i) } + TokenKind.key_const { generate_const(out, p.tokens, i) } } } } diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index c1cffb4d91..0b5a117db8 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -816,9 +816,9 @@ fn (p mut Parser) struct_decl() { p.check(.colon) mut val := '' match p.tok { - .name => { val = p.check_name() } - .str => { val = p.check_string() } - else => { + .name { val = p.check_name() } + .str { val = p.check_string() } + else { p.error('attribute value should be either name or string') } } @@ -1144,7 +1144,7 @@ fn (p mut Parser) statements_no_rcbr() string { mut i := 0 mut last_st_typ := '' for p.tok != .rcbr && p.tok != .eof && p.tok != .key_case && - p.tok != .key_default && p.peek() != .arrow { + p.tok != .key_default { // println('stm: '+p.tok.str()+', next: '+p.peek().str()) last_st_typ = p.statement(true) // println('last st typ=$last_st_typ') @@ -3691,15 +3691,7 @@ fn (p mut Parser) match_statement(is_expr bool) string { if p.tok == .key_else { p.check(.key_else) if p.tok == .arrow { - /* - p.warn('=> is no longer needed in match statements, use\n' + -'match foo { - 1 { bar } - 2 { baz } - else { ... } -}') -*/ - + p.warn(match_arrow_warning) p.check(.arrow) } @@ -3827,7 +3819,10 @@ fn (p mut Parser) match_statement(is_expr bool) string { } p.gen(')') - p.check(.arrow) + if p.tok == .arrow { + p.warn(match_arrow_warning) + p.check(.arrow) + } // statements are dissallowed (if match is expression) so user cant declare variables there and so on if is_expr {