Example #1
0
    def primary(self):
        if self.curr_token_is([TokenType.OPEN_PAREN]):
            self.pop_token()
            paren_expr = self.expression()
            self.pop_token_expect([TokenType.CLOSE_PAREN],
                                  'PARSER ERROR: Expecting right parenthesis')
            return Expression.Grouping(paren_expr)

        primary_tokens = [
            TokenType.NUMBER, TokenType.STRING, TokenType.FALSE,
            TokenType.TRUE, TokenType.NIL, TokenType.IDENTIFIER
        ]
        curr_token = self.pop_token()
        if curr_token.token_type not in primary_tokens:
            error_msg = 'PARSER ERROR: Expecting one of: {0}, but got {1}'.format(
                primary_tokens, curr_token.token_type)
            self.lox_instance.raise_error_with_token(curr_token, error_msg)
            raise ParseError()

        if curr_token.token_type == TokenType.IDENTIFIER:
            return Expression.Variable(curr_token)

        primary_token_map = {
            TokenType.NUMBER: curr_token.literal,
            TokenType.STRING: curr_token.literal,
            TokenType.FALSE: False,
            TokenType.TRUE: True,
            TokenType.NIL: None
        }
        return Expression.Literal(primary_token_map[curr_token.token_type])
Example #2
0
def convert_not_literal(group_names, name, val):
    return Expression.Literal(chr(val), 1)
Example #3
0
def Any(s):
    """(s) -> match any character in s"""
    if len(s) == 1:
        return Expression.Literal(s)
    return Expression.Any(s, 0)