Best Python code snippet using autotest_python
parser.py
Source:parser.py
...37 self.cur_statement_start_pos_col = self.token.pos_col38 # each statement starts with TokenType.T_LPAREN39 # raise error otherwise40 if not self.token_type_is(TokenType.T_LPAREN):41 self.raise_parse_error(msg="There is no open left paren at the statement begining")42 self.next_token()43 # try to parse fun def statement44 statement = self.get_fun_def_statement()45 if statement:46 return statement47 # try to parse non fun def statement48 statement = self.get_no_fun_def_statement()49 if statement:50 return statement51 # if it is not fun def statement and not non fin def statement, raise error52 self.raise_parse_error(msg="undefined statement")53 def get_no_fun_def_statement(self, include_return_statement=False):54 # try to parse var assignment statement55 statement = self.get_var_assignment_statement()56 if statement:57 return statement58 # try to parse var create statement59 statement = self.get_var_create_statement()60 if statement:61 return statement62 # try to parse input/output statement63 statement = self.get_communicate_statement()64 if statement:65 return statement66 67 # try to parse arithmetic statement68 statement = self.get_arithmetic_statement()69 if statement:70 return statement71 72 # try to parse if/ifelse statement73 statement = self.get_conditional_statement(include_return_statement)74 if statement:75 return statement76 # try to parse loop statement77 statement = self.get_repeat_statement(include_return_statement)78 if statement:79 return statement80 # try to parse fun call statement81 statement = self.get_fun_call_statement()82 if statement:83 return statement 84 # try to parse concat statement85 statement = self.get_concat_statement()86 if statement:87 return statement 88 # try to parse condition statement89 # (< 1 2)90 statement = self.get_condition_statement()91 if statement:92 return statement93 # try to parse return statement94 statement = self.get_return_statement() 95 if statement:96 # if return statement was detected but it is outside of fun block erase error97 if include_return_statement:98 return statement99 else:100 self.raise_parse_error(msg="return statement is not allowed here")101 # if fun def statement was detected but it is inside of block erase error102 statement = self.get_fun_def_statement() 103 if statement:104 self.raise_parse_error(msg="fun def statement is not allowed here")105 return None106 def get_condition_statement(self):107 # < > <= >= == !=108 if self.token_type_is_binary_compare_operator():109 comp_operator = self.token110 self.next_token()111 # try to get first condition arg112 first_condition_arg = self.get_condition_arg()113 # try to get second condition arg114 second_condition_arg = self.get_condition_arg()115 # statement shoud ended with TokenType.T_RPAREN, otherwise raise error116 if self.token_type_is(TokenType.T_RPAREN):117 self.next_token()118 return ConditionStatement(condition=BinaryComparison(comp_operator=comp_operator.get_value(), first_condition_arg=first_condition_arg, second_condition_arg=second_condition_arg))119 else:120 self.raise_parse_error(msg="there is no closing paren in condition statement")121 122 # !123 elif self.token_type_is_unary_compare_operator():124 comp_operator = self.token125 self.next_token()126 # try to get first condition arg127 first_condition_arg = self.get_condition_arg()128 # statement shoud ended with TokenType.T_RPAREN, otherwise raise error129 if self.token_type_is(TokenType.T_RPAREN):130 self.next_token()131 return ConditionStatement(condition=UnaryComparison(comp_operator=comp_operator.get_value(), condition_arg=first_condition_arg))132 else:133 self.raise_parse_error(msg="there is no closing paren in condition statement")134 # or end135 elif self.token_type_is(TokenType.T_OR) or self.token_type_is(TokenType.T_AND):136 log_operator = self.token137 self.next_token()138 # try to get first condition arg139 first_condition_arg = self.get_condition_arg()140 # try to get second condition arg141 second_condition_arg = self.get_condition_arg()142 # statement shoud ended with TokenType.T_RPAREN, otherwise raise error143 if self.token_type_is(TokenType.T_RPAREN):144 self.next_token()145 return ConditionStatement(condition=LogComparison(log_operator=log_operator.get_value(), first_condition_arg=first_condition_arg, second_condition_arg=second_condition_arg))146 else:147 self.raise_parse_error(msg="there is no closing paren in condition statement")148 149 else:150 return None151 def get_concat_statement(self):152 # .153 if self.token_type_is(TokenType.T_CONCAT):154 self.next_token()155 # try to get first concat arg156 first_concat_arg = self.get_concat_arg()157 # try to get second concat arg158 second_concat_arg = self.get_concat_arg()159 # statement shoud ended with TokenType.T_RPAREN, otherwise raise error160 if self.token_type_is(TokenType.T_RPAREN):161 self.next_token()162 return ConcatStatement(first_concat_arg=first_concat_arg, second_concat_arg=second_concat_arg)163 else:164 self.raise_parse_error(msg="there is no closing paren in concat statement")165 else:166 return None167 168 def get_concat_arg(self):169 # (170 # concat arg as another statement171 if self.token_type_is(TokenType.T_LPAREN):172 self.next_token()173 # as fun call statement174 statement = self.get_fun_call_statement()175 if statement:176 # as concat statement177 return ConcatArg(concat_arg=statement)178 statement = self.get_concat_statement()179 if statement:180 return ConcatArg(concat_arg=statement)181 self.raise_parse_error(msg="invalid concat arg format")182 # as identifier or textconst183 elif (184 self.token_type_is(TokenType.T_TXTCONST) or185 self.token_type_is(TokenType.T_IDENTIFIER)186 ):187 value = self.token188 self.next_token()189 # build proper objects190 if value.get_type() == TokenType.T_TXTCONST:191 return ConcatArg(concat_arg=TextLiteral(value=value.get_value()))192 else:193 return ConcatArg(concat_arg=Identifier(value=value.get_value()))194 else:195 self.raise_parse_error(msg="invalid concat arg format")196 def get_fun_def_statement(self):197 # fun def keyword198 if self.token_type_is(TokenType.T_FUN):199 self.next_token()200 # next should be identifier201 if self.token_type_is(TokenType.T_IDENTIFIER):202 fun_identifier = self.token203 self.next_token()204 # next return type205 if self.token_type_is_datatype() or self.token_type_is(TokenType.T_NIL):206 return_type = self.token.get_type()207 self.next_token()208 args = []209 # next args210 while not self.token_type_is(TokenType.T_LBRACE):211 args.append(self.get_arg_for_fun_def())212 # next fun block213 fun_block = self.get_fun_block(return_type)214 # should end with TokenType.T_RPAREN215 if self.token_type_is(TokenType.T_RPAREN):216 self.next_token()217 return FunDefStatement(fun_identifier=fun_identifier.get_value(), return_type=return_type, args=args, fun_block=fun_block)218 else:219 self.raise_parse_error(msg="there is no closing paren in fun def statement")220 else:221 self.raise_parse_error(msg="wrong return data type in fun def")222 else:223 self.raise_parse_error(msg="invalid fun name")224 else:225 return None226 def get_fun_block(self, return_type):227 # start with {228 if self.token_type_is(TokenType.T_LBRACE):229 self.next_token()230 statements = []231 # next read statements from block232 while not self.token_type_is(TokenType.T_RBRACE):233 if self.token_type_is(TokenType.T_LPAREN):234 self.next_token()235 statement = self.get_no_fun_def_statement(include_return_statement=True)236 if statement:237 # save it for object building238 statements.append(statement)239 else:240 self.raise_parse_error(msg="invalid statement in fun block")241 else:242 print(self.token.get_type())243 self.raise_parse_error(msg="there is no open left paren at the statement begining")244 # should be return statement in return type is not void245 return_statement = next((el for el in statements if isinstance(el, ReturnStatement)), None)246 if not return_statement and return_type != TokenType.T_NIL:247 self.raise_parse_error(msg="there is no return statement in non-nil-return fun def")248 self.next_token()249 return FunBlock(statements=statements)250 else:251 self.raise_parse_error(msg="there is no open brace in fun block")252 def get_return_statement(self):253 # start with right keyword254 if self.token_type_is(TokenType.T_RET):255 self.next_token()256 # can return identifier literal void257 if (258 self.token_type_is(TokenType.T_IDENTIFIER) or259 self.token_type_is_literal() or260 self.token_type_is(TokenType.T_NIL) 261 ):262 return_token = self.token263 self.next_token()264 if self.token_type_is(TokenType.T_RPAREN):265 self.next_token()266 # build proper objects267 if return_token.get_type() == TokenType.T_IDENTIFIER:268 return ReturnStatement(return_arg=self.build_identifier_obj(return_token))269 elif return_token.get_type() == TokenType.T_NIL:270 return ReturnStatement(return_arg=self.build_void_obj(return_token))271 else:272 return ReturnStatement(return_arg=self.build_literal_obj(return_token))273 else:274 self.raise_parse_error(msg="there is no closing paren in return statement")275 # return another statement276 elif self.token_type_is(TokenType.T_LPAREN):277 self.next_token()278 # return fun call279 statement = self.get_fun_call_statement()280 if statement:281 if self.token_type_is(TokenType.T_RPAREN):282 self.next_token()283 return ReturnStatement(return_arg=statement)284 else:285 self.raise_parse_error(msg="there is no closing paren in return statement")286 # return arithmetic287 statement = self.get_arithmetic_statement()288 if statement:289 if self.token_type_is(TokenType.T_RPAREN):290 self.next_token()291 return ReturnStatement(return_arg=statement)292 else:293 self.raise_parse_error(msg="there is no closing paren in return statement")294 # return concat295 statement = self.get_concat_statement()296 if statement:297 if self.token_type_is(TokenType.T_RPAREN):298 self.next_token()299 return ReturnStatement(return_arg=statement)300 else:301 self.raise_parse_error(msg="there is no closing paren in return statement")302 # return condition303 statement = self.get_condition_statement()304 if statement:305 if self.token_type_is(TokenType.T_RPAREN):306 self.next_token()307 return ReturnStatement(return_arg=statement)308 else:309 self.raise_parse_error(msg="there is no closing paren in return statement")310 self.raise_parse_error(msg="Invalid return value") 311 else:312 self.raise_parse_error(msg="Invalid return value") 313 else:314 return None315 def get_arg_for_fun_def(self):316 # start with (317 if self.token_type_is(TokenType.T_LPAREN):318 self.next_token()319 # next datatype320 if self.token_type_is_datatype():321 var_type = self.token322 self.next_token()323 # next identifier324 if self.token_type_is(TokenType.T_IDENTIFIER):325 var_identifier = self.token326 self.next_token()327 # end with )328 if self.token_type_is(TokenType.T_RPAREN):329 self.next_token()330 return FunDefArg(var_type=keyword_dict[var_type.get_value()], var_identifier=var_identifier.get_value())331 else:332 self.raise_parse_error(msg="there is no closing paren after arg in fun def")333 else:334 self.raise_parse_error(msg="invalid arg name in fun def")335 else:336 self.raise_parse_error(msg="invalid arg data type in fun def")337 else:338 self.raise_parse_error(msg="there is no opening paren before arg in fun def")339 def get_repeat_statement(self, include_return_statement):340 # start with right keyword341 if self.token_type_is(TokenType.T_LOOP):342 self.next_token()343 # next (344 if self.token_type_is(TokenType.T_LPAREN):345 self.next_token()346 # next condition347 condition = self.get_condition()348 # next )349 if self.token_type_is(TokenType.T_RPAREN): 350 self.next_token()351 # next block352 block = self.get_block(include_return_statement)353 # should end with )354 if self.token_type_is(TokenType.T_RPAREN):355 self.next_token()356 return RepeatStatement(condition=condition, block=block)357 else:358 self.raise_parse_error(msg="there is no closing paren in loop statement") 359 else:360 self.raise_parse_error(msg="there is no right paren in loop statement after condition")361 else:362 self.raise_parse_error(msg="there is no left paren in loop statement before condition")363 else:364 return None365 def get_conditional_statement(self, include_return_statement):366 # try to prase if statement367 statement = self.get_if_statement(include_return_statement)368 if statement:369 return statement370 # try to parse if else statement 371 statement = self.get_ifelse_statement(include_return_statement)372 if statement:373 return statement374 return None375 def get_ifelse_statement(self, include_return_statement):376 # start with right keyword377 if self.token_type_is(TokenType.T_IFEL):378 self.next_token()379 # next (380 if self.token_type_is(TokenType.T_LPAREN):381 self.next_token()382 # next condition383 condition = self.get_condition()384 if self.token_type_is(TokenType.T_RPAREN): 385 # next )386 self.next_token()387 # next first block 388 firstBlock = self.get_block(include_return_statement)389 # next second block390 secondBlock = self.get_block(include_return_statement)391 # should end with )392 if self.token_type_is(TokenType.T_RPAREN):393 self.next_token()394 return IfElseStatement(condition=condition, block_true=firstBlock, block_false=secondBlock)395 else:396 self.raise_parse_error(msg="there is no closing paren in ifelse statement") 397 else:398 self.raise_parse_error(msg="there is no right paren in if else statement after condition")399 else:400 self.raise_parse_error(msg="there is no left paren in if else statement before condition")401 else:402 return None403 def get_if_statement(self, include_return_statement):404 # start with right keyword405 if self.token_type_is(TokenType.T_IF):406 self.next_token()407 # next (408 if self.token_type_is(TokenType.T_LPAREN):409 self.next_token()410 # next condition411 condition = self.get_condition()412 # next )413 if self.token_type_is(TokenType.T_RPAREN): 414 self.next_token()415 # next block416 block = self.get_block(include_return_statement)417 # should end with )418 if self.token_type_is(TokenType.T_RPAREN):419 self.next_token()420 return IfStatement(condition=condition, block=block)421 else:422 self.raise_parse_error(msg="there is no closing paren in if statement") 423 else:424 self.raise_parse_error(msg="there is no right paren in if statement after condition")425 else:426 self.raise_parse_error(msg="there is no left paren in if statement before condition")427 else:428 return None429 def get_condition(self):430 # condition as statement431 if self.token_type_is(TokenType.T_LPAREN):432 self.next_token()433 # as fun call434 statement = self.get_fun_call_statement()435 if statement:436 return Condition(condition=statement)437 # as arithmetic438 statement = self.get_arithmetic_statement()439 if statement:440 return Condition(condition=statement)441 # as condition442 statement = self.get_condition_statement()443 if statement:444 return Condition(condition=statement)445 self.raise_parse_error(msg="invalid condition")446 # condition as identifier447 elif self.token_type_is(TokenType.T_IDENTIFIER):448 condition = self.token449 self.next_token()450 return Condition(condition=self.build_identifier_obj(condition))451 # condition as literal452 elif self.token_type_is_literal():453 condition = self.token454 self.next_token()455 return Condition(condition=self.build_literal_obj(condition))456 # condition as binary compare ex. (< 1 2)457 elif self.token_type_is_binary_compare_operator():458 comp_operator = self.token459 self.next_token()460 first_condition_arg = self.get_condition_arg()461 second_condition_arg = self.get_condition_arg()462 return Condition(condition=BinaryComparison(comp_operator=comp_operator.get_value(), first_condition_arg=first_condition_arg, second_condition_arg=second_condition_arg))463 # condition as unary compare ex. (! 1)464 elif self.token_type_is_unary_compare_operator():465 comp_operator = self.token466 self.next_token()467 first_condition_arg = self.get_condition_arg()468 return Condition(condition=UnaryComparison(comp_operator=comp_operator.get_value(), condition_arg=first_condition_arg))469 # or and and ex. or arg1 arg2470 elif self.token_type_is(TokenType.T_OR) or self.token_type_is(TokenType.T_AND):471 log_operator = self.token472 self.next_token()473 first_condition_arg = self.get_condition_arg()474 second_condition_arg = self.get_condition_arg()475 return Condition(condition=LogComparison(log_operator=log_operator.get_value(), first_condition_arg=first_condition_arg, second_condition_arg=second_condition_arg))476 else:477 self.raise_parse_error(msg="unknown condition format")478 def get_condition_arg(self):479 # condition arg as another statement480 if self.token_type_is(TokenType.T_LPAREN):481 self.next_token()482 # as fun call483 statement = self.get_fun_call_statement()484 if statement:485 return ConditionArg(condition_arg=statement)486 # as arithmetic487 statement = self.get_arithmetic_statement()488 if statement:489 return ConditionArg(condition_arg=statement)490 # as condition491 statement = self.get_condition_statement()492 if statement:493 return ConditionArg(condition_arg=statement)494 self.raise_parse_error(msg="invalid condition arg format")495 # as literal496 elif self.token_type_is_literal():497 value = self.token498 self.next_token()499 return ConditionArg(condition_arg=self.build_literal_obj(value))500 # as identifier501 elif self.token_type_is(TokenType.T_IDENTIFIER):502 value = self.token503 self.next_token()504 return ConditionArg(condition_arg=self.build_identifier_obj(value))505 else:506 self.raise_parse_error(msg="invalid condition arg format")507 def token_type_is_number(self):508 # if self.token_type_is(TokenType.T_ROMANCONST):509 # return self.is_correct_roman_number()510 return (511 self.token_type_is(TokenType.T_INTCONST) or512 self.token_type_is(TokenType.T_REALCONST) or513 self.token_type_is(TokenType.T_ROMANCONST)514 )515 516 # def is_correct_roman_number(self):517 # num_string = self.token.get_value()518 # return re.search(r"^(-)?M{0,9}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$", num_string)519 520 def token_type_is_datatype(self):521 return (522 self.token_type_is(TokenType.T_INT) or523 self.token_type_is(TokenType.T_REAL) or 524 self.token_type_is(TokenType.T_ROM) or525 self.token_type_is(TokenType.T_TXT)526 )527 def token_type_is_literal(self):528 return (529 self.token_type_is_number() or530 self.token_type_is(TokenType.T_TXTCONST)531 )532 def token_type_is_binary_compare_operator(self):533 return (534 self.token_type_is(TokenType.T_LESSTHAN) or535 self.token_type_is(TokenType.T_GREATERTHAN) or536 self.token_type_is(TokenType.T_LESSEQUAL) or537 self.token_type_is(TokenType.T_GREATEREQUAL) or538 self.token_type_is(TokenType.T_EQUAL) or539 self.token_type_is(TokenType.T_NOTEQUAL)540 )541 def token_type_is_unary_compare_operator(self):542 return self.token_type_is(TokenType.T_UNEGATION)543 def get_block(self, include_return_statement):544 # start with {545 if self.token_type_is(TokenType.T_LBRACE):546 self.next_token()547 # next try to parse statements untill } symbol548 statements = []549 while(not self.token_type_is(TokenType.T_RBRACE)):550 if self.token_type_is(TokenType.T_LPAREN):551 self.next_token()552 statement = self.get_no_fun_def_statement(include_return_statement)553 # save statement for object building554 if statement:555 statements.append(statement)556 else:557 self.raise_parse_error(msg="invalid statement in block")558 else:559 self.raise_parse_error(msg="there is no open left paren at the statement begining")560 self.next_token()561 return Block(statements=statements)562 else:563 self.raise_parse_error(msg="there is no open brace in block")564 def get_output_statement(self):565 # start with proper keyword566 if self.token_type_is(TokenType.T_WRITE):567 # next output value568 self.next_token()569 # as identifier ot literal570 if (self.token_type_is(TokenType.T_IDENTIFIER)571 or self.token_type_is_literal()):572 output_arg = self.token573 self.next_token()574 if self.token_type_is(TokenType.T_RPAREN):575 self.next_token()576 # build proper objects577 if output_arg.get_type() == TokenType.T_IDENTIFIER:578 return OutputStatement(output_arg=self.build_identifier_obj(output_arg))579 else:580 return OutputStatement(output_arg=self.build_literal_obj(output_arg))581 else: 582 self.raise_parse_error(msg="there is no closing paren in the end of output statement")583 # as another statement584 elif self.token_type_is(TokenType.T_LPAREN):585 self.next_token()586 # as fun call587 statement = self.get_fun_call_statement()588 if statement:589 if self.token_type_is(TokenType.T_RPAREN):590 self.next_token()591 return OutputStatement(output_arg=statement)592 else:593 self.raise_parse_error(msg="there is no closing paren in the end of output statement")594 # as arithmetic595 statement = self.get_arithmetic_statement()596 if statement:597 if self.token_type_is(TokenType.T_RPAREN):598 self.next_token()599 return OutputStatement(output_arg=statement)600 else:601 self.raise_parse_error(msg="there is no closing paren in the end of output statement")602 # as concat603 statement = self.get_concat_statement()604 if statement:605 if self.token_type_is(TokenType.T_RPAREN):606 self.next_token()607 return OutputStatement(output_arg=statement)608 else:609 self.raise_parse_error(msg="there is no closing paren in the end of output statement")610 # as condition611 statement = self.get_condition_statement()612 if statement:613 if self.token_type_is(TokenType.T_RPAREN):614 self.next_token()615 return OutputStatement(output_arg=statement)616 else:617 self.raise_parse_error(msg="there is no closing paren in the end of output statement")618 self.raise_parse_error(msg="invalid arg in output statement")619 else:620 self.raise_parse_error(msg="invalid arg in output statement")621 else: 622 return None623 def build_identifier_obj(self, token):624 if token.get_type() == TokenType.T_IDENTIFIER:625 return Identifier(value=token.get_value())626 return None627 def build_number_obj(self, token):628 if token.get_type() == TokenType.T_INTCONST:629 return IntLiteral(value=token.get_value())630 elif token.get_type() == TokenType.T_REALCONST:631 return RealLiteral(value=token.get_value())632 elif token.get_type() == TokenType.T_ROMANCONST:633 try:634 return RomanLiteral(value=token.get_value())635 except:636 self.raise_parse_error(msg="Invalid rom number")637 return None638 def build_literal_obj(self, token):639 if token.get_type() == TokenType.T_TXTCONST:640 return TextLiteral(value=token.get_value())641 642 return self.build_number_obj(token)643 644 def build_void_obj(self, token):645 if token.get_type() == TokenType.T_NIL:646 return Void(value=None)647 return None648 649 def get_input_statement(self):650 # start with proper keyword651 if self.token_type_is(TokenType.T_READ):652 self.next_token()653 # next identifier of var where to read654 if self.token_type_is(TokenType.T_IDENTIFIER):655 input_arg = self.token656 self.next_token()657 # should end with )658 if self.token_type_is(TokenType.T_RPAREN):659 self.next_token()660 return InputStatement(input_arg=input_arg.get_value())661 else:662 self.raise_parse_error(msg="there is no closing paren in the end of input statement")663 else:664 self.raise_parse_error(msg="there is no identifier in input statement")665 else:666 return None667 def get_communicate_statement(self):668 # try to parse input statement669 statement = self.get_input_statement()670 if statement:671 return statement672 # try to parse output statement673 statement = self.get_output_statement()674 if statement:675 return statement676 return None677 def get_var_create_statement(self):678 # start with datatype679 if self.token_type_is_datatype():680 var_type = self.token681 self.next_token()682 # next identifier of new creating var683 if self.token_type_is(TokenType.T_IDENTIFIER):684 var_identifier = self.token 685 self.next_token()686 # should end with )687 if self.token_type_is(TokenType.T_RPAREN):688 self.next_token()689 return VarCreateStatement(var_type=var_type.get_type(), var_identifier=var_identifier.get_value())690 else:691 self.raise_parse_error(msg="There is no closing paren in var creae statement")692 else:693 self.raise_parse_error(msg="there is no identificaor in var create statement")694 else:695 return None696 def get_var_assignment_statement(self):697 # start with =698 if self.token_type_is(TokenType.T_ASSIGNMENT):699 self.next_token()700 # next identifier of var where to write701 if self.token_type_is(TokenType.T_IDENTIFIER):702 # next value to write703 var_identifier = self.token704 self.next_token()705 # as literal or identifier706 if self.token_type_is_literal() or self.token_type_is(TokenType.T_IDENTIFIER):707 var_value = self.token708 self.next_token()709 if self.token_type_is(TokenType.T_RPAREN):710 self.next_token()711 if var_value.token_type == TokenType.T_IDENTIFIER:712 return AssignmentStatement(var_identifier=var_identifier.get_value(), var_value=self.build_identifier_obj(var_value))713 else:714 return AssignmentStatement(var_identifier=var_identifier.get_value(), var_value=self.build_literal_obj(var_value))715 else:716 self.raise_parse_error(msg="there is no closing paren in the end of assignment statement")717 # as literal or identifier718 if self.token_type_is(TokenType.T_LPAREN):719 self.next_token()720 statement = self.get_fun_call_statement()721 # as fun call722 if statement:723 if self.token_type_is(TokenType.T_RPAREN):724 self.next_token()725 return AssignmentStatement(var_identifier=var_identifier.get_value(), var_value=statement)726 else:727 self.raise_parse_error(msg="there is no closing paren in the end of assignment statement")728 # as arithmetic729 statement = self.get_arithmetic_statement()730 if statement:731 if self.token_type_is(TokenType.T_RPAREN):732 self.next_token()733 return AssignmentStatement(var_identifier=var_identifier.get_value(), var_value=statement)734 else:735 self.raise_parse_error(msg="there is no closing paren in the end of assignment statement")736 # as condition737 statement = self.get_condition_statement()738 if statement:739 if self.token_type_is(TokenType.T_RPAREN):740 self.next_token()741 return AssignmentStatement(var_identifier=var_identifier.get_value(), var_value=statement)742 else:743 self.raise_parse_error(msg="there is no closing paren in the end of assignment statement")744 # as concat745 statement = self.get_concat_statement()746 if statement:747 if self.token_type_is(TokenType.T_RPAREN):748 self.next_token()749 return AssignmentStatement(var_identifier=var_identifier.get_value(), var_value=statement)750 else:751 self.raise_parse_error(msg="there is no closing paren in the end of assignment statement")752 self.raise_parse_error(msg="invalid 2nd argument in assignment statement")753 else:754 self.raise_parse_error(msg="thre is no identifier afer = in assignment satement")755 else:756 return None757 def get_fun_call_statement(self):758 # start with proper keyword759 if self.token_type_is(TokenType.T_IDENTIFIER):760 # next identifier of funcall to call761 fun_identifier = self.token762 self.next_token()763 # read args for run calling764 fun_args = []765 # end with )766 while (not self.token_type_is(TokenType.T_RPAREN)):767 fun_args.append(self.get_arg_for_fun_calling())768 self.next_token()769 return FunCallStatement(fun_identifier=fun_identifier.get_value(), fun_args=fun_args)770 else:771 return None772 def get_arg_for_fun_calling(self):773 # arg to pass when fun calling774 # as literal775 if self.token_type_is_literal():776 value = self.token777 self.next_token()778 return FunCallArg(fun_call_arg=self.build_literal_obj(value))779 # as identifier780 elif self.token_type_is(TokenType.T_IDENTIFIER):781 value = self.token782 self.next_token()783 return FunCallArg(fun_call_arg=self.build_identifier_obj(value))784 # as another statement785 elif self.token_type_is(TokenType.T_LPAREN):786 self.next_token()787 # as fun call788 statement = self.get_fun_call_statement()789 if statement:790 return FunCallArg(fun_call_arg=statement)791 # as arithmetic792 statement = self.get_arithmetic_statement()793 if statement:794 return FunCallArg(fun_call_arg=statement)795 # as concat796 statement = self.get_concat_statement()797 if statement:798 return FunCallArg(fun_call_arg=statement)799 # as condition800 statement = self.get_condition_statement()801 if statement:802 return FunCallArg(fun_call_arg=statement)803 self.raise_parse_error(msg="invalid argument for fun call")804 else:805 self.raise_parse_error(msg="invalid argument for fun call")806 def get_arithmetic_statement(self):807 # start with arithmetic operator + - * /808 if self.token_type_is_arithm_op():809 action_token = self.token810 self.next_token()811 # first arithmetic arg812 first_arithm_arg = self.get_arithmetic_arg()813 # second arithmetic arg814 second_arithm_arg = self.get_arithmetic_arg()815 # should end with )816 if self.token_type_is(TokenType.T_RPAREN): 817 self.next_token()818 return ArithmeticStatement(operator=action_token.get_value(), first_arithm_arg=first_arithm_arg, second_arithm_arg=second_arithm_arg)819 else:820 self.raise_parse_error(msg="there is no closing paren in the end of arithmetic statement")821 else:822 return None823 def get_arithmetic_arg(self):824 # arithmetic argument825 # as number826 if self.token_type_is_number():827 value = self.token828 self.next_token()829 return ArithmeticArg(arithmetic_arg=self.build_number_obj(value))830 # as identifier831 if self.token_type_is(TokenType.T_IDENTIFIER):832 value = self.token833 self.next_token()834 return ArithmeticArg(arithmetic_arg=self.build_identifier_obj(value))835 836 # as another statement837 if self.token_type_is(TokenType.T_LPAREN):838 self.next_token()839 # as fun call840 statement = self.get_fun_call_statement()841 if statement:842 return ArithmeticArg(arithmetic_arg=statement)843 # as arithmetic844 statement = self.get_arithmetic_statement()845 if statement:846 return ArithmeticArg(arithmetic_arg=statement)847 # as condition848 statement = self.get_condition_statement()849 if statement:850 return ArithmeticArg(arithmetic_arg=statement)851 self.raise_parse_error(msg="invalid arithmetic statement argument")852 else:853 self.raise_parse_error(msg="invalid arithmetic argument")854 def token_type_is_arithm_op(self):855 return (856 self.token_type_is(TokenType.T_PLUS) or857 self.token_type_is(TokenType.T_MINUS) or858 self.token_type_is(TokenType.T_MULTIPLY) or859 self.token_type_is(TokenType.T_DIVIDE)860 )861 862 def token_type_is(self, token_type):863 return self.token.get_type() == token_type864 865 def next_token(self):866 self.token = self.lexer.get_next_token()867 if self.token_type_is(TokenType.T_ERROR):868 raise LexerError(msg=self.token.error.error_msg , row=self.token.error.pos_row, col=self.token.error.pos_col, code=self.token.error.error_code)869 def raise_parse_error(self, msg, code=None):...
template.py
Source:template.py
...625 else:626 return self.text[self.pos + key]627 def __str__(self):628 return self.text[self.pos:]629 def raise_parse_error(self, msg):630 raise ParseError(msg, self.name, self.line)631def _format_code(code):632 lines = code.splitlines()633 format = "%%%dd %%s\n" % len(repr(len(lines) + 1))634 return "".join([format % (i + 1, line) for (i, line) in enumerate(lines)])635def _parse(reader, template, in_block=None, in_loop=None):636 body = _ChunkList([])637 while True:638 # Find next template directive639 curly = 0640 while True:641 curly = reader.find("{", curly)642 if curly == -1 or curly + 1 == reader.remaining():643 # EOF644 if in_block:645 reader.raise_parse_error(646 "Missing {%% end %%} block for %s" % in_block)647 body.chunks.append(_Text(reader.consume(), reader.line,648 reader.whitespace))649 return body650 # If the first curly brace is not the start of a special token,651 # start searching from the character after it652 if reader[curly + 1] not in ("{", "%", "#"):653 curly += 1654 continue655 # When there are more than 2 curlies in a row, use the656 # innermost ones. This is useful when generating languages657 # like latex where curlies are also meaningful658 if (curly + 2 < reader.remaining() and659 reader[curly + 1] == '{' and reader[curly + 2] == '{'):660 curly += 1661 continue662 break663 # Append any text before the special token664 if curly > 0:665 cons = reader.consume(curly)666 body.chunks.append(_Text(cons, reader.line,667 reader.whitespace))668 start_brace = reader.consume(2)669 line = reader.line670 # Template directives may be escaped as "{{!" or "{%!".671 # In this case output the braces and consume the "!".672 # This is especially useful in conjunction with jquery templates,673 # which also use double braces.674 if reader.remaining() and reader[0] == "!":675 reader.consume(1)676 body.chunks.append(_Text(start_brace, line,677 reader.whitespace))678 continue679 # Comment680 if start_brace == "{#":681 end = reader.find("#}")682 if end == -1:683 reader.raise_parse_error("Missing end comment #}")684 contents = reader.consume(end).strip()685 reader.consume(2)686 continue687 # Expression688 if start_brace == "{{":689 end = reader.find("}}")690 if end == -1:691 reader.raise_parse_error("Missing end expression }}")692 contents = reader.consume(end).strip()693 reader.consume(2)694 if not contents:695 reader.raise_parse_error("Empty expression")696 body.chunks.append(_Expression(contents, line))697 continue698 # Block699 assert start_brace == "{%", start_brace700 end = reader.find("%}")701 if end == -1:702 reader.raise_parse_error("Missing end block %}")703 contents = reader.consume(end).strip()704 reader.consume(2)705 if not contents:706 reader.raise_parse_error("Empty block tag ({% %})")707 operator, space, suffix = contents.partition(" ")708 suffix = suffix.strip()709 # Intermediate ("else", "elif", etc) blocks710 intermediate_blocks = {711 "else": set(["if", "for", "while", "try"]),712 "elif": set(["if"]),713 "except": set(["try"]),714 "finally": set(["try"]),715 }716 allowed_parents = intermediate_blocks.get(operator)717 if allowed_parents is not None:718 if not in_block:719 reader.raise_parse_error("%s outside %s block" %720 (operator, allowed_parents))721 if in_block not in allowed_parents:722 reader.raise_parse_error(723 "%s block cannot be attached to %s block" %724 (operator, in_block))725 body.chunks.append(_IntermediateControlBlock(contents, line))726 continue727 # End tag728 elif operator == "end":729 if not in_block:730 reader.raise_parse_error("Extra {% end %} block")731 return body732 elif operator in ("extends", "include", "set", "import", "from",733 "comment", "autoescape", "whitespace", "raw",734 "module"):735 if operator == "comment":736 continue737 if operator == "extends":738 suffix = suffix.strip('"').strip("'")739 if not suffix:740 reader.raise_parse_error("extends missing file path")741 block = _ExtendsBlock(suffix)742 elif operator in ("import", "from"):743 if not suffix:744 reader.raise_parse_error("import missing statement")745 block = _Statement(contents, line)746 elif operator == "include":747 suffix = suffix.strip('"').strip("'")748 if not suffix:749 reader.raise_parse_error("include missing file path")750 block = _IncludeBlock(suffix, reader, line)751 elif operator == "set":752 if not suffix:753 reader.raise_parse_error("set missing statement")754 block = _Statement(suffix, line)755 elif operator == "autoescape":756 fn = suffix.strip()757 if fn == "None":758 fn = None759 template.autoescape = fn760 continue761 elif operator == "whitespace":762 mode = suffix.strip()763 # Validate the selected mode764 filter_whitespace(mode, '')765 reader.whitespace = mode766 continue767 elif operator == "raw":768 block = _Expression(suffix, line, raw=True)769 elif operator == "module":770 block = _Module(suffix, line)771 body.chunks.append(block)772 continue773 elif operator in ("apply", "block", "try", "if", "for", "while"):774 # parse inner body recursively775 if operator in ("for", "while"):776 block_body = _parse(reader, template, operator, operator)777 elif operator == "apply":778 # apply creates a nested function so syntactically it's not779 # in the loop.780 block_body = _parse(reader, template, operator, None)781 else:782 block_body = _parse(reader, template, operator, in_loop)783 if operator == "apply":784 if not suffix:785 reader.raise_parse_error("apply missing method name")786 block = _ApplyBlock(suffix, line, block_body)787 elif operator == "block":788 if not suffix:789 reader.raise_parse_error("block missing name")790 block = _NamedBlock(suffix, block_body, template, line)791 else:792 block = _ControlBlock(contents, line, block_body)793 body.chunks.append(block)794 continue795 elif operator in ("break", "continue"):796 if not in_loop:797 reader.raise_parse_error("%s outside %s block" %798 (operator, set(["for", "while"])))799 body.chunks.append(_Statement(contents, line))800 continue801 else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!