You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently we use Swift error handling for parser
errors. While this is convenient, it has a number
of drawbacks:
- Any AST parsed gets thrown away as soon as we
encounter an error. This prevents clients from
being able to get any useful information from
invalid AST (rdar://93677069).
- Multiple diagnostics cannot be issued, meaning
that e.g a basic syntactic error could obscure a
more useful semantic error.
- It doesn't extend nicely to e.g warning
diagnostics, meaning that we'd eventually end up
with 2 ways of emitting diagnostics.
- The thrown errors relied on `recordLoc` blocks
to annotate them with source location info, which
could lead to errors without location info if we
forgot to add the appropriate `recordLoc` calls.
Additionally, in some cases we want a more fine
grained location info than the block would give us.
Therefore this commit removes the use of Swift
error handling throughout the parser. The parser
is now a total function that _always_ returns an
AST. If errors are encountered while parsing, they
are recorded, and are attached to the resulting
AST by the parser. The parser attempts to recover
as much of the AST it can when encountering an
error. As such, there is now are now `.invalid`
atom and character property kinds. Sema then runs
and can attach more diagnostics onto the AST.
For now, the compiler interface remains the same,
and we pick a single error to `throw`, but this
will be changed in a later PR to allow multiple
errors and warnings, as well as AST recovery. This
also means we can better preserve the capture type
in the presence of parser errors.
Fortunately, in most cases, this is quite a
mechanical transformation. It entails:
- Moving the lexical analysis methods onto the
`Parser`. We were already passing `ParsingContext`
parameters for most of them, so it's not clear they
were benefitting from the isolation that `Source`
offered. Effectively this means that all parsing
has access to the context and diagnostics.
- Converting error throwing statements into calls
to the parser's `error` method (or `unreachable`
method for unreachables).
This commit also updates the parser tests to be
able to be able to match against multiple
diagnostics.
0 commit comments