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