~bduggan/raku-protobuf

ref: 204a01ffb6bd0849698e8adf47a3b1bc4030f020 raku-protobuf/lib/Protobuf/Grammar.rakumod -rw-r--r-- 7.1 KiB
204a01ffBrian Duggan doc 7 months ago
                                                                                
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
use Grammar::PrettyErrors;

grammar Protobuf::Grammar does Grammar::PrettyErrors {

token ws {
  <!ww>
  [
    \s
    | [ '//' \V* [$|\n] ]
  ]*
}

# letter = "A" … "Z" | "a" … "z"
regex letter { <[A..Z] + [a..z]> }

# decimalDigit = "0" … "9"
regex decimalDigit { <[0..9]> }

# octalDigit   = "0" … "7"
regex octalDigit { <[0..7]> }

# hexDigit     = "0" … "9" | "A" … "F" | "a" … "f"
regex hexDigit { <[0..9A..Fa..f]> }

# ident = letter { letter | decimalDigit | "_" }
regex ident {
  <.letter>
  [ <.letter> | <.decimalDigit> | "_" ]*
}

# fullIdent = ident { "." ident }
regex fullIdent { <ident>+ % '.' }

# messageName = ident
# enumName = ident
# fieldName = ident
# oneofName = ident
# mapName = ident
# serviceName = ident
# rpcName = ident
regex messageName { <ident> }
regex enumName { <ident> }
regex fieldName { <ident> }
regex oneofName { <ident> }
regex mapName { <ident> }
regex serviceName { <ident> }
regex rpcName { <ident> }

# messageType = [ "." ] { ident "." } messageName
regex messageType { [ "." ]? [ <ident> '.' ]* <messageName> }

# enumType = [ "." ] { ident "." } enumName
regex enumType { [ '.' ] [ <ident> '.']+ <enumName> }

# intLit     = decimalLit | octalLit | hexLit
regex intLit { <decimalLit> | <octalLit> | <hexLit> }

# decimalLit = ( "1" … "9" ) { decimalDigit }
regex decimalLit { <[1..9]> <decimalDigit>* }

# octalLit   = "0" { octalDigit }
regex octalLit { '0' <octalDigit>* }

# hexLit = "0" ( "x" | "X" ) hexDigit { hexDigit }
regex hexLit { '0' <[xX]> <hexDigit>+ }

# floatLit = ( decimals "." [ decimals ] [ exponent ] | decimals exponent | "."decimals [ exponent ] )
#            | "inf" | "nan"
regex floatLit {
  [ | <decimals> '.' <decimals>? <exponent>?
    | <decimals> <exponent>
    | '.' <decimals> <exponent>? 
    ]
  | 'inf' | 'nan'
}

# decimals  = decimalDigit { decimalDigit }
regex decimals { <decimalDigit>+ }

# exponent  = ( "e" | "E" ) [ "+" | "-" ] decimals
regex exponent  {
  <[eE]> <[+-]> <decimals>
}

# boolLit = "true" | "false"
regex boolLit {
  true|false
}

# strLit = ( "'" { charValue } "'" ) |  ( '"' { charValue } '"' )
regex strLit {
  | "'" <.charValue>* "'"
  | '"' <.charValue>* '"'
}

# charValue = hexEscape | octEscape | charEscape | /[^\0\n\\]/
regex charValue {
  <hexEscape> | <octEscape> | <charEscape> | <-[\0\n\\]>
}

# hexEscape = '\' ( "x" | "X" ) hexDigit hexDigit
regex hexEscape {
  '\\' <[xX]> <hexDigit>**2
}

# octEscape = '\' octalDigit octalDigit octalDigit
regex octEscape {
  '\\' <octalDigit>**3
}

# charEscape = '\' ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | '\' | "'" | '"' )
regex charEscape {
  '\\' <[abfnrtv\\'"]>
}

# quote = "'" | '"'
regex quote {
  <['"]>
}

# emptyStatement = ";"
regex emptyStatement {
  ';'
}

# constant = fullIdent | ( [ "-" | "+" ] intLit ) | ( [ "-" | "+" ] floatLit ) | strLit | boolLit
regex constant {
  | <fullIdent>
  | [ <[-+]>? <intLit> ]
  | [ <[-+]>? <floatLit> ]
  | <strLit>
  | <boolLit>
}

# syntax = "syntax" "=" quote "proto3" quote ";"
rule syntax {
  syntax '=' <quote> proto3 $<quote> ';'
}

# import = "import" [ "weak" | "public" ] strLit ";" </pre>
rule import {
  import [ weak | public | "" ] <strLit> ';'
}

# package = "package" fullIdent ";"
rule package {
  package <fullIdent> ';'
}

# option = "option" optionName  "=" constant ";"
rule option {
    option <optionName> '=' <constant> ';'
}

# optionName = ( ident | "(" fullIdent ")" ) { "." ident }
rule optionName {
  [ <ident> | '(' <fullIdent> ')' ]
  [ '.' <ident> ]*
}

# type = "double" | "float" | "int32" | "int64" | "uint32" | "uint64"
#       | "sint32" | "sint64" | "fixed32" | "fixed64" | "sfixed32" | "sfixed64"
#       | "bool" | "string" | "bytes" | messageType | enumType
regex type {
 < double float int32 int64 uint32 uint64
   sint32 sint64 fixed32 fixed64 sfixed32 sfixed64
   bool string bytes> | <messageType> | <enumType>
}

# fieldNumber = intLit
regex fieldNumber {
  <intLit>
}

# field = [ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
rule field {
  [ repeated ]? <type> <fieldName> '=' <fieldNumber> [ '[' <fieldOptions> ']' ]? ';'
}

# fieldOptions = fieldOption { ","  fieldOption }
regex fieldOptions {
  <fieldOption>+ % ','
}

# fieldOption = optionName "=" constant
regex fieldOption {
 <optionName> '=' <constant>
}

# oneof = "oneof" oneofName "{" { oneofField | emptyStatement } "}"
rule oneof {
  oneof <oneofName> '{' [ <oneofField> | <emptyStatement> ]* '}'
}

# oneofField = type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
rule oneofField {
  <type> <fieldName> '=' <fieldNumber> [ '[' <fieldOptions> ']' ]? ';'
}

# mapField="map" "<" " "," keytype type>" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
rule mapField {
  map '<' <keyType> ',' <type> '>' <mapName> '=' <fieldNumber> [ '[' <fieldOptions> ']' ]? ';'
}
#   map<int32, string> my_map = 4;

# keyType = "int32" | "int64" | "uint32" | "uint64" | "sint32" | "sint64" |
#           "fixed32" | "fixed64" | "sfixed32" | "sfixed64" | "bool" | "string"
regex keyType {
 < int32 int64 uint32 uint64 sint32 sint64 
   fixed32 fixed64 sfixed32 sfixed64 bool string >
}

# reserved = "reserved" ( ranges | fieldNames ) ";"
rule reserved {
 reserved [ <ranges> | <fieldNames> ] ';'
}

# ranges = range { "," range }
rule ranges {
  <range>+ % ','
}

# range =  intLit [ "to" ( intLit | "max" ) ]
rule range {
  <intLit> [ to [ <intLit> | max ] ]?
}

# fieldNames = fieldName { "," fieldName }
rule fieldNames {
  <fieldName>+ % ','
}

# enum = "enum" enumName enumBody
rule enum {
 enum <enumName> <enumBody>
}

# enumBody = "{" { option | enumField | emptyStatement } "}"
rule enumBody {
  '{' [ <option> | <enumField> | <emptyStatement> ]* '}'
}

# enumField = ident "=" intLit [ "[" enumValueOption { ","  enumValueOption } "]" ]";"
rule enumField {
  <ident> '=' <intLit>
  [ '[' <enumValueOption>+ % ',' ']' ]?
  ';'
}

# enumValueOption = optionName "=" constant</pre>
rule enumValueOption {
  <optionName> '=' <constant>
}

# message = "message" messageName messageBody
rule message {
 message <messageName> <messageBody>
}

# messageBody = "{" { field | enum | message | option | oneof | mapField | reserved | emptyStatement } "}"
rule messageBody {
  '{' [ <field> | <enum> | <message> | <option> | <oneof> | <mapField> | <reserved> | <emptyStatement> ]* '}'
}

# service = "service" serviceName "{" { option | rpc | emptyStatement } "}"
rule service {
  service <serviceName> '{' [
    <option> | <rpc> | <emptyStatement>
  ]* '}'
}

# rpc = "rpc" rpcName "(" [ "stream" ] messageType ")" "returns" "(" [ "stream" ]
#        messageType ")" (( "{" {option | emptyStatement } "}" ) | ";")
rule rpc {
  rpc
  <rpcName> '(' [ stream ]? <request=messageType> ')'
  returns
     '(' [ stream ]? <response=messageType> ')'
  [[ '{' [<option> | <emptyStatement>]* '}' ] | ';']
}

# proto = syntax { import | package | option | topLevelDef | emptyStatement }
rule proto {
  <syntax> [ <import> | <package> | <option> | <topLevelDef> | <emptyStatement> ]*
}

# topLevelDef = message | enum | service
rule topLevelDef {
 <message> | <enum> | <service>
}

rule TOP {
  :my %*messages;
  [ \s
    | [ '//' \V* [$|\n] ]
  ]*
  <proto>
}

}