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

parser: fix left shift precedence

This commit is contained in:
joe-conigliaro 2020-04-15 01:41:14 +10:00
parent 2b3204603a
commit b33a9fc9e7
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1

View File

@ -38,6 +38,7 @@ mut:
is_amp bool is_amp bool
returns bool returns bool
inside_match_case bool // to separate `match_expr { }` from `Struct{}` inside_match_case bool // to separate `match_expr { }` from `Struct{}`
is_stmt_ident bool // true while the beginning of a statement is an ident/selector
} }
// for tests // for tests
@ -355,6 +356,7 @@ pub fn (p mut Parser) comment() ast.Comment {
} }
pub fn (p mut Parser) stmt() ast.Stmt { pub fn (p mut Parser) stmt() ast.Stmt {
p.is_stmt_ident = p.tok.kind == .name
match p.tok.kind { match p.tok.kind {
.lcbr { .lcbr {
stmts := p.parse_block() stmts := p.parse_block()
@ -727,6 +729,8 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
// println('\n\nparser.expr()') // println('\n\nparser.expr()')
mut typ := table.void_type mut typ := table.void_type
mut node := ast.Expr{} mut node := ast.Expr{}
is_stmt_ident := p.is_stmt_ident
p.is_stmt_ident = false
// defer { // defer {
// if p.tok.kind == .comment { // if p.tok.kind == .comment {
// p.comment() // p.comment()
@ -736,6 +740,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
match p.tok.kind { match p.tok.kind {
.name { .name {
node = p.name_expr() node = p.name_expr()
p.is_stmt_ident = is_stmt_ident
} }
.string { .string {
node = p.string_expr() node = p.string_expr()
@ -844,6 +849,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
node = p.assign_expr(node) node = p.assign_expr(node)
} else if p.tok.kind == .dot { } else if p.tok.kind == .dot {
node = p.dot_expr(node) node = p.dot_expr(node)
p.is_stmt_ident = is_stmt_ident
} else if p.tok.kind == .lsbr { } else if p.tok.kind == .lsbr {
node = p.index_expr(node) node = p.index_expr(node)
} else if p.tok.kind == .key_as { } else if p.tok.kind == .key_as {
@ -855,10 +861,8 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
typ: typ typ: typ
pos: pos pos: pos
} }
} else if p.tok.kind == .left_shift { } else if p.tok.kind == .left_shift && p.is_stmt_ident {
// TODO: handle in later stages since this // arr << elem
// will fudge left shift as it makes it right assoc
// `arr << 'a'` | `arr << 'a' + 'b'`
tok := p.tok tok := p.tok
pos := tok.position() pos := tok.position()
p.next() p.next()