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

parser: anonymous structs (part 1)

This commit is contained in:
Alexander Medvednikov 2022-07-05 23:17:00 +03:00
parent 5f78647137
commit 9b4dec7b98
5 changed files with 19 additions and 10 deletions

View File

@ -353,7 +353,7 @@ fn (mut w Walker) expr(node_ ast.Expr) {
} }
} }
ast.None {} ast.None {}
ast.Nil{} ast.Nil {}
ast.ParExpr { ast.ParExpr {
w.expr(node.expr) w.expr(node.expr)
} }

View File

@ -133,7 +133,9 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr {
node = p.select_expr() node = p.select_expr()
} }
.key_nil { .key_nil {
node = ast.Nil{pos:p.tok.pos()} node = ast.Nil{
pos: p.tok.pos()
}
p.next() p.next()
} }
.number { .number {

View File

@ -423,6 +423,13 @@ pub fn (mut p Parser) parse_type() ast.Type {
nr_muls++ nr_muls++
p.next() p.next()
} }
// Anon structs
if p.tok.kind == .key_struct {
p.struct_decl(true)
p.next()
return p.table.find_type_idx('anon_struct') // TODO
}
language := p.parse_language() language := p.parse_language()
mut typ := ast.void_type mut typ := ast.void_type
is_array := p.tok.kind == .lsbr is_array := p.tok.kind == .lsbr

View File

@ -620,7 +620,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
return p.fn_decl() return p.fn_decl()
} }
.key_struct, .key_union { .key_struct, .key_union {
return p.struct_decl() return p.struct_decl(false)
} }
.key_interface { .key_interface {
return p.interface_decl() return p.interface_decl()
@ -662,7 +662,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
return p.fn_decl() return p.fn_decl()
} }
.key_struct { .key_struct {
return p.struct_decl() return p.struct_decl(false)
} }
.dollar { .dollar {
if p.peek_tok.kind == .eof { if p.peek_tok.kind == .eof {
@ -689,7 +689,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
return p.enum_decl() return p.enum_decl()
} }
.key_union { .key_union {
return p.struct_decl() return p.struct_decl(false)
} }
.comment { .comment {
return p.comment_stmt() return p.comment_stmt()

View File

@ -7,7 +7,7 @@ import v.ast
import v.token import v.token
import v.util import v.util
fn (mut p Parser) struct_decl() ast.StructDecl { fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
p.top_level_statement_start() p.top_level_statement_start()
// save attributes, they will be changed later in fields // save attributes, they will be changed later in fields
attrs := p.attrs attrs := p.attrs
@ -39,7 +39,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
if p.disallow_declarations_in_script_mode() { if p.disallow_declarations_in_script_mode() {
return ast.StructDecl{} return ast.StructDecl{}
} }
mut name := p.check_name() mut name := if is_anon { '' } else { p.check_name()}
if name.len == 1 && name[0].is_capital() { if name.len == 1 && name[0].is_capital() {
p.error_with_pos('single letter capital names are reserved for generic template types.', p.error_with_pos('single letter capital names are reserved for generic template types.',
name_pos) name_pos)
@ -322,7 +322,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
p.check(.rcbr) p.check(.rcbr)
} }
is_minify := attrs.contains('minify') is_minify := attrs.contains('minify')
mut t := ast.TypeSymbol{ mut sym := ast.TypeSymbol{
kind: .struct_ kind: .struct_
language: language language: language
name: name name: name
@ -341,11 +341,11 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
} }
is_pub: is_pub is_pub: is_pub
} }
if p.table.has_deep_child_no_ref(&t, name) { if p.table.has_deep_child_no_ref(&sym, name) {
p.error_with_pos('invalid recursive struct `$orig_name`', name_pos) p.error_with_pos('invalid recursive struct `$orig_name`', name_pos)
return ast.StructDecl{} return ast.StructDecl{}
} }
mut ret := p.table.register_sym(t) mut ret := p.table.register_sym(sym)
// allow duplicate c struct declarations // allow duplicate c struct declarations
if ret == -1 && language != .c { if ret == -1 && language != .c {
p.error_with_pos('cannot register struct `$name`, another type with this name exists', p.error_with_pos('cannot register struct `$name`, another type with this name exists',