Skip to the content.

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 the value 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, the unclaimedTokens sequence would end with the matching entry (either a token or a sequence of tokens) in the end_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, the nextToken would be the matching token in the end_before clause.

In case the error is being captured at the end of input, nextToken would be nil.

Computed properties

lastResolvedSymbol: (symbolCode: CitronSymbolCode, value: Any)?

The last entry in the resolvedSymbols array. In case resolvedSymbols is empty, this is nil.

erroringToken: (token: CitronToken, tokenCode: CitronTokenCode)?

The first entry in the unclaimedTokens array. In case unclaimedTokens is empty, this is the same as nextToken.

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: