MCPcopy
hub / github.com/pallets/jinja / Parser

Class Parser

src/jinja2/parser.py:48–1049  ·  view source on GitHub ↗

This is the central parsing class Jinja uses. It's passed to extensions and can be used to parse expressions or statements.

Source from the content-addressed store, hash-verified

46
47
48class Parser:
49 """This is the central parsing class Jinja uses. It's passed to
50 extensions and can be used to parse expressions or statements.
51 """
52
53 def __init__(
54 self,
55 environment: "Environment",
56 source: str,
57 name: t.Optional[str] = None,
58 filename: t.Optional[str] = None,
59 state: t.Optional[str] = None,
60 ) -> None:
61 self.environment = environment
62 self.stream = environment._tokenize(source, name, filename, state)
63 self.name = name
64 self.filename = filename
65 self.closed = False
66 self.extensions: t.Dict[
67 str, t.Callable[[Parser], t.Union[nodes.Node, t.List[nodes.Node]]]
68 ] = {}
69 for extension in environment.iter_extensions():
70 for tag in extension.tags:
71 self.extensions[tag] = extension.parse
72 self._last_identifier = 0
73 self._tag_stack: t.List[str] = []
74 self._end_token_stack: t.List[t.Tuple[str, ...]] = []
75
76 def fail(
77 self,
78 msg: str,
79 lineno: t.Optional[int] = None,
80 exc: t.Type[TemplateSyntaxError] = TemplateSyntaxError,
81 ) -> "te.NoReturn":
82 """Convenience method that raises `exc` with the message, passed
83 line number or last line number as well as the current name and
84 filename.
85 """
86 if lineno is None:
87 lineno = self.stream.current.lineno
88 raise exc(msg, lineno, self.name, self.filename)
89
90 def _fail_ut_eof(
91 self,
92 name: t.Optional[str],
93 end_token_stack: t.List[t.Tuple[str, ...]],
94 lineno: t.Optional[int],
95 ) -> "te.NoReturn":
96 expected: t.Set[str] = set()
97 for exprs in end_token_stack:
98 expected.update(map(describe_token_expr, exprs))
99 if end_token_stack:
100 currently_looking: t.Optional[str] = " or ".join(
101 map(repr, map(describe_token_expr, end_token_stack[-1]))
102 )
103 else:
104 currently_looking = None
105

Callers 2

_parseMethod · 0.85
compile_expressionMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected