-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdr.y
211 lines (177 loc) · 4.19 KB
/
xdr.y
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
%{
package main
%}
%union {
decl decl;
typespec typespec;
str string;
bool bool;
enumItem enumItem;
enumItems []enumItem;
decls []decl;
typeUnion typeUnion;
unionCasesDef unionCasesDef;
unionCaseDecls []unionCaseDecl;
unionCaseDecl unionCaseDecl;
strs []string;
progCall progCall;
progCalls []progCall;
progVer progVer;
progVers []progVer;
typespecOpt typespecOpt;
}
%token KWCONST
%token KWTYPEDEF
%token KWENUM
%token KWSTRUCT
%token KWUNION
%token KWSWITCH
%token KWCASE
%token KWDEFAULT
%token KWVOID
%token KWOPAQUE
%token KWSTRING
%token KWUNSIGNED
%token KWINT
%token KWHYPER
%token KWFLOAT
%token KWDOUBLE
%token KWQUADRUPLE
%token KWBOOL
%token KWPROGRAM
%token KWVERSION
%token <str> CONST
%token <str> IDENT
%token '='
%token ';'
%token '<'
%token '>'
%token '['
%token ']'
%token '{'
%token '}'
%token ','
%token ':'
%token '*'
%type <decl> decl
%type <typespec> typespec enumtypespec structtypespec uniontypespec
%type <str> val varlen
%type <bool> maybeunsig
%type <enumItem> enumitem
%type <enumItems> enumbody enumitems
%type <decls> structbody structdecls
%type <typeUnion> unionbody
%type <unionCasesDef> unioncasesdef
%type <unionCaseDecls> unioncases
%type <unionCaseDecl> unioncase
%type <strs> caselist
%type <progCall> progcall
%type <progCalls> progcalls
%type <progVers> progvers
%type <progVer> progver
%type <typespecOpt> typespecopt
%%
spec: | spec defn
defn: typedef | constdef | progdef
decl: typespec IDENT
{ $$ = declName{declTypeTypespec{$1}, $2} }
| typespec IDENT '[' val ']'
{ $$ = declName{declTypeArray{$1, $4}, $2} }
| typespec IDENT varlen
{ $$ = declName{declTypeVarArray{$1, $3}, $2} }
| KWOPAQUE IDENT '[' val ']'
{ $$ = declName{declTypeOpaqueArray{$4}, $2} }
| KWOPAQUE IDENT varlen
{ $$ = declName{declTypeOpaqueVarArray{$3}, $2} }
| KWSTRING IDENT varlen
{ $$ = declName{declTypeString{$3}, $2} }
| typespec '*' IDENT
{ $$ = declName{declTypePtr{$1}, $3} }
| KWVOID
{ $$ = declVoid{} }
varlen: '<' '>'
{ $$ = "" }
| '<' val '>'
{ $$ = $2 }
val: CONST
{ $$ = $1 }
| IDENT
{ $$ = $1 }
typespec: maybeunsig KWINT
{ $$ = typeInt{$1} }
| maybeunsig KWHYPER
{ $$ = typeHyper{$1} }
| KWFLOAT
{ $$ = typeFloat{} }
| KWDOUBLE
{ $$ = typeDouble{} }
| KWQUADRUPLE
{ $$ = typeQuadruple{} }
| KWBOOL
{ $$ = typeBool{} }
| enumtypespec
{ $$ = $1 }
| structtypespec
{ $$ = $1 }
| uniontypespec
{ $$ = $1 }
| IDENT
{ $$ = typeIdent{$1} }
maybeunsig: { $$ = false } | KWUNSIGNED { $$ = true }
enumtypespec: KWENUM enumbody
{ $$ = typeEnum{$2} }
enumbody: '{' enumitems '}'
{ $$ = $2 }
enumitems: enumitem
{ $$ = []enumItem{$1} }
| enumitems ',' enumitem
{ $$ = append($1, $3) }
enumitem: IDENT '=' val
{ $$ = enumItem{$1, $3} }
structtypespec: KWSTRUCT structbody
{ $$ = typeStruct{$2} }
structbody: '{' structdecls '}'
{ $$ = $2 }
structdecls: { $$ = nil } | structdecls decl ';'
{ $$ = append($1, $2) }
uniontypespec: KWUNION unionbody
{ $$ = $2 }
unionbody: KWSWITCH '(' decl ')' '{' unioncasesdef '}'
{ $$ = typeUnion{switchDecl: $3, cases: $6} }
unioncasesdef: unioncases
{ $$ = unionCasesDef{$1, nil} }
| unioncases KWDEFAULT ':' decl ';'
{ $$ = unionCasesDef{$1, $4} }
unioncases: { $$ = nil } | unioncases unioncase
{ $$ = append($1, $2) }
unioncase: caselist decl ';'
{ $$ = unionCaseDecl{$1, $2} }
caselist: KWCASE val ':'
{ $$ = []string{$2} }
| caselist KWCASE val ':'
{ $$ = append($1, $3) }
constdef: KWCONST IDENT '=' CONST ';'
{ emitConst($2, $4) }
typedef: KWTYPEDEF decl ';'
{ emitTypedef($2) }
| KWENUM IDENT enumbody ';'
{ emitEnum($2, $3) }
| KWSTRUCT IDENT structbody ';'
{ emitStruct($2, $3) }
| KWUNION IDENT unionbody ';'
{ emitUnion($2, $3) }
progdef: KWPROGRAM IDENT '{' progvers '}' '=' CONST ';'
{ emitProg(progDef{$2, $4, $7}) }
progvers: { $$ = nil } | progvers progver
{ $$ = append($1, $2) }
progver: KWVERSION IDENT '{' progcalls '}' '=' CONST ';'
{ $$ = progVer{$2, $4, $7} }
progcalls: { $$ = nil } | progcalls progcall
{ $$ = append($1, $2) }
progcall: typespecopt IDENT '(' typespecopt ')' '=' CONST ';'
{ $$ = progCall{$2, $4, $1, $7} }
typespecopt: KWVOID
{ $$ = typespecOpt{ isVoid: true } }
| typespec
{ $$ = typespecOpt{ isVoid: false, t: $1 } }
%%