parser: allow C struct declaration that lacks body

pull/4294/head
hazohelet 2020-04-08 23:54:49 +09:00 committed by GitHub
parent a61654009f
commit 10c4c44d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 58 additions and 52 deletions

View File

@ -1460,72 +1460,78 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
p.next() // .
}
is_typedef := p.attr == 'typedef'
no_body := p.peek_tok.kind != .lcbr
if !is_c && no_body {
p.error('`$p.tok.lit` lacks body')
}
mut name := p.check_name()
// println('struct decl $name')
p.check(.lcbr)
mut ast_fields := []ast.StructField
mut fields := []table.Field
mut mut_pos := -1
mut pub_pos := -1
mut pub_mut_pos := -1
for p.tok.kind != .rcbr {
mut comment := ast.Comment{}
if p.tok.kind == .comment {
comment = p.comment()
}
if p.tok.kind == .key_pub {
p.check(.key_pub)
if p.tok.kind == .key_mut {
p.check(.key_mut)
pub_mut_pos = fields.len
} else {
pub_pos = fields.len
if !no_body {
p.check(.lcbr)
for p.tok.kind != .rcbr {
mut comment := ast.Comment{}
if p.tok.kind == .comment {
comment = p.comment()
}
p.check(.colon)
} else if p.tok.kind == .key_mut {
p.check(.key_mut)
p.check(.colon)
mut_pos = fields.len
} else if p.tok.kind == .key_global {
p.check(.key_global)
p.check(.colon)
}
field_name := p.check_name()
field_pos := p.tok.position()
// p.warn('field $field_name')
typ := p.parse_type()
/*
if name == '_net_module_s' {
if p.tok.kind == .key_pub {
p.check(.key_pub)
if p.tok.kind == .key_mut {
p.check(.key_mut)
pub_mut_pos = fields.len
} else {
pub_pos = fields.len
}
p.check(.colon)
} else if p.tok.kind == .key_mut {
p.check(.key_mut)
p.check(.colon)
mut_pos = fields.len
} else if p.tok.kind == .key_global {
p.check(.key_global)
p.check(.colon)
}
field_name := p.check_name()
field_pos := p.tok.position()
// p.warn('field $field_name')
typ := p.parse_type()
/*
if name == '_net_module_s' {
s := p.table.get_type_symbol(typ)
println('XXXX' + s.str())
}
*/
mut default_expr := '' // ast.Expr{}
if p.tok.kind == .assign {
// Default value
p.next()
default_expr = p.tok.lit
p.expr(0)
// default_expr = p.expr(0)
mut default_expr := '' // ast.Expr{}
if p.tok.kind == .assign {
// Default value
p.next()
default_expr = p.tok.lit
p.expr(0)
// default_expr = p.expr(0)
}
if p.tok.kind == .comment {
comment = p.comment()
}
ast_fields << ast.StructField{
name: field_name
pos: field_pos
typ: typ
comment: comment
default_expr: default_expr
}
fields << table.Field{
name: field_name
typ: typ
default_val: default_expr
}
// println('struct field $ti.name $field_name')
}
if p.tok.kind == .comment {
comment = p.comment()
}
ast_fields << ast.StructField{
name: field_name
pos: field_pos
typ: typ
comment: comment
default_expr: default_expr
}
fields << table.Field{
name: field_name
typ: typ
default_val: default_expr
}
// println('struct field $ti.name $field_name')
p.check(.rcbr)
}
p.check(.rcbr)
if is_c {
name = 'C.$name'
} else {