parser: add information about the ending line for a few elements (#7414)
parent
fb0c4556fd
commit
d1fc65c260
|
@ -382,7 +382,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
rec_mut: rec_mut
|
rec_mut: rec_mut
|
||||||
language: language
|
language: language
|
||||||
no_body: no_body
|
no_body: no_body
|
||||||
pos: start_pos.extend(end_pos)
|
pos: start_pos.extend_with_last_line(end_pos, p.prev_tok.line_nr)
|
||||||
body_pos: body_start_pos
|
body_pos: body_start_pos
|
||||||
file: p.file_name
|
file: p.file_name
|
||||||
is_builtin: p.builtin_mod || p.mod in util.builtin_module_parts
|
is_builtin: p.builtin_mod || p.mod in util.builtin_module_parts
|
||||||
|
|
|
@ -8,7 +8,7 @@ import v.table
|
||||||
|
|
||||||
fn (mut p Parser) for_stmt() ast.Stmt {
|
fn (mut p Parser) for_stmt() ast.Stmt {
|
||||||
p.check(.key_for)
|
p.check(.key_for)
|
||||||
pos := p.tok.position()
|
mut pos := p.tok.position()
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
p.inside_for = true
|
p.inside_for = true
|
||||||
if p.tok.kind == .key_match {
|
if p.tok.kind == .key_match {
|
||||||
|
@ -20,6 +20,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
|
||||||
if p.tok.kind == .lcbr {
|
if p.tok.kind == .lcbr {
|
||||||
p.inside_for = false
|
p.inside_for = false
|
||||||
stmts := p.parse_block_no_scope(false)
|
stmts := p.parse_block_no_scope(false)
|
||||||
|
pos.last_line = p.prev_tok.line_nr - 1
|
||||||
for_stmt := ast.ForStmt{
|
for_stmt := ast.ForStmt{
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
pos: pos
|
pos: pos
|
||||||
|
@ -63,6 +64,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
|
||||||
}
|
}
|
||||||
p.inside_for = false
|
p.inside_for = false
|
||||||
stmts := p.parse_block_no_scope(false)
|
stmts := p.parse_block_no_scope(false)
|
||||||
|
pos.last_line = p.prev_tok.line_nr - 1
|
||||||
for_c_stmt := ast.ForCStmt{
|
for_c_stmt := ast.ForCStmt{
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
has_init: has_init
|
has_init: has_init
|
||||||
|
@ -151,6 +153,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
|
||||||
}
|
}
|
||||||
p.inside_for = false
|
p.inside_for = false
|
||||||
stmts := p.parse_block_no_scope(false)
|
stmts := p.parse_block_no_scope(false)
|
||||||
|
pos.last_line = p.prev_tok.line_nr - 1
|
||||||
// println('nr stmts=$stmts.len')
|
// println('nr stmts=$stmts.len')
|
||||||
for_in_stmt := ast.ForInStmt{
|
for_in_stmt := ast.ForInStmt{
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
|
@ -170,6 +173,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
|
||||||
cond := p.expr(0)
|
cond := p.expr(0)
|
||||||
p.inside_for = false
|
p.inside_for = false
|
||||||
stmts := p.parse_block_no_scope(false)
|
stmts := p.parse_block_no_scope(false)
|
||||||
|
pos.last_line = p.prev_tok.line_nr - 1
|
||||||
for_stmt := ast.ForStmt{
|
for_stmt := ast.ForStmt{
|
||||||
cond: cond
|
cond: cond
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
|
|
|
@ -605,8 +605,9 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
|
||||||
p.is_stmt_ident = p.tok.kind == .name
|
p.is_stmt_ident = p.tok.kind == .name
|
||||||
match p.tok.kind {
|
match p.tok.kind {
|
||||||
.lcbr {
|
.lcbr {
|
||||||
pos := p.tok.position()
|
mut pos := p.tok.position()
|
||||||
stmts := p.parse_block()
|
stmts := p.parse_block()
|
||||||
|
pos.last_line = p.prev_tok.line_nr
|
||||||
return ast.Block{
|
return ast.Block{
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
pos: pos
|
pos: pos
|
||||||
|
@ -716,7 +717,7 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
|
||||||
stmts := p.parse_block()
|
stmts := p.parse_block()
|
||||||
return ast.DeferStmt{
|
return ast.DeferStmt{
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
pos: spos.extend(p.tok.position())
|
pos: spos.extend_with_last_line(p.tok.position(), p.prev_tok.line_nr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.key_go {
|
.key_go {
|
||||||
|
@ -2012,7 +2013,7 @@ $pubfn (mut e $enum_name) toggle(flag $enum_name) { unsafe{ *e = int(*e) ^ (
|
||||||
is_flag: is_flag
|
is_flag: is_flag
|
||||||
is_multi_allowed: is_multi_allowed
|
is_multi_allowed: is_multi_allowed
|
||||||
fields: fields
|
fields: fields
|
||||||
pos: start_pos.extend(end_pos)
|
pos: start_pos.extend_with_last_line(end_pos, p.prev_tok.line_nr)
|
||||||
attrs: p.attrs
|
attrs: p.attrs
|
||||||
comments: enum_decl_comments
|
comments: enum_decl_comments
|
||||||
}
|
}
|
||||||
|
@ -2238,7 +2239,7 @@ pub fn (mut p Parser) mark_var_as_used(varname string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut p Parser) unsafe_stmt() ast.Stmt {
|
fn (mut p Parser) unsafe_stmt() ast.Stmt {
|
||||||
pos := p.tok.position()
|
mut pos := p.tok.position()
|
||||||
p.next()
|
p.next()
|
||||||
if p.tok.kind != .lcbr {
|
if p.tok.kind != .lcbr {
|
||||||
p.error_with_pos('please use `unsafe {`', p.tok.position())
|
p.error_with_pos('please use `unsafe {`', p.tok.position())
|
||||||
|
@ -2251,6 +2252,7 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt {
|
||||||
}
|
}
|
||||||
if p.tok.kind == .rcbr {
|
if p.tok.kind == .rcbr {
|
||||||
// `unsafe {}`
|
// `unsafe {}`
|
||||||
|
pos.last_line = p.tok.line_nr - 1
|
||||||
p.next()
|
p.next()
|
||||||
return ast.Block{
|
return ast.Block{
|
||||||
is_unsafe: true
|
is_unsafe: true
|
||||||
|
@ -2287,6 +2289,7 @@ fn (mut p Parser) unsafe_stmt() ast.Stmt {
|
||||||
for p.tok.kind != .rcbr {
|
for p.tok.kind != .rcbr {
|
||||||
stmts << p.stmt(false)
|
stmts << p.stmt(false)
|
||||||
}
|
}
|
||||||
|
pos.last_line = p.tok.line_nr
|
||||||
p.next()
|
p.next()
|
||||||
return ast.Block{
|
return ast.Block{
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
|
|
|
@ -85,6 +85,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
mut is_field_mut := false
|
mut is_field_mut := false
|
||||||
mut is_field_pub := false
|
mut is_field_pub := false
|
||||||
mut is_field_global := false
|
mut is_field_global := false
|
||||||
|
mut last_line := -1
|
||||||
mut end_comments := []ast.Comment{}
|
mut end_comments := []ast.Comment{}
|
||||||
if !no_body {
|
if !no_body {
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
|
@ -252,6 +253,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
p.attrs = []
|
p.attrs = []
|
||||||
}
|
}
|
||||||
p.top_level_statement_end()
|
p.top_level_statement_end()
|
||||||
|
last_line = p.tok.line_nr
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
}
|
}
|
||||||
if language == .c {
|
if language == .c {
|
||||||
|
@ -290,7 +292,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
name: name
|
name: name
|
||||||
is_pub: is_pub
|
is_pub: is_pub
|
||||||
fields: ast_fields
|
fields: ast_fields
|
||||||
pos: start_pos.extend(name_pos)
|
pos: start_pos.extend_with_last_line(name_pos, last_line)
|
||||||
mut_pos: mut_pos
|
mut_pos: mut_pos
|
||||||
pub_pos: pub_pos
|
pub_pos: pub_pos
|
||||||
pub_mut_pos: pub_mut_pos
|
pub_mut_pos: pub_mut_pos
|
||||||
|
@ -377,7 +379,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||||
|
|
||||||
fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
||||||
p.top_level_statement_start()
|
p.top_level_statement_start()
|
||||||
start_pos := p.tok.position()
|
mut start_pos := p.tok.position()
|
||||||
is_pub := p.tok.kind == .key_pub
|
is_pub := p.tok.kind == .key_pub
|
||||||
if is_pub {
|
if is_pub {
|
||||||
p.next()
|
p.next()
|
||||||
|
@ -457,6 +459,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
||||||
}
|
}
|
||||||
p.top_level_statement_end()
|
p.top_level_statement_end()
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
|
start_pos.last_line = p.prev_tok.line_nr
|
||||||
return ast.InterfaceDecl{
|
return ast.InterfaceDecl{
|
||||||
name: interface_name
|
name: interface_name
|
||||||
methods: methods
|
methods: methods
|
||||||
|
|
|
@ -8,10 +8,12 @@ pub:
|
||||||
len int // length of the literal in the source
|
len int // length of the literal in the source
|
||||||
line_nr int // the line number in the source where the token occured
|
line_nr int // the line number in the source where the token occured
|
||||||
pos int // the position of the token in scanner text
|
pos int // the position of the token in scanner text
|
||||||
|
pub mut:
|
||||||
|
last_line int // the line number where the ast object ends (used by vfmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (pos Position) str() string {
|
pub fn (pos Position) str() string {
|
||||||
return 'Position{ line_nr: $pos.line_nr, pos: $pos.pos, len: $pos.len }'
|
return 'Position{ line_nr: $pos.line_nr, last_line: $pos.last_line, pos: $pos.pos, len: $pos.len }'
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (pos Position) extend(end Position) Position {
|
pub fn (pos Position) extend(end Position) Position {
|
||||||
|
@ -21,6 +23,15 @@ pub fn (pos Position) extend(end Position) Position {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (pos Position) extend_with_last_line(end Position, last_line int) Position {
|
||||||
|
return {
|
||||||
|
len: end.pos - pos.pos + end.len
|
||||||
|
line_nr: pos.line_nr
|
||||||
|
last_line: last_line - 1
|
||||||
|
pos: pos.pos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (tok &Token) position() Position {
|
pub fn (tok &Token) position() Position {
|
||||||
return Position{
|
return Position{
|
||||||
|
|
Loading…
Reference in New Issue