2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-04-17 18:11:04 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module parser
|
|
|
|
|
|
|
|
import v.ast
|
2021-01-09 15:11:49 +01:00
|
|
|
import v.vet
|
2020-04-17 18:11:04 +02:00
|
|
|
import v.table
|
|
|
|
import v.token
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
pub fn (mut p Parser) expr(precedence int) ast.Expr {
|
2020-07-21 14:58:17 +02:00
|
|
|
$if trace_parser ? {
|
|
|
|
tok_pos := p.tok.position()
|
|
|
|
eprintln('parsing file: ${p.file_name:-30} | tok.kind: ${p.tok.kind:-10} | tok.lit: ${p.tok.lit:-10} | tok_pos: ${tok_pos.str():-45} | expr($precedence)')
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
// println('\n\nparser.expr()')
|
2020-04-21 05:11:50 +02:00
|
|
|
mut node := ast.Expr{}
|
2020-04-25 10:57:12 +02:00
|
|
|
is_stmt_ident := p.is_stmt_ident
|
|
|
|
p.is_stmt_ident = false
|
2020-07-17 19:13:22 +02:00
|
|
|
if !p.pref.is_fmt {
|
2021-02-08 17:16:02 +01:00
|
|
|
p.eat_comments({})
|
2020-07-17 19:13:22 +02:00
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
// Prefix
|
|
|
|
match p.tok.kind {
|
2020-07-07 01:57:31 +02:00
|
|
|
.key_mut, .key_shared, .key_atomic, .key_static {
|
2020-12-26 11:22:06 +01:00
|
|
|
node = p.parse_ident(table.Language.v)
|
2020-06-16 13:20:16 +02:00
|
|
|
p.is_stmt_ident = is_stmt_ident
|
2020-05-15 23:14:53 +02:00
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
.name {
|
2020-06-16 12:14:22 +02:00
|
|
|
if p.tok.lit == 'sql' && p.peek_tok.kind == .name {
|
2020-06-21 23:09:17 +02:00
|
|
|
p.inside_match = true // reuse the same var for perf instead of inside_sql TODO rename
|
2020-06-16 12:14:22 +02:00
|
|
|
node = p.sql_expr()
|
2020-06-21 23:09:17 +02:00
|
|
|
p.inside_match = false
|
2020-06-16 12:14:22 +02:00
|
|
|
} else {
|
2021-01-22 13:49:56 +01:00
|
|
|
if p.inside_if && p.is_generic_name() {
|
2021-01-06 18:58:53 +01:00
|
|
|
// $if T is string {}
|
|
|
|
p.expecting_type = true
|
|
|
|
}
|
2020-06-16 12:14:22 +02:00
|
|
|
node = p.name_expr()
|
|
|
|
p.is_stmt_ident = is_stmt_ident
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
.string {
|
|
|
|
node = p.string_expr()
|
|
|
|
}
|
2020-07-17 19:13:22 +02:00
|
|
|
.comment {
|
|
|
|
node = p.comment()
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
.dot {
|
|
|
|
// .enum_val
|
|
|
|
node = p.enum_val()
|
|
|
|
}
|
2020-11-05 09:12:32 +01:00
|
|
|
.at {
|
|
|
|
node = p.at()
|
|
|
|
}
|
2020-06-20 03:12:35 +02:00
|
|
|
.dollar {
|
2020-09-18 00:58:54 +02:00
|
|
|
match p.peek_tok.kind {
|
2020-12-04 19:34:05 +01:00
|
|
|
.name {
|
2021-01-14 15:20:11 +01:00
|
|
|
return p.comp_call()
|
2020-12-04 19:34:05 +01:00
|
|
|
}
|
|
|
|
.key_if {
|
|
|
|
return p.if_expr(true)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.error_with_pos('unexpected `$`', p.peek_tok.position())
|
|
|
|
return ast.Expr{}
|
|
|
|
}
|
2020-06-20 03:12:35 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
.chartoken {
|
|
|
|
node = ast.CharLiteral{
|
|
|
|
val: p.tok.lit
|
2020-04-20 14:49:26 +02:00
|
|
|
pos: p.tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
2021-01-24 22:09:51 +01:00
|
|
|
.amp, .mul, .not, .bit_not, .arrow {
|
2020-10-21 17:37:30 +02:00
|
|
|
// -1, -a, !x, &x, ~x, <-a
|
2020-04-17 18:11:04 +02:00
|
|
|
node = p.prefix_expr()
|
|
|
|
}
|
2021-01-24 22:09:51 +01:00
|
|
|
.minus {
|
|
|
|
if p.peek_tok.kind == .number {
|
|
|
|
node = p.parse_number_literal()
|
|
|
|
} else {
|
|
|
|
node = p.prefix_expr()
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 14:08:52 +01:00
|
|
|
.key_go {
|
|
|
|
stmt := p.stmt(false)
|
|
|
|
go_stmt := stmt as ast.GoStmt
|
|
|
|
node = ast.GoExpr{
|
|
|
|
go_stmt: go_stmt
|
|
|
|
pos: go_stmt.pos
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
.key_true, .key_false {
|
|
|
|
node = ast.BoolLiteral{
|
|
|
|
val: p.tok.kind == .key_true
|
2020-04-20 14:49:26 +02:00
|
|
|
pos: p.tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
.key_match {
|
|
|
|
node = p.match_expr()
|
|
|
|
}
|
2020-09-16 15:34:57 +02:00
|
|
|
.key_select {
|
|
|
|
node = p.select_expr()
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
.number {
|
|
|
|
node = p.parse_number_literal()
|
|
|
|
}
|
|
|
|
.lpar {
|
2021-01-19 14:49:40 +01:00
|
|
|
mut pos := p.tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
p.check(.lpar)
|
|
|
|
node = p.expr(0)
|
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.ParExpr{
|
|
|
|
expr: node
|
2021-01-19 14:49:40 +01:00
|
|
|
pos: pos.extend(p.prev_tok.position())
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.key_if {
|
2020-09-18 00:58:54 +02:00
|
|
|
node = p.if_expr(false)
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
2020-07-12 12:58:33 +02:00
|
|
|
.key_unsafe {
|
2020-09-19 18:18:36 +02:00
|
|
|
// unsafe {
|
2020-12-25 15:52:16 +01:00
|
|
|
mut pos := p.tok.position()
|
2020-10-20 21:57:24 +02:00
|
|
|
p.next()
|
|
|
|
if p.inside_unsafe {
|
|
|
|
p.error_with_pos('already inside `unsafe` block', pos)
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Expr{}
|
2020-10-20 21:57:24 +02:00
|
|
|
}
|
2020-07-12 12:58:33 +02:00
|
|
|
p.inside_unsafe = true
|
2020-09-19 18:18:36 +02:00
|
|
|
p.check(.lcbr)
|
2020-12-25 15:52:16 +01:00
|
|
|
e := p.expr(0)
|
|
|
|
p.check(.rcbr)
|
2021-01-19 14:49:40 +01:00
|
|
|
pos.update_last_line(p.prev_tok.line_nr)
|
2020-07-29 21:41:47 +02:00
|
|
|
node = ast.UnsafeExpr{
|
2020-12-25 15:52:16 +01:00
|
|
|
expr: e
|
2020-07-29 21:41:47 +02:00
|
|
|
pos: pos
|
|
|
|
}
|
2020-09-19 18:18:36 +02:00
|
|
|
p.inside_unsafe = false
|
2020-07-12 12:58:33 +02:00
|
|
|
}
|
2020-07-04 12:44:25 +02:00
|
|
|
.key_lock, .key_rlock {
|
|
|
|
node = p.lock_expr()
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
.lsbr {
|
2020-05-05 14:19:31 +02:00
|
|
|
if p.expecting_type {
|
|
|
|
// parse json.decode type (`json.decode([]User, s)`)
|
|
|
|
node = p.name_expr()
|
2021-01-23 22:54:57 +01:00
|
|
|
} else if p.is_amp && p.peek_tok.kind == .rsbr && p.peek_tok3.kind != .lcbr {
|
2021-01-04 17:22:04 +01:00
|
|
|
pos := p.tok.position()
|
|
|
|
typ := p.parse_type().to_ptr()
|
|
|
|
p.check(.lpar)
|
|
|
|
expr := p.expr(0)
|
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.CastExpr{
|
|
|
|
typ: typ
|
|
|
|
expr: expr
|
|
|
|
pos: pos
|
|
|
|
}
|
2020-05-05 14:19:31 +02:00
|
|
|
} else {
|
|
|
|
node = p.array_init()
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
.key_none {
|
2020-06-15 07:10:45 +02:00
|
|
|
pos := p.tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
p.next()
|
2020-06-15 07:10:45 +02:00
|
|
|
node = ast.None{
|
|
|
|
pos: pos
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
.key_sizeof {
|
2020-04-20 07:04:31 +02:00
|
|
|
p.next() // sizeof
|
2020-04-17 18:11:04 +02:00
|
|
|
p.check(.lpar)
|
2021-02-18 08:38:57 +01:00
|
|
|
pos := p.tok.position()
|
2021-01-13 06:13:39 +01:00
|
|
|
is_known_var := p.mark_var_as_used(p.tok.lit)
|
|
|
|
// assume mod. prefix leads to a type
|
|
|
|
if is_known_var || !(p.known_import(p.tok.lit) || p.tok.kind.is_start_of_type()) {
|
2021-01-01 14:38:11 +01:00
|
|
|
expr := p.expr(0)
|
2020-04-17 18:11:04 +02:00
|
|
|
node = ast.SizeOf{
|
2020-07-01 00:53:53 +02:00
|
|
|
is_type: false
|
|
|
|
expr: expr
|
|
|
|
pos: pos
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-13 06:13:39 +01:00
|
|
|
p.register_used_import(p.tok.lit)
|
2020-07-17 10:31:35 +02:00
|
|
|
save_expr_mod := p.expr_mod
|
|
|
|
p.expr_mod = ''
|
2020-07-01 00:53:53 +02:00
|
|
|
sizeof_type := p.parse_type()
|
2020-07-17 10:31:35 +02:00
|
|
|
p.expr_mod = save_expr_mod
|
2020-04-17 18:11:04 +02:00
|
|
|
node = ast.SizeOf{
|
2020-07-02 15:44:03 +02:00
|
|
|
is_type: true
|
2020-04-17 18:11:04 +02:00
|
|
|
typ: sizeof_type
|
2020-07-01 00:53:53 +02:00
|
|
|
pos: pos
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check(.rpar)
|
|
|
|
}
|
|
|
|
.key_typeof {
|
2020-11-04 12:34:12 +01:00
|
|
|
spos := p.tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
p.next()
|
|
|
|
p.check(.lpar)
|
|
|
|
expr := p.expr(0)
|
|
|
|
p.check(.rpar)
|
2021-01-07 20:32:02 +01:00
|
|
|
if p.tok.kind != .dot && p.tok.line_nr == p.prev_tok.line_nr {
|
|
|
|
p.warn_with_pos('use e.g. `typeof(expr).name` or `sum_type_instance.type_name()` instead',
|
|
|
|
spos)
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
node = ast.TypeOf{
|
|
|
|
expr: expr
|
2020-11-04 12:34:12 +01:00
|
|
|
pos: spos.extend(p.tok.position())
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-30 12:57:09 +01:00
|
|
|
.key_offsetof {
|
|
|
|
pos := p.tok.position()
|
|
|
|
p.next() // __offsetof
|
|
|
|
p.check(.lpar)
|
|
|
|
st := p.parse_type()
|
|
|
|
p.check(.comma)
|
|
|
|
if p.tok.kind != .name {
|
|
|
|
p.error_with_pos('unexpected `$p.tok.lit`, expecting struct field', p.tok.position())
|
|
|
|
return ast.Expr{}
|
|
|
|
}
|
|
|
|
field := p.tok.lit
|
|
|
|
p.next()
|
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.OffsetOf{
|
|
|
|
struct_type: st
|
|
|
|
field: field
|
|
|
|
pos: pos
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 17:08:31 +02:00
|
|
|
.key_likely, .key_unlikely {
|
|
|
|
is_likely := p.tok.kind == .key_likely
|
2020-06-09 16:36:18 +02:00
|
|
|
p.next()
|
|
|
|
p.check(.lpar)
|
|
|
|
lpos := p.tok.position()
|
|
|
|
expr := p.expr(0)
|
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.Likely{
|
|
|
|
expr: expr
|
|
|
|
pos: lpos
|
2020-06-09 17:08:31 +02:00
|
|
|
is_likely: is_likely
|
2020-06-09 16:36:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
.lcbr {
|
|
|
|
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
|
|
|
p.next()
|
2021-01-01 17:23:32 +01:00
|
|
|
if p.tok.kind in [.chartoken, .number, .string] {
|
2021-02-06 22:13:24 +01:00
|
|
|
// TODO deprecate
|
2020-04-17 18:11:04 +02:00
|
|
|
node = p.map_init()
|
|
|
|
} else {
|
|
|
|
// it should be a struct
|
2021-02-08 16:03:05 +01:00
|
|
|
if p.tok.kind == .name && p.peek_tok.kind == .pipe {
|
|
|
|
p.warn_with_pos('use e.g. `...struct_var` instead', p.peek_tok.position())
|
2020-04-17 18:11:04 +02:00
|
|
|
node = p.assoc()
|
2021-02-07 03:43:51 +01:00
|
|
|
} else if (p.tok.kind == .name && p.peek_tok.kind == .colon)
|
|
|
|
|| p.tok.kind in [.rcbr, .comment, .ellipsis] {
|
2020-04-20 07:04:31 +02:00
|
|
|
node = p.struct_init(true) // short_syntax: true
|
2020-04-17 18:11:04 +02:00
|
|
|
} else if p.tok.kind == .name {
|
|
|
|
p.next()
|
2021-02-07 03:40:00 +01:00
|
|
|
p.error_with_pos('unexpected $p.tok, expecting `:` after struct field name',
|
|
|
|
p.tok.position())
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Expr{}
|
2020-04-17 18:11:04 +02:00
|
|
|
} else {
|
2021-02-07 03:40:00 +01:00
|
|
|
p.error_with_pos('unexpected $p.tok, expecting struct field name',
|
2020-11-28 22:39:45 +01:00
|
|
|
p.tok.position())
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Expr{}
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
2020-04-20 07:04:31 +02:00
|
|
|
.key_fn {
|
2020-09-18 00:58:54 +02:00
|
|
|
if p.expecting_type {
|
|
|
|
// Anonymous function type
|
|
|
|
start_pos := p.tok.position()
|
|
|
|
return ast.Type{
|
|
|
|
typ: p.parse_type()
|
|
|
|
pos: start_pos.extend(p.prev_tok.position())
|
2020-07-07 17:10:39 +02:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
} else {
|
|
|
|
// Anonymous function
|
|
|
|
node = p.anon_fn()
|
|
|
|
// its a call
|
|
|
|
// NOTE: this could be moved to just before the pratt loop
|
|
|
|
// then anything can be a call, eg. `index[2]()` or `struct.field()`
|
|
|
|
// but this would take a bit of modification
|
|
|
|
if p.tok.kind == .lpar {
|
|
|
|
p.next()
|
|
|
|
pos := p.tok.position()
|
|
|
|
args := p.call_args()
|
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.CallExpr{
|
|
|
|
name: 'anon'
|
|
|
|
left: node
|
|
|
|
args: args
|
|
|
|
pos: pos
|
2020-12-12 09:01:12 +01:00
|
|
|
scope: p.scope
|
2020-09-18 00:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
2020-07-07 17:10:39 +02:00
|
|
|
}
|
2020-04-20 07:04:31 +02:00
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
else {
|
2020-12-03 20:11:43 +01:00
|
|
|
if p.tok.kind != .eof {
|
|
|
|
// eof should be handled where it happens
|
2021-02-09 16:07:30 +01:00
|
|
|
p.error_with_pos('invalid expression: unexpected $p.tok', p.tok.position())
|
2020-12-04 19:34:05 +01:00
|
|
|
return ast.Expr{}
|
2020-12-03 20:11:43 +01:00
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 18:18:36 +02:00
|
|
|
return p.expr_with_left(node, precedence, is_stmt_ident)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_ident bool) ast.Expr {
|
|
|
|
mut node := left
|
2020-04-17 18:11:04 +02:00
|
|
|
// Infix
|
|
|
|
for precedence < p.tok.precedence() {
|
2020-06-16 13:20:16 +02:00
|
|
|
if p.tok.kind == .dot {
|
2020-04-17 18:11:04 +02:00
|
|
|
node = p.dot_expr(node)
|
2020-12-22 13:00:23 +01:00
|
|
|
if p.name_error {
|
|
|
|
return node
|
|
|
|
}
|
2020-04-25 10:57:12 +02:00
|
|
|
p.is_stmt_ident = is_stmt_ident
|
2021-01-17 05:30:41 +01:00
|
|
|
} else if p.tok.kind == .lsbr && (p.inside_fn || p.tok.line_nr == p.prev_tok.line_nr) {
|
2020-04-17 18:11:04 +02:00
|
|
|
node = p.index_expr(node)
|
2020-06-16 13:20:16 +02:00
|
|
|
p.is_stmt_ident = is_stmt_ident
|
2020-12-15 04:24:57 +01:00
|
|
|
if p.tok.kind == .lpar && p.tok.line_nr == p.prev_tok.line_nr && node is ast.IndexExpr {
|
|
|
|
p.next()
|
|
|
|
pos := p.tok.position()
|
|
|
|
args := p.call_args()
|
|
|
|
p.check(.rpar)
|
|
|
|
node = ast.CallExpr{
|
|
|
|
left: node
|
|
|
|
args: args
|
|
|
|
pos: pos
|
|
|
|
scope: p.scope
|
|
|
|
}
|
|
|
|
p.is_stmt_ident = is_stmt_ident
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
} else if p.tok.kind == .key_as {
|
2020-06-18 16:33:16 +02:00
|
|
|
// sum type as cast `x := SumType as Variant`
|
2020-04-17 18:11:04 +02:00
|
|
|
pos := p.tok.position()
|
|
|
|
p.next()
|
2020-09-19 18:18:36 +02:00
|
|
|
typ := p.parse_type()
|
2020-04-17 18:11:04 +02:00
|
|
|
node = ast.AsCast{
|
|
|
|
expr: node
|
|
|
|
typ: typ
|
|
|
|
pos: pos
|
|
|
|
}
|
2020-04-25 10:57:12 +02:00
|
|
|
} else if p.tok.kind == .left_shift && p.is_stmt_ident {
|
2020-04-17 18:11:04 +02:00
|
|
|
// arr << elem
|
|
|
|
tok := p.tok
|
2021-01-19 14:49:40 +01:00
|
|
|
mut pos := tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
p.next()
|
|
|
|
right := p.expr(precedence - 1)
|
2021-01-19 14:49:40 +01:00
|
|
|
pos.update_last_line(p.prev_tok.line_nr)
|
2021-02-16 14:08:01 +01:00
|
|
|
if mut node is ast.IndexExpr {
|
|
|
|
node.recursive_mapset_is_setter(true)
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
node = ast.InfixExpr{
|
|
|
|
left: node
|
|
|
|
right: right
|
|
|
|
op: tok.kind
|
|
|
|
pos: pos
|
|
|
|
}
|
2020-06-18 20:21:08 +02:00
|
|
|
} else if p.tok.kind.is_infix() {
|
2020-09-29 03:13:10 +02:00
|
|
|
if p.tok.kind.is_prefix() && p.tok.line_nr != p.prev_tok.line_nr {
|
|
|
|
// return early for deref assign `*x = 2` goes to prefix expr
|
|
|
|
if p.tok.kind == .mul && p.peek_tok2.kind == .assign {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
// added 10/2020: LATER this will be parsed as PrefixExpr instead
|
2020-10-15 22:12:59 +02:00
|
|
|
p.warn_with_pos('move infix `$p.tok.kind` operator before new line (if infix intended) or use brackets for a prefix expression',
|
|
|
|
p.tok.position())
|
2020-04-25 10:07:30 +02:00
|
|
|
}
|
|
|
|
// continue on infix expr
|
2020-04-17 18:11:04 +02:00
|
|
|
node = p.infix_expr(node)
|
2020-07-09 17:14:14 +02:00
|
|
|
// return early `if bar is SumType as b {`
|
|
|
|
if p.tok.kind == .key_as && p.inside_if {
|
|
|
|
return node
|
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
} else if p.tok.kind in [.inc, .dec] || (p.tok.kind == .question && p.inside_ct_if_expr) {
|
2020-04-17 18:11:04 +02:00
|
|
|
// Postfix
|
2020-08-25 08:50:06 +02:00
|
|
|
// detect `f(x++)`, `a[x++]`
|
2020-10-15 22:12:59 +02:00
|
|
|
if p.peek_tok.kind in [.rpar, .rsbr] && p.mod !in ['builtin', 'regex', 'strconv'] { // temp
|
|
|
|
p.warn_with_pos('`$p.tok.kind` operator can only be used as a statement',
|
|
|
|
p.peek_tok.position())
|
2020-08-25 08:50:06 +02:00
|
|
|
}
|
2021-02-07 23:10:16 +01:00
|
|
|
if p.tok.kind in [.inc, .dec] && p.prev_tok.line_nr != p.tok.line_nr {
|
|
|
|
p.error_with_pos('$p.tok must be on the same line as the previous token',
|
|
|
|
p.tok.position())
|
|
|
|
}
|
2021-02-16 14:08:01 +01:00
|
|
|
if mut node is ast.IndexExpr {
|
|
|
|
node.recursive_mapset_is_setter(true)
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
node = ast.PostfixExpr{
|
|
|
|
op: p.tok.kind
|
|
|
|
expr: node
|
|
|
|
pos: p.tok.position()
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
// return node // TODO bring back, only allow ++/-- in exprs in translated code
|
|
|
|
} else {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
|
2020-04-17 18:11:04 +02:00
|
|
|
op := p.tok.kind
|
2020-08-31 10:44:39 +02:00
|
|
|
if op == .arrow {
|
2021-01-06 21:19:40 +01:00
|
|
|
p.or_is_handled = true
|
2020-08-31 10:44:39 +02:00
|
|
|
p.register_auto_import('sync')
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
precedence := p.tok.precedence()
|
2021-01-19 14:49:40 +01:00
|
|
|
mut pos := p.tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
p.next()
|
2020-04-21 05:11:50 +02:00
|
|
|
mut right := ast.Expr{}
|
2020-09-18 00:58:54 +02:00
|
|
|
prev_expecting_type := p.expecting_type
|
2020-06-02 16:18:12 +02:00
|
|
|
if op in [.key_is, .not_is] {
|
2020-05-05 14:19:31 +02:00
|
|
|
p.expecting_type = true
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
right = p.expr(precedence)
|
2020-09-18 00:58:54 +02:00
|
|
|
p.expecting_type = prev_expecting_type
|
2021-01-23 09:33:22 +01:00
|
|
|
if p.pref.is_vet && op in [.key_in, .not_in] && right is ast.ArrayInit
|
|
|
|
&& (right as ast.ArrayInit).exprs.len == 1 {
|
2021-01-09 15:11:49 +01:00
|
|
|
p.vet_error('Use `var == value` instead of `var in [value]`', pos.line_nr, vet.FixKind.vfmt)
|
2020-07-19 19:58:34 +02:00
|
|
|
}
|
2021-01-06 21:19:40 +01:00
|
|
|
mut or_stmts := []ast.Stmt{}
|
|
|
|
mut or_kind := ast.OrKind.absent
|
|
|
|
mut or_pos := p.tok.position()
|
|
|
|
// allow `x := <-ch or {...}` to handle closed channel
|
|
|
|
if op == .arrow {
|
|
|
|
if p.tok.kind == .key_orelse {
|
|
|
|
p.next()
|
|
|
|
p.open_scope()
|
|
|
|
p.scope.register(ast.Var{
|
|
|
|
name: 'errcode'
|
|
|
|
typ: table.int_type
|
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
|
|
|
})
|
|
|
|
p.scope.register(ast.Var{
|
|
|
|
name: 'err'
|
|
|
|
typ: table.string_type
|
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
|
|
|
})
|
|
|
|
or_kind = .block
|
|
|
|
or_stmts = p.parse_block_no_scope(false)
|
|
|
|
or_pos = or_pos.extend(p.prev_tok.position())
|
|
|
|
p.close_scope()
|
|
|
|
}
|
|
|
|
if p.tok.kind == .question {
|
|
|
|
p.next()
|
|
|
|
or_kind = .propagate
|
|
|
|
}
|
|
|
|
p.or_is_handled = false
|
|
|
|
}
|
2021-01-19 14:49:40 +01:00
|
|
|
pos.update_last_line(p.prev_tok.line_nr)
|
2020-04-27 22:53:26 +02:00
|
|
|
return ast.InfixExpr{
|
2020-04-17 18:11:04 +02:00
|
|
|
left: left
|
|
|
|
right: right
|
|
|
|
op: op
|
|
|
|
pos: pos
|
2021-01-06 21:19:40 +01:00
|
|
|
or_block: ast.OrExpr{
|
|
|
|
stmts: or_stmts
|
|
|
|
kind: or_kind
|
|
|
|
pos: or_pos
|
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:11:50 +02:00
|
|
|
fn (mut p Parser) prefix_expr() ast.PrefixExpr {
|
2021-01-19 14:49:40 +01:00
|
|
|
mut pos := p.tok.position()
|
2020-04-17 18:11:04 +02:00
|
|
|
op := p.tok.kind
|
|
|
|
if op == .amp {
|
|
|
|
p.is_amp = true
|
|
|
|
}
|
2020-08-31 10:44:39 +02:00
|
|
|
if op == .arrow {
|
2020-12-19 21:44:16 +01:00
|
|
|
p.or_is_handled = true
|
2020-08-31 10:44:39 +02:00
|
|
|
p.register_auto_import('sync')
|
|
|
|
}
|
2020-05-11 16:05:59 +02:00
|
|
|
// if op == .mul && !p.inside_unsafe {
|
|
|
|
// p.warn('unsafe')
|
|
|
|
// }
|
2020-04-17 18:11:04 +02:00
|
|
|
p.next()
|
2020-12-27 14:20:30 +01:00
|
|
|
mut right := if op == .minus {
|
2021-01-08 17:41:52 +01:00
|
|
|
p.expr(int(token.Precedence.call))
|
2020-12-27 14:20:30 +01:00
|
|
|
} else {
|
2021-01-08 17:41:52 +01:00
|
|
|
p.expr(int(token.Precedence.prefix))
|
2020-12-27 14:20:30 +01:00
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
p.is_amp = false
|
2020-11-21 00:05:57 +01:00
|
|
|
if mut right is ast.CastExpr {
|
2020-08-10 18:05:26 +02:00
|
|
|
right.in_prexpr = true
|
|
|
|
}
|
2020-08-23 02:12:05 +02:00
|
|
|
mut or_stmts := []ast.Stmt{}
|
|
|
|
mut or_kind := ast.OrKind.absent
|
2020-11-04 15:02:01 +01:00
|
|
|
mut or_pos := p.tok.position()
|
2020-08-23 02:12:05 +02:00
|
|
|
// allow `x := <-ch or {...}` to handle closed channel
|
2020-08-31 10:44:39 +02:00
|
|
|
if op == .arrow {
|
|
|
|
if p.tok.kind == .key_orelse {
|
2020-08-23 02:12:05 +02:00
|
|
|
p.next()
|
|
|
|
p.open_scope()
|
2020-12-02 14:40:25 +01:00
|
|
|
p.scope.register(ast.Var{
|
2020-08-23 02:12:05 +02:00
|
|
|
name: 'errcode'
|
|
|
|
typ: table.int_type
|
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
|
|
|
})
|
2020-12-02 14:40:25 +01:00
|
|
|
p.scope.register(ast.Var{
|
2020-08-23 02:12:05 +02:00
|
|
|
name: 'err'
|
|
|
|
typ: table.string_type
|
|
|
|
pos: p.tok.position()
|
|
|
|
is_used: true
|
|
|
|
})
|
|
|
|
or_kind = .block
|
|
|
|
or_stmts = p.parse_block_no_scope(false)
|
2020-11-04 15:02:01 +01:00
|
|
|
or_pos = or_pos.extend(p.prev_tok.position())
|
2020-08-23 02:12:05 +02:00
|
|
|
p.close_scope()
|
2020-08-31 10:44:39 +02:00
|
|
|
}
|
|
|
|
if p.tok.kind == .question {
|
|
|
|
p.next()
|
|
|
|
or_kind = .propagate
|
|
|
|
}
|
2020-12-19 21:44:16 +01:00
|
|
|
p.or_is_handled = false
|
2020-08-23 02:12:05 +02:00
|
|
|
}
|
2021-01-19 14:49:40 +01:00
|
|
|
pos.update_last_line(p.prev_tok.line_nr)
|
2020-04-17 18:11:04 +02:00
|
|
|
return ast.PrefixExpr{
|
|
|
|
op: op
|
|
|
|
right: right
|
|
|
|
pos: pos
|
2020-08-23 02:12:05 +02:00
|
|
|
or_block: ast.OrExpr{
|
|
|
|
stmts: or_stmts
|
|
|
|
kind: or_kind
|
2020-11-04 15:02:01 +01:00
|
|
|
pos: or_pos
|
2020-08-23 02:12:05 +02:00
|
|
|
}
|
2020-04-17 18:11:04 +02:00
|
|
|
}
|
|
|
|
}
|