/
cxx_parser.py
2189 lines (1932 loc) · 71 KB
/
cxx_parser.py
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#-------------------------------------------------------------------------
# CxxTest: A lightweight C++ unit testing library.
# Copyright (c) 2008 Sandia Corporation.
# This software is distributed under the LGPL License v2.1
# For more information, see the COPYING file in the top CxxTest directory.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
#-------------------------------------------------------------------------
# vim: fileencoding=utf-8
#
# This is a PLY parser for the entire ANSI C++ grammar. This grammar was
# adapted from the FOG grammar developed by E. D. Willink. See
#
# http://www.computing.surrey.ac.uk/research/dsrg/fog/
#
# for further details.
#
# The goal of this grammar is to extract information about class, function and
# class method declarations, along with their associated scope. Thus, this
# grammar can be used to analyze classes in an inheritance heirarchy, and then
# enumerate the methods in a derived class.
#
# This grammar parses blocks of <>, (), [] and {} in a generic manner. Thus,
# There are several capabilities that this grammar does not support:
#
# 1. Ambiguous template specification. This grammar cannot parse template
# specifications that do not have paired <>'s in their declaration. In
# particular, ambiguous declarations like
#
# foo<A, c<3 >();
#
# cannot be correctly parsed.
#
# 2. Template class specialization. Although the goal of this grammar is to
# extract class information, specialization of templated classes is
# not supported. When a template class definition is parsed, it's
# declaration is archived without information about the template
# parameters. Class specializations will be stored separately, and
# thus they can be processed after the fact. However, this grammar
# does not attempt to correctly process properties of class inheritence
# when template class specialization is employed.
#
#
# TODO: document usage of this file
#
from __future__ import division
import os
import ply.lex as lex
import ply.yacc as yacc
import re
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
lexer = None
scope_lineno = 0
identifier_lineno = {}
_parse_info=None
_parsedata=None
noExceptionLogic = True
def ply_init(data):
global _parsedata
_parsedata=data
class Scope(object):
def __init__(self,name,abs_name,scope_t,base_classes,lineno):
self.function=[]
self.name=name
self.scope_t=scope_t
self.sub_scopes=[]
self.base_classes=base_classes
self.abs_name=abs_name
self.lineno=lineno
def insert(self,scope):
self.sub_scopes.append(scope)
class CppInfo(object):
def __init__(self, filter=None):
self.verbose=0
if filter is None:
self.filter=re.compile("[Tt][Ee][Ss][Tt]|createSuite|destroySuite")
else:
self.filter=filter
self.scopes=[""]
self.index=OrderedDict()
self.index[""]=Scope("","::","namespace",[],1)
self.function=[]
def push_scope(self,ns,scope_t,base_classes=[]):
name = self.scopes[-1]+"::"+ns
if self.verbose>=2:
print "-- Starting "+scope_t+" "+name
self.scopes.append(name)
self.index[name] = Scope(ns,name,scope_t,base_classes,scope_lineno-1)
def pop_scope(self):
scope = self.scopes.pop()
if self.verbose>=2:
print "-- Stopping "+scope
return scope
def add_function(self, fn):
fn = str(fn)
if self.filter.search(fn):
self.index[self.scopes[-1]].function.append((fn, identifier_lineno.get(fn,lexer.lineno-1)))
tmp = self.scopes[-1]+"::"+fn
if self.verbose==2:
print "-- Function declaration "+fn+" "+tmp
elif self.verbose==1:
print "-- Function declaration "+tmp
def get_functions(self,name,quiet=False):
if name == "::":
name = ""
scope = self.index[name]
fns=scope.function
for key in scope.base_classes:
cname = self.find_class(key,scope)
if cname is None:
if not quiet:
print "Defined classes: ",list(self.index.keys())
print "WARNING: Unknown class "+key
else:
fns += self.get_functions(cname,quiet)
return fns
def find_class(self,name,scope):
if ':' in name:
if name in self.index:
return name
else:
return None
tmp = scope.abs_name.split(':')
name1 = ":".join(tmp[:-1] + [name])
if name1 in self.index:
return name1
name2 = "::"+name
if name2 in self.index:
return name2
return None
def __repr__(self):
return str(self)
def is_baseclass(self,cls,base):
'''Returns true if base is a base-class of cls'''
if cls in self.index:
bases = self.index[cls]
elif "::"+cls in self.index:
bases = self.index["::"+cls]
else:
return False
#raise IOError, "Unknown class "+cls
if base in bases.base_classes:
return True
for name in bases.base_classes:
if self.is_baseclass(name,base):
return True
return False
def __str__(self):
ans=""
keys = list(self.index.keys())
keys.sort()
for key in keys:
scope = self.index[key]
ans += scope.scope_t+" "+scope.abs_name+"\n"
if scope.scope_t == "class":
ans += " Base Classes: "+str(scope.base_classes)+"\n"
for fn in self.get_functions(scope.abs_name):
ans += " "+fn+"\n"
else:
for fn in scope.function:
ans += " "+fn+"\n"
return ans
def flatten(x):
"""Flatten nested list"""
try:
strtypes = basestring
except: # for python3 etc
strtypes = (str, bytes)
result = []
for el in x:
if hasattr(el, "__iter__") and not isinstance(el, strtypes):
result.extend(flatten(el))
else:
result.append(el)
return result
#
# The lexer (and/or a preprocessor) is expected to identify the following
#
# Punctuation:
#
#
literals = "+-*/%^&|~!<>=:()?.\'\"\\@$;,"
#
reserved = {
'private' : 'PRIVATE',
'protected' : 'PROTECTED',
'public' : 'PUBLIC',
'bool' : 'BOOL',
'char' : 'CHAR',
'double' : 'DOUBLE',
'float' : 'FLOAT',
'int' : 'INT',
'long' : 'LONG',
'short' : 'SHORT',
'signed' : 'SIGNED',
'unsigned' : 'UNSIGNED',
'void' : 'VOID',
'wchar_t' : 'WCHAR_T',
'class' : 'CLASS',
'enum' : 'ENUM',
'namespace' : 'NAMESPACE',
'struct' : 'STRUCT',
'typename' : 'TYPENAME',
'union' : 'UNION',
'const' : 'CONST',
'volatile' : 'VOLATILE',
'auto' : 'AUTO',
'explicit' : 'EXPLICIT',
'export' : 'EXPORT',
'extern' : 'EXTERN',
'__extension__' : 'EXTENSION',
'friend' : 'FRIEND',
'inline' : 'INLINE',
'mutable' : 'MUTABLE',
'register' : 'REGISTER',
'static' : 'STATIC',
'template' : 'TEMPLATE',
'typedef' : 'TYPEDEF',
'using' : 'USING',
'virtual' : 'VIRTUAL',
'asm' : 'ASM',
'break' : 'BREAK',
'case' : 'CASE',
'catch' : 'CATCH',
'const_cast' : 'CONST_CAST',
'continue' : 'CONTINUE',
'default' : 'DEFAULT',
'delete' : 'DELETE',
'do' : 'DO',
'dynamic_cast' : 'DYNAMIC_CAST',
'else' : 'ELSE',
'false' : 'FALSE',
'for' : 'FOR',
'goto' : 'GOTO',
'if' : 'IF',
'new' : 'NEW',
'operator' : 'OPERATOR',
'reinterpret_cast' : 'REINTERPRET_CAST',
'return' : 'RETURN',
'sizeof' : 'SIZEOF',
'static_cast' : 'STATIC_CAST',
'switch' : 'SWITCH',
'this' : 'THIS',
'throw' : 'THROW',
'true' : 'TRUE',
'try' : 'TRY',
'typeid' : 'TYPEID',
'while' : 'WHILE',
'"C"' : 'CLiteral',
'"C++"' : 'CppLiteral',
'__attribute__' : 'ATTRIBUTE',
'__cdecl__' : 'CDECL',
'__typeof' : 'uTYPEOF',
'typeof' : 'TYPEOF',
'CXXTEST_STD' : 'CXXTEST_STD'
}
tokens = [
"CharacterLiteral",
"FloatingLiteral",
"Identifier",
"IntegerLiteral",
"StringLiteral",
"RBRACE",
"LBRACE",
"RBRACKET",
"LBRACKET",
"ARROW",
"ARROW_STAR",
"DEC",
"EQ",
"GE",
"INC",
"LE",
"LOG_AND",
"LOG_OR",
"NE",
"SHL",
"SHR",
"ASS_ADD",
"ASS_AND",
"ASS_DIV",
"ASS_MOD",
"ASS_MUL",
"ASS_OR",
"ASS_SHL",
"ASS_SHR",
"ASS_SUB",
"ASS_XOR",
"DOT_STAR",
"ELLIPSIS",
"SCOPE",
] + list(reserved.values())
t_ignore = " \t\r"
t_LBRACE = r"(\{)|(<%)"
t_RBRACE = r"(\})|(%>)"
t_LBRACKET = r"(\[)|(<:)"
t_RBRACKET = r"(\])|(:>)"
t_ARROW = r"->"
t_ARROW_STAR = r"->\*"
t_DEC = r"--"
t_EQ = r"=="
t_GE = r">="
t_INC = r"\+\+"
t_LE = r"<="
t_LOG_AND = r"&&"
t_LOG_OR = r"\|\|"
t_NE = r"!="
t_SHL = r"<<"
t_SHR = r">>"
t_ASS_ADD = r"\+="
t_ASS_AND = r"&="
t_ASS_DIV = r"/="
t_ASS_MOD = r"%="
t_ASS_MUL = r"\*="
t_ASS_OR = r"\|="
t_ASS_SHL = r"<<="
t_ASS_SHR = r">>="
t_ASS_SUB = r"-="
t_ASS_XOR = r"^="
t_DOT_STAR = r"\.\*"
t_ELLIPSIS = r"\.\.\."
t_SCOPE = r"::"
# Discard comments
def t_COMMENT(t):
r'(/\*(.|\n)*?\*/)|(//.*?\n)|(\#.*?\n)'
t.lexer.lineno += t.value.count("\n")
t_IntegerLiteral = r'(0x[0-9A-F]+)|([0-9]+(L){0,1})'
t_FloatingLiteral = r"[0-9]+[eE\.\+-]+[eE\.\+\-0-9]+"
t_CharacterLiteral = r'\'([^\'\\]|\\.)*\''
#t_StringLiteral = r'"([^"\\]|\\.)*"'
def t_StringLiteral(t):
r'"([^"\\]|\\.)*"'
t.type = reserved.get(t.value,'StringLiteral')
return t
def t_Identifier(t):
r"[a-zA-Z_][a-zA-Z_0-9\.]*"
t.type = reserved.get(t.value,'Identifier')
return t
def t_error(t):
print "Illegal character '%s'" % t.value[0]
#raise IOError, "Parse error"
#t.lexer.skip()
def t_newline(t):
r'[\n]+'
t.lexer.lineno += len(t.value)
precedence = (
( 'right', 'SHIFT_THERE', 'REDUCE_HERE_MOSTLY', 'SCOPE'),
( 'nonassoc', 'ELSE', 'INC', 'DEC', '+', '-', '*', '&', 'LBRACKET', 'LBRACE', '<', ':', ')')
)
start = 'translation_unit'
#
# The %prec resolves the 14.2-3 ambiguity:
# Identifier '<' is forced to go through the is-it-a-template-name test
# All names absorb TEMPLATE with the name, so that no template_test is
# performed for them. This requires all potential declarations within an
# expression to perpetuate this policy and thereby guarantee the ultimate
# coverage of explicit_instantiation.
#
# The %prec also resolves a conflict in identifier : which is forced to be a
# shift of a label for a labeled-statement rather than a reduction for the
# name of a bit-field or generalised constructor. This is pretty dubious
# syntactically but correct for all semantic possibilities. The shift is
# only activated when the ambiguity exists at the start of a statement.
# In this context a bit-field declaration or constructor definition are not
# allowed.
#
def p_identifier(p):
'''identifier : Identifier
| CXXTEST_STD '(' Identifier ')'
'''
if p[1][0] in ('t','T','c','d'):
identifier_lineno[p[1]] = p.lineno(1)
p[0] = p[1]
def p_id(p):
'''id : identifier %prec SHIFT_THERE
| template_decl
| TEMPLATE id
'''
p[0] = get_rest(p)
def p_global_scope(p):
'''global_scope : SCOPE
'''
p[0] = get_rest(p)
def p_id_scope(p):
'''id_scope : id SCOPE'''
p[0] = get_rest(p)
def p_id_scope_seq(p):
'''id_scope_seq : id_scope
| id_scope id_scope_seq
'''
p[0] = get_rest(p)
#
# A :: B :: C; is ambiguous How much is type and how much name ?
# The %prec maximises the (type) length which is the 7.1-2 semantic constraint.
#
def p_nested_id(p):
'''nested_id : id %prec SHIFT_THERE
| id_scope nested_id
'''
p[0] = get_rest(p)
def p_scoped_id(p):
'''scoped_id : nested_id
| global_scope nested_id
| id_scope_seq
| global_scope id_scope_seq
'''
global scope_lineno
scope_lineno = lexer.lineno
data = flatten(get_rest(p))
if data[0] != None:
p[0] = "".join(data)
#
# destructor_id has to be held back to avoid a conflict with a one's
# complement as per 5.3.1-9, It gets put back only when scoped or in a
# declarator_id, which is only used as an explicit member name.
# Declarations of an unscoped destructor are always parsed as a one's
# complement.
#
def p_destructor_id(p):
'''destructor_id : '~' id
| TEMPLATE destructor_id
'''
p[0]=get_rest(p)
#def p_template_id(p):
# '''template_id : empty
# | TEMPLATE
# '''
# pass
def p_template_decl(p):
'''template_decl : identifier '<' nonlgt_seq_opt '>'
'''
#
# WEH: should we include the lt/gt symbols to indicate that this is a
# template class? How is that going to be used later???
#
#p[0] = [p[1] ,"<",">"]
p[0] = p[1]
def p_special_function_id(p):
'''special_function_id : conversion_function_id
| operator_function_id
| TEMPLATE special_function_id
'''
p[0]=get_rest(p)
def p_nested_special_function_id(p):
'''nested_special_function_id : special_function_id
| id_scope destructor_id
| id_scope nested_special_function_id
'''
p[0]=get_rest(p)
def p_scoped_special_function_id(p):
'''scoped_special_function_id : nested_special_function_id
| global_scope nested_special_function_id
'''
p[0]=get_rest(p)
# declarator-id is all names in all scopes, except reserved words
def p_declarator_id(p):
'''declarator_id : scoped_id
| scoped_special_function_id
| destructor_id
'''
p[0]=p[1]
#
# The standard defines pseudo-destructors in terms of type-name, which is
# class/enum/typedef, of which class-name is covered by a normal destructor.
# pseudo-destructors are supposed to support ~int() in templates, so the
# grammar here covers built-in names. Other names are covered by the lack
# of identifier/type discrimination.
#
def p_built_in_type_id(p):
'''built_in_type_id : built_in_type_specifier
| built_in_type_id built_in_type_specifier
'''
pass
def p_pseudo_destructor_id(p):
'''pseudo_destructor_id : built_in_type_id SCOPE '~' built_in_type_id
| '~' built_in_type_id
| TEMPLATE pseudo_destructor_id
'''
pass
def p_nested_pseudo_destructor_id(p):
'''nested_pseudo_destructor_id : pseudo_destructor_id
| id_scope nested_pseudo_destructor_id
'''
pass
def p_scoped_pseudo_destructor_id(p):
'''scoped_pseudo_destructor_id : nested_pseudo_destructor_id
| global_scope scoped_pseudo_destructor_id
'''
pass
#-------------------------------------------------------------------------------
# A.2 Lexical conventions
#-------------------------------------------------------------------------------
#
def p_literal(p):
'''literal : IntegerLiteral
| CharacterLiteral
| FloatingLiteral
| StringLiteral
| TRUE
| FALSE
'''
pass
#-------------------------------------------------------------------------------
# A.3 Basic concepts
#-------------------------------------------------------------------------------
def p_translation_unit(p):
'''translation_unit : declaration_seq_opt
'''
pass
#-------------------------------------------------------------------------------
# A.4 Expressions
#-------------------------------------------------------------------------------
#
# primary_expression covers an arbitrary sequence of all names with the
# exception of an unscoped destructor, which is parsed as its unary expression
# which is the correct disambiguation (when ambiguous). This eliminates the
# traditional A(B) meaning A B ambiguity, since we never have to tack an A
# onto the front of something that might start with (. The name length got
# maximised ab initio. The downside is that semantic interpretation must split
# the names up again.
#
# Unification of the declaration and expression syntax means that unary and
# binary pointer declarator operators:
# int * * name
# are parsed as binary and unary arithmetic operators (int) * (*name). Since
# type information is not used
# ambiguities resulting from a cast
# (cast)*(value)
# are resolved to favour the binary rather than the cast unary to ease AST
# clean-up. The cast-call ambiguity must be resolved to the cast to ensure
# that (a)(b)c can be parsed.
#
# The problem of the functional cast ambiguity
# name(arg)
# as call or declaration is avoided by maximising the name within the parsing
# kernel. So primary_id_expression picks up
# extern long int const var = 5;
# as an assignment to the syntax parsed as "extern long int const var". The
# presence of two names is parsed so that "extern long into const" is
# distinguished from "var" considerably simplifying subsequent
# semantic resolution.
#
# The generalised name is a concatenation of potential type-names (scoped
# identifiers or built-in sequences) plus optionally one of the special names
# such as an operator-function-id, conversion-function-id or destructor as the
# final name.
#
def get_rest(p):
return [p[i] for i in range(1, len(p))]
def p_primary_expression(p):
'''primary_expression : literal
| THIS
| suffix_decl_specified_ids
| abstract_expression %prec REDUCE_HERE_MOSTLY
'''
p[0] = get_rest(p)
#
# Abstract-expression covers the () and [] of abstract-declarators.
#
def p_abstract_expression(p):
'''abstract_expression : parenthesis_clause
| LBRACKET bexpression_opt RBRACKET
| TEMPLATE abstract_expression
'''
pass
def p_postfix_expression(p):
'''postfix_expression : primary_expression
| postfix_expression parenthesis_clause
| postfix_expression LBRACKET bexpression_opt RBRACKET
| postfix_expression LBRACKET bexpression_opt RBRACKET attributes
| postfix_expression '.' declarator_id
| postfix_expression '.' scoped_pseudo_destructor_id
| postfix_expression ARROW declarator_id
| postfix_expression ARROW scoped_pseudo_destructor_id
| postfix_expression INC
| postfix_expression DEC
| DYNAMIC_CAST '<' nonlgt_seq_opt '>' '(' expression ')'
| STATIC_CAST '<' nonlgt_seq_opt '>' '(' expression ')'
| REINTERPRET_CAST '<' nonlgt_seq_opt '>' '(' expression ')'
| CONST_CAST '<' nonlgt_seq_opt '>' '(' expression ')'
| TYPEID parameters_clause
'''
#print "HERE",str(p[1])
p[0] = get_rest(p)
def p_bexpression_opt(p):
'''bexpression_opt : empty
| bexpression
'''
pass
def p_bexpression(p):
'''bexpression : nonbracket_seq
| nonbracket_seq bexpression_seq bexpression_clause nonbracket_seq_opt
| bexpression_seq bexpression_clause nonbracket_seq_opt
'''
pass
def p_bexpression_seq(p):
'''bexpression_seq : empty
| bexpression_seq bexpression_clause nonbracket_seq_opt
'''
pass
def p_bexpression_clause(p):
'''bexpression_clause : LBRACKET bexpression_opt RBRACKET
'''
pass
def p_expression_list_opt(p):
'''expression_list_opt : empty
| expression_list
'''
pass
def p_expression_list(p):
'''expression_list : assignment_expression
| expression_list ',' assignment_expression
'''
pass
def p_unary_expression(p):
'''unary_expression : postfix_expression
| INC cast_expression
| DEC cast_expression
| ptr_operator cast_expression
| suffix_decl_specified_scope star_ptr_operator cast_expression
| '+' cast_expression
| '-' cast_expression
| '!' cast_expression
| '~' cast_expression
| SIZEOF unary_expression
| new_expression
| global_scope new_expression
| delete_expression
| global_scope delete_expression
'''
p[0] = get_rest(p)
def p_delete_expression(p):
'''delete_expression : DELETE cast_expression
'''
pass
def p_new_expression(p):
'''new_expression : NEW new_type_id new_initializer_opt
| NEW parameters_clause new_type_id new_initializer_opt
| NEW parameters_clause
| NEW parameters_clause parameters_clause new_initializer_opt
'''
pass
def p_new_type_id(p):
'''new_type_id : type_specifier ptr_operator_seq_opt
| type_specifier new_declarator
| type_specifier new_type_id
'''
pass
def p_new_declarator(p):
'''new_declarator : ptr_operator new_declarator
| direct_new_declarator
'''
pass
def p_direct_new_declarator(p):
'''direct_new_declarator : LBRACKET bexpression_opt RBRACKET
| direct_new_declarator LBRACKET bexpression RBRACKET
'''
pass
def p_new_initializer_opt(p):
'''new_initializer_opt : empty
| '(' expression_list_opt ')'
'''
pass
#
# cast-expression is generalised to support a [] as well as a () prefix. This covers the omission of
# DELETE[] which when followed by a parenthesised expression was ambiguous. It also covers the gcc
# indexed array initialisation for free.
#
def p_cast_expression(p):
'''cast_expression : unary_expression
| abstract_expression cast_expression
'''
p[0] = get_rest(p)
def p_pm_expression(p):
'''pm_expression : cast_expression
| pm_expression DOT_STAR cast_expression
| pm_expression ARROW_STAR cast_expression
'''
p[0] = get_rest(p)
def p_multiplicative_expression(p):
'''multiplicative_expression : pm_expression
| multiplicative_expression star_ptr_operator pm_expression
| multiplicative_expression '/' pm_expression
| multiplicative_expression '%' pm_expression
'''
p[0] = get_rest(p)
def p_additive_expression(p):
'''additive_expression : multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression
'''
p[0] = get_rest(p)
def p_shift_expression(p):
'''shift_expression : additive_expression
| shift_expression SHL additive_expression
| shift_expression SHR additive_expression
'''
p[0] = get_rest(p)
# | relational_expression '<' shift_expression
# | relational_expression '>' shift_expression
# | relational_expression LE shift_expression
# | relational_expression GE shift_expression
def p_relational_expression(p):
'''relational_expression : shift_expression
'''
p[0] = get_rest(p)
def p_equality_expression(p):
'''equality_expression : relational_expression
| equality_expression EQ relational_expression
| equality_expression NE relational_expression
'''
p[0] = get_rest(p)
def p_and_expression(p):
'''and_expression : equality_expression
| and_expression '&' equality_expression
'''
p[0] = get_rest(p)
def p_exclusive_or_expression(p):
'''exclusive_or_expression : and_expression
| exclusive_or_expression '^' and_expression
'''
p[0] = get_rest(p)
def p_inclusive_or_expression(p):
'''inclusive_or_expression : exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
'''
p[0] = get_rest(p)
def p_logical_and_expression(p):
'''logical_and_expression : inclusive_or_expression
| logical_and_expression LOG_AND inclusive_or_expression
'''
p[0] = get_rest(p)
def p_logical_or_expression(p):
'''logical_or_expression : logical_and_expression
| logical_or_expression LOG_OR logical_and_expression
'''
p[0] = get_rest(p)
def p_conditional_expression(p):
'''conditional_expression : logical_or_expression
| logical_or_expression '?' expression ':' assignment_expression
'''
p[0] = get_rest(p)
#
# assignment-expression is generalised to cover the simple assignment of a braced initializer in order to
# contribute to the coverage of parameter-declaration and init-declaration.
#
# | logical_or_expression assignment_operator assignment_expression
def p_assignment_expression(p):
'''assignment_expression : conditional_expression
| logical_or_expression assignment_operator nonsemicolon_seq
| logical_or_expression '=' braced_initializer
| throw_expression
'''
p[0]=get_rest(p)
def p_assignment_operator(p):
'''assignment_operator : '='
| ASS_ADD
| ASS_AND
| ASS_DIV
| ASS_MOD
| ASS_MUL
| ASS_OR
| ASS_SHL
| ASS_SHR
| ASS_SUB
| ASS_XOR
'''
pass
#
# expression is widely used and usually single-element, so the reductions are arranged so that a
# single-element expression is returned as is. Multi-element expressions are parsed as a list that
# may then behave polymorphically as an element or be compacted to an element.
#
def p_expression(p):
'''expression : assignment_expression
| expression_list ',' assignment_expression
'''
p[0] = get_rest(p)
def p_constant_expression(p):
'''constant_expression : conditional_expression
'''
pass
#---------------------------------------------------------------------------------------------------
# A.5 Statements
#---------------------------------------------------------------------------------------------------
# Parsing statements is easy once simple_declaration has been generalised to cover expression_statement.
#
#
# The use of extern here is a hack. The 'extern "C" {}' block gets parsed
# as a function, so when nested 'extern "C"' declarations exist, they don't
# work because the block is viewed as a list of statements... :(
#
def p_statement(p):
'''statement : compound_statement
| declaration_statement
| try_block
| labeled_statement
| selection_statement
| iteration_statement
| jump_statement
'''
pass
def p_compound_statement(p):
'''compound_statement : LBRACE statement_seq_opt RBRACE
'''
pass
def p_statement_seq_opt(p):
'''statement_seq_opt : empty
| statement_seq_opt statement
'''
pass
#
# The dangling else conflict is resolved to the innermost if.
#
def p_selection_statement(p):
'''selection_statement : IF '(' condition ')' statement %prec SHIFT_THERE
| IF '(' condition ')' statement ELSE statement
| SWITCH '(' condition ')' statement
'''
pass
def p_condition_opt(p):
'''condition_opt : empty
| condition
'''
pass
def p_condition(p):
'''condition : nonparen_seq
| nonparen_seq condition_seq parameters_clause nonparen_seq_opt
| condition_seq parameters_clause nonparen_seq_opt
'''
pass
def p_condition_seq(p):
'''condition_seq : empty
| condition_seq parameters_clause nonparen_seq_opt
'''
pass
def p_labeled_statement(p):
'''labeled_statement : identifier ':' statement
| CASE constant_expression ':' statement
| DEFAULT ':' statement
'''
pass
def p_try_block(p):
'''try_block : TRY compound_statement handler_seq
'''
global noExceptionLogic
noExceptionLogic=False
def p_jump_statement(p):
'''jump_statement : BREAK ';'
| CONTINUE ';'
| RETURN nonsemicolon_seq ';'
| GOTO identifier ';'
'''
pass
def p_iteration_statement(p):
'''iteration_statement : WHILE '(' condition ')' statement
| DO statement WHILE '(' expression ')' ';'
| FOR '(' nonparen_seq_opt ')' statement
'''
pass
def p_declaration_statement(p):
'''declaration_statement : block_declaration
'''
pass
#---------------------------------------------------------------------------------------------------
# A.6 Declarations
#---------------------------------------------------------------------------------------------------
def p_compound_declaration(p):
'''compound_declaration : LBRACE declaration_seq_opt RBRACE
'''
pass
def p_declaration_seq_opt(p):
'''declaration_seq_opt : empty
| declaration_seq_opt declaration
'''
pass