def parse_if_expr(self): expr = ast.IfExpression(self.cur_tok, None, None, None) if not self.expect(token.LPAREN): return None self.next() expr.condition = self.parse_expr(LOWEST) if not self.expect(token.RPAREN): return None if not self.expect(token.LBRACE): return None expr.consequence = self.parse_block_statement() if self.peek_is(token.ELSE): self.next() if not self.expect(token.LBRACE): return None expr.alternative = self.parse_block_statement() elif self.peek_is(token.ELIF): self.next() expr.alternative = ast.BlockStatement( self.cur_tok, [ast.ExpressionStatement(self.cur_tok, self.parse_if_expr())]) return expr
def _parse_block_statement(self) -> ast.BlockStatement: token = self._current_token statements = [] self._next_token() while not self._current_token_is(TokenType.RBRACE): stmt = self._parse_statement() if stmt is not None: statements.append(stmt) self._next_token() return ast.BlockStatement(token, statements)
def parseBlockStatement(self): block = ast.BlockStatement(Token=self.curToken, Statements= None) Statements = [] self.nextToken() while not self.curTokenIs(tokens.RBRACE) and not self.curTokenIs(tokens.EOF): stmt = self.parseStatement() if stmt is not None: Statements.append(stmt) self.nextToken() block.Statements = Statements return block
def parseBlockStatement(self): block = ast.BlockStatement() block.Statements = [] self.nextToken() while not self.curTokenIs(token.RBRACE) and not self.curTokenIs(token.EOF): stmt = self.parseStatement() if stmt != None: block.Statements.append(stmt) self.nextToken() return block
def parse_block_statement(self): block = ast.BlockStatement(self.cur_tok, []) self.next() while not self.cur_is(token.RBRACE) and not self.cur_is(token.EOF): stmt = self.parse_stmt() if stmt != None: block.statements.append(stmt) self.next() return block
def block(self): """ Block ⇒ { Statements } """ # Check for '{'. if self.currToken[0] == lexer.Lexer.CURLYLEFT: # Move to the next token. self.currToken = next(self.tokens) # Look for statements to store in the BlockStatement object. The first token in a statement can be ';', '{', # an identifier, 'if', 'while' or 'print'. # Create a list to store the found statements. stateList = [] while self.currToken[0] in (lexer.Lexer.SEMICOLON, lexer.Lexer.CURLYLEFT, lexer.Lexer.ID, lexer.Lexer.KWDIF, lexer.Lexer.KWDWHILE, lexer.Lexer.KWDPRINT): # Create a Statement object (either a SemiStatement, BlockStatement, Assignment, IfWhileStatement or # PrintStatement object). aStatement = self.statement() # A statement was successfully created. Add it to the list of statements. stateList.append(aStatement) # Check for '}'. if self.currToken[0] == lexer.Lexer.CURLYRIGHT: # Move to the next token. self.currToken = next(self.tokens) # Create a BlockStatement object. aBlock = ast.BlockStatement(stateList) # Return the statement. return aBlock # Unexpected token encountered. else: raise CLiteSyntaxError(self.currToken, '}') # Unexpected token encountered. else: raise CLiteSyntaxError(self.currToken, '{')