Repository navigation
context keys: implement a new parser (and a scanner/lexer) for 'when' clauses - #174471
Conversation
a07d899 to
6d6a7df
Compare
Alexandru Dima (alexdima)
left a comment
There was a problem hiding this comment.
Nice! I've left some comments.
| // TODO@ulugbekna: the whole error reporting needs reworking - especially, when we introduce it to package.json linting | ||
| private _error(errMsg: string) { | ||
| this._parsingErrors.push(errMsg); | ||
| return new ParseError(); |
There was a problem hiding this comment.
I think we should store the errMsg as the .message property of the error, otherwise all these ParseError instances are indistinguishable from each other
|
Alexandru Dima (@alexdima) Thanks for the review. I will re-request a review as soon as I address all your comments. Meanwhile, I (1) rewrote the scanner in eager style and (2) rewrote lexing of a key name using a regex. The latter speeded up things significantly - where we saw ~15-17ms, now I see ~10ms. (JS Profiler tool is great) |
and remove hard-to-maintain and debug tests
8f1dc35 to
967aa7d
Compare
|
Alexandru Dima (@alexdima) This PR leaves the old parser as the default one, so I propose that we merge as long as
All resolved comments are addressed. The unresolved comments:
During my current linting work, I may rework how error reporting is done in general -- the main point is that ideally we wouldn't report strings as errors, but objects that are then converted to strings with appropriate messages.
I will address this in a later PR that aims at perf improvements in general, if that's okay. I would like to rewrite the scanner using regexes, which should speed the scanner up a lot. |
This PR introduces a new parser for "when" clause contexts.
The new parser has a (semi-)formal syntax grammar provided at the end of this description. As a result it has more strict parsing rules that we did before.
When I say "key" or "key name" below, I mean the identifier that is looked up from the when-clause's context.
New features introduced
support for parenthesis (fixes Add support for parenthesis in "when" conditions #91473)
Data (based on ~16040 when-clauses from ~18100 most popular extensions): at least 35 when-clauses in the wild already use parentheses already (ie people generally just assume parentheses support)
support for all regex flags, the old parser supported only
i; the new one supports all: i g s m y uData: there're at least 21 when-clauses in the wild that use regex flags besides
i.syntax grammar that users can address to write when-clauses correctly
For example, if a user wrote a when-clause:
the old parser would recognize
!isEditoras a key rather than a negation because there was no clear specification of how a when-clause was allowed to look like. The new parser shows a parsing error because we support only negation of a key (we can change this behavior, but it is still better than old parser's behavior).Breakages
Regexes
we stop supporting regex that do not escape slashes, e.g., writing
foo =~ /file:////was supported in the old parser, while the new parser requiresfoo =~ /file:\\/\\/\\/. This looks uglier but it's very hard to lex a regex with unescaped slashes, e.g., javascript also can't parseconst foo = /foo//.Data: around 50 when-clauses that use unescaped slashes in their regexes.
Note: the old parser had its own problems parsing such expressions, where
foo =~ /&&/would be parsed as(foo =~ /invalid/) && /, i.e., an AND expression with/as a key.Comparison operators
The old parser allowed comparison operators
>and<in key names, e.g.,vim.use<C-r>, but only if it's not within a comparison --vim.usesOf<C-r> < 1would all be parsed as a single key name, ie{ "key" : "vim.usesOf<C-r> < 1"}.The new parser allows
<and>in key names to not break existing key names such asvim.use<C-r>, but then we don't allow non-space-separated comparison expressions such asfoo<1. We still supportfoo<=1as normal comparison (though at some time cost).Space-separated strings
The old parser allowed
foo == Bar Jarasfoo == 'Bar Jar',the new parser reports an error in such a case. One can wrap the space-separated string in single-quotes.
Data: I found ~9 when-clauses with space-separated but unquoted strings.
Syntax grammar
(Uses EBNF)