Skip to content

context keys: implement a new parser (and a scanner/lexer) for 'when' clauses - #174471

Merged
Ulugbek Abdullaev (ulugbekna) merged 26 commits into
mainfrom
ulugbekna/when-clause-parsing
Feb 20, 2023
Merged

Ulugbek Abdullaev (ulugbekna) merged 26 commits into
mainfrom
ulugbekna/when-clause-parsing

Conversation

@ulugbekna

@ulugbekna Ulugbek Abdullaev (ulugbekna) commented Feb 15, 2023 •

Copy link
Copy Markdown
Contributor

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 u

    Data: 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:

!isEditor == 'true'

the old parser would recognize !isEditor as 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).

  • lexical and syntax error reporting

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 requires foo =~ /file:\\/\\/\\/. This looks uglier but it's very hard to lex a regex with unescaped slashes, e.g., javascript also can't parse const 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> < 1 would 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 as vim.use<C-r>, but then we don't allow non-space-separated comparison expressions such as foo<1. We still support foo<=1 as normal comparison (though at some time cost).

  • From 16K when-clauses I've observed, not a single use non-space separated comparison currently. I propose that we disallow comparison without spaces around.

Space-separated strings

The old parser allowed foo == Bar Jar as foo == '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)


expression ::= or

or ::= and { '||' and }*

and ::= term { '&&' term }*

term ::=
	| '!' (KEY | 'true' | 'false')
	| primary

primary ::=
	| 'true'
	| 'false'
	| '(' expression ')'
	| KEY '=~' REGEX
	| KEY [ ('==' | '!=' | '<' | '<=' | '>' | '>=' | 'not' 'in' | 'in') value ]

value ::=
	| 'true'
	| 'false'
	| 'in'     
	| KEY
	| SINGLE_QUOTED_STR
	| EMPTY_STR

@alexdima Alexandru Dima (alexdima) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should store the errMsg as the .message property of the error, otherwise all these ParseError instances are indistinguishable from each other

@ulugbekna

Copy link
Copy Markdown
Contributor Author

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
@ulugbekna

Ulugbek Abdullaev (ulugbekna) commented Feb 20, 2023 •

Copy link
Copy Markdown
Contributor Author

Alexandru Dima (@alexdima) This PR leaves the old parser as the default one, so I propose that we merge as long as

  1. you're okay with it
  2. tests pass

All resolved comments are addressed. The unresolved comments:

I think we should store the errMsg as the .message property of the error, otherwise all these ParseError instances are indistinguishable from each other

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.

It would be better to use String.charCodeAt instead of the array access and to work everywhere with the CharCode const enum from src/vs/base/common/charCode.ts. That would be also a low hanging perf improvement.

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.

@alexdima Alexandru Dima (alexdima) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice!

@ulugbekna
Ulugbek Abdullaev (ulugbekna) deleted the ulugbekna/when-clause-parsing branch February 20, 2023 15:33
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for parenthesis in "when" conditions

3 participants