def statements(self): res = ParseResult() statements = [] pos_start = self.current_tok.pos_start.copy() while self.current_tok.type == token.T_NEWLINE: res.register_advancement() self.advance() statement = res.register(self.statement()) if res.error: return res statements.append(statement) more_statements = True while True: newline_count = 0 while self.current_tok.type == token.T_NEWLINE: res.register_advancement() self.advance() newline_count += 1 if newline_count == 0: more_statements = False if not more_statements: break statement = res.try_register(self.statement()) if not statement: self.reverse(res.to_reverse_count) more_statements = False continue statements.append(statement) return res.success( nodes.ListNode(statements, pos_start, self.current_tok.pos_end.copy()))
def parse(self): if len(self.tokens) == 1 and self.tokens[0].type == token.T_EOF: return ParseResult().success(nodes.ListNode([], self.tokens[0].pos_start, self.tokens[0].pos_end)) res = self.statements() if not res.error and self.current_tok.type != token.T_EOF: return res.failure(errors.InvalidSyntaxError( self.current_tok.pos_start, self.current_tok.pos_end, "Expected '+', '-', '*' or '/'" )) return res
def list_expr(self): res = ParseResult() element_nodes = [] pos_start = self.current_tok.pos_start.copy() if self.current_tok.type != token.T_LSQUARE: return res.failure( errors.InvalidSyntaxError(self.current_tok.pos_start, self.current_tok.pos_end, f"Expected '['")) res.register_advancement() self.advance() if self.current_tok.type == token.T_RSQUARE: res.register_advancement() self.advance() else: element_nodes.append(res.register(self.expr())) if res.error: return res.failure( errors.InvalidSyntaxError( self.current_tok.pos_start, self.current_tok.pos_end, "Expected ']', 'var', 'if', 'for', 'while', 'func', int, float, identifier, '+', '-', '(', '[' or 'not'" )) while self.current_tok.type == token.T_COMMA: res.register_advancement() self.advance() element_nodes.append(res.register(self.expr())) if res.error: return res if self.current_tok.type != token.T_RSQUARE: return res.failure( errors.InvalidSyntaxError(self.current_tok.pos_start, self.current_tok.pos_end, "Expected ',' or ']'")) res.register_advancement() self.advance() return res.success( nodes.ListNode(element_nodes, pos_start, self.current_tok.pos_end.copy()))