def test_highlight_partial_three_lines(self): source = 'hello\ngreen\nworld' span = SourceSpan(source, Position(0, 1, 3), Position(len(source) - 3, 3, 4)) result = highlight(span) assert result == '1. hello\n ~~~\n2. green\n ~~~~~\n3. world\n ~~~'
def test_highlight_two_lines(self): source = 'hello\nworld' span = SourceSpan(source, Position.initial(), Position(len(source) - 1, 2, 6)) result = highlight(span) assert result == '1. hello\n ~~~~~\n2. world\n ~~~~~'
def __unary(self) -> Expr: if self.__match(TokenType.BANG, TokenType.MINUS): operator = token_to_unary_operator(self.__previous()) inner_expr = self.__unary() expr = exprs.Unary(operator, inner_expr) expr.span = SourceSpan.from_spans(operator.span, inner_expr.span) return expr return self.__primary()
def test_highlight_single_line(self): source = 'hello world' span = SourceSpan(source, Position.initial(), Position(len(source) - 1, 1, len(source) + 1)) result = highlight(span) assert result == '1. hello world\n ~~~~~~~~~~~'
def test_highlight_partial_single_line(self): source = 'hello world' span = SourceSpan(source, Position(0, 1, 4), Position(len(source) - 3, 1, len(source) - 1)) result = highlight(span) assert result == '1. hello world\n ~~~~~~'
def __multiplication(self) -> Expr: expr = self.__unary() while self.__match(TokenType.SLASH, TokenType.STAR): operator = token_to_binary_operator(self.__previous()) right = self.__unary() new_expr = exprs.Binary(expr, operator, right) new_expr.span = SourceSpan.from_spans(expr.span, right.span) expr = new_expr return expr
def __comparison(self) -> Expr: expr = self.__addition() while self.__match(TokenType.GREATER, TokenType.GREATER_EQUAL, TokenType.LESS, TokenType.LESS_EQUAL): operator = token_to_binary_operator(self.__previous()) right = self.__addition() new_expr = exprs.Binary(expr, operator, right) new_expr.span = SourceSpan.from_spans(expr.span, right.span) expr = new_expr return expr
def __equality(self) -> Expr: expr = self.__comparison() while self.__match(TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL): operator = token_to_binary_operator(self.__previous()) right = self.__comparison() new_expr = exprs.Binary(expr, operator, right) new_expr.span = SourceSpan.from_spans(expr.span, right.span) expr = new_expr return expr
def __primary(self) -> Expr: def literal(value) -> Expr: expr = exprs.Literal(value) expr.span = self.__previous().span return expr if self.__match(TokenType.FALSE): return literal(False) if self.__match(TokenType.TRUE): return literal(True) if self.__match(TokenType.NUMBER, TokenType.STRING): return literal(self.__previous().literal) if self.__match(TokenType.LEFT_PAREN): start = self.__previous() expr = exprs.Grouping(self.expression()) end = self.__consume(TokenType.RIGHT_PAREN, 'Expected \')\' after expression') expr.span = SourceSpan.from_spans(start.span, end.span) return expr raise ParserException(self.__peek(), 'Expected expression')