Citron > Parsing interface > CitronErrorCaptureState
CitronErrorCaptureState
Structure
A structure populated by Citron to pass the error capturing state to
CitronErrorCaptureDelegate
methods.
In case an error has been previously saved for capturing, and when Citron has found a matching synchronization point for capturing onto some error-capturing non-terminal, this structure describes the partially resolved state of that non-terminal.
The type of this struct should be accessed through the CitronErrorCaptureState
associated type on CitronParser
.
Stored properties
resolvedSymbols: [(symbolCode:
CitronSymbolCode
, value: Any)]
An array of the constitient symbols of the error-capturing non-terminal that have been resolved so far. The
symbolCode
fields indicate which symbol it is, and thevalue
field stores the semantic value of that symbol.
unclaimedTokens: [(token:
CitronToken
, tokenCode:
CitronTokenCode
)]
The tokens that were passed in since the error occurred and till the synchronization point was found.
If there was no error, these tokens would have been resolved, and along with the
resolvedSymbols
, would have formed the complete error-capturing non-terminal.In case the synchronization point was matched using an
end_after
clause, theunclaimedTokens
sequence would end with the matching entry (either a token or a sequence of tokens) in theend_after
clause.
nextToken: (token:
CitronToken
, tokenCode:
CitronTokenCode
)?
The current look-ahead token, at the time of attempting error capturing.
In case the synchronization point was matched using an
end_before
clause, thenextToken
would be the matching token in theend_before
clause.In case the error is being captured at the end of input,
nextToken
would benil
.
Computed properties
lastResolvedSymbol: (symbolCode:
CitronSymbolCode
, value: Any)?
The last entry in the
resolvedSymbols
array. In caseresolvedSymbols
is empty, this isnil
.
erroringToken: (token:
CitronToken
, tokenCode:
CitronTokenCode
)?
The first entry in the
unclaimedTokens
array. In caseunclaimedTokens
is empty, this is the same asnextToken
.
Example
For example, if the grammar file contains these lines for a param
non-terminal representing a Swift function parameter:
param ::= external_param_name local_param_name type_annotation.
type_annotation ::= Colon type.
type ::= Identifier.
%capture_errors param
end_before(Comma | CloseBracket).
Consider the following input to the parser:
func isOdd(number n Int) -> Bool
Let’s assume that the grammar identifies the param
non-terminal
to start with the number token, and also identifies the
number token as an external_param_name
, and the n token as a
local_param_name
. The grammar expects a Colon
token as the next
token, but it gets Int, causing a syntax error.
With error capturing turned on, Citron will save this error and start
looking for a synchronization point to capture this error. When it sees
the ) in the input, or a CloseBracket
token, it has a match in the
end_before
clause of the param
non-terminal.
At this point, Citron will call the shouldCaptureErrorOnParam
method
of the errorCaptureDelegate
object with the state
argument set to
a CitronErrorCaptureState
object with the following values:
-
- Symbol
external_param_name
with value number - Symbol
local_param_name
with value n
- Symbol
-
- Token representing Int with code
Identifier
- Token representing Int with code
-
- Token representing ) with code
CloseBracket
- Token representing ) with code