terrajobst / nquery-vnext

A Roslyn inspired rewrite of NQuery
MIT License
72 stars 16 forks source link

Ensure SyntaxToken.Parent is never null #41

Closed terrajobst closed 2 years ago

terrajobst commented 2 years ago

Right now, we have a path where if you call SyntaxFacts.ParseToken() you get back tokens that don't have a parent and thus don't belong to any syntax tree. That is odd. However, lexing a bunch of tokens without parsing them into anything specific is useful, for example, for performance reasons or because you just want a bunch of tokens without any error recovery performed by the parser (e.g. no inserted tokens).

To accommodate that, we allow queries to be empty. We parse an empty query into a CompilationUnitwhose Root property is null.

The semantics are:

When parsing individual tokens, we parse them into a CompilationRoot whose Root property is null and all the tokens are contained as leading skipped token trivia for the end-of-file token. Thus, when flattening the tokens it will produce the input sequence but each token still has a defined parent and syntax tree. Also, the returned SyntaxTree is valid and can be bound and evaluated with the semantics above.