diff options
author | wszqkzqk <wszqkzqk@qq.com> | 2023-01-04 19:42:17 +0800 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2023-01-05 12:56:47 +0100 |
commit | f82f63c58128be1dbba03bb4991f6fb55892a85d (patch) | |
tree | d241a997bfdebc5d5515b83c6e344cccf4448d4e /vala | |
parent | 8812129a8d912a9343dccb34f8d7126071f6ec19 (diff) | |
download | vala-f82f63c58128be1dbba03bb4991f6fb55892a85d.tar.gz |
parser: Support one-line declaration of multiple fields in classes/structs
Fixes https://gitlab.gnome.org/GNOME/vala/issues/6
Diffstat (limited to 'vala')
-rw-r--r-- | vala/valaparser.vala | 73 |
1 files changed, 38 insertions, 35 deletions
diff --git a/vala/valaparser.vala b/vala/valaparser.vala index 25769ec54..9ac94e4a5 100644 --- a/vala/valaparser.vala +++ b/vala/valaparser.vala @@ -2793,6 +2793,7 @@ public class Vala.Parser : CodeVisitor { return; } case TokenType.ASSIGN: + case TokenType.COMMA: case TokenType.SEMICOLON: rollback (begin); switch (last_keyword) { @@ -3143,46 +3144,48 @@ public class Vala.Parser : CodeVisitor { var access = parse_access_modifier ((parent is Struct) ? SymbolAccessibility.PUBLIC : SymbolAccessibility.PRIVATE); var flags = parse_member_declaration_modifiers (); var type = parse_type (true, true); - string id = parse_identifier (); - type = parse_inline_array_type (type); + do { + string id = parse_identifier (); + var ftype = parse_inline_array_type (type.copy ()); - var f = new Field (id, type, null, get_src (begin), comment); - f.access = access; + var f = new Field (id, ftype, null, get_src (begin), comment); + f.access = access; - set_attributes (f, attrs); - if (ModifierFlags.STATIC in flags && ModifierFlags.CLASS in flags) { - Report.error (f.source_reference, "only one of `static' or `class' may be specified"); - } else if (ModifierFlags.STATIC in flags) { - f.binding = MemberBinding.STATIC; - } else if (ModifierFlags.CLASS in flags) { - f.binding = MemberBinding.CLASS; - } else if (parent is Namespace) { - // default to static member binding in namespace - f.binding = MemberBinding.STATIC; - } + set_attributes (f, attrs); + if (ModifierFlags.STATIC in flags && ModifierFlags.CLASS in flags) { + Report.error (f.source_reference, "only one of `static' or `class' may be specified"); + } else if (ModifierFlags.STATIC in flags) { + f.binding = MemberBinding.STATIC; + } else if (ModifierFlags.CLASS in flags) { + f.binding = MemberBinding.CLASS; + } else if (parent is Namespace) { + // default to static member binding in namespace + f.binding = MemberBinding.STATIC; + } - if (!parent.external_package && parent is Struct - && f.access != SymbolAccessibility.PUBLIC && f.binding == MemberBinding.INSTANCE) { - Report.warning (f.source_reference, "accessibility of struct fields can only be `public`"); - } + if (!parent.external_package && parent is Struct + && f.access != SymbolAccessibility.PUBLIC && f.binding == MemberBinding.INSTANCE) { + Report.warning (f.source_reference, "accessibility of struct fields can only be `public`"); + } - if (ModifierFlags.ABSTRACT in flags - || ModifierFlags.VIRTUAL in flags - || ModifierFlags.OVERRIDE in flags) { - Report.error (f.source_reference, "abstract, virtual, and override modifiers are not applicable to fields"); - } - if (ModifierFlags.EXTERN in flags) { - f.is_extern = true; - } - if (ModifierFlags.NEW in flags) { - f.hides = true; - } - if (accept (TokenType.ASSIGN)) { - f.initializer = parse_expression (); - } - expect (TokenType.SEMICOLON); + if (ModifierFlags.ABSTRACT in flags + || ModifierFlags.VIRTUAL in flags + || ModifierFlags.OVERRIDE in flags) { + Report.error (f.source_reference, "abstract, virtual, and override modifiers are not applicable to fields"); + } + if (ModifierFlags.EXTERN in flags) { + f.is_extern = true; + } + if (ModifierFlags.NEW in flags) { + f.hides = true; + } + if (accept (TokenType.ASSIGN)) { + f.initializer = parse_expression (); + } - parent.add_field (f); + parent.add_field (f); + } while (accept (TokenType.COMMA)); + expect (TokenType.SEMICOLON); } InitializerList parse_initializer () throws ParseError { |