From 10c4c44d0337ff30411845a2f2511aac7b7d52aa Mon Sep 17 00:00:00 2001 From: hazohelet <48541090+hazohelet@users.noreply.github.com> Date: Wed, 8 Apr 2020 23:54:49 +0900 Subject: [PATCH] parser: allow C struct declaration that lacks body --- vlib/v/parser/parser.v | 110 ++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 60e903d1c1..32d38ac850 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 {