1# Copyright (c) 2009 The Hewlett-Packard Development Company 2# Copyright (c) 2017 Google Inc. 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer; 9# redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the 11# documentation and/or other materials provided with the distribution; 12# neither the name of the copyright holders nor the names of its 13# contributors may be used to endorse or promote products derived from 14# this software without specific prior written permission. 15# 16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27# 28# Authors: Nathan Binkert 29# Lena Olson 30 31import os.path 32import re 33import sys 34 35from m5.util import code_formatter 36from m5.util.grammar import Grammar, ParseError 37 38import slicc.ast as ast 39import slicc.util as util 40from slicc.symbols import SymbolTable 41 42class SLICC(Grammar): 43 def __init__(self, filename, base_dir, verbose=False, traceback=False, **kwargs): 44 self.protocol = None 45 self.traceback = traceback 46 self.verbose = verbose 47 self.symtab = SymbolTable(self) 48 self.base_dir = base_dir 49 50 try: 51 self.decl_list = self.parse_file(filename, **kwargs) 52 except ParseError, e: 53 if not self.traceback: 54 sys.exit(str(e)) 55 raise 56 57 def currentLocation(self): 58 return util.Location(self.current_source, self.current_line, 59 no_warning=not self.verbose) 60 61 def codeFormatter(self, *args, **kwargs): 62 code = code_formatter(*args, **kwargs) 63 code['protocol'] = self.protocol 64 return code 65 66 def process(self): 67 self.decl_list.generate() 68 69 def writeCodeFiles(self, code_path, includes): 70 self.symtab.writeCodeFiles(code_path, includes) 71 72 def writeHTMLFiles(self, html_path): 73 self.symtab.writeHTMLFiles(html_path) 74 75 def files(self): 76 f = set(['Types.hh']) 77 78 f |= self.decl_list.files() 79 80 return f 81 82 t_ignore = '\t ' 83 84 # C or C++ comment (ignore) 85 def t_c_comment(self, t): 86 r'/\*(.|\n)*?\*/' 87 t.lexer.lineno += t.value.count('\n') 88 89 def t_cpp_comment(self, t): 90 r'//.*' 91 92 # Define a rule so we can track line numbers 93 def t_newline(self, t): 94 r'\n+' 95 t.lexer.lineno += len(t.value) 96 97 reserved = { 98 'protocol' : 'PROTOCOL', 99 'include' : 'INCLUDE', 100 'global' : 'GLOBAL', 101 'machine' : 'MACHINE', 102 'in_port' : 'IN_PORT', 103 'out_port' : 'OUT_PORT', 104 'action' : 'ACTION', 105 'transition' : 'TRANS', 106 'structure' : 'STRUCT', 107 'external_type' : 'EXTERN_TYPE', 108 'enumeration' : 'ENUM', 109 'state_declaration' : 'STATE_DECL', 110 'peek' : 'PEEK', 111 'stall_and_wait' : 'STALL_AND_WAIT', 112 'enqueue' : 'ENQUEUE', 113 'check_allocate' : 'CHECK_ALLOCATE', 114 'check_next_cycle' : 'CHECK_NEXT_CYCLE', 115 'check_stop_slots' : 'CHECK_STOP_SLOTS', 116 'check_on_cache_probe' : 'CHECK_PROBE', 117 'static_cast' : 'STATIC_CAST', 118 'if' : 'IF', 119 'is_valid' : 'IS_VALID', 120 'is_invalid' : 'IS_INVALID', 121 'else' : 'ELSE', 122 'return' : 'RETURN', 123 'void' : 'VOID', 124 'new' : 'NEW', 125 'OOD' : 'OOD', 126 } 127 128 literals = ':[]{}(),=' 129 130 tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE', 131 'LEFTSHIFT', 'RIGHTSHIFT', 132 'NOT', 'AND', 'OR', 133 'PLUS', 'DASH', 'STAR', 'SLASH', 134 'INCR', 'DECR', 135 'DOUBLE_COLON', 'SEMI', 136 'ASSIGN', 'DOT', 137 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ] 138 tokens += reserved.values() 139 140 t_EQ = r'==' 141 t_NE = r'!=' 142 t_LT = r'<' 143 t_GT = r'>' 144 t_LE = r'<=' 145 t_GE = r'>=' 146 t_LEFTSHIFT = r'<<' 147 t_RIGHTSHIFT = r'>>' 148 t_NOT = r'!' 149 t_AND = r'&&' 150 t_OR = r'\|\|' 151 t_PLUS = r'\+' 152 t_DASH = r'-' 153 t_STAR = r'\*' 154 t_SLASH = r'/' 155 t_DOUBLE_COLON = r'::' 156 t_SEMI = r';' 157 t_ASSIGN = r':=' 158 t_DOT = r'\.' 159 t_INCR = r'\+\+' 160 t_DECR = r'--' 161 162 precedence = ( 163 ('left', 'INCR', 'DECR'), 164 ('left', 'OR'), 165 ('left', 'AND'), 166 ('left', 'EQ', 'NE'), 167 ('left', 'LT', 'GT', 'LE', 'GE'), 168 ('left', 'RIGHTSHIFT', 'LEFTSHIFT'), 169 ('left', 'PLUS', 'DASH'), 170 ('left', 'STAR', 'SLASH'), 171 ('right', 'NOT', 'UMINUS'), 172 ) 173 174 def t_IDENT(self, t): 175 r'[a-zA-Z_][a-zA-Z_0-9]*' 176 if t.value == 'true': 177 t.type = 'LIT_BOOL' 178 t.value = True 179 return t 180 181 if t.value == 'false': 182 t.type = 'LIT_BOOL' 183 t.value = False 184 return t 185 186 # Check for reserved words 187 t.type = self.reserved.get(t.value, 'IDENT') 188 return t 189 190 def t_FLOATNUMBER(self, t): 191 '[0-9]+[.][0-9]+' 192 try: 193 t.value = float(t.value) 194 except ValueError: 195 raise ParseError("Illegal float", t) 196 return t 197 198 def t_NUMBER(self, t): 199 r'[0-9]+' 200 try: 201 t.value = int(t.value) 202 except ValueError: 203 raise ParseError("Illegal number", t) 204 return t 205 206 def t_STRING1(self, t): 207 r'\"[^"\n]*\"' 208 t.type = 'STRING' 209 t.value = t.value[1:-1] 210 return t 211 212 def t_STRING2(self, t): 213 r"\'[^'\n]*\'" 214 t.type = 'STRING' 215 t.value = t.value[1:-1] 216 return t 217 218 def p_file(self, p): 219 "file : decls" 220 p[0] = p[1] 221 222 def p_empty(self, p): 223 "empty :" 224 225 def p_decls(self, p): 226 "decls : declsx" 227 p[0] = ast.DeclListAST(self, p[1]) 228 229 def p_declsx__list(self, p): 230 "declsx : decl declsx" 231 if isinstance(p[1], ast.DeclListAST): 232 decls = p[1].decls 233 elif p[1] is None: 234 decls = [] 235 else: 236 decls = [ p[1] ] 237 p[0] = decls + p[2] 238 239 def p_declsx__none(self, p): 240 "declsx : empty" 241 p[0] = [] 242 243 def p_decl__protocol(self, p): 244 "decl : PROTOCOL STRING SEMI" 245 if self.protocol: 246 msg = "Protocol can only be set once! Error at %s:%s\n" % \ 247 (self.current_source, self.current_line) 248 raise ParseError(msg) 249 self.protocol = p[2] 250 p[0] = None 251 252 def p_decl__include(self, p): 253 "decl : INCLUDE STRING SEMI" 254 dirname = os.path.dirname(self.current_source) 255 if os.path.exists(os.path.join(dirname, p[2])): 256 filename = os.path.join(dirname, p[2]) 257 else: 258 filename = os.path.join(self.base_dir, p[2]) 259 p[0] = self.parse_file(filename) 260 261 def p_decl__machine0(self, p): 262 "decl : MACHINE '(' enumeration ')' ':' obj_decls '{' decls '}'" 263 p[0] = ast.MachineAST(self, p[3], [], p[7], p[9]) 264 265 def p_decl__machine1(self, p): 266 "decl : MACHINE '(' enumeration pairs ')' ':' obj_decls '{' decls '}'" 267 p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9]) 268 269 def p_decl__action(self, p): 270 "decl : ACTION '(' ident pairs ')' statements" 271 p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6]) 272 273 def p_decl__in_port(self, p): 274 "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements" 275 p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10]) 276 277 def p_decl__out_port(self, p): 278 "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI" 279 p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8]) 280 281 def p_decl__trans0(self, p): 282 "decl : TRANS '(' idents ',' idents ',' ident_or_star ')' idents" 283 p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], p[7], p[9]) 284 285 def p_decl__trans1(self, p): 286 "decl : TRANS '(' idents ',' idents ')' idents" 287 p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], None, p[7]) 288 289 def p_decl__trans2(self, p): 290 "decl : TRANS '(' idents ',' idents ',' ident_or_star ')' idents idents" 291 p[0] = ast.TransitionDeclAST(self, p[9], p[3], p[5], p[7], p[10]) 292 293 def p_decl__trans3(self, p): 294 "decl : TRANS '(' idents ',' idents ')' idents idents" 295 p[0] = ast.TransitionDeclAST(self, p[7], p[3], p[5], None, p[8]) 296 297 def p_decl__extern0(self, p): 298 "decl : EXTERN_TYPE '(' type pairs ')' SEMI" 299 p[4]["external"] = "yes" 300 p[0] = ast.TypeDeclAST(self, p[3], p[4], []) 301 302 def p_decl__global(self, p): 303 "decl : GLOBAL '(' type pairs ')' '{' type_members '}'" 304 p[4]["global"] = "yes" 305 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7]) 306 307 def p_decl__struct(self, p): 308 "decl : STRUCT '(' type pairs ')' '{' type_members '}'" 309 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7]) 310 311 def p_decl__enum(self, p): 312 "decl : ENUM '(' type pairs ')' '{' type_enums '}'" 313 p[4]["enumeration"] = "yes" 314 p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7]) 315 316 def p_decl__state_decl(self, p): 317 "decl : STATE_DECL '(' type pairs ')' '{' type_states '}'" 318 p[4]["enumeration"] = "yes" 319 p[4]["state_decl"] = "yes" 320 p[0] = ast.StateDeclAST(self, p[3], p[4], p[7]) 321 322 # Type fields 323 def p_obj_decls__list(self, p): 324 "obj_decls : obj_decl obj_decls" 325 p[0] = [ p[1] ] + p[2] 326 327 def p_obj_decls__empty(self, p): 328 "obj_decls : empty" 329 p[0] = [] 330 331 def p_type_members__list(self, p): 332 "type_members : type_member type_members" 333 p[0] = [ p[1] ] + p[2] 334 335 def p_type_members__empty(self, p): 336 "type_members : empty" 337 p[0] = [] 338 339 def p_type_member__0(self, p): 340 """type_member : obj_decl 341 | func_decl 342 | func_def""" 343 p[0] = p[1] 344 345 # Member / Variable declarations 346 def p_decl__obj_decl(self, p): 347 "decl : obj_decl" 348 p[0] = p[1] 349 350 def p_obj_decl__0(self, p): 351 "obj_decl : type ident pairs SEMI" 352 p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3], None, False) 353 354 def p_obj_decl__1(self, p): 355 "obj_decl : type STAR ident pairs SEMI" 356 p[0] = ast.ObjDeclAST(self, p[1], p[3], p[4], None, True) 357 358 def p_obj_decl__2(self, p): 359 "obj_decl : type ident ASSIGN expr SEMI" 360 p[0] = ast.ObjDeclAST(self, p[1], p[2], ast.PairListAST(self), p[4], 361 False) 362 363 def p_obj_decl__3(self, p): 364 "obj_decl : type STAR ident ASSIGN expr SEMI" 365 p[0] = ast.ObjDeclAST(self, p[1], p[3], ast.PairListAST(self), p[5], 366 True) 367 368 # Function definition and declaration 369 def p_decl__func_decl(self, p): 370 "decl : func_decl" 371 p[0] = p[1] 372 373 def p_func_decl__0(self, p): 374 """func_decl : void ident '(' params ')' pairs SEMI 375 | type ident '(' params ')' pairs SEMI""" 376 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None) 377 378 def p_func_decl__1(self, p): 379 """func_decl : void ident '(' types ')' pairs SEMI 380 | type ident '(' types ')' pairs SEMI""" 381 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None) 382 383 def p_decl__func_def(self, p): 384 "decl : func_def" 385 p[0] = p[1] 386 387 def p_func_def__0(self, p): 388 """func_def : void ident '(' params ')' pairs statements 389 | type ident '(' params ')' pairs statements""" 390 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7]) 391 392 # Enum fields 393 def p_type_enums__list(self, p): 394 "type_enums : type_enum type_enums" 395 p[0] = [ p[1] ] + p[2] 396 397 def p_type_enums__empty(self, p): 398 "type_enums : empty" 399 p[0] = [] 400 401 def p_type_enum(self, p): 402 "type_enum : ident pairs SEMI" 403 p[0] = ast.TypeFieldEnumAST(self, p[1], p[2]) 404 405 # States 406 def p_type_states__list(self, p): 407 "type_states : type_state type_states" 408 p[0] = [ p[1] ] + p[2] 409 410 def p_type_states__empty(self, p): 411 "type_states : empty" 412 p[0] = [] 413 414 def p_type_state(self, p): 415 "type_state : ident ',' enumeration pairs SEMI" 416 p[0] = ast.TypeFieldStateAST(self, p[1], p[3], p[4]) 417 418 # Formal Param 419 def p_params__many(self, p): 420 "params : param ',' params" 421 p[0] = [ p[1] ] + p[3] 422 423 def p_params__one(self, p): 424 "params : param" 425 p[0] = [ p[1] ] 426 427 def p_params__none(self, p): 428 "params : empty" 429 p[0] = [] 430 431 def p_param(self, p): 432 "param : type ident" 433 p[0] = ast.FormalParamAST(self, p[1], p[2]) 434 435 def p_param__pointer(self, p): 436 "param : type STAR ident" 437 p[0] = ast.FormalParamAST(self, p[1], p[3], None, True) 438 439 def p_param__pointer_default(self, p): 440 "param : type STAR ident ASSIGN STRING" 441 p[0] = ast.FormalParamAST(self, p[1], p[3], p[5], True) 442 443 def p_param__default_number(self, p): 444 "param : type ident ASSIGN NUMBER" 445 p[0] = ast.FormalParamAST(self, p[1], p[2], p[4]) 446 447 def p_param__default_bool(self, p): 448 "param : type ident ASSIGN LIT_BOOL" 449 p[0] = ast.FormalParamAST(self, p[1], p[2], p[4]) 450 451 def p_param__default_string(self, p): 452 "param : type ident ASSIGN STRING" 453 p[0] = ast.FormalParamAST(self, p[1], p[2], p[4]) 454 455 # Type 456 def p_types__multiple(self, p): 457 "types : type ',' types" 458 p[0] = [ p[1] ] + p[3] 459 460 def p_types__one(self, p): 461 "types : type" 462 p[0] = [ p[1] ] 463 464 def p_types__empty(self, p): 465 "types : empty" 466 p[0] = [] 467 468 def p_typestr__multi(self, p): 469 "typestr : typestr DOUBLE_COLON ident" 470 p[0] = '%s::%s' % (p[1], p[3]) 471 472 def p_typestr__single(self, p): 473 "typestr : ident" 474 p[0] = p[1] 475 476 def p_type__one(self, p): 477 "type : typestr" 478 p[0] = ast.TypeAST(self, p[1]) 479 480 def p_void(self, p): 481 "void : VOID" 482 p[0] = ast.TypeAST(self, p[1]) 483 484 # Idents and lists 485 def p_idents__braced(self, p): 486 "idents : '{' identx '}'" 487 p[0] = p[2] 488 489 def p_idents__bare(self, p): 490 "idents : ident" 491 p[0] = [ p[1] ] 492 493 def p_identx__multiple_1(self, p): 494 """identx : ident SEMI identx 495 | ident ',' identx""" 496 p[0] = [ p[1] ] + p[3] 497 498 def p_identx__multiple_2(self, p): 499 "identx : ident identx" 500 p[0] = [ p[1] ] + p[2] 501 502 def p_identx__single(self, p): 503 "identx : empty" 504 p[0] = [ ] 505 506 def p_ident(self, p): 507 "ident : IDENT" 508 p[0] = p[1] 509 510 def p_ident_or_star(self, p): 511 """ident_or_star : ident 512 | STAR""" 513 p[0] = p[1] 514 515 # Pair and pair lists 516 def p_pairs__list(self, p): 517 "pairs : ',' pairsx" 518 p[0] = p[2] 519 520 def p_pairs__empty(self, p): 521 "pairs : empty" 522 p[0] = ast.PairListAST(self) 523 524 def p_pairsx__many(self, p): 525 "pairsx : pair ',' pairsx" 526 p[0] = p[3] 527 p[0].addPair(p[1]) 528 529 def p_pairsx__one(self, p): 530 "pairsx : pair" 531 p[0] = ast.PairListAST(self) 532 p[0].addPair(p[1]) 533 534 def p_pair__assign(self, p): 535 """pair : ident '=' STRING 536 | ident '=' ident 537 | ident '=' NUMBER""" 538 p[0] = ast.PairAST(self, p[1], p[3]) 539 540 def p_pair__literal(self, p): 541 "pair : STRING" 542 p[0] = ast.PairAST(self, "short", p[1]) 543 544 # Below are the rules for action descriptions 545 def p_statements__inner(self, p): 546 "statements : '{' statements_inner '}'" 547 p[0] = ast.StatementListAST(self, p[2]) 548 549 def p_statements__none(self, p): 550 "statements : '{' '}'" 551 p[0] = ast.StatementListAST(self, []) 552 553 def p_statements_inner__many(self, p): 554 "statements_inner : statement statements_inner" 555 p[0] = [ p[1] ] + p[2] 556 557 def p_statements_inner__one(self, p): 558 "statements_inner : statement" 559 p[0] = [ p[1] ] 560 561 def p_exprs__multiple(self, p): 562 "exprs : expr ',' exprs" 563 p[0] = [ p[1] ] + p[3] 564 565 def p_exprs__one(self, p): 566 "exprs : expr" 567 p[0] = [ p[1] ] 568 569 def p_exprs__empty(self, p): 570 "exprs : empty""" 571 p[0] = [] 572 573 def p_statement__expression(self, p): 574 "statement : expr SEMI" 575 p[0] = ast.ExprStatementAST(self, p[1]) 576 577 def p_statement__assign(self, p): 578 "statement : expr ASSIGN expr SEMI" 579 p[0] = ast.AssignStatementAST(self, p[1], p[3]) 580 581 def p_statement__enqueue(self, p): 582 "statement : ENQUEUE '(' var ',' type ')' statements" 583 p[0] = ast.EnqueueStatementAST(self, p[3], p[5], None, p[7]) 584 585 def p_statement__enqueue_latency(self, p): 586 "statement : ENQUEUE '(' var ',' type ',' expr ')' statements" 587 p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[7], p[9]) 588 589 def p_statement__stall_and_wait(self, p): 590 "statement : STALL_AND_WAIT '(' var ',' var ')' SEMI" 591 p[0] = ast.StallAndWaitStatementAST(self, p[3], p[5]) 592 593 def p_statement__peek(self, p): 594 "statement : PEEK '(' var ',' type pairs ')' statements" 595 p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek") 596 597 def p_statement__check_allocate(self, p): 598 "statement : CHECK_ALLOCATE '(' var ')' SEMI" 599 p[0] = ast.CheckAllocateStatementAST(self, p[3]) 600 601 def p_statement__check_next_cycle(self, p): 602 "statement : CHECK_NEXT_CYCLE '(' ')' SEMI" 603 p[0] = ast.CheckNextCycleAST(self) 604 605 def p_statement__check_stop(self, p): 606 "statement : CHECK_STOP_SLOTS '(' var ',' STRING ',' STRING ')' SEMI" 607 p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7]) 608 609 def p_statement__check_probe(self, p): 610 "statement : CHECK_PROBE '(' var ',' var ')' SEMI" 611 p[0] = ast.CheckProbeStatementAST(self, p[3], p[5]) 612 613 def p_statement__return(self, p): 614 "statement : RETURN expr SEMI" 615 p[0] = ast.ReturnStatementAST(self, p[2]) 616 617 def p_statement__if(self, p): 618 "statement : if_statement" 619 p[0] = p[1] 620 621 def p_if_statement__if(self, p): 622 "if_statement : IF '(' expr ')' statements" 623 p[0] = ast.IfStatementAST(self, p[3], p[5], None) 624 625 def p_if_statement__if_else(self, p): 626 "if_statement : IF '(' expr ')' statements ELSE statements" 627 p[0] = ast.IfStatementAST(self, p[3], p[5], p[7]) 628 629 def p_statement__if_else_if(self, p): 630 "if_statement : IF '(' expr ')' statements ELSE if_statement" 631 p[0] = ast.IfStatementAST(self, p[3], p[5], 632 ast.StatementListAST(self, p[7])) 633 634 def p_expr__static_cast(self, p): 635 "aexpr : STATIC_CAST '(' type ',' expr ')'" 636 p[0] = ast.StaticCastAST(self, p[3], "ref", p[5]) 637 638 def p_expr__static_cast_ptr(self, p): 639 "aexpr : STATIC_CAST '(' type ',' STRING ',' expr ')'" 640 p[0] = ast.StaticCastAST(self, p[3], p[5], p[7]) 641 642 def p_expr__var(self, p): 643 "aexpr : var" 644 p[0] = p[1] 645 646 def p_expr__localvar(self, p): 647 "aexpr : type ident" 648 p[0] = ast.LocalVariableAST(self, p[1], p[2]) 649 650 def p_expr__literal(self, p): 651 "aexpr : literal" 652 p[0] = p[1] 653 654 def p_expr__enumeration(self, p): 655 "aexpr : enumeration" 656 p[0] = p[1] 657 658 def p_expr__func_call(self, p): 659 "aexpr : ident '(' exprs ')'" 660 p[0] = ast.FuncCallExprAST(self, p[1], p[3]) 661 662 def p_expr__new(self, p): 663 "aexpr : NEW type" 664 p[0] = ast.NewExprAST(self, p[2]) 665 666 def p_expr__null(self, p): 667 "aexpr : OOD" 668 p[0] = ast.OodAST(self) 669 670 def p_expr__member(self, p): 671 "aexpr : aexpr DOT ident" 672 p[0] = ast.MemberExprAST(self, p[1], p[3]) 673 674 def p_expr__member_method_call(self, p): 675 "aexpr : aexpr DOT ident '(' exprs ')'" 676 p[0] = ast.MemberMethodCallExprAST(self, p[1], 677 ast.FuncCallExprAST(self, p[3], p[5])) 678 679 def p_expr__member_method_call_lookup(self, p): 680 "aexpr : aexpr '[' exprs ']'" 681 p[0] = ast.MemberMethodCallExprAST(self, p[1], 682 ast.FuncCallExprAST(self, "lookup", p[3])) 683 684 def p_expr__class_method_call(self, p): 685 "aexpr : type DOUBLE_COLON ident '(' exprs ')'" 686 p[0] = ast.ClassMethodCallExprAST(self, p[1], 687 ast.FuncCallExprAST(self, p[3], p[5])) 688 689 def p_expr__aexpr(self, p): 690 "expr : aexpr" 691 p[0] = p[1] 692 693 def p_expr__binary_op(self, p): 694 """expr : expr STAR expr 695 | expr SLASH expr 696 | expr PLUS expr 697 | expr DASH expr 698 | expr LT expr 699 | expr GT expr 700 | expr LE expr 701 | expr GE expr 702 | expr EQ expr 703 | expr NE expr 704 | expr AND expr 705 | expr OR expr 706 | expr RIGHTSHIFT expr 707 | expr LEFTSHIFT expr""" 708 p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3]) 709 710 # FIXME - unary not 711 def p_expr__unary_op(self, p): 712 """expr : NOT expr 713 | INCR expr 714 | DECR expr 715 | DASH expr %prec UMINUS""" 716 p[0] = ast.PrefixOperatorExprAST(self, p[1], p[2]) 717 718 def p_expr__parens(self, p): 719 "aexpr : '(' expr ')'" 720 p[0] = p[2] 721 722 def p_expr__is_valid_ptr(self, p): 723 "aexpr : IS_VALID '(' var ')'" 724 p[0] = ast.IsValidPtrExprAST(self, p[3], True) 725 726 def p_expr__is_invalid_ptr(self, p): 727 "aexpr : IS_INVALID '(' var ')'" 728 p[0] = ast.IsValidPtrExprAST(self, p[3], False) 729 730 def p_literal__string(self, p): 731 "literal : STRING" 732 p[0] = ast.LiteralExprAST(self, p[1], "std::string") 733 734 def p_literal__number(self, p): 735 "literal : NUMBER" 736 p[0] = ast.LiteralExprAST(self, p[1], "int") 737 738 def p_literal__float(self, p): 739 "literal : FLOATNUMBER" 740 p[0] = ast.LiteralExprAST(self, p[1], "int") 741 742 def p_literal__bool(self, p): 743 "literal : LIT_BOOL" 744 p[0] = ast.LiteralExprAST(self, p[1], "bool") 745 746 def p_enumeration(self, p): 747 "enumeration : ident ':' ident" 748 p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3]) 749 750 def p_var(self, p): 751 "var : ident" 752 p[0] = ast.VarExprAST(self, p[1]) 753