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