]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/CParser4/C.g4
BaseTools: ECC tool Python3 adaption
[mirror_edk2.git] / BaseTools / Source / Python / Ecc / CParser4 / C.g4
1 /* @file
2 This file is used to be the grammar file of ECC tool
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 */
13
14
15 grammar C;
16 options {
17 language=Python;
18 }
19
20
21 @header {
22 ## @file
23 # The file defines the parser for C source files.
24 #
25 # THIS FILE IS AUTO-GENENERATED. PLEASE DON NOT MODIFY THIS FILE.
26 # This file is generated by running:
27 # java org.antlr.Tool C.g
28 #
29 # Copyright (c) 2009 - 2010, Intel Corporation All rights reserved.
30 #
31 # This program and the accompanying materials are licensed and made available
32 # under the terms and conditions of the BSD License which accompanies this
33 # distribution. The full text of the license may be found at:
34 # http://opensource.org/licenses/bsd-license.php
35 #
36 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
38 #
39 ##
40
41 import Ecc.CodeFragment as CodeFragment
42 import Ecc.FileProfile as FileProfile
43 }
44
45 @members {
46
47 def printTokenInfo(self, line, offset, tokenText):
48 print(str(line)+ ',' + str(offset) + ':' + str(tokenText))
49
50 def StorePredicateExpression(self, StartLine, StartOffset, EndLine, EndOffset, Text):
51 PredExp = CodeFragment.PredicateExpression(Text, (StartLine, StartOffset), (EndLine, EndOffset))
52 FileProfile.PredicateExpressionList.append(PredExp)
53
54 def StoreEnumerationDefinition(self, StartLine, StartOffset, EndLine, EndOffset, Text):
55 EnumDef = CodeFragment.EnumerationDefinition(Text, (StartLine, StartOffset), (EndLine, EndOffset))
56 FileProfile.EnumerationDefinitionList.append(EnumDef)
57
58 def StoreStructUnionDefinition(self, StartLine, StartOffset, EndLine, EndOffset, Text):
59 SUDef = CodeFragment.StructUnionDefinition(Text, (StartLine, StartOffset), (EndLine, EndOffset))
60 FileProfile.StructUnionDefinitionList.append(SUDef)
61
62 def StoreTypedefDefinition(self, StartLine, StartOffset, EndLine, EndOffset, FromText, ToText):
63 Tdef = CodeFragment.TypedefDefinition(FromText, ToText, (StartLine, StartOffset), (EndLine, EndOffset))
64 FileProfile.TypedefDefinitionList.append(Tdef)
65
66 def StoreFunctionDefinition(self, StartLine, StartOffset, EndLine, EndOffset, ModifierText, DeclText, LeftBraceLine, LeftBraceOffset, DeclLine, DeclOffset):
67 FuncDef = CodeFragment.FunctionDefinition(ModifierText, DeclText, (StartLine, StartOffset), (EndLine, EndOffset), (LeftBraceLine, LeftBraceOffset), (DeclLine, DeclOffset))
68 FileProfile.FunctionDefinitionList.append(FuncDef)
69
70 def StoreVariableDeclaration(self, StartLine, StartOffset, EndLine, EndOffset, ModifierText, DeclText):
71 VarDecl = CodeFragment.VariableDeclaration(ModifierText, DeclText, (StartLine, StartOffset), (EndLine, EndOffset))
72 FileProfile.VariableDeclarationList.append(VarDecl)
73
74 def StoreFunctionCalling(self, StartLine, StartOffset, EndLine, EndOffset, FuncName, ParamList):
75 FuncCall = CodeFragment.FunctionCalling(FuncName, ParamList, (StartLine, StartOffset), (EndLine, EndOffset))
76 FileProfile.FunctionCallingList.append(FuncCall)
77
78 }
79
80 translation_unit
81 : external_declaration*
82 ;
83
84
85 external_declaration
86 : ( declaration_specifiers? declarator declaration* '{' )
87 | function_definition
88 | declaration
89 | macro_statement (';')?
90 ;
91
92 function_definition
93 locals [String ModifierText = '', String DeclText = '', int LBLine = 0, int LBOffset = 0, int DeclLine = 0, int DeclOffset = 0]
94 @init {
95 ModifierText = '';
96 DeclText = '';
97 LBLine = 0;
98 LBOffset = 0;
99 DeclLine = 0;
100 DeclOffset = 0;
101 }
102 @after{
103 self.StoreFunctionDefinition(localctx.start.line, localctx.start.column, localctx.stop.line, localctx.stop.column, ModifierText, DeclText, LBLine, LBOffset, DeclLine, DeclOffset)
104 }
105 : d=declaration_specifiers? declarator
106 ( declaration+ a=compound_statement // K&R style
107 | b=compound_statement // ANSI style
108 ) {
109 if localctx.d != None:
110 ModifierText = $declaration_specifiers.text
111 else:
112 ModifierText = ''
113 DeclText = $declarator.text
114 DeclLine = $declarator.start.line
115 DeclOffset = $declarator.start.column
116 if localctx.a != None:
117 LBLine = $a.start.line
118 LBOffset = $a.start.column
119 else:
120 LBLine = $b.start.line
121 LBOffset = $b.start.column
122 }
123 ;
124
125
126 declaration_specifiers
127 : ( storage_class_specifier
128 | type_specifier
129 | type_qualifier
130 )+
131 ;
132
133 declaration
134 : a='typedef' b=declaration_specifiers? c=init_declarator_list d=';'
135 {
136 if localctx.b is not None:
137 self.StoreTypedefDefinition(localctx.a.line, localctx.a.column, $d.line, localctx.d.column, $b.text, $c.text)
138 else:
139 self.StoreTypedefDefinition(localctx.a.line, localctx.a.column, $d.line, localctx.d.column, '', $c.text)
140 }
141 | s=declaration_specifiers t=init_declarator_list? e=';'
142 {
143 if localctx.t is not None:
144 self.StoreVariableDeclaration($s.start.line, $s.start.column, $t.start.line, $t.start.column, $s.text, $t.text)
145 }
146 ;
147
148 init_declarator_list
149 : init_declarator (',' init_declarator)*
150 ;
151
152 init_declarator
153 : declarator ('=' initializer)?
154 ;
155
156 storage_class_specifier
157 : 'extern'
158 | 'static'
159 | 'auto'
160 | 'register'
161 | 'STATIC'
162 ;
163
164 type_specifier
165 : 'void'
166 | 'char'
167 | 'short'
168 | 'int'
169 | 'long'
170 | 'float'
171 | 'double'
172 | 'signed'
173 | 'unsigned'
174 | s=struct_or_union_specifier
175 {
176 if localctx.s.stop is not None:
177 self.StoreStructUnionDefinition($s.start.line, $s.start.column, $s.stop.line, $s.stop.column, $s.text)
178 }
179 | e=enum_specifier
180 {
181 if localctx.e.stop is not None:
182 self.StoreEnumerationDefinition($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)
183 }
184 | (IDENTIFIER type_qualifier* declarator)
185 | type_id
186 ;
187
188 type_id
189 : IDENTIFIER
190 //{self.printTokenInfo($a.line, $a.pos, $a.text)}
191 ;
192
193 struct_or_union_specifier
194 : struct_or_union IDENTIFIER? '{' struct_declaration_list '}'
195 | struct_or_union IDENTIFIER
196 ;
197
198 struct_or_union
199 : 'struct'
200 | 'union'
201 ;
202
203 struct_declaration_list
204 : struct_declaration+
205 ;
206
207 struct_declaration
208 : specifier_qualifier_list struct_declarator_list ';'
209 ;
210
211 specifier_qualifier_list
212 : ( type_qualifier | type_specifier )+
213 ;
214
215 struct_declarator_list
216 : struct_declarator (',' struct_declarator)*
217 ;
218
219 struct_declarator
220 : declarator (':' constant_expression)?
221 | ':' constant_expression
222 ;
223
224 enum_specifier
225 : 'enum' '{' enumerator_list ','? '}'
226 | 'enum' IDENTIFIER '{' enumerator_list ','? '}'
227 | 'enum' IDENTIFIER
228 ;
229
230 enumerator_list
231 : enumerator (',' enumerator)*
232 ;
233
234 enumerator
235 : IDENTIFIER ('=' constant_expression)?
236 ;
237
238 type_qualifier
239 : 'const'
240 | 'volatile'
241 | 'IN'
242 | 'OUT'
243 | 'OPTIONAL'
244 | 'CONST'
245 | 'UNALIGNED'
246 | 'VOLATILE'
247 | 'GLOBAL_REMOVE_IF_UNREFERENCED'
248 | 'EFIAPI'
249 | 'EFI_BOOTSERVICE'
250 | 'EFI_RUNTIMESERVICE'
251 | 'PACKED'
252 ;
253
254 declarator
255 : pointer? ('EFIAPI')? ('EFI_BOOTSERVICE')? ('EFI_RUNTIMESERVICE')? direct_declarator
256 // | ('EFIAPI')? ('EFI_BOOTSERVICE')? ('EFI_RUNTIMESERVICE')? pointer? direct_declarator
257 | pointer
258 ;
259
260 direct_declarator
261 : IDENTIFIER declarator_suffix*
262 | '(' ('EFIAPI')? declarator ')' declarator_suffix+
263 ;
264
265 declarator_suffix
266 : '[' constant_expression ']'
267 | '[' ']'
268 | '(' parameter_type_list ')'
269 | '(' identifier_list ')'
270 | '(' ')'
271 ;
272
273 pointer
274 : '*' type_qualifier+ pointer?
275 | '*' pointer
276 | '*'
277 ;
278
279 parameter_type_list
280 : parameter_list (',' ('OPTIONAL')? '...')?
281 ;
282
283 parameter_list
284 : parameter_declaration (',' ('OPTIONAL')? parameter_declaration)*
285 ;
286
287 parameter_declaration
288 : declaration_specifiers (declarator|abstract_declarator)* ('OPTIONAL')?
289 //accomerdate user-defined type only, no declarator follow.
290 | pointer* IDENTIFIER
291 ;
292
293 identifier_list
294 : IDENTIFIER
295 (',' IDENTIFIER)*
296 ;
297
298 type_name
299 : specifier_qualifier_list abstract_declarator?
300 | type_id
301 ;
302
303 abstract_declarator
304 : pointer direct_abstract_declarator?
305 | direct_abstract_declarator
306 ;
307
308 direct_abstract_declarator
309 : ( '(' abstract_declarator ')' | abstract_declarator_suffix ) abstract_declarator_suffix*
310 ;
311
312 abstract_declarator_suffix
313 : '[' ']'
314 | '[' constant_expression ']'
315 | '(' ')'
316 | '(' parameter_type_list ')'
317 ;
318
319 initializer
320
321 : assignment_expression
322 | '{' initializer_list ','? '}'
323 ;
324
325 initializer_list
326 : initializer (',' initializer )*
327 ;
328
329 // E x p r e s s i o n s
330
331 argument_expression_list
332 : assignment_expression ('OPTIONAL')? (',' assignment_expression ('OPTIONAL')?)*
333 ;
334
335 additive_expression
336 : (multiplicative_expression) ('+' multiplicative_expression | '-' multiplicative_expression)*
337 ;
338
339 multiplicative_expression
340 : (cast_expression) ('*' cast_expression | '/' cast_expression | '%' cast_expression)*
341 ;
342
343 cast_expression
344 : '(' type_name ')' cast_expression
345 | unary_expression
346 ;
347
348 unary_expression
349 : postfix_expression
350 | '++' unary_expression
351 | '--' unary_expression
352 | unary_operator cast_expression
353 | 'sizeof' unary_expression
354 | 'sizeof' '(' type_name ')'
355 ;
356
357 postfix_expression
358 locals [FuncCallText='']
359 @init
360 {
361 self.FuncCallText=''
362 }
363 : p=primary_expression {self.FuncCallText += $p.text}
364 ( '[' expression ']'
365 | '(' a=')'{self.StoreFunctionCalling($p.start.line, $p.start.column, $a.line, localctx.a.column, self.FuncCallText, '')}
366 | '(' c=argument_expression_list b=')' {self.StoreFunctionCalling($p.start.line, $p.start.column, $b.line, localctx.b.column, self.FuncCallText, $c.text)}
367 | '(' macro_parameter_list ')'
368 | '.' x=IDENTIFIER {self.FuncCallText += '.' + $x.text}
369 | '*' y=IDENTIFIER {self.FuncCallText = $y.text}
370 | '->' z=IDENTIFIER {self.FuncCallText += '->' + $z.text}
371 | '++'
372 | '--'
373 )*
374 ;
375
376 macro_parameter_list
377 : parameter_declaration (',' parameter_declaration)*
378 ;
379
380 unary_operator
381 : '&'
382 | '*'
383 | '+'
384 | '-'
385 | '~'
386 | '!'
387 ;
388
389 primary_expression
390 : IDENTIFIER
391 | constant
392 | '(' expression ')'
393 ;
394
395 constant
396 : HEX_LITERAL
397 | OCTAL_LITERAL
398 | DECIMAL_LITERAL
399 | CHARACTER_LITERAL
400 | (IDENTIFIER* STRING_LITERAL+)+ IDENTIFIER*
401 | FLOATING_POINT_LITERAL
402 ;
403
404 /////
405
406 expression
407 : assignment_expression (',' assignment_expression)*
408 ;
409
410 constant_expression
411 : conditional_expression
412 ;
413
414 assignment_expression
415 : lvalue assignment_operator assignment_expression
416 | conditional_expression
417 ;
418
419 lvalue
420 : unary_expression
421 ;
422
423 assignment_operator
424 : '='
425 | '*='
426 | '/='
427 | '%='
428 | '+='
429 | '-='
430 | '<<='
431 | '>>='
432 | '&='
433 | '^='
434 | '|='
435 ;
436
437 conditional_expression
438 : e=logical_or_expression ('?' expression ':' conditional_expression {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)})?
439 ;
440
441 logical_or_expression
442 : logical_and_expression ('||' logical_and_expression)*
443 ;
444
445 logical_and_expression
446 : inclusive_or_expression ('&&' inclusive_or_expression)*
447 ;
448
449 inclusive_or_expression
450 : exclusive_or_expression ('|' exclusive_or_expression)*
451 ;
452
453 exclusive_or_expression
454 : and_expression ('^' and_expression)*
455 ;
456
457 and_expression
458 : equality_expression ('&' equality_expression)*
459 ;
460 equality_expression
461 : relational_expression (('=='|'!=') relational_expression )*
462 ;
463
464 relational_expression
465 : shift_expression (('<'|'>'|'<='|'>=') shift_expression)*
466 ;
467
468 shift_expression
469 : additive_expression (('<<'|'>>') additive_expression)*
470 ;
471
472 // S t a t e m e n t s
473
474 statement
475 : labeled_statement
476 | compound_statement
477 | expression_statement
478 | selection_statement
479 | iteration_statement
480 | jump_statement
481 | macro_statement
482 | asm2_statement
483 | asm1_statement
484 | asm_statement
485 | declaration
486 ;
487
488 asm2_statement
489 : '__asm__'? IDENTIFIER '(' (~(';'))* ')' ';'
490 ;
491
492 asm1_statement
493 : '_asm' '{' (~('}'))* '}'
494 ;
495
496 asm_statement
497 : '__asm' '{' (~('}'))* '}'
498 ;
499
500 macro_statement
501 : IDENTIFIER '(' declaration* statement_list? expression? ')'
502 ;
503
504 labeled_statement
505 : IDENTIFIER ':' statement
506 | 'case' constant_expression ':' statement
507 | 'default' ':' statement
508 ;
509
510 compound_statement
511 : '{' declaration* statement_list? '}'
512 ;
513
514 statement_list
515 : statement+
516 ;
517
518 expression_statement
519 : ';'
520 | expression ';'
521 ;
522
523 selection_statement
524 : 'if' '(' e=expression ')' {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)} statement (:'else' statement)?
525 | 'switch' '(' expression ')' statement
526 ;
527
528 iteration_statement
529 : 'while' '(' e=expression ')' statement {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)}
530 | 'do' statement 'while' '(' e=expression ')' ';' {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)}
531 //| 'for' '(' expression_statement e=expression_statement expression? ')' statement {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)}
532 ;
533
534 jump_statement
535 : 'goto' IDENTIFIER ';'
536 | 'continue' ';'
537 | 'break' ';'
538 | 'return' ';'
539 | 'return' expression ';'
540 ;
541
542 IDENTIFIER
543 : LETTER (LETTER|'0'..'9')*
544 ;
545
546 fragment
547 LETTER
548 : '$'
549 | 'A'..'Z'
550 | 'a'..'z'
551 | '_'
552 ;
553
554 CHARACTER_LITERAL
555 : ('L')? '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
556 ;
557
558 STRING_LITERAL
559 : ('L')? '"' ( EscapeSequence | ~('\\'|'"') )* '"'
560 ;
561
562 HEX_LITERAL : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;
563
564 DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;
565
566 OCTAL_LITERAL : '0' ('0'..'7')+ IntegerTypeSuffix? ;
567
568 fragment
569 HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
570
571 fragment
572 IntegerTypeSuffix
573 : ('u'|'U')
574 | ('l'|'L')
575 | ('u'|'U') ('l'|'L')
576 | ('u'|'U') ('l'|'L') ('l'|'L')
577 ;
578
579 FLOATING_POINT_LITERAL
580 : ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
581 | '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
582 | ('0'..'9')+ Exponent FloatTypeSuffix?
583 | ('0'..'9')+ Exponent? FloatTypeSuffix
584 ;
585
586 fragment
587 Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
588
589 fragment
590 FloatTypeSuffix : ('f'|'F'|'d'|'D') ;
591
592 fragment
593 EscapeSequence
594 : '\\' ('b'|'t'|'n'|'f'|'r'|'\''|'\\')
595 | OctalEscape
596 ;
597
598 fragment
599 OctalEscape
600 : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
601 | '\\' ('0'..'7') ('0'..'7')
602 | '\\' ('0'..'7')
603 ;
604
605 fragment
606 UnicodeEscape
607 : '\\' 'u' HexDigit HexDigit HexDigit HexDigit
608 ;
609
610 WS : (' '|'\r'|'\t'|'\u000C'|'\n')
611 -> channel(HIDDEN)
612 ;
613
614 // ingore '\' of line concatenation
615 BS : ('\\')
616 -> channel(HIDDEN)
617 ;
618
619 UnicodeVocabulary
620 : '\u0003'..'\uFFFE'
621 ;
622
623 COMMENT
624 : '/*' .*? '*/'
625 -> channel(HIDDEN)
626 ;
627
628 LINE_COMMENT
629 : '//' ~('\n'|'\r')* '\r'? '\n'
630 -> channel(HIDDEN)
631 ;
632
633 // ignore #line info for now
634 LINE_COMMAND
635 : '#' ~('\n'|'\r')* '\r'? '\n'
636 -> channel(HIDDEN)
637 ;