parser: use .next instead of .check, when a token is already known

pull/4765/head
yuyi 2020-05-07 12:51:36 +08:00 committed by GitHub
parent 59aa31cee5
commit 2f52106253
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 33 additions and 33 deletions

View File

@ -83,7 +83,7 @@ fn (mut p Parser) parse_assign_lhs() []ast.Ident {
} }
is_static := p.tok.kind == .key_static is_static := p.tok.kind == .key_static
if is_static { if is_static {
p.check(.key_static) p.next()
} }
mut ident := p.parse_ident(false, false) mut ident := p.parse_ident(false, false)
ident.is_mut = is_mut ident.is_mut = is_mut
@ -93,7 +93,7 @@ fn (mut p Parser) parse_assign_lhs() []ast.Ident {
} }
idents << ident idents << ident
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
} else { } else {
break break
} }
@ -108,7 +108,7 @@ fn (mut p Parser) parse_assign_rhs() []ast.Expr {
expr := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
} else { } else {
break break
} }

View File

@ -113,7 +113,7 @@ fn (mut p Parser) comp_if() ast.CompIf {
} }
if p.tok.kind == .dollar && p.peek_tok.kind == .key_else { if p.tok.kind == .dollar && p.peek_tok.kind == .key_else {
p.next() p.next()
p.check(.key_else) p.next()
node.has_else = true node.has_else = true
node.else_stmts = p.parse_block() node.else_stmts = p.parse_block()
} }

View File

@ -21,7 +21,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
if p.tok.kind == .rsbr { if p.tok.kind == .rsbr {
// []typ => `[]` and `typ` must be on the same line // []typ => `[]` and `typ` must be on the same line
line_nr := p.tok.line_nr line_nr := p.tok.line_nr
p.check(.rsbr) p.next()
// []string // []string
if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr { if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr {
elem_type = p.parse_type() elem_type = p.parse_type()
@ -37,7 +37,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
expr := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
} }
// p.check_comment() // p.check_comment()
} }

View File

@ -75,7 +75,7 @@ pub fn (mut p Parser) call_args() []ast.CallArg {
for p.tok.kind != .rpar { for p.tok.kind != .rpar {
mut is_mut := false mut is_mut := false
if p.tok.kind == .key_mut { if p.tok.kind == .key_mut {
p.check(.key_mut) p.next()
is_mut = true is_mut = true
} }
e := p.expr(0) e := p.expr(0)
@ -335,10 +335,10 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool) {
arg_name := 'arg_$arg_no' arg_name := 'arg_$arg_no'
is_mut := p.tok.kind == .key_mut is_mut := p.tok.kind == .key_mut
if is_mut { if is_mut {
p.check(.key_mut) p.next()
} }
if p.tok.kind == .ellipsis { if p.tok.kind == .ellipsis {
p.check(.ellipsis) p.next()
is_variadic = true is_variadic = true
} }
mut arg_type := p.parse_type() mut arg_type := p.parse_type()
@ -367,7 +367,7 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool) {
mut arg_names := [p.check_name()] mut arg_names := [p.check_name()]
// `a, b, c int` // `a, b, c int`
for p.tok.kind == .comma { for p.tok.kind == .comma {
p.check(.comma) p.next()
arg_names << p.check_name() arg_names << p.check_name()
} }
if p.tok.kind == .key_mut { if p.tok.kind == .key_mut {
@ -375,7 +375,7 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool) {
is_mut = true is_mut = true
} }
if p.tok.kind == .ellipsis { if p.tok.kind == .ellipsis {
p.check(.ellipsis) p.next()
is_variadic = true is_variadic = true
} }
mut typ := p.parse_type() mut typ := p.parse_type()

View File

@ -71,7 +71,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
mut key_var_name := '' mut key_var_name := ''
mut val_var_name := p.check_name() mut val_var_name := p.check_name()
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
key_var_pos := val_var_pos key_var_pos := val_var_pos
val_var_pos = p.tok.position() val_var_pos = p.tok.position()
key_var_name = val_var_name key_var_name = val_var_name
@ -103,7 +103,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
mut is_range := false mut is_range := false
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
is_range = true is_range = true
p.check(.dotdot) p.next()
high_expr = p.expr(0) high_expr = p.expr(0)
p.scope.register(val_var_name, ast.Var{ p.scope.register(val_var_name, ast.Var{
name: val_var_name name: val_var_name

View File

@ -16,7 +16,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
start_pos := p.tok.position() start_pos := p.tok.position()
mut comment := ast.Comment{} mut comment := ast.Comment{}
if p.tok.kind == .key_if { if p.tok.kind == .key_if {
p.check(.key_if) p.next()
} else { } else {
// if p.tok.kind == .comment { // if p.tok.kind == .comment {
// p.error('place comments inside {}') // p.error('place comments inside {}')
@ -24,7 +24,7 @@ fn (mut p Parser) if_expr() ast.IfExpr {
// comment = p.check_comment() // comment = p.check_comment()
p.check(.key_else) p.check(.key_else)
if p.tok.kind == .key_if { if p.tok.kind == .key_if {
p.check(.key_if) p.next()
} else { } else {
has_else = true has_else = true
p.inside_if = false p.inside_if = false

View File

@ -21,7 +21,7 @@ pub fn (mut p Parser) parse_array_type() table.Type {
elem_type := p.parse_type() elem_type := p.parse_type()
mut nr_dims := 1 mut nr_dims := 1
for p.tok.kind == .lsbr { for p.tok.kind == .lsbr {
p.check(.lsbr) p.next()
p.check(.rsbr) p.check(.rsbr)
nr_dims++ nr_dims++
} }
@ -54,7 +54,7 @@ pub fn (mut p Parser) parse_multi_return_type() table.Type {
mr_type := p.parse_type() mr_type := p.parse_type()
mr_types << mr_type mr_types << mr_type
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
} else { } else {
break break
} }

View File

@ -464,7 +464,7 @@ pub fn (mut p Parser) stmt() ast.Stmt {
} else if p.tok.kind == .name && p.peek_tok.kind == .colon { } else if p.tok.kind == .name && p.peek_tok.kind == .colon {
// `label:` // `label:`
name := p.check_name() name := p.check_name()
p.check(.colon) p.next()
return ast.GotoLabel{ return ast.GotoLabel{
name: name name: name
} }
@ -659,7 +659,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
expr = p.expr(0) expr = p.expr(0)
// TODO, string(b, len) // TODO, string(b, len)
if p.tok.kind == .comma && to_typ.idx() == table.string_type_idx { if p.tok.kind == .comma && to_typ.idx() == table.string_type_idx {
p.check(.comma) p.next()
arg = p.expr(0) // len arg = p.expr(0) // len
has_arg = true has_arg = true
} }
@ -735,7 +735,7 @@ fn (mut p Parser) index_expr(left ast.Expr) ast.IndexExpr {
mut has_high := false mut has_high := false
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
// [start..end] or [start..] // [start..end] or [start..]
p.check(.dotdot) p.next()
mut high := ast.Expr{} mut high := ast.Expr{}
if p.tok.kind != .rsbr { if p.tok.kind != .rsbr {
has_high = true has_high = true
@ -890,7 +890,7 @@ fn (mut p Parser) string_expr() ast.Expr {
if p.tok.kind != .str_dollar { if p.tok.kind != .str_dollar {
continue continue
} }
p.check(.str_dollar) p.next()
exprs << p.expr(0) exprs << p.expr(0)
mut efmt := []string{} mut efmt := []string{}
if p.tok.kind == .colon { if p.tok.kind == .colon {
@ -945,7 +945,7 @@ fn (mut p Parser) module_decl() ast.Module {
mut name := 'main' mut name := 'main'
is_skipped := p.tok.kind != .key_module is_skipped := p.tok.kind != .key_module
if !is_skipped { if !is_skipped {
p.check(.key_module) p.next()
name = p.check_name() name = p.check_name()
} }
full_mod := p.table.qualify_module(name, p.file_name) full_mod := p.table.qualify_module(name, p.file_name)
@ -966,13 +966,13 @@ fn (mut p Parser) import_stmt() ast.Import {
mut mod_name := p.check_name() mut mod_name := p.check_name()
mut mod_alias := mod_name mut mod_alias := mod_name
for p.tok.kind == .dot { for p.tok.kind == .dot {
p.check(.dot) p.next()
submod_name := p.check_name() submod_name := p.check_name()
mod_name += '.' + submod_name mod_name += '.' + submod_name
mod_alias = submod_name mod_alias = submod_name
} }
if p.tok.kind == .key_as { if p.tok.kind == .key_as {
p.check(.key_as) p.next()
mod_alias = p.check_name() mod_alias = p.check_name()
} }
p.imports[mod_alias] = mod_name p.imports[mod_alias] = mod_name
@ -1039,7 +1039,7 @@ fn (mut p Parser) return_stmt() ast.Return {
expr := p.expr(0) expr := p.expr(0)
exprs << expr exprs << expr
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
} else { } else {
break break
} }
@ -1171,7 +1171,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
} }
first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y` first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y`
if p.tok.kind == .pipe { if p.tok.kind == .pipe {
p.check(.pipe) p.next()
sum_variants << first_type sum_variants << first_type
// type SumType = A | B | c // type SumType = A | B | c
for { for {
@ -1235,7 +1235,7 @@ fn (mut p Parser) assoc() ast.Assoc {
expr := p.expr(0) expr := p.expr(0)
vals << expr vals << expr
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
} }
if p.tok.kind == .rcbr { if p.tok.kind == .rcbr {
break break

View File

@ -16,7 +16,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
} }
is_union := p.tok.kind == .key_union is_union := p.tok.kind == .key_union
if p.tok.kind == .key_struct { if p.tok.kind == .key_struct {
p.check(.key_struct) p.next()
} else { } else {
p.check(.key_union) p.check(.key_union)
} }
@ -51,12 +51,12 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
comment = p.comment() comment = p.comment()
} }
if p.tok.kind == .key_pub { if p.tok.kind == .key_pub {
p.check(.key_pub) p.next()
if p.tok.kind == .key_mut { if p.tok.kind == .key_mut {
if pub_mut_pos != -1 { if pub_mut_pos != -1 {
p.error('redefinition of `pub mut` section') p.error('redefinition of `pub mut` section')
} }
p.check(.key_mut) p.next()
pub_mut_pos = fields.len pub_mut_pos = fields.len
is_field_pub = true is_field_pub = true
is_field_mut = true is_field_mut = true
@ -75,7 +75,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
if mut_pos != -1 { if mut_pos != -1 {
p.error('redefinition of `mut` section') p.error('redefinition of `mut` section')
} }
p.check(.key_mut) p.next()
p.check(.colon) p.check(.colon)
mut_pos = fields.len mut_pos = fields.len
is_field_pub = false is_field_pub = false
@ -85,7 +85,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
if global_pos != -1 { if global_pos != -1 {
p.error('redefinition of `global` section') p.error('redefinition of `global` section')
} }
p.check(.key_global) p.next()
p.check(.colon) p.check(.colon)
global_pos = fields.len global_pos = fields.len
is_field_pub = true is_field_pub = true
@ -234,7 +234,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
} }
i++ i++
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.next()
} }
p.check_comment() p.check_comment()
} }