Repository navigation
Add a Real Parser. Closes #229 and closes #225. - #235
Conversation
This reverts commit 24ddaf1.
|
The CI seems to break but the tests pass fine on my computer. I will look into why they fail later. I think I just have to |
|
Haven't looked at the code yet, but it sounds awesome from the description. Although "fast" might be more important than "awesome" :-( |
|
cc @hornairs for performance discussion |
|
Herm. I don't think we can accept a perf hit for cleaner code either. That sucks tho cause this is glorious. I tried removing the but as you said @trishume it looks like a lot of the time is spent doing Awesome job @trishume EDIT: added stuff about method inlining |
|
@hornairs I don't think the Regexps can be rearanged that much. I used to have them in optimal order and it was a little bit faster but then I kept finding problems like floating point numbers being lexed as an integer and then a dot and another integer. Right now they are in the right order for dependencies. I also tried putting all the smaller regexes into a bigger one and then disambiguating with capture groups but that wasn't any faster. Also keep in mind that this is not only about cleaner code. It also has the significant benefit of telling theme editors where they made a mistake rather than silently doing the wrong thing. I'll see what I can do about optimizing it some more. |
|
I've never written a complex parser before so I'm not sure this is practical, but I thought it'd be worth throwing out there. Is it possible with the new parser to look at a Liquid segment/tag before parsing it? If so, it may save on performance to parse a segment/tag with the existing regex parser if it can be known beforehand that the segment/tag uses proper syntax. I realize this adds an extra regex check for every segment/tag, but if the cost of this check pays off in a high enough percentage of cases (where the Liquid code is very simple) by using the faster parser, it may be worth exploring. For example, to check for simple, valid segments: r = /\{\{ *\w+(?: *\| *\w+)* *\}\}/
# matches
"{{some_var}}" =~ r #=> 0
"{{some_var|upcase}}" =~ r #=> 0
"{{ some_var | upcase }}" =~ r #=> 0
"{{ some_var | upcase | downcase }}" =~ r #=> 0
# non-matches
"{{'val'}}" =~ r #=> nil
"{{ array[0] }}" =~ r #=> nil
"{{ var | test: 1 }}" =~ r #=> nil
"{{ var | test 1 }}" =~ r #=> nil
"{{ var | test: }}" =~ r #=> nilThis regex will match the simplest segments: a variable reference with zero or more filters with no filter parameters. My guess is that a large chunk of the invalid Liquid syntax is in filter parameters (the other large chunk being in tag attributes). Something similar might be practical for simple tags, though it would be need to be a bit more complex to also match a high percentage of tags. If this is worth looking into, it may also be worth knowing what percentage of Liquid segments in Shopify templates are without any filter parameters. I just used this to check all the templates in my app: simple = 0
total = 0
Template.all.each do |template|
segments = template.text.to_s.scan(/\{\{.*?\}\}/)
total += segments.size
simple += segments.select do |segment|
segment =~ /\{\{ *\w+(?: *\| *\w+)* *\}\}/
end.size
end
puts "#{simple}/#{total} = #{simple/total.to_f}"And by the way, awesome work on this @trishume. I'm hoping the performance hit can be worked out or at least be shown to be worth it. |
There was a problem hiding this comment.
Maybe I don't get it, but this whole case block looks like it's scanning the same string 6 times in the worst case. Shouldn't that be possible with only one iteration?
There was a problem hiding this comment.
Tobi, Harry and I talked about that, I'm going to try something out.
Add Better Parse Warnings To recursive-parser Branch
|
Can someone else besides me (one of @arthurnn, @sirupsen, @hornairs, @csfrancis, ... maybe) please review this too please? (preferably today) 👍 from me, but this is pretty big, so I want a second opinion. For reference, see also https://github.com/Shopify/shopify/pull/7885. |
There was a problem hiding this comment.
Are the most common cases in usual Liquid code first here?
Also I'm not sure if I'm a fan of using then, I'd prefer newlines.
There was a problem hiding this comment.
They are in the right order such that they will parse correctly. For example, comparison can be contains which is also a valid identifier so it has to parse comparisons first.
As for the thens, there are just so many cases and the body of the cases is so small that splitting it up might make it harder to read.
|
Overall it looks like excellent work @trishume! It seems clean, the Liquid parsing errors are going to be amazing. 👍 Huge props. |
|
Ready to merge? CI is passing, it is ready to be used in Shopify, any last comments? |
|
🐳 🐳 🐳 |
|
Yay! 🎊 ✨ |
…ying store, without any plugin overwrites

@fw42 @RichardBlair @boourns @tobi
This massive pull request contains a brand new parser and lexer for variable tags and if statements that can be easily extended to work with all other tags. It uses proper parsing so it is both easier to understand and less fraught with quirks and bugs. It gives real syntax errors that tell the user what they did wrong.
The parser and lexer are made as utilities that can easily be used in other tags.
This fixes many problems with the liquid parser including closing #229 and #225. It makes the variable tag sane and
tells users what they did wrong instead of silently doing whatever the heck it wants.
Backwards Compatibility
I also added a system for determining the parsing mode. By default it is set to a compatibility mode where if a syntax error occurs it will add an error message and then fall back on the old parser instead of crashing. It can also be set to use either only the old parser or only the new parser.
Shopify could be configured to use the warning mode so that existing templates would continue working but it would show syntax warnings in the theme editor. In Liquid 2.6 the warning mode should be the default but in Liquid 3.0 the strict mode could be the default.
Tests
I added many tests for both the lexer and parser classes as well as fixing tests to expect syntax errors. I kept the tests for the lax parser and added a helper that sets the parsing mode to lax. Interestingly, there were even tests
that asserted that certain bugs like #225 existed.
Internals
Two new classes were added
Liquid::ParserandLiquid::Lexer. The lexer is a hand-written one usingStringScannerand the parser is a hand-written LL(k) recursive descent parser.Tags can use the parser class as a utility to do their own parsing. Here is an example of using the parser class:
Examples
Here are some examples of what it fixes:
Benchmarks
I added a new benchmarking command
rake benchmark:laxwhich runs the benchmarks with the old parser.Old parser (
rake benchmark:lax):New parser (
rake benchmark:run):The new parser is a bit slower. According to basic profiling the performance issues are mostly in the lexer, which I could try writing in Ragel but I think the added complexity of Ragel outweighs the performance benefits.