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