Repository navigation
Update the expression parser to Parlot 2.3.0 - #408
Open
sebastienros wants to merge 3 commits into
Open
sebastienros wants to merge 3 commits into
sebastienros wants to merge 3 commits into
Conversation
Since 2.3.0, a When that rejects a value reports the failure at the start of the value. ParsePrimary's syntax errors then pointed at the start of the expression instead of the token that can't follow it, where the former parser reported them: "a|b" at column 1 instead of 2. When the condition fails, the primary expression is now read again and fails after its end, which reports that token again, on the error path only. Signed-off-by: Sébastien Ros <1165805+sebastienros@users.noreply.github.com>
The escape sequences of the language, a doubled quote and no backslash escape, are those of StringEscapes.Sql. String() returns the text between the quotes, decoded only when it has a doubled quote, so ToPooledString() gets the value without first copying the token and then its content: two strings less per literal, and none when the literal was already read, as when an uncached expression is parsed again. The lexer's ScanString still finds the strings of type names and of syntax errors. Signed-off-by: Sébastien Ros <1165805+sebastienros@users.noreply.github.com>
They apply at most once to a logical expression. The comment and the README said the last operand of ?: being a whole expression kept it out of the table, which an operator table can now parse. Signed-off-by: Sébastien Ros <1165805+sebastienros@users.noreply.github.com>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A follow-up to #375. Parlot 2.3.0 has new combinators and
Operators()features: this updates the source generator anduses the one that helps this grammar. The language doesn't change, nor do the trees or the syntax errors.
Changes
Whenthat rejects a value reports the failure at the startof the value (Report the start of what a generated parser rejected as the failure sebastienros/parlot#497).
ParsePrimarythen reported its errors at the start of the expression insteadof at the token that can't follow it, where the ANTLR parser reported them:
a|bat column 1 instead of 2. Threeentries of the golden file caught it, and 15.5% of the errors of
ParsePrimaryin the differential test below moved.When the follow check fails, the primary expression is now read again and fails after its end, which reports that token
again. This runs on the error path only.
String()and pool them.Terms.String(StringLiteralQuotes.Single, StringEscapes.Sql)reads the strings of the language: a doubled quote is a quote and a backslash is an ordinary char
(Support the escape sequences of other languages in String() sebastienros/parlot#535). It returns the text between the quotes, so
ToPooledString()gets the value without copyingthe token and then its content: two strings less per literal, and none when the literal was already read, as when
ExpressionEvaluator.GetValueparses the same expression again. The pool is bounded, takes no lock to find a value,and turns itself off when values rarely repeat.
=,??and?:are not levels of the operator table. Comment and README only: they apply atmost once to a logical expression. Both said that the last operand of
?:being a whole expression kept it out ofthe table, which 2.3.0 can now parse (see below).
Same trees and errors
expressions and mutations of both, 104,204 inputs through
Parse,ParsePrimary,ParsePropertyandParseLambda.This branch returns the same trees and the same errors, message and column, as 2.2.1 for all 416,816 parses, on .NET 10
(arm64) and on .NET Framework 4.8 (x64), where the generated parser takes its downlevel paths.
ExpressionGrammarTests, with the golden trees and errors of the ANTLR parser, and the other expression tests pass.Spring.Globalization.Formatters, which depend on the culture data of the OS and pass on Windows.Expression.Parsereads on a small stack is the same with 2.2.1 and this branch: 47parentheses or calls on a 256 KB thread, 271 parentheses and 255 calls on 1 MB (.NET 10, arm64, Release), so the
nesting limits of the README don't move.
Benchmarks
ExpressionBenchmarkandObjectWrapperBenchmarkfrom #375. BenchmarkDotNet in process, 3 warmup and 10 iterations of150 ms, .NET 10, builds alternated.
Parsing, Apple M4 Pro (arm64), mean of 2 to 4 runs per build:
ParseSmallParseMediumParseLargebetween the three 2.3.0 columns are within the spread of the runs of a build (±4%): the small expression has no string
literal, and its 228 ns in (2) comes from one run at 247 ns, the others took 215 and 221 ns.
ObjectWrapper.PropertyPath, which goes throughParsePrimary, took 527 to 628 ns and allocated 1.42 KB with everybuild. Alternated four times, it took 541 ns with 2.3.0 alone and 540 ns with the fix of (1), which only adds a check of
the next char on a successful parse.
From parsing to evaluation, AMD EPYC 7763 (x64, Windows), 2.2.1 against this branch, mean of 2 runs each:
ParseSmallParseMediumGetValueUncachedSmallGetValueUncachedMediumParseThenGetValue100SmallParseThenGetValue100MediumGetValuePreparedSmall (evaluation only)GetValuePreparedMedium (evaluation only)GetValuePrepared, whose code this branch doesn't change: itevaluates a tree parsed once. These differences come from where the code of the evaluation lands, not from the
parser. On the Mac too, evaluating alone took the same time with and without these changes (51 ns and 50 ns).
(2.2.1 took 56 and 70 µs to parse it, this branch 17 and 16 µs, while the medium one takes 2.5 µs).
What 2.3.0 has that this grammar doesn't use
Operators().Ternarywith a parser for the last operand (Operators().Ternary: parse the last operand with a given parser sebastienros/parlot#524):?:,=and??apply at most once, to a logicalexpression. A
Ternarylevel applies again to its own value and lets the levels after it apply to it: with one, 16 ofthe 104,204 inputs parse that the language rejects, such as
a ? b : c = d ?? e, read as(a ? b : (c = d)) ?? e.levels of this grammar have a factory per operator and need no context, and the table already looks up the operator
after an operand with a single choice. A valued level would cost the same.
Peek(Add Peek, a positive lookahead combinator sebastienros/parlot#526): the syntactic predicates of the ANTLR grammar are single parsers here (id.And(call), one parser for aparenthesized expression and an expression list). A lookahead would read the same text twice, and for
(LPAREN expression SEMI) =>it would bring back the exponential time of nested parentheses.ThenTry(Add ThenTry, a conversion that can reject the value sebastienros/parlot#537),WithState(Add WithState to set a state for the duration of a parser sebastienros/parlot#527),Separatedwith an empty first value (Separated: accept a first value that matches without consuming input sebastienros/parlot#542),Identifierwith code points(Match identifiers of code points, with escape sequences and surrogate pairs sebastienros/parlot#528),
Longest(Add Longest, a choice which keeps the longest match sebastienros/parlot#543),StringLiteral(returnDecoded)(Add a returnDecoded option to StringLiteral sebastienros/parlot#541): nothing here rejects a value once read, depends on anenclosing construct, has an empty list item, reads non-ASCII identifiers, or needs another longest match than the one
the token scanners already make.