def parse_compound_statement(index): """Parse a compound statement. A compound statement is a collection of several statements/declarations, enclosed in braces. """ index = match_token(index, token_kinds.open_brack, ParserError.GOT) # Read block items (statements/declarations) until there are no more. items = [] while True: try: item, index = parse_statement(index) items.append(item) continue except ParserError as e: log_error(e) try: item, index = parse_declaration(index) items.append(item) continue except ParserError as e: log_error(e) # When both of our parsing attempts fail, break out of the loop break index = match_token(index, token_kinds.close_brack, ParserError.GOT) return nodes.Compound(items), index
def parse_compound_statement(index): """Parse a compound statement. A compound statement is a collection of several statements/declarations, enclosed in braces. """ p.symbols.new_scope() index = match_token(index, token_kinds.open_brack, ParserError.GOT) # Read block items (statements/declarations) until there are no more. items = [] while True: with log_error(): item, index = parse_statement(index) items.append(item) continue with log_error(): item, index = parse_declaration(index) items.append(item) continue break index = match_token(index, token_kinds.close_brack, ParserError.GOT) p.symbols.end_scope() return nodes.Compound(items), index
def parse_root(index): """Parse the given tokens into an AST.""" items = [] while True: try: item, index = parse_main(index) items.append(item) except ParserError as e: log_error(e) else: continue try: item, index = parse_declaration(index) items.append(item) except ParserError as e: log_error(e) else: continue # If neither parse attempt above worked, break break # If there are tokens that remain unparsed, complain if not p.tokens[index:]: return nodes.Root(items), index else: raise_error("unexpected token", index, ParserError.AT)
def _get_first_for_clause(index): """Get the first clause of a for-statement. index - Index of the beginning of the first clause in the for-statement. returns - Tuple. First element is a node if a clause is found and None if there is no clause (i.e. semicolon terminating the clause). Second element is an integer index where the next token begins. If malformed, raises exception. """ if token_is(index, token_kinds.semicolon): return None, index + 1 with log_error(): return parse_declaration(index) clause, index = parse_expression(index) index = match_token(index, token_kinds.semicolon, ParserError.AFTER) return clause, index
def parse_root(index): """Parse the given tokens into an AST.""" items = [] while True: with log_error(): item, index = parse_func_definition(index) items.append(item) continue with log_error(): item, index = parse_declaration(index) items.append(item) continue # If neither parse attempt above worked, break break # If there are tokens that remain unparsed, complain if not p.tokens[index:]: return nodes.Root(items), index else: raise_error("unexpected token", index, ParserError.AT)