Debian Codegraph @

hawtbuf-protoc/src/main/javacc/proto-parser.jj

raw
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 options {
18 STATIC=false;
19 }
20
21 PARSER_BEGIN(ProtoParser)
22 /**
23 * Licensed to the Apache Software Foundation (ASF) under one or more
24 * contributor license agreements. See the NOTICE file distributed with
25 * this work for additional information regarding copyright ownership.
26 * The ASF licenses this file to You under the Apache License, Version 2.0
27 * (the "License"); you may not use this file except in compliance with
28 * the License. You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
37 */
38 package org.fusesource.hawtbuf.proto.compiler.parser;
39
40 import org.fusesource.hawtbuf.proto.compiler.*;
41 import java.util.LinkedHashMap;
42 import java.util.ArrayList;
43 import java.util.Hashtable;
44 import java.util.Iterator;
45 import java.io.File;
46 import java.io.FileReader;
47 import java.io.FileNotFoundException;
48 import java.io.IOException;
49
50 /**
51 * This class is generated with JavaCC. Do not modify manually.
52 */
53 public class ProtoParser {
54 }
55
56 PARSER_END(ProtoParser)
57
58 SKIP :
59 {
60 " "
61 | "\t"
62 | "\n"
63 | "\r"
64 }
65
66 SPECIAL_TOKEN :
67 {
68 "//" : COMMENT
69 }
70
71 <COMMENT> SPECIAL_TOKEN :
72 {
73 <("\n" | "\r" | "\r\n" | "|")> : DEFAULT
74 }
75
76 <COMMENT> MORE :
77 {
78 <~[]>
79 }
80
81 TOKEN :
82 {
83 <IMPORT:"import">
84 | <PACKAGE:"package">
85 | <SERVICE:"service">
86 | <RPC:"rpc">
87 | <OPTION:"option">
88 | <MESSAGE:"message">
89 | <EXTENSIONS:"extensions">
90 | <EXTEND:"extend">
91 | <ENUM:"enum">
92 | <GROUP:"group">
93 | <REQURIED:"required">
94 | <OPTIONAL:"optional">
95 | <REPEATED:"repeated">
96 | <RETURNS:"returns">
97 | <TO:"to">
98 | <MAX:"max">
99
100 | <LBRACE: "{">
101 | <RBRACE: "}">
102 | <EQUALS: "=">
103 | <SEMICOLON: ";">
104 | <LBRACKET: "[">
105 | <RBRACKET: "]">
106 | <LPAREN: "(">
107 | <RPAREN: ")">
108 | <PERIOD: ".">
109 | <COMMA: ",">
110
111 | < INTEGER: ("-")? (
112 <DECIMAL_LITERAL> (["l","L"])?
113 | <HEX_LITERAL> (["l","L"])?
114 | <OCTAL_LITERAL> (["l","L"])?
115 ) >
116 | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
117 | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
118 | < #OCTAL_LITERAL: "0" (["0"-"7"])* >
119 | < FLOAT: ("-")? (
120 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
121 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
122 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
123 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
124 )>
125 | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
126 | <STRING:
127 "\""
128 ( (~["\"","\\","\n","\r"])
129 | ("\\"
130 ( ["a","v","n","t","b","r","f","\\","'","\""]
131 | ["x","X"]["0"-"9","A"-"F","a"-"f"]["0"-"9","A"-"F","a"-"f"]
132 | ["0"-"7"] ( ["0"-"7"] )?
133 | ["0"-"3"] ["0"-"7"] ["0"-"7"]
134 )
135 )
136 )*
137 "\"">
138 | <ID: ["A"-"Z","a"-"z"] (["a"-"z","A"-"Z","0"-"9","_"])*>
139 }
140
141 ProtoDescriptor ProtoDescriptor() :
142 {
143 ProtoDescriptor proto = new ProtoDescriptor();
144 String packageName=null;
145 LinkedHashMap<String,OptionDescriptor> opts = new LinkedHashMap<String,OptionDescriptor>();
146 LinkedHashMap<String,MessageDescriptor> messages = new LinkedHashMap<String,MessageDescriptor>();
147 LinkedHashMap<String,EnumDescriptor> enums = new LinkedHashMap<String,EnumDescriptor>();
148 ArrayList<MessageDescriptor> extendsList = new ArrayList<MessageDescriptor>();
149 LinkedHashMap<String,ServiceDescriptor> services = new LinkedHashMap<String,ServiceDescriptor>();
150 ArrayList<String> imports = new ArrayList<String>();
151
152 OptionDescriptor optionD;
153 MessageDescriptor messageD;
154 EnumDescriptor enumD;
155 ServiceDescriptor serviceD;
156 MessageDescriptor extendD;
157 String o;
158 }
159 {
160 (
161 <PACKAGE> packageName=PackageID() <SEMICOLON>
162 |
163 <OPTION> optionD = OptionDescriptor() <SEMICOLON>
164 { opts.put(optionD.getName(),optionD); }
165 |
166 <IMPORT>
167 o = StringLitteral()
168 <SEMICOLON>
169 { imports.add((String)o); }
170 |
171 messageD = MessageDescriptor(proto, null) (<SEMICOLON>)?
172 { messages.put(messageD.getName(),messageD); }
173
174 |
175 enumD = EnumDescriptor(proto, null) (<SEMICOLON>)?
176 { enums.put(enumD.getName(),enumD); }
177 |
178 serviceD = ServiceDescriptor(proto) (<SEMICOLON>)?
179 { services.put(serviceD.getName(),serviceD); }
180 |
181 extendD = ExtendDescriptor(proto, null) (<SEMICOLON>)?
182 { extendsList.add(extendD); }
183 )+
184 <EOF>
185 {
186 proto.setPackageName(packageName);
187 proto.setImports(imports);
188 proto.setOptions(opts);
189 proto.setMessages(messages);
190 proto.setEnums(enums);
191 proto.setServices(services);
192 proto.setExtends(extendsList);
193 return proto;
194 }
195 }
196
197
198 MessageDescriptor MessageDescriptor(ProtoDescriptor proto, MessageDescriptor parent) :
199 {
200 String name;
201 LinkedHashMap<String,FieldDescriptor> fields = new LinkedHashMap<String,FieldDescriptor>();
202 LinkedHashMap<String,MessageDescriptor> messages = new LinkedHashMap<String,MessageDescriptor>();
203 LinkedHashMap<String,EnumDescriptor> enums = new LinkedHashMap<String,EnumDescriptor>();
204 ArrayList<MessageDescriptor> extendsList = new ArrayList<MessageDescriptor>();
205 LinkedHashMap<String,OptionDescriptor> opts = new LinkedHashMap<String,OptionDescriptor>();
206
207 MessageDescriptor rc = new MessageDescriptor(proto, parent);
208 OptionDescriptor optionD;
209 ExtensionsDescriptor extensionsD=null;
210 FieldDescriptor fieldD;
211 MessageDescriptor messageD;
212 EnumDescriptor enumD;
213 MessageDescriptor extendD;
214 }
215 {
216 <MESSAGE> name = ID() <LBRACE>
217 (
218 <OPTION> optionD = OptionDescriptor() <SEMICOLON>
219 { opts.put(optionD.getName(),optionD); }
220 |
221 fieldD = FieldDescriptor(rc)
222 { fields.put(fieldD.getName(), fieldD); }
223 |
224 messageD = MessageDescriptor(proto, rc) (<SEMICOLON>)?
225 { messages.put(messageD.getName(),messageD); }
226 |
227 enumD = EnumDescriptor(proto, rc) (<SEMICOLON>)?
228 { enums.put(enumD.getName(), enumD); }
229 |
230 extensionsD = ExtensionsDescriptor(rc) <SEMICOLON>
231 |
232 extendD = ExtendDescriptor(proto, rc) (<SEMICOLON>)?
233 { extendsList.add(extendD); }
234 )*
235 <RBRACE>
236 {
237 rc.setName(name);
238 rc.setFields(fields);
239 rc.setMessages(messages);
240 rc.setEnums(enums);
241 rc.setExtensions(extensionsD);
242 rc.setOptions(opts);
243 return rc;
244 }
245 }
246
247 /**
248 * Parses something like:
249 * optional string foo = 1;
250 */
251 FieldDescriptor FieldDescriptor(MessageDescriptor parent) :
252 {
253 String rule;
254 String type;
255 String name;
256 int tag;
257 LinkedHashMap<String,OptionDescriptor> opts = new LinkedHashMap<String,OptionDescriptor>();
258 LinkedHashMap<String,FieldDescriptor> fields = new LinkedHashMap<String,FieldDescriptor>();
259 OptionDescriptor optionD;
260 FieldDescriptor fieldD;
261 FieldDescriptor rc = new FieldDescriptor(parent);
262 MessageDescriptor group = new MessageDescriptor(parent.getProtoDescriptor(), parent);
263 }
264 {
265
266 rule = Rule()
267 (
268 // We have to go through the look ahead trouble because based on if 'type' is 'group'
269 // we need to look ahead to see if it looks like a group declaration instead it being
270 // message type called group.
271
272 LOOKAHEAD(5)
273 type = PackageID()
274 name = ID()
275 <EQUALS>
276 tag = Integer()
277 (
278 <LBRACKET>
279 optionD = OptionDescriptor()
280 {
281 opts.put(optionD.getName(), optionD);
282 }
283 (
284 <COMMA>
285 optionD = OptionDescriptor()
286 {
287 opts.put(optionD.getName(), optionD);
288 }
289 )*
290 <RBRACKET>
291 )?
292 <SEMICOLON>
293 |
294 {
295 }
296 <GROUP>
297 name = ID()
298 <EQUALS>
299 tag = Integer()
300 <LBRACE>
301 (
302 fieldD = FieldDescriptor(group)
303 {
304 fields.put(fieldD.getName(), fieldD);
305 }
306 )*
307 <RBRACE>
308 ( <SEMICOLON> )?
309 {
310 type = name;
311 group.setName(name);
312 group.setFields(fields);
313 rc.setGroup(group);
314 }
315 )
316 {
317 rc.setName(name);
318 rc.setType(type);
319 rc.setRule(rule);
320 rc.setTag(tag);
321 rc.setOptions(opts);
322 return rc;
323 }
324 }
325
326 ServiceDescriptor ServiceDescriptor(ProtoDescriptor proto) :
327 {
328 String name;
329 ArrayList<MethodDescriptor> methods = new ArrayList<MethodDescriptor>();
330 MethodDescriptor method;
331 }
332 {
333 <SERVICE> name = ID() <LBRACE>
334 (
335 method = MethodDescriptor(proto) <SEMICOLON>
336 {
337 methods.add(method);
338 }
339 )*
340 <RBRACE>
341 {
342 ServiceDescriptor rc = new ServiceDescriptor(proto);
343 rc.setName(name);
344 rc.setMethods(methods);
345 return rc;
346 }
347 }
348
349 MethodDescriptor MethodDescriptor(ProtoDescriptor proto) :
350 {
351 String name;
352 String input;
353 String output;
354 }
355 {
356 <RPC> name = ID() <LPAREN> input = PackageID() <RPAREN> <RETURNS> <LPAREN> output = PackageID() <RPAREN>
357 {
358 MethodDescriptor rc = new MethodDescriptor(proto);
359 rc.setName(name);
360 rc.setParameter(input);
361 rc.setReturns(output);
362 return rc;
363 }
364 }
365
366 OptionDescriptor OptionDescriptor() :
367 {
368 String name;
369 String value;
370 }
371 {
372 name = ID()
373 <EQUALS>
374 value=Value()
375 {
376 OptionDescriptor rc = new OptionDescriptor();
377 rc.setName(name);
378 rc.setValue(value);
379 return rc;
380 }
381 }
382
383 MessageDescriptor ExtendDescriptor(ProtoDescriptor proto, MessageDescriptor parent) :
384 {
385 String name;
386 LinkedHashMap<String, FieldDescriptor> fields = new LinkedHashMap<String,FieldDescriptor>();
387 MessageDescriptor rc = new MessageDescriptor(proto, parent);
388 FieldDescriptor fieldD;
389 }
390 {
391 <EXTEND> name = ID() <LBRACE>
392 (
393 fieldD = FieldDescriptor(rc)
394 { fields.put(fieldD.getName(), fieldD); }
395 )*
396 <RBRACE>
397 {
398 rc.setName(name);
399 rc.setFields(fields);
400 return rc;
401 }
402 }
403
404 ExtensionsDescriptor ExtensionsDescriptor(MessageDescriptor parent) :
405 {
406 int first;
407 int last;
408 }
409 {
410 <EXTENSIONS>
411 first = Integer()
412 <TO>
413 (
414 last = Integer()
415 |
416 <MAX>
417 {
418 last=536870911;
419 }
420 )
421 {
422 ExtensionsDescriptor rc = new ExtensionsDescriptor(parent);
423 rc.setFirst(first);
424 rc.setLast(last);
425 return rc;
426 }
427 }
428
429 EnumDescriptor EnumDescriptor(ProtoDescriptor proto, MessageDescriptor parent) :
430 {
431 Token name;
432 LinkedHashMap<String,EnumFieldDescriptor> fields = new LinkedHashMap<String,EnumFieldDescriptor>();
433 EnumDescriptor rc = new EnumDescriptor(proto, parent);
434 LinkedHashMap<String,OptionDescriptor> opts = new LinkedHashMap<String,OptionDescriptor>();
435
436 EnumFieldDescriptor enumD;
437 OptionDescriptor optionD;
438 }
439 {
440 <ENUM> name = <ID> <LBRACE>
441 (
442 LOOKAHEAD(2)
443 <OPTION> optionD = OptionDescriptor() <SEMICOLON>
444 { opts.put(optionD.getName(),optionD); }
445 |
446 enumD = EnumFieldDescriptor(rc) <SEMICOLON>
447 { fields.put(enumD.getName(),enumD); }
448 )*
449 <RBRACE>
450 {
451 rc.setName(name.image);
452 rc.setFields(fields);
453 rc.setOptions(opts);
454 return rc;
455 }
456 }
457
458 /**
459 * Parses something like:
460 * optional string foo = 1;
461 */
462 EnumFieldDescriptor EnumFieldDescriptor(EnumDescriptor parent) :
463 {
464 String name;
465 int value=0;
466 }
467 {
468
469 name = ID()
470 <EQUALS>
471 value = Integer()
472 {
473 EnumFieldDescriptor rc = new EnumFieldDescriptor(parent);
474 rc.setName(name);
475 rc.setValue(value);
476 return rc;
477 }
478 }
479
480 int Integer() :
481 {
482 Token t;
483 }
484 {
485 t = <INTEGER>
486 {
487 return Integer.parseInt(t.image);
488 }
489 }
490
491
492 String Rule() :
493 {
494 Token t;
495 }
496 {
497 (
498 t = <REQURIED>
499 |
500 t = <OPTIONAL>
501 |
502 t = <REPEATED>
503 )
504 {
505 return t.image;
506 }
507 }
508
509 String Value() :
510 {
511 Token t;
512 String value=null;
513 }
514 {
515 (
516 value = StringLitteral()
517 |
518 value = ID()
519 |
520 t = <INTEGER>
521 {
522 value = t.image;
523 }
524 |
525 t = <FLOAT>
526 {
527 value = t.image;
528 }
529 )
530 {
531 return value;
532 }
533 }
534
535 String ID() :
536 {
537 Token t;
538 }
539 {
540 (
541 t = <ID>
542 | t = <GROUP>
543 | t = <IMPORT>
544 | t = <PACKAGE>
545 | t = <SERVICE>
546 | t = <RPC>
547 | t = <OPTION>
548 | t = <MESSAGE>
549 | t = <EXTENSIONS>
550 | t = <EXTEND>
551 | t = <ENUM>
552 | t = <REQURIED>
553 | t = <OPTIONAL>
554 | t = <REPEATED>
555 | t = <RETURNS>
556 | t = <TO>
557 | t = <MAX>
558 )
559 {
560 return t.image;
561 }
562 }
563
564 String PackageID() :
565 {
566 String t;
567 StringBuffer sb = new StringBuffer();
568 }
569 {
570 (
571 t = ID()
572 {
573 sb.append(t);
574 }
575 (
576 <PERIOD>
577 t=ID()
578 {
579 sb.append(".");
580 sb.append(t);
581 }
582 )*
583 )
584 {
585 return sb.toString();
586 }
587 }
588
589
590 String StringLitteral() :
591 {
592 Token t;
593 }
594 {
595 t = <STRING>
596 {
597 return ParserSupport.decodeString(t);
598 }
599 }