Add unions, enums

edge_only_predecessors
Max Brunsfeld 2014-09-06 17:32:13 +07:00
parent c91415338d
commit 474975e32b
3 changed files with 142 additions and 43 deletions

@ -16,15 +16,19 @@ module.exports = compiler.grammar
rules:
program: -> repeat(choice(
@preproc_call,
@typedef,
@var_declaration))
preproc_call: -> choice(
@preproc_include,
@preproc_define,
seq(@preproc_directive, /.*/)
)
@preproc_call,
# declarations
@struct_declaration,
@union_declaration,
@enum_declaration,
@function_declaration,
@var_declaration,
@typedef,
))
preproc_include: -> seq(
keyword("#include"),
@ -35,6 +39,9 @@ module.exports = compiler.grammar
@identifier,
/.+/)
preproc_call: -> choice(
seq(@preproc_directive, /.*/))
preproc_directive: -> /#\a\w+/
typedef: -> seq(
@ -44,33 +51,74 @@ module.exports = compiler.grammar
";")
var_declaration: -> seq(
repeat(@type_modifier),
@identifier,
@type,
commaSep1(@type_expression),
";")
type_expression: -> choice(
function_declaration: -> seq(
@type,
@identifier,
@pointer_type)
"(", @formal_parameters, ")",
";")
type: -> seq(
repeat(@type_modifier),
choice(
@identifier,
@struct_type))
formal_parameters: ->
err(commaSep(@field))
type: -> choice(
type_expression: -> choice(
@identifier,
@struct_type)
@pointer_type)
pointer_type: -> seq(
"*", @type_expression)
struct_declaration: -> seq(
@struct_type, ";")
struct_type: -> seq(
keyword("struct"),
"{", err(repeat(seq(@field, ";"))), "}")
choice(
@identifier,
seq(
optional(@identifier),
"{", err(repeat(seq(@field, ";"))), "}")))
union_declaration: -> seq(
@union_type, ";")
union_type: -> seq(
keyword("union"),
choice(
@identifier,
seq(
optional(@identifier),
"{", err(repeat(seq(@field, ";"))), "}")))
enum_declaration: -> seq(
@enum_type, ";")
enum_type: -> seq(
keyword("enum"),
choice(
@identifier,
seq(
optional(@identifier),
"{", err(commaSep(@enum_value)), "}")))
enum_value: -> seq(
@identifier, optional(seq("=", @number)))
field: -> seq(@type, @type_expression)
type_modifier: -> choice(
keyword("const"),
keyword("static"),
keyword("inline"),
keyword("extern"))
keyword("const"))
number: -> /\d+(\.\d+)?/
string: -> token(seq(
'"', repeat(choice(/[^"]/, '\\"')), '"'))

@ -2,30 +2,90 @@
Simple variable declarations
============================================
size_t the_size;
int x;
float y, z;
---
(program
(var_declaration (identifier) (identifier)))
(var_declaration (type (identifier)) (identifier))
(var_declaration (type (identifier)) (identifier) (identifier)))
============================================
Pointer variable declarations
============================================
char *the_string;
const char **the_string, **the_other_string;
const char **the_strings;
---
(program
(var_declaration (identifier) (pointer_type (identifier)))
(var_declaration (type_modifier) (identifier)
(pointer_type (pointer_type (identifier)))
(var_declaration (type (identifier)) (pointer_type (identifier)))
(var_declaration (type (type_modifier) (identifier))
(pointer_type (pointer_type (identifier)))))
============================================
Simple typedefs
Struct declarations
============================================
struct s1;
struct s2 {
int x;
float y;
};
---
(program
(struct_declaration (struct_type (identifier)))
(struct_declaration (struct_type (identifier)
(field (type (identifier)) (identifier))
(field (type (identifier)) (identifier)))))
============================================
Union declarations
============================================
union u1;
union s2 {
int x;
float y;
};
---
(program
(union_declaration (union_type (identifier)))
(union_declaration (union_type (identifier)
(field (type (identifier)) (identifier))
(field (type (identifier)) (identifier)))))
============================================
Enum declarations
============================================
enum e1;
enum e2 {
val1,
val2 = 5,
val3
};
---
(program
(enum_declaration (enum_type (identifier)))
(enum_declaration (enum_type (identifier)
(identifier)
(enum_value (identifier) (number))
(identifier))))
============================================
Typedefs
============================================
typedef int my_int;
@ -33,29 +93,20 @@ typedef int my_int;
---
(program
(typedef (identifier) (identifier)))
(typedef (type (identifier)) (identifier)))
============================================
Struct typedefs
Function declarations
============================================
typedef struct {
size_t i;
struct {
float x;
float y;
} pos;
} my_struct;
int main(int argc, const char **argv);
---
(program (typedef
(struct_type
(field (identifier) (identifier))
(field
(struct_type
(field (identifier) (identifier))
(field (identifier) (identifier)))
(identifier)))
(identifier)))
(program (function_declaration
(type (identifier))
(identifier)
(formal_parameters
(field (type (identifier)) (identifier))
(field (type (type_modifier) (identifier)) (pointer_type (pointer_type (identifier)))))))