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() // . p.next() // .
} }
is_typedef := p.attr == 'typedef' 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() mut name := p.check_name()
// println('struct decl $name') // println('struct decl $name')
p.check(.lcbr)
mut ast_fields := []ast.StructField mut ast_fields := []ast.StructField
mut fields := []table.Field mut fields := []table.Field
mut mut_pos := -1 mut mut_pos := -1
mut pub_pos := -1 mut pub_pos := -1
mut pub_mut_pos := -1 mut pub_mut_pos := -1
for p.tok.kind != .rcbr { if !no_body {
mut comment := ast.Comment{} p.check(.lcbr)
if p.tok.kind == .comment { for p.tok.kind != .rcbr {
comment = p.comment() mut comment := ast.Comment{}
} if p.tok.kind == .comment {
if p.tok.kind == .key_pub { comment = p.comment()
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) if p.tok.kind == .key_pub {
} else if p.tok.kind == .key_mut { p.check(.key_pub)
p.check(.key_mut) if p.tok.kind == .key_mut {
p.check(.colon) p.check(.key_mut)
mut_pos = fields.len pub_mut_pos = fields.len
} else if p.tok.kind == .key_global { } else {
p.check(.key_global) pub_pos = fields.len
p.check(.colon) }
} p.check(.colon)
field_name := p.check_name() } else if p.tok.kind == .key_mut {
field_pos := p.tok.position() p.check(.key_mut)
// p.warn('field $field_name') p.check(.colon)
typ := p.parse_type() mut_pos = fields.len
/* } else if p.tok.kind == .key_global {
if name == '_net_module_s' { 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) s := p.table.get_type_symbol(typ)
println('XXXX' + s.str()) println('XXXX' + s.str())
} }
*/ */
mut default_expr := '' // ast.Expr{} mut default_expr := '' // ast.Expr{}
if p.tok.kind == .assign { if p.tok.kind == .assign {
// Default value // Default value
p.next() p.next()
default_expr = p.tok.lit default_expr = p.tok.lit
p.expr(0) p.expr(0)
// default_expr = 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 { p.check(.rcbr)
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)
if is_c { if is_c {
name = 'C.$name' name = 'C.$name'
} else { } else {