-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfbs.y
592 lines (540 loc) · 16.2 KB
/
fbs.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// This file is written according to
// https://google.github.io/flatbuffers/flatbuffers_grammar.html
//
// However, the grammar listed in the link is somewhat outdated and
// contains mistakes and ambiguities.
//
// We make some rearrangements, for example,
//
// `object` related grammars are eliminated since no actual usage can be found:
//
// object = { commasep( ident : value ) }
// value = single_value | object | [ commasep( value ) ]
//
// 'values' and 'value' are also eliminated.
//
// 'table' and 'struct' declarations are separated.
//
// 'enum' and 'union' declarations are separated, also are 'enumVals' and 'unionVals'.
//
// The second node of attribute declaration should be string literal rather than identifier.
//
// The fileds of table counld be empty.
//
// 'typeName' can have dots inside.
//
// For .y (yacc) file format, refer to http://dinosaur.compilertools.net/yacc/index.html
// and goyacc https://pkg.go.dev/golang.org/x/tools/cmd/goyacc
//
// You may want to take a look at this .fbs example file:
// https://github.com/google/flatbuffers/blob/master/tests/monster_test.fbs
//
// `%{ }%` encloses declarations and definitions.
%{
package fbs
import "trpc.group/trpc-go/fbs/internal/ast"
%}
// %union define types used in yacc and map them to Go types.
%union{
schema *ast.SchemaNode
include *ast.IncludeNode
includes []*ast.IncludeNode
decl ast.DeclElement
decls []ast.DeclElement
namespaceDecl *ast.NamespaceDeclNode
tableDecl *ast.TableDeclNode
structDecl *ast.StructDeclNode
enumDecl *ast.EnumDeclNode
unionDecl *ast.UnionDeclNode
rootDecl *ast.RootDeclNode
fileExtDecl *ast.FileExtDeclNode
fileIdentDecl *ast.FileIdentDeclNode
attrDecl *ast.AttrDeclNode
rpcDecl *ast.RPCDeclNode
idents *ast.IdentList
typeName *ast.TypeNameNode
identLit ast.IdentLiteralElement
metadata *ast.MetadataNode
field *ast.FieldNode
fields []*ast.FieldNode
metadataEntry *ast.MetadataEntryNode
metadataEntries []*ast.MetadataEntryNode
rpcMethod *ast.RPCMethodNode
rpcMethods []*ast.RPCMethodNode
enumVal *ast.EnumValueNode
enumVals []*ast.EnumValueNode
unionVal *ast.UnionValueNode
unionVals []*ast.UnionValueNode
v ast.ValueNode
iv ast.IntValueNode
fv ast.FloatValueNode
s *ast.StringLiteralNode
b *ast.BoolLiteralNode
i *ast.UintLiteralNode
f *ast.FloatLiteralNode
id *ast.IdentNode
r *ast.RuneNode
err error
}
// non-terminals. %type is used to associate union member names with non-terminals.
%type <schema> schema
// ^^^^^^ ^^^^^^
// | non-terminal
// union member names (declared in %union)
%type <include> include
%type <includes> includes
%type <decl> decl
%type <decls> decls
%type <namespaceDecl> namespaceDecl
%type <tableDecl> tableDecl
%type <structDecl> structDecl
%type <enumDecl> enumDecl
%type <unionDecl> unionDecl
%type <rootDecl> rootDecl
%type <fileExtDecl> fileExtDecl
%type <fileIdentDecl> fileIdentDecl
%type <attrDecl> attrDecl
%type <rpcDecl> rpcDecl
%type <idents> idents
%type <typeName> typeName
%type <metadata> metadata
%type <field> field
%type <fields> fields
%type <metadataEntry> metadataEntry
%type <metadataEntries> metadataEntries
%type <v> singleVal scalar
%type <b> boolLit
%type <iv> intLit
%type <fv> floatLit
%type <identLit> typeLit
%type <id> builtinType
%type <rpcMethod> rpcMethod
%type <rpcMethods> rpcMethods
%type <enumVal> enumVal
%type <enumVals> enumVals
%type <unionVal> unionVal
%type <unionVals> unionVals
// terminals. %token associates union member names with terminals.
%token <s> StrLit // string literal
%token <i> IntLit // integer literal
%token <f> FloatLit // floating point literal
%token <id> Ident // identifiers
%token <id> True False // true false
%token <id> Attribute Bool Byte Double Enum FileExtension
%token <id> FileIdentifier Float Float32 Float64 Include Inf
%token <id> Int Int16 Int32 Int64 Int8 Long Namespace Nan
%token <id> RootType RPCService Short String Struct Table
%token <id> Ubyte Ushort Uint Uint16 Uint32 Uint64 Uint8 Ulong Union
%token <err> Error
// Although some of the tokens defined below are not used directly, they can help
// improve error messages.
%token <r> '=' ';' ':' '{' '}' '\\' '/' '?' '.' ',' '>' '<' '+' '-' '(' ')' '[' ']' '*' '&' '^' '%' '$' '#' '@' '!' '~' '`'
%% // A pair of `%%` `%%` encloses production rules.
// schema is the root node of AST, it is of type *ast.SchemaNode
// $$ represents left hand side of colon, in this case is `schema`
// $1 represents the first node on the right hand side of colon, `includes`
// $2 refers to the second node `decls`
// $3 $4 ..
schema: includes decls {
$$ = ast.NewSchemaNode($1, $2)
fbslex.(*fbsLex).res = $$ // store result into lexer
}
// includes is of type []*ast.IncludeNode
includes: includes include {
if $2 != nil {
$$ = append($1, $2)
} else {
$$ = $1
}
}
| include {
if $1 != nil {
$$ = []*ast.IncludeNode{$1}
} else {
$$ = nil
}
}
| {
$$ = nil
}
// include is of type *ast.IncludeNode
include: Include StrLit ';' {
$$ = ast.NewIncludeNode($1.ToKeyword(), $2, $3)
}
// decls is of type []ast.DeclElement
decls: decls decl {
if $2 != nil {
$$ = append($1, $2)
} else {
$$ = $1
}
}
| decl {
if $1 != nil {
$$ = []ast.DeclElement{$1}
} else {
$$ = nil
}
}
| {
$$ = nil
}
// decl is of type ast.DeclElement (an interface)
decl: namespaceDecl {
$$ = $1
}
| tableDecl {
$$ = $1
}
| structDecl {
$$ = $1
}
| enumDecl {
$$ = $1
}
| unionDecl {
$$ = $1
}
| rootDecl {
$$ = $1
}
| fileExtDecl {
$$ = $1
}
| fileIdentDecl {
$$ = $1
}
| attrDecl {
$$ = $1
}
| rpcDecl {
$$ = $1
}
| error ';' {
$$ = nil
}
| error {
$$ = nil
}
// namespaceDecl is of type *ast.NamespaceDeclNode
namespaceDecl: Namespace idents ';' {
$$ = ast.NewNamespaceDeclNode($1.ToKeyword(), $2.ToIdentValueNode(nil), $3)
}
// idents is of type *ast.IdentList
idents: Ident {
$$ = &ast.IdentList{$1, nil, nil}
}
| Ident '.' idents {
$$ = &ast.IdentList{$1, $2, $3}
}
// attrDecl is of type *ast.AttrDeclNode
// The second node should be string literal rather than identifier, the original grammar is wrong.
attrDecl: Attribute StrLit ';' {
$$ = ast.NewAttrDeclNode($1.ToKeyword(), $2, $3)
}
// tableDecl is of type *ast.TableDeclNode
tableDecl: Table Ident metadata '{' fields '}' {
var opts []ast.TableDeclOption
opts = append(opts, ast.WithTableKeyword($1.ToKeyword()))
opts = append(opts, ast.WithTableName($2))
opts = append(opts, ast.WithTableMetadata($3))
opts = append(opts, ast.WithTableOpenBrace($4))
opts = append(opts, ast.WithTableFields($5))
opts = append(opts, ast.WithTableCloseBrace($6))
$$ = ast.NewTableDeclNode(opts...)
}
// structDecl is of type *ast.StructDeclNode
structDecl: Struct Ident metadata '{' fields '}' {
var opts []ast.StructDeclOption
opts = append(opts, ast.WithStructKeyword($1.ToKeyword()))
opts = append(opts, ast.WithStructName($2))
opts = append(opts, ast.WithStructMetadata($3))
opts = append(opts, ast.WithStructOpenBrace($4))
opts = append(opts, ast.WithStructFields($5))
opts = append(opts, ast.WithStructCloseBrace($6))
$$ = ast.NewStructDeclNode(opts...)
}
// enumDecl is of type *ast.EnumDeclNode
enumDecl: Enum Ident ':' typeName metadata '{' enumVals '}' {
var opts []ast.EnumDeclOption
opts = append(opts, ast.WithEnumKeyword($1.ToKeyword()))
opts = append(opts, ast.WithEnumName($2))
opts = append(opts, ast.WithEnumColon($3))
opts = append(opts, ast.WithEnumTypeName($4))
opts = append(opts, ast.WithEnumMetadata($5))
opts = append(opts, ast.WithEnumOpenBrace($6))
opts = append(opts, ast.WithEnumDecls($7))
opts = append(opts, ast.WithEnumCloseBrace($8))
$$ = ast.NewEnumDeclNode(opts...)
}
// unionDecl is of type *ast.UnionDeclNode
unionDecl: Union Ident metadata '{' unionVals '}' {
var opts []ast.UnionDeclOption
opts = append(opts, ast.WithUnionKeyword($1.ToKeyword()))
opts = append(opts, ast.WithUnionName($2))
opts = append(opts, ast.WithUnionMetadata($3))
opts = append(opts, ast.WithUnionOpenBrace($4))
opts = append(opts, ast.WithUnionDecls($5))
opts = append(opts, ast.WithUnionCloseBrace($6))
$$ = ast.NewUnionDeclNode(opts...)
}
// rootDecl is of type *ast.RootDeclNode
rootDecl: RootType Ident ';' {
$$ = ast.NewRootDeclNode($1.ToKeyword(), $2, $3)
}
// fileExtDecl is of type *ast.FileExtDeclNode
fileExtDecl: FileExtension StrLit ';' {
$$ = ast.NewFileExtDeclNode($1.ToKeyword(), $2, $3)
}
// fileIdentDecl is of type *ast.FileIdentDeclNode
fileIdentDecl: FileIdentifier StrLit ';' {
$$ = ast.NewFileIdentDeclNode($1.ToKeyword(), $2, $3)
}
// rpcDecl is of type *ast.RPCDeclNode
rpcDecl: RPCService Ident '{' rpcMethods '}' {
$$ = ast.NewRPCDeclNode($1.ToKeyword(), $2, $3, $4, $5)
}
// rpcMethods is of type []*ast.RPCMethodNode
rpcMethods: rpcMethod {
$$ = []*ast.RPCMethodNode{$1}
}
| rpcMethods rpcMethod {
$$ = append($1, $2)
}
// rpcMethod is of type *ast.RPCMethodNode
rpcMethod: Ident '(' idents ')' ':' idents metadata ';' {
var opts []ast.MethodOption
opts = append(opts, ast.WithMethodName($1))
opts = append(opts, ast.WithMethodOpenParen($2))
opts = append(opts, ast.WithMethodReqName($3.ToIdentValueNode(nil)))
opts = append(opts, ast.WithMethodCloseParen($4))
opts = append(opts, ast.WithMethodColon($5))
opts = append(opts, ast.WithMethodRspName($6.ToIdentValueNode(nil)))
opts = append(opts, ast.WithMethodMetadata($7))
opts = append(opts, ast.WithMethodSemicolon($8))
$$ = ast.NewRPCMethodNode(opts...)
}
// enumVals is of type []*ast.EnumValueNode
enumVals: enumVal {
$$ = []*ast.EnumValueNode{$1}
}
| enumVal ',' enumVals { // so the last item can have trailing ','
if $3 != nil {
$$ = append($3, $1)
} else {
$$ = []*ast.EnumValueNode{$1}
}
}
| {
$$ = nil
}
// enumVal is of type *ast.EnumValueNode
enumVal: Ident {
$$ = ast.NewEnumValueNode($1, nil, nil)
}
| Ident '=' intLit {
$$ = ast.NewEnumValueNode($1, $2, $3)
}
// unionVals is of type []*ast.UnionValueNode
unionVals: unionVal {
$$ = []*ast.UnionValueNode{$1}
}
| unionVal ',' unionVals { // so the last item can have trailing ','
if $3 != nil {
$$ = append($3, $1)
} else {
$$ = []*ast.UnionValueNode{$1}
}
}
| {
$$ = nil
}
// unionVal is of type *ast.UnionValueNode
unionVal: typeName {
$$ = ast.NewUnionValueNode(nil, nil, $1)
}
| Ident ':' typeName {
$$ = ast.NewUnionValueNode($1, $2, $3)
}
// fields is of type []*ast.FieldNode
fields: field {
$$ = []*ast.FieldNode{$1}
}
| fields field {
$$ = append($1, $2)
}
| {
$$ = nil // allow empty (different from original grammar, but is needed in monster.fbs)
}
// field is of type *ast.FieldNode
field: Ident ':' typeName metadata ';' {
var opts []ast.FieldOption
opts = append(opts, ast.WithFieldName($1))
opts = append(opts, ast.WithFieldColon($2))
opts = append(opts, ast.WithFieldTypeName($3))
opts = append(opts, ast.WithFieldMetadata($4))
opts = append(opts, ast.WithFieldSemicolon($5))
$$ = ast.NewFieldNode(opts...)
}
| Ident ':' typeName '=' scalar metadata ';' {
var opts []ast.FieldOption
opts = append(opts, ast.WithFieldName($1))
opts = append(opts, ast.WithFieldColon($2))
opts = append(opts, ast.WithFieldTypeName($3))
opts = append(opts, ast.WithFieldEqual($4))
opts = append(opts, ast.WithFieldScalar($5))
opts = append(opts, ast.WithFieldMetadata($6))
opts = append(opts, ast.WithFieldSemicolon($7))
$$ = ast.NewFieldNode(opts...)
}
| Ident ':' typeName '=' Ident metadata ';' { // case: "color: Color = Green;"
var opts []ast.FieldOption
opts = append(opts, ast.WithFieldName($1))
opts = append(opts, ast.WithFieldColon($2))
opts = append(opts, ast.WithFieldTypeName($3))
opts = append(opts, ast.WithFieldEqual($4))
opts = append(opts, ast.WithFieldScalar($5))
opts = append(opts, ast.WithFieldMetadata($6))
opts = append(opts, ast.WithFieldSemicolon($7))
$$ = ast.NewFieldNode(opts...)
}
// metadata is of type *ast.MetadataNode
metadata: '(' metadataEntries ')' {
$$ = ast.NewMetadataNode($1, $2, $3)
}
| {
$$ = nil
}
// metadataEntries is of type []*ast.MetadataEntryNode
metadataEntries: metadataEntry {
$$ = []*ast.MetadataEntryNode{$1}
}
| metadataEntries ',' metadataEntry {
$$ = append($1, $3)
}
| {
$$ = nil
}
// metadataEntry is of type *ast.MetadataEntryNode
metadataEntry: Ident {
$$ = ast.NewMetadataEntryNode($1, nil, nil)
}
| Ident ':' singleVal {
$$ = ast.NewMetadataEntryNode($1, $2, $3)
}
// singleVal is of type ast.ValueNode (an interface)
singleVal: scalar { // ast.ValueNode
$$ = $1
}
| StrLit { // *ast.StringLiteralNode
$$ = $1
}
// singleVal(ValueNode)
// |
// scalar(ValueNode) or StrLit(*StringLiteralNode)
// |
// boolLit or intLit or floatLit
// (*BoolLiteralNode) (IntValueNode) (FloatValueNode)
// | | \
// True or False (*UintLiteralNode) (*FloatLiteralNode)
// (*IdentNode) (*NegativeIntLiteralNode) (*SpecialFloatLiteralNode)
// (*PositiveUintLiteralNode) (*SignedFloatLiteralNode)
//
// Note: types without '*' are all `interface`s.
// scalar is of type ast.ValueNode
scalar: boolLit { // *ast.BoolLiteralNode
$$ = $1
}
| intLit { // ast.IntValueNode (an interface)
$$ = $1
}
| floatLit { // ast.FloatValueNode (an interface)
$$ = $1
}
// boolLit is of type *ast.BoolLiteralNode
boolLit: True {
$$ = ast.NewBoolLiteralNode($1.ToKeyword())
}
| False {
$$ = ast.NewBoolLiteralNode($1.ToKeyword())
}
// intLit is of type ast.IntValueNode
intLit: IntLit { // *ast.UintLiteralNode
$$ = $1
}
| '+' IntLit { // *ast.PositiveUintLiteralNode
$$ = ast.NewPositiveUintLiteralNode($1, $2)
}
| '-' IntLit { // *ast.NegativeIntLiteralNode
$$ = ast.NewNegativeIntLiteralNode($1, $2)
}
// floatLit is of type ast.FloatValueNode (an interface)
floatLit: FloatLit { // *ast.FloatLiteralNode
$$ = $1
}
| '-' FloatLit { // *ast.SignedFloatLiteralNode
$$ = ast.NewSignedFloatLiteralNode($1, $2)
}
| '+' FloatLit { // *ast.SignedFloatLiteralNode
$$ = ast.NewSignedFloatLiteralNode($1, $2)
}
| Inf { // *ast.SpecialFloatLiteralNode
$$ = ast.NewSpecialFloatLiteralNode($1.ToKeyword())
}
| '+' Inf { // *ast.SignedFloatLiteralNode
f := ast.NewSpecialFloatLiteralNode($2.ToKeyword())
$$ = ast.NewSignedFloatLiteralNode($1, f)
}
| '-' Inf { // *ast.SignedFloatLiteralNode
f := ast.NewSpecialFloatLiteralNode($2.ToKeyword())
$$ = ast.NewSignedFloatLiteralNode($1, f)
}
| Nan { // *ast.SpecialFloatLiteralNode
$$ = ast.NewSpecialFloatLiteralNode($1.ToKeyword())
}
| '+' Nan { // *ast.SignedFloatLiteralNode
f := ast.NewSpecialFloatLiteralNode($2.ToKeyword())
$$ = ast.NewSignedFloatLiteralNode($1, f)
}
| '-' Nan { // *ast.SignedFloatLiteralNode
f := ast.NewSpecialFloatLiteralNode($2.ToKeyword())
$$ = ast.NewSignedFloatLiteralNode($1, f)
}
// typeName is of type *ast.TypeNameNode
typeName: typeLit {
$$ = ast.NewTypeNameNode(nil, $1, nil)
}
| '[' typeLit ']' { // [typeName] means vector of types
$$ = ast.NewTypeNameNode($1, $2, $3)
}
// typeLit is of type ast.IdentLiteralElement
typeLit: idents { // *ast.IdentList => ast.IdentLiteralElement
$$ = $1.ToIdentValueNode(nil)
}
| builtinType {
$$ = ast.IdentLiteralElement($1)
}
// builtinType is of type *ast.IdentNode
builtinType: Bool
| Byte
| Ubyte
| Short
| Ushort
| Int
| Uint
| Float
| Long
| Ulong
| Double
| Int8
| Uint8
| Int16
| Uint16
| Int32
| Uint32
| Int64
| Uint64
| Float32
| Float64
| String
%%