-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add template tag parser and compiler #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JuroOravec
wants to merge
5
commits into
main
Choose a base branch
from
jo-feat-template-tag-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
878899d
feat: add template tag parser and compiler
JuroOravec e4c389a
refactor: minor updates for the pest grammar
JuroOravec 78b4fc9
refactor: fix tests
JuroOravec f4846fc
docs: document flow for parsing and compiling
JuroOravec de9b038
refactor: rename dynamic expressiosn to template strings
JuroOravec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The template syntax parsing was implemented using Pest. Pest works in 3 parts:
"grammar rules" - definition of patterns that are supported in the.. language? I'm not sure about the correct terminology.
Pest defines it's own language for defining these rules, see
djc-template-parser/src/grammar.pest.This is similar to Backus–Naur Form, e.g.
Or the MDN's formal syntax, e.g. here:
Well and this Pest grammar is where all the permissible patterns are defined. E.g. here's a high-level example for a
{% ... %}template tag (NOTE: outdated version):Parsing and handling of the matched grammar rules.
So each defined rule has its own name, e.g.
django_tag.When a text is parsed with Pest in Rust, we get a list of parsed rules (or a single rule?).
Since the grammar definition specifies the entire
{% .. %}template tag, and we pass in a string starting and ending in{% ... %}, we should match exactly the top-leveltag_wrapperrule.If we match anything else in its place, we raise an error.
Once we have
tag_wrapper, we walk down it, rule by rule, constructing the AST from the patterns we come across.Constructing the AST.
The AST consists of these nodes - Tag, TagAttr, TagToken, TagValue, TagValueFilter
Tag- the entire{% ... %}, e.g{% my_tag x ...[1, 2, 3] key=val / %}The first word inside a
Tagis thetag_name, e.g.my_tag.After the tag name, there are zero or more
TagAttrs. This is ALL inputs, both positional and keywordx,...[1, 2, 3],key=valTagAttrs.TagAttrsMUST have a value.TagValue holds a single value, may have a filter, e.g.
"cool"|upper_('mystr'), etc. The specific kind is identified by what rules we parse, and the resulting TagValue nodes are distinguished by theValueKind, an enum with values like"string","float", etc.[1, 2, 3]|append:4[1|add:1, 2|add:2]Any TagValue can have 0 or more filters applied to it. Filters have a name and an optional argument, e.g.
3|add:2- filter nameadd, arg2. These filters are held byTagValueFilter.[1]|extend:[2, 3]Lastly,
TagTokenis a secondary object used by the nodes above. It contains info about the original raw string, and the line / col where the string was found.The final AST can look like this:
INPUT:
AST: