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

ast, parser: add type_pos to TypeDecl nodes (#9571)

This commit is contained in:
Ned Palacios 2021-04-09 19:51:25 +08:00 committed by GitHub
parent 46e7e27ba3
commit a706215e52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 6 deletions

View File

@ -29,7 +29,7 @@ pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var
// TODO: replace Param
pub type Node = CallArg | ConstField | EmptyNode | EnumField | Expr | File | GlobalField |
IfBranch | MatchBranch | NodeError | Param | ScopeObject | SelectBranch | Stmt | StructField |
StructInitField
StructInitField | SumTypeVariant
pub struct TypeNode {
pub:
@ -913,6 +913,7 @@ pub:
is_pub bool
parent_type Type
pos token.Position
type_pos token.Position
comments []Comment
}
@ -940,6 +941,7 @@ pub:
is_pub bool
typ Type
pos token.Position
type_pos token.Position
comments []Comment
}
@ -1606,6 +1608,17 @@ pub fn (node Node) position() token.Position {
for sym in node.syms {
pos = pos.extend(sym.pos)
}
} else if node is TypeDecl {
match node {
FnTypeDecl, AliasTypeDecl {
pos = pos.extend(node.type_pos)
}
SumTypeDecl {
for variant in node.variants {
pos = pos.extend(variant.pos)
}
}
}
}
if node is AssignStmt {
return pos.extend(node.right.last().position())
@ -1621,9 +1634,13 @@ pub fn (node Node) position() token.Position {
StructField {
return node.pos.extend(node.type_pos)
}
MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg {
MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg,
SumTypeVariant {
return node.pos
}
Param {
return node.pos.extend(node.type_pos)
}
IfBranch {
return node.pos.extend(node.body_pos)
}
@ -1643,9 +1660,6 @@ pub fn (node Node) position() token.Position {
}
}
}
Param {
return node.pos.extend(node.type_pos)
}
File {
mut pos := token.Position{}
if node.stmts.len > 0 {
@ -1767,6 +1781,11 @@ pub fn (node Node) children() []Node {
children << node.params.map(Node(it))
children << node.stmts.map(Node(it))
}
TypeDecl {
if node is SumTypeDecl {
children << node.variants.map(Node(it))
}
}
else {}
}
} else if node is ScopeObject {

View File

@ -1304,6 +1304,7 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
}
f.comments(node.comments, has_nl: false)
f.writeln('')
}
pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {

View File

@ -3,7 +3,6 @@ type FooBar = Bar | Foo
pub type PublicBar = Bar | Foo | FooBar
type Uint = byte | u16 | u32 | u64 // This should stay on the same line
type Float = f32 | f64
// Alias type
@ -14,6 +13,7 @@ pub type Abc = f32
// Fn type decl
type EmptyFn = fn ()
type OneArgFn = fn (i int)
type TwoDiffArgs = fn (i int, s string) bool

View File

@ -510,6 +510,7 @@ fn (mut p Parser) fn_receiver(mut params []ast.Param, mut rec ReceiverParsingInf
is_mut: rec.is_mut
is_auto_rec: is_auto_rec
typ: rec.typ
type_pos: rec.type_pos
}
p.check(.rpar)

View File

@ -2955,12 +2955,14 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
// function type: `type mycallback = fn(string, int)`
fn_name := p.prepend_mod(name)
fn_type := p.parse_fn_type(fn_name)
type_pos = type_pos.extend(p.tok.position())
comments = p.eat_comments(same_line: true)
return ast.FnTypeDecl{
name: fn_name
is_pub: is_pub
typ: fn_type
pos: decl_pos
type_pos: type_pos
comments: comments
}
}
@ -3045,6 +3047,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
name: name
is_pub: is_pub
parent_type: parent_type
type_pos: type_pos
pos: decl_pos
comments: comments
}