1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

update match statement

This commit is contained in:
Alexander Medvednikov 2019-10-23 00:56:25 +03:00
parent 4574039e4d
commit 9743b04fe6
3 changed files with 21 additions and 17 deletions

View File

@ -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 { ... }
}'
)

View File

@ -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) }
}
}
}

View File

@ -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 {