update match statement

pull/2504/head
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

@ -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 ] == `\n` { s.pos ++ return }
if s.text[ s.pos ] == `\r` { 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 continue
} }
match tok.tok { match tok.tok {
TokenKind.key_fn => { generate_fn(out, p.tokens, i) } TokenKind.key_fn { generate_fn(out, p.tokens, i) }
TokenKind.key_const => { generate_const(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) p.check(.colon)
mut val := '' mut val := ''
match p.tok { match p.tok {
.name => { val = p.check_name() } .name { val = p.check_name() }
.str => { val = p.check_string() } .str { val = p.check_string() }
else => { else {
p.error('attribute value should be either name or string') 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 i := 0
mut last_st_typ := '' mut last_st_typ := ''
for p.tok != .rcbr && p.tok != .eof && p.tok != .key_case && 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()) // println('stm: '+p.tok.str()+', next: '+p.peek().str())
last_st_typ = p.statement(true) last_st_typ = p.statement(true)
// println('last st typ=$last_st_typ') // 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 { if p.tok == .key_else {
p.check(.key_else) p.check(.key_else)
if p.tok == .arrow { if p.tok == .arrow {
/* p.warn(match_arrow_warning)
p.warn('=> is no longer needed in match statements, use\n' +
'match foo {
1 { bar }
2 { baz }
else { ... }
}')
*/
p.check(.arrow) p.check(.arrow)
} }
@ -3827,7 +3819,10 @@ fn (p mut Parser) match_statement(is_expr bool) string {
} }
p.gen(')') p.gen(')')
if p.tok == .arrow {
p.warn(match_arrow_warning)
p.check(.arrow) p.check(.arrow)
}
// statements are dissallowed (if match is expression) so user cant declare variables there and so on // statements are dissallowed (if match is expression) so user cant declare variables there and so on
if is_expr { if is_expr {