Skip to content

Commit

Permalink
Pass current line number to expression parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp committed Dec 25, 2024
1 parent b01e86e commit bcd515f
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 31 deletions.
4 changes: 3 additions & 1 deletion liquid/builtin/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ class Statement(Tag):
def parse(self, stream: TokenStream) -> StatementNode:
tok = stream.current
expect(stream, TOKEN_STATEMENT)
return self.node_class(tok, self.env.parse_filtered_expression_value(tok.value))
return self.node_class(
tok, self.env.parse_filtered_expression_value(tok.value, tok.linenum)
)
10 changes: 7 additions & 3 deletions liquid/builtin/tags/assign_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class AssignTag(Tag):
block = False
node_class = AssignNode

def _parse_expression(self, value: str) -> Expression:
return self.env.parse_filtered_expression_value(value)
def _parse_expression(self, value: str, linenum: int) -> Expression:
return self.env.parse_filtered_expression_value(value, linenum)

def parse(self, stream: TokenStream) -> AssignNode:
expect(stream, TOKEN_TAG, value=TAG_ASSIGN)
Expand All @@ -83,5 +83,9 @@ def parse(self, stream: TokenStream) -> AssignNode:
)

return self.node_class(
tok, AssignmentExpression(name, self._parse_expression(right))
tok,
AssignmentExpression(
name,
self._parse_expression(right, stream.current.linenum),
),
)
6 changes: 3 additions & 3 deletions liquid/builtin/tags/echo_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class EchoTag(Tag):
block = False
node_class = EchoNode

def _parse_expression(self, value: str) -> Expression:
return self.env.parse_filtered_expression_value(value)
def _parse_expression(self, value: str, linenum: int) -> Expression:
return self.env.parse_filtered_expression_value(value, linenum)

def parse(self, stream: TokenStream) -> Node: # noqa: D102
expect(stream, TOKEN_TAG, value=TAG_ECHO)
Expand All @@ -42,5 +42,5 @@ def parse(self, stream: TokenStream) -> Node: # noqa: D102
expr: Expression = NIL
else:
expect(stream, TOKEN_EXPRESSION)
expr = self._parse_expression(stream.current.value)
expr = self._parse_expression(stream.current.value, tok.linenum)
return self.node_class(tok, expression=expr)
5 changes: 4 additions & 1 deletion liquid/builtin/tags/for_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ def parse(self, stream: TokenStream) -> Node:
stream.next_token()

expect(stream, TOKEN_EXPRESSION)
expr = self.env.parse_loop_expression_value(stream.current.value)
expr = self.env.parse_loop_expression_value(
stream.current.value,
stream.current.linenum,
)
stream.next_token()

block = parser.parse_block(stream, ENDFORBLOCK)
Expand Down
5 changes: 4 additions & 1 deletion liquid/builtin/tags/if_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ def __init__(self, env: Environment):
def parse_expression(self, stream: TokenStream) -> Expression:
"""Pare a boolean expression from a stream of tokens."""
expect(stream, TOKEN_EXPRESSION)
return self.env.parse_boolean_expression_value(stream.current.value)
return self.env.parse_boolean_expression_value(
stream.current.value,
stream.current.linenum,
)

def parse(self, stream: TokenStream) -> Node:
expect(stream, TOKEN_TAG, value=TAG_IF)
Expand Down
4 changes: 3 additions & 1 deletion liquid/builtin/tags/tablerow_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ def parse(self, stream: TokenStream) -> TablerowNode:
stream.next_token()

expect(stream, TOKEN_EXPRESSION)
loop_expression = self.env.parse_loop_expression_value(stream.current.value)
loop_expression = self.env.parse_loop_expression_value(
stream.current.value, stream.current.linenum
)
stream.next_token()

block = parser.parse_block(stream, END_TAGBLOCK)
Expand Down
4 changes: 3 additions & 1 deletion liquid/builtin/tags/unless_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def __init__(self, env: Environment):
def parse_expression(self, stream: TokenStream) -> Expression:
"""Parse a boolean expression from a stream of tokens."""
expect(stream, TOKEN_EXPRESSION)
return self.env.parse_boolean_expression_value(stream.current.value)
return self.env.parse_boolean_expression_value(
stream.current.value, stream.current.linenum
)

def parse(self, stream: TokenStream) -> Union[UnlessNode, IllegalNode]:
expect(stream, TOKEN_TAG, value=TAG_UNLESS)
Expand Down
15 changes: 8 additions & 7 deletions liquid/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ async def analyze_tags_async(
)

def make_globals(
self, globals: Optional[Mapping[str, object]] = None # noqa: A002
self,
globals: Optional[Mapping[str, object]] = None, # noqa: A002
) -> Dict[str, object]:
"""Combine environment globals with template globals."""
if globals:
Expand Down Expand Up @@ -577,12 +578,12 @@ def set_expression_cache_size(self, maxsize: int = 0) -> None:
def _get_expression_parsers(
self, cache_size: int = 0
) -> Tuple[
Callable[[str], "BooleanExpression"],
Callable[[str], "BooleanExpression"],
Callable[[str], "FilteredExpression"],
Callable[[str], "FilteredExpression"],
Callable[[str], "FilteredExpression"],
Callable[[str], "LoopExpression"],
Callable[[str, int], "BooleanExpression"],
Callable[[str, int], "BooleanExpression"],
Callable[[str, int], "FilteredExpression"],
Callable[[str, int], "FilteredExpression"],
Callable[[str, int], "FilteredExpression"],
Callable[[str, int], "LoopExpression"],
]:
if cache_size >= 1:
return (
Expand Down
23 changes: 13 additions & 10 deletions liquid/extra/tags/if_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def parse(self, stream: TokenStream) -> StatementNode:
tok = stream.current
expect(stream, TOKEN_STATEMENT)
return StatementNode(
tok, self.env.parse_conditional_expression_value(tok.value)
tok, self.env.parse_conditional_expression_value(tok.value, tok.linenum)
)


Expand All @@ -35,17 +35,17 @@ class InlineIfAssignTag(AssignTag):
inline `if` expressions.
"""

def _parse_expression(self, value: str) -> Expression:
return self.env.parse_conditional_expression_value(value)
def _parse_expression(self, value: str, linenum: int) -> Expression:
return self.env.parse_conditional_expression_value(value, linenum)


class InlineIfEchoTag(EchoTag):
"""A drop-in replacement for the standard `echo` tag that supports
inline `if` expressions.
"""

def _parse_expression(self, value: str) -> Expression:
return self.env.parse_conditional_expression_value(value)
def _parse_expression(self, value: str, linenum: int) -> Expression:
return self.env.parse_conditional_expression_value(value, linenum)


class InlineIfStatementWithParens(Statement):
Expand All @@ -58,7 +58,10 @@ def parse(self, stream: TokenStream) -> StatementNode:
tok = stream.current
expect(stream, TOKEN_STATEMENT)
return StatementNode(
tok, self.env.parse_conditional_expression_value_with_parens(tok.value)
tok,
self.env.parse_conditional_expression_value_with_parens(
tok.value, tok.linenum
),
)


Expand All @@ -68,8 +71,8 @@ class InlineIfAssignTagWithParens(AssignTag):
terms with parentheses.
"""

def _parse_expression(self, value: str) -> Expression:
return self.env.parse_conditional_expression_value_with_parens(value)
def _parse_expression(self, value: str, linenum: int) -> Expression:
return self.env.parse_conditional_expression_value_with_parens(value, linenum)


class InlineIfEchoTagWithParens(EchoTag):
Expand All @@ -78,5 +81,5 @@ class InlineIfEchoTagWithParens(EchoTag):
terms with parentheses.
"""

def _parse_expression(self, value: str) -> Expression:
return self.env.parse_conditional_expression_value_with_parens(value)
def _parse_expression(self, value: str, linenum: int) -> Expression:
return self.env.parse_conditional_expression_value_with_parens(value, linenum)
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ build-backend = "hatchling.build"
requires = ["hatchling"]

[project]
authors = [{name = "James Prior", email = "[email protected]"}]
authors = [{ name = "James Prior", email = "[email protected]" }]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand All @@ -18,7 +18,11 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = ["python-dateutil>=2.8.1", "typing-extensions>=4.2.0", "importlib-resources>=5.10.0"]
dependencies = [
"python-dateutil>=2.8.1",
"typing-extensions>=4.2.0",
"importlib-resources>=5.10.0",
]
description = "A Python engine for the Liquid template language."
dynamic = ["version"]
license = "MIT"
Expand Down Expand Up @@ -150,7 +154,6 @@ select = [
"SLF",
"T10",
"T20",
"TCH",
"YTT",
]
# TODO: review ignores
Expand Down

0 comments on commit bcd515f

Please sign in to comment.