-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
41 lines (32 loc) · 1.2 KB
/
parser.mly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
%token LEFT_BRACE RIGHT_BRACE
%token LEFT_BRACKET RIGHT_BRACKET
%token TYPE ENUM STRUCT
%token DOT STAR SEMICOLON
%token <string> INTEGER IDENT
%token EOF
%start <(string * Ast.typ) list> main
%%
main: types = list(typ) EOF { types };
typ: TYPE name = IDENT lit = type_literal SEMICOLON? { (name, lit) };
type_literal:
| STRUCT LEFT_BRACE entries = separated_list(SEMICOLON, struct_entry) RIGHT_BRACE { Ast.Struct entries }
| ENUM LEFT_BRACE entries = separated_list(SEMICOLON, enum_entry) RIGHT_BRACE { Ast.Enum entries }
;
type_ref:
| LEFT_BRACKET RIGHT_BRACKET sel = type_ref { Ast.Slice sel }
| LEFT_BRACKET n = INTEGER RIGHT_BRACKET sel = type_ref { Ast.Array (n, sel) }
| STAR sel = type_ref { Ast.Pointer sel }
| name = IDENT sel = type_sel { Ast.Selector (name, sel) }
;
type_sel:
| DOT name = IDENT sel = type_sel { name :: sel }
| { [] }
;
struct_entry:
| TYPE name = IDENT lit = type_literal { Ast.StructNested (name, lit) }
| name = IDENT ref = type_ref { Ast.StructField (name, ref) }
;
enum_entry:
| TYPE name = IDENT lit = type_literal { Ast.EnumNested (name, lit) }
| name = IDENT ref = option(type_ref) { Ast.EnumCase (name, ref) }
;