Kevin [Tue, 12 May 2020 01:50:57 +0000 (21:50 -0400)]
Fix `{% for in %}` loop parsing
This fixes the fact that most `{% for in %}` loops will be parsed
using the `in` operator now, so that operator must be detected and
extracted out in order to make it parse the same way as before. This
is the start of the special cases within the parser for handling
Jinja's previous parsing style.
Kevin [Tue, 12 May 2020 01:45:43 +0000 (21:45 -0400)]
Support "in" operator in grammar
This changes the previous comparison operations from being marked
as solely comparion expressions and expands them out to be generation
operator expressions. This allows us to easily support the "in"
operator, which in the current Jinja parser is handled exactly the
same as the other operations, but it does require us to special case
the automated conversion of the "not ... in" expression to a "notin"
expression.
Kevin [Tue, 12 May 2020 01:25:29 +0000 (21:25 -0400)]
Add concat expression support to grammar and parser
This is probably going to be reclassified in the grammar and parser
as something different from the conditional expressions once more
support for math operators is added in.
Kevin [Tue, 12 May 2020 01:11:32 +0000 (21:11 -0400)]
Simplify variable values in grammars
Now that variable identifers are able to be used as conditional
expressions, we can just specify that variable expressions in the
grammar are looking for a conditional expression as the name of the
variable.
Kevin [Tue, 12 May 2020 01:08:06 +0000 (21:08 -0400)]
Allow variable identifiers to be conditionals
This aligns with the Python behaviour and pre-existing Jinja behaviour
where a variable expression can be used as a test for a conditional.
This was necessary to allow variable identifiers to be used in places
which was expecting a possible conditional expression, like in a if/else
expression.
Kevin [Tue, 12 May 2020 01:05:37 +0000 (21:05 -0400)]
Restrict where conditional expressions are allowed
Previously conditional expressions were only allowed in things which
accepted parameters to call accessors, which is most things, but this
was found to be too broad. Many contexts to not actually allow conditional
expressions so this was restricted back to block parameters and variable
calls.
Kevin [Tue, 12 May 2020 01:03:45 +0000 (21:03 -0400)]
Add support for if/else expressions in grammar
These fall under a special type of conditional expression and can
only be used in certain places. The grammar for test functions
needed to be updated to rejected test function parameters if they
are only called "else", since that is likely to be for an if/else
expression. This matches the existing behaviour of the Jinja parser.
Kevin [Tue, 12 May 2020 00:14:40 +0000 (20:14 -0400)]
Add support dictionary literals to grammar
This also allows dictionary values to be variables instead of just
regular identifiers, a change which might be made to other literals
such as lists and tuples in the future as we determine what those
also support.
Kevin [Tue, 12 May 2020 00:02:48 +0000 (20:02 -0400)]
Support single-parameter tests without parentheses
This adds support for the optional parantheses in tests that are only
being supplied a single parameter. Tests which use parantheses are
currently not supported in the parser, but are supported in the grammar.
Kevin [Sun, 10 May 2020 21:34:50 +0000 (17:34 -0400)]
Fix AST for conditional expressions
Previously all conditional expressions were left associative which
produced an AST that was nothing like the one in Jinja and one which
did not respect order of operations within conditions. The conditional
expression part of the grammar has been rewritten to be more explicit
about what can and cannot match which appears to have fixed those
issues with the AST.
Kevin [Sun, 10 May 2020 20:44:08 +0000 (16:44 -0400)]
Combine common template data/outputs in AST
This makes it easier to compare the AST generated by the old Jinja
parser and the AST generated by the new one, since the new AST
separates out template data character by character currently.
Kevin [Sun, 10 May 2020 20:24:12 +0000 (16:24 -0400)]
Allow variable identifiers to be aliased in block params
This is only really supported on the `{% from %}` block currently,
but the ability exists to use this elsewhere if someone is looking
for the ability to alias variable identifiers. This also allows
value-only parameters to be comma-separated within the block
parameters, since before that was only allows for key-value parameters.
Kevin [Sun, 10 May 2020 19:52:07 +0000 (15:52 -0400)]
Mark with targets as parameters
This fixes a bug where the targets of a `{% with %}` block would not
be marked as a parameter. This is because they were not being marked
at all as a variable which results in an invalid AST. For reference
counting purposes, this must also be marked specifically as a parameter
variable instead of as a stored variable to ensure it does not leak
out of the block.
Kevin [Sun, 10 May 2020 19:50:15 +0000 (15:50 -0400)]
Properly mark set target as variable
Previously it wasn't being marked as a variable at all if it was just
a string literal, so this fixes it so Jinja knows that the assignment
should be stored on the target variable.
Kevin [Sun, 10 May 2020 19:47:53 +0000 (15:47 -0400)]
Allow optional comma after block keyword parameters
It looks like this is only currently support within `{% with %}`
blocks in Jinja, instead of space-separating the parameters, but
this may also be happening in extensions as well.
Kevin [Sun, 10 May 2020 18:08:48 +0000 (14:08 -0400)]
Support assignment blocks using `{% set %}`
This adds support for the usage of `{% set %}` where the contents of
the block are assigned to the variable instead of handling that within
the block parameters.
Because Jinja separates the filter from the variable within the
`AssignBlock` node, we have to detect when there is a wrapping filter
and extract it so that it can slot in properly.
Kevin [Sun, 10 May 2020 16:04:38 +0000 (12:04 -0400)]
Add initial parsing of for blocks
This will correctly parse out most of the starting block as well as
the contents, but it does not yet handle the parsing of the `{% else %}`
block that can be contained within the contents of the block.
Kevin [Sun, 10 May 2020 16:03:32 +0000 (12:03 -0400)]
Parse variable tuples
Right now these are custom parsed since they are not the same as
tuple literals and generally serve a different purpose. They are
tuples which contain variable identifiers and are generally used
for assignment.
Kevin [Sun, 10 May 2020 15:13:15 +0000 (11:13 -0400)]
Add filter parsing for variables
Filters and their arguments are now properly parsed into the right
Jinja nodes. Dynamic arguments (`*args`) and dynamic keyword arguments
(`**kwargs`) are not currently parsed as they are not supported by
the grammar.
Kevin [Sun, 10 May 2020 14:48:16 +0000 (10:48 -0400)]
Dot accessors can only be identifiers
This fixes a bug where the grammar allowed dot accessors to be any
variable type. This results in the filters that were meant for the
variable that was being accessed to be captured by the accessor
itself, which generated an unexpected and invalid AST.
Kevin [Sun, 10 May 2020 14:43:50 +0000 (10:43 -0400)]
Output nodes are always lists
Right now we don't optimize Output nodes so that ones next to each
other are combined, but they should all contain a list of a single
node in preparation for eventually doing that.
Kevin [Sun, 10 May 2020 14:43:00 +0000 (10:43 -0400)]
Parse the contents of the body of with block
This logic will inevitable be generalized out in the future for
other full blocks, but for now it properly handled parsing the
contents of the block and converting it to the Jinja AST.
Kevin [Sun, 10 May 2020 14:42:21 +0000 (10:42 -0400)]
Properly parse with targets
The targets of with expressions (the variables to load) should be
properly parsed as a variable using the same load logic which is
used in other areas of the parser.
Kevin [Sun, 10 May 2020 14:29:48 +0000 (10:29 -0400)]
Start working on new AST parser
This parser will take the Tatsu-generated AST and generate a compatible
Jinja AST from it. This should allow us to refine the grammar to
generate a better AST and also verify that it is producing comparable
Jinja ASTs that can be used by the current compiler.
Kevin [Sun, 10 May 2020 02:23:32 +0000 (22:23 -0400)]
Support filters on all variables
This adds support to filters on all variables instead of to just
variables which are being printed within templates. This now allows
us to pass in variables that are using filters as parameters within
a block, so they can be used for setting new variables or to perform
transformations on the fly.
Kevin Brown [Sat, 9 May 2020 03:53:44 +0000 (23:53 -0400)]
Variable filters should not capture whitespace
This should have no functional difference on the behaviour of the
parser but it does make filters consistent with other areas where
it expects to start with the first character instead of starting
with whitespace.
This also changes the test script to time the individual steps so
we can confirm that the grammar compilation is considerably slower
than the parsing of the template using the grammar.
Kevin Brown [Fri, 8 May 2020 23:55:24 +0000 (19:55 -0400)]
Enable whitespace control within closing blocks
This enables the ability to force whitespace to be stripped following
the end of a block statement by adding a `-` before the closing
tag. This is supported both for stripped the space at the beginning
of a block as well as stripping the space that follows a block.
Kevin Brown [Fri, 8 May 2020 23:25:50 +0000 (19:25 -0400)]
Remove whitespace parsing from inside block
This fixes an issue where trailing whitespace within blocks was being
processed as an expression instead of being collected into the block
definition. Now the whitespace is consisitently stored within the
block expression which should make whitespace handling easier to
implement.
Kevin Brown [Fri, 8 May 2020 23:18:38 +0000 (19:18 -0400)]
Add support for optional ":" at end of line statement
This does not fully work for the example file because the grammar
for a test expression can span multiple lines which causes the HTML
in the line following the block expression to be detected as a test
expression instead of being detected as content.
Kevin Brown [Fri, 8 May 2020 22:57:33 +0000 (18:57 -0400)]
Fix parameters eating whitespace
This was causing issues for anything that depends on significant
whitespace to follow a parameter, since it wasn't being made
available within the grammar.
Kevin Brown [Fri, 8 May 2020 21:19:04 +0000 (17:19 -0400)]
Converted more of parse tree to dictionary
This should make it easier to detect what type of literal has been
parsed (we don't differentiate between single and double quoted
strings) as well as determining the specific accessors that are
called on a given variable.
Tuple and list literals have also been normalized to hold their
values in a key called `value` which is the same as other literals.
Implicit identifier tuples have not been switched yet because those
are not currently parses like tuple literals.
Kevin Brown [Fri, 8 May 2020 20:56:26 +0000 (16:56 -0400)]
Support implicit tuples in block parameters
This fixes an issue where implicit tuples were not parsed correctly
when they were used as a key in a block parameter. Now for loops
and set statements with implicit tuples work properly. This only
supports implicit tuples when all values are identifiers, since
these are generally used for assignment and you cannot assign a
new value to a literal.
Kevin Brown [Fri, 8 May 2020 17:51:14 +0000 (13:51 -0400)]
Add support for tests
Right now these are very basic and don't appear to form the correct
parse tree for logical comparisons which use tests as well. But it
parses at lease somewaht correctly, so there is something to work
with here.
Kevin Brown [Fri, 8 May 2020 14:01:10 +0000 (10:01 -0400)]
Support assignment expressions in generic blocks
Because of the way assignment expressions handle implicit tuples,
it's now possible to support the complex for loops within the
standard generic block grammar.
Kevin Brown [Fri, 8 May 2020 13:54:35 +0000 (09:54 -0400)]
Added grammar for `for` loops
Since this needs to support assignment targets, it is difficult to
properly support this within a generic block syntax because of the
ability to create implicit tuples.
Kevin Brown [Fri, 8 May 2020 12:49:24 +0000 (08:49 -0400)]
Added tuple literal support
This maintains the expectation that tuple literals will always end
with a comma if there is a single item.
The example is the combined one from the Jinja docs but it does not
include the comma for the tuple assignment since the grammar does
not currently support that.
Kevin Brown [Fri, 8 May 2020 11:13:52 +0000 (07:13 -0400)]
Support key-value block parameters
Block parameters should support all of the things that a function
call parameter would normally support. This includes key-value
paramters and in our test we include the transaltion example from
the documentation.
Kevin Brown [Fri, 8 May 2020 11:08:04 +0000 (07:08 -0400)]
Support named parameters to calls
This also switches the parameters in calls to be returned as a
dictionary which should make it easier to differentiate between a
value-only parameter and a key-value parameter.
Kevin Brown [Fri, 8 May 2020 10:26:02 +0000 (06:26 -0400)]
Support dot accessors on variables
This adds support for dot accessors on variables in such a way that
it is flexible enough to match the handling provided by the existing
template engine.
Kevin Brown [Fri, 8 May 2020 10:21:58 +0000 (06:21 -0400)]
Start definining variable identifiers
Variables are standard identifiers or literals that can be
augmented by accessors (either dictionary or dot style). This
finally defines what a string is.
Kevin Brown [Fri, 8 May 2020 09:33:28 +0000 (05:33 -0400)]
Fix content overtaking expressions
This fixes a issue where content would try to overtake everything
following it, even if there was a better expression to match after
the content. This was fixed by telling content to match everything
but the start of different expressions, which appears to solve a
bunch of issues.