all: fixes related to Node.pos (#9613)

pull/9607/head
Lukas Neubert 2021-04-06 15:16:19 +02:00 committed by GitHub
parent 87a16bbfe9
commit a0648a3ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 8 deletions

View File

@ -1477,10 +1477,16 @@ pub fn (expr Expr) position() token.Position {
} }
NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr,
ChanInit, CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector, EnumVal, DumpExpr, ChanInit, CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector, EnumVal, DumpExpr,
FloatLiteral, GoExpr, Ident, IfExpr, IndexExpr, IntegerLiteral, Likely, LockExpr, MapInit, FloatLiteral, GoExpr, Ident, IfExpr, IntegerLiteral, Likely, LockExpr, MapInit, MatchExpr,
MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr,
SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, TypeNode, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, TypeNode, TypeOf, UnsafeExpr
TypeOf, UnsafeExpr { {
return expr.pos
}
IndexExpr {
if expr.or_expr.kind != .absent {
return expr.or_expr.pos
}
return expr.pos return expr.pos
} }
IfGuardExpr { IfGuardExpr {
@ -1597,6 +1603,12 @@ pub fn (node Node) position() token.Position {
pos = pos.extend(sym.pos) pos = pos.extend(sym.pos)
} }
} }
if node is AssignStmt {
return pos.extend(node.right.last().position())
}
if node is AssertStmt {
return pos.extend(node.expr.position())
}
return pos return pos
} }
Expr { Expr {

View File

@ -74,3 +74,25 @@ fn between_orm_blocks() {
select from Upper where id == 1 select from Upper where id == 1
} }
} }
fn no_empty_lines() {
_ := $embed_file('testy.v')
mut files := map[string][]FileData{}
code := 'foo
bar'
params[0] = {
...params[0]
typ: params[0].typ.set_nr_muls(1)
}
env_value = environ()[env_lit] or {
return error('the environment variable "$env_lit" does not exist.')
}
assert '$mr_one_two()' == "(One{
value: 'one'
}, Two{
value: 'two'
})"
r := m[key] ?
compile_time_env := $env('ENV_VAR')
func()
}

View File

@ -74,6 +74,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall {
args_var: s args_var: s
is_env: true is_env: true
env_pos: spos env_pos: spos
pos: spos.extend(p.prev_tok.position())
} }
} }
p.check(.lpar) p.check(.lpar)
@ -121,6 +122,7 @@ fn (mut p Parser) comp_call() ast.ComptimeCall {
rpath: literal_string_param rpath: literal_string_param
apath: epath apath: epath
} }
pos: start_pos.extend(p.prev_tok.position())
} }
} }
// Compile vweb html template to V code, parse that V code and embed the resulting V function // Compile vweb html template to V code, parse that V code and embed the resulting V function

View File

@ -1850,7 +1850,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
} }
return ast.MapInit{ return ast.MapInit{
typ: map_type typ: map_type
pos: p.tok.position() pos: p.prev_tok.position()
} }
} }
// `chan typ{...}` // `chan typ{...}`
@ -2138,8 +2138,9 @@ fn (mut p Parser) index_expr(left ast.Expr) ast.IndexExpr {
} }
// `a[i] ?` // `a[i] ?`
if p.tok.kind == .question { if p.tok.kind == .question {
p.next() or_pos = p.tok.position()
or_kind = .propagate or_kind = .propagate
p.next()
} }
} }
return ast.IndexExpr{ return ast.IndexExpr{

View File

@ -425,7 +425,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
update_expr_comments: update_expr_comments update_expr_comments: update_expr_comments
has_update_expr: has_update_expr has_update_expr: has_update_expr
name_pos: first_pos name_pos: first_pos
pos: first_pos.extend(p.prev_tok.position()) pos: first_pos.extend(if short_syntax { p.tok.position() } else { p.prev_tok.position() })
is_short: no_keys is_short: no_keys
pre_comments: pre_comments pre_comments: pre_comments
} }

View File

@ -758,8 +758,10 @@ fn (mut s Scanner) text_scan() token.Token {
return s.new_token(.question, '', 1) return s.new_token(.question, '', 1)
} }
scanner.single_quote, scanner.double_quote { scanner.single_quote, scanner.double_quote {
start_line := s.line_nr
ident_string := s.ident_string() ident_string := s.ident_string()
return s.new_token(.string, ident_string, ident_string.len + 2) // + two quotes return s.new_multiline_token(.string, ident_string, ident_string.len + 2,
start_line) // + two quotes
} }
`\`` { `\`` {
// ` // apostrophe balance comment. do not remove // ` // apostrophe balance comment. do not remove