def parse(self, tokens): from pas_parser_utils import parse_statement logger.debug("Parsing case statement") tokens.match_token(TokenKind.Identifier, 'case') self._expression = PascalExpression(self._block) self._expression.parse(tokens) # expression will consume the 'of' keyword while not (tokens.match_lookahead(TokenKind.Identifier, 'end') or tokens.match_lookahead(TokenKind.Identifier, 'else') or tokens.match_lookahead(TokenKind.Identifier, 'otherwise')): constant = None if (tokens.match_lookahead(TokenKind.Number)): constant = tokens.match_token(TokenKind.Number).value elif (tokens.match_lookahead(TokenKind.String)): constant = tokens.match_token(TokenKind.String.value) if len(constant) > 1: raise_error(("Constant expected: %s" %tokens.next_token()), '', is_critical=False) elif tokens.match_lookahead(TokenKind.Identifier): constant = self._block.resolve_variable(tokens.match_token(TokenKind.Identifier).value) else: raise_error(("CaseStatement: Ordinal type expected: %s", tokens.next_token()), '', is_critical=False) tokens.match_token(TokenKind.Symbol, ':') statement = parse_statement(tokens, self._block) tokens.match_lookahead(TokenKind.Symbol, ';', consume=True) self._case.append(tuple((constant, statement))) if tokens.match_lookahead(TokenKind.Identifier, 'else', consume=True) or tokens.match_lookahead(TokenKind.Identifier, 'otherwise', consume=True): while not (tokens.match_lookahead(TokenKind.Identifier, 'end', consume=True)): self._else_statements.append(parse_statement(tokens, self._block)) else: tokens.match_token(TokenKind.Identifier, 'end') tokens.match_token(TokenKind.Symbol, ';') logger.debug("Finished parsing if statement")
def parse(self, tokens): from pas_parser_utils import parse_statement logger.debug("Parsing if statement") tokens.match_token(TokenKind.Identifier, 'if') self._expression = PascalExpression(self._block) self._expression.parse(tokens) self._statement = parse_statement(tokens, self._block) # expression will consume the 'then' delimiter if tokens.match_lookahead(TokenKind.Identifier, 'else'): tokens.match_token(TokenKind.Identifier, 'else') self._else_statement = parse_statement(tokens, self._block) tokens.match_lookahead(TokenKind.Symbol, ';', consume=True) logger.debug("Finished parsing if statement")
def parse(self, tokens): from pas_parser_utils import parse_statement logger.debug("Parsing case statement") tokens.match_token(TokenKind.Identifier, 'case') self._expression = PascalExpression(self._block) self._expression.parse(tokens) # expression will consume the 'of' keyword while not (tokens.match_lookahead(TokenKind.Identifier, 'end') or tokens.match_lookahead(TokenKind.Identifier, 'else') or tokens.match_lookahead(TokenKind.Identifier, 'otherwise')): constant = None if (tokens.match_lookahead(TokenKind.Number)): constant = tokens.match_token(TokenKind.Number).value elif (tokens.match_lookahead(TokenKind.String)): constant = tokens.match_token(TokenKind.String.value) if len(constant) > 1: raise_error( ("Constant expected: %s" % tokens.next_token()), '', is_critical=False) elif tokens.match_lookahead(TokenKind.Identifier): constant = self._block.resolve_variable( tokens.match_token(TokenKind.Identifier).value) else: raise_error(("CaseStatement: Ordinal type expected: %s", tokens.next_token()), '', is_critical=False) tokens.match_token(TokenKind.Symbol, ':') statement = parse_statement(tokens, self._block) tokens.match_lookahead(TokenKind.Symbol, ';', consume=True) self._case.append(tuple((constant, statement))) if tokens.match_lookahead( TokenKind.Identifier, 'else', consume=True) or tokens.match_lookahead( TokenKind.Identifier, 'otherwise', consume=True): while not (tokens.match_lookahead( TokenKind.Identifier, 'end', consume=True)): self._else_statements.append( parse_statement(tokens, self._block)) else: tokens.match_token(TokenKind.Identifier, 'end') tokens.match_token(TokenKind.Symbol, ';') logger.debug("Finished parsing if statement")
def parse(self, tokens): from pas_parser_utils import parse_statement logger.debug("Parsing while statement") tokens.match_token(TokenKind.Identifier, 'while') self._expression = PascalExpression(self._block) self._expression.parse(tokens) self._statement = parse_statement(tokens, self._block) # expression will consume the 'then' delimiter logger.debug("Finished parsing while statement")
def parse(self, tokens): from pas_parser_utils import parse_statement logger.debug("Parsing repeat statement") tokens.match_token(TokenKind.Identifier, 'repeat') while not tokens.match_lookahead(TokenKind.Identifier, 'until'): self._statements.append(parse_statement(tokens, self._block)) tokens.match_token(TokenKind.Identifier, 'until') self._expression = PascalExpression(self._block) self._expression.parse(tokens) # expression will consume the 'then' delimiter logger.debug("Finished parsing repeat statement")
def parse(self, tokens): logger.debug("Parsing compound statement") if tokens.match_lookahead(TokenKind.Identifier, 'begin'): tokens.match_token(TokenKind.Identifier, 'begin') while (True): # compound statement currently consumes the end keyword, but not the symbol ';' or '.' if tokens.match_lookahead(TokenKind.Identifier, 'end'): tokens.match_token(TokenKind.Identifier, 'end') # consume end token break elif tokens.match_lookahead(TokenKind.Symbol, ';') or tokens.match_lookahead(TokenKind.Symbol, '.'): # consumed end already -> somehow? tokens.match_token(TokenKind.Symbol) break elif tokens.match_lookahead(TokenKind.Identifier, 'until'): # repeat..until needs to have the until token so that it knows it's at the end break else: self._statements.append(parse_statement(tokens, self._block)) logger.debug("Finished parsing compound statement")
def parse(self, tokens): logger.debug("Parsing compound statement") if tokens.match_lookahead(TokenKind.Identifier, 'begin'): tokens.match_token(TokenKind.Identifier, 'begin') while (True): # compound statement currently consumes the end keyword, but not the symbol ';' or '.' if tokens.match_lookahead(TokenKind.Identifier, 'end'): tokens.match_token(TokenKind.Identifier, 'end') # consume end token break elif tokens.match_lookahead(TokenKind.Symbol, ';') or tokens.match_lookahead( TokenKind.Symbol, '.'): # consumed end already -> somehow? tokens.match_token(TokenKind.Symbol) break elif tokens.match_lookahead(TokenKind.Identifier, 'until'): # repeat..until needs to have the until token so that it knows it's at the end break else: self._statements.append(parse_statement(tokens, self._block)) logger.debug("Finished parsing compound statement")