Citron > Parsing interface > CitronParser

CitronParser

Protocol

Defines the Citron parser interface.

The parser code generated by Citron defines a class that conforms to this protocol.


Parsing

consume(token: CitronToken, tokenCode: CitronTokenCode)

Consumes one token.

This should be called multiple times to pass a sequence of tokens to the parser. Typically, a separate tokenization stage would generate this sequence of tokens.

Parameters:

Return value:

Throws:

endParsing()

Signifies the end of input. This should be called when there are no more tokens to consume.

Parameters:

Return value:

Throws:

Errors

UnexpectedTokenError

A token was encountered at a point that is not supported by the specified grammar.

UnexpectedEndOfInputError

End of input was encountered before the grammar could be satisfied.

StackOverflowError

Signifies that parsing was aborted because to continue parsing the number of entries in the stack needs to exceed the maxStackSize set.

If maxStackSize is nil (the default), this error is never thrown, and the parser stack is allowed to grow without overflowing.

Stack size

maxStackSize: Int?

If this is nil (the default), there is no restriction on how many entries the parser stack can have.

If this is non-nil, this specifies the maximum number of entries that the parser’s stack can be allowed to hold. If an input requires the stack to grow above that, it would cause a StackOverflowError to be thrown, and parsing shall be aborted.

maxAttainedStackSize: Int

This can be queried at the end of the parse to see the maximum number of entries that the stack ever held during the parse.

We can, for example, observe the effect of left-recursive vs right-recursive rules on how the stack grows.

Tracing

isTracingEnabled: Bool

If this is set to true, information on how the parsing happens is printed out. This can be used to debug the parser.

The default value is false.

isTracingPrintsSymbolValues: Bool

If this is set to false (the default), tracing prints only the symbol codes and not the semantic values of the symbols.

If this is set to true, tracing also prints semantic values of the symbols using the String(describing:) string initializer.

The default value is false.

isTracingPrintsTokenValues: Bool

If this is set to false (the default), tracing prints only the token codes and not the semantic values of the tokens.

If this is set to true, tracing also prints semantic values of the tokens using the String(describing:) string initializer.

The default value is false.

Error capturing

errorCaptureDelegate: CitronErrorCaptureDelegate?

To activate error capturing, this should be set to a class conforming to the protocol referred to by the CitronErrorCaptureDelegate associated type.

If this is nil, error capturing is not activated even if the grammar file contains %capture_errors directives.

The default value is nil.

consume(lexerError: Error)

To enable error capturing of lexer errors, the lexer errors can be passed to the parser using this method. Each lexer error should be passed to this method, one at a time, as and when it is found during tokenization.

Parameters:

Return value:

Throws:

If the error cannot be saved for capturing, this method throws the error passed in as lexerError.

This can happen in one of these scenarios:

  1. If there are no error capturing nonterminals currently being parsed, or
  2. The errorCaptureDelegate’s shouldSaveErrorForCapturing(error:) method returns false

Associated Types

These associated types are bound to types defined by the Citron-generated parser code.

CitronToken

The semantic type of each token, as seen by the code blocks in the grammar. This is the type defined by %token_type type in the grammar file.

CitronTokenCode

This is an enum of all tokens (or terminals) used in the grammar file.

If a %tokencode_prefix is specified, the enum values will use that prefix.

CitronNonTerminalCode

This is an enum of all non-terminals used in the grammar file.

If a %nonterminalcode_prefix is specified, the enum values will use that prefix.

CitronSymbolCode

This is an enum that represents all symbols (both terminals and non-terminals) used in the grammar file.

CitronResult

The semantic type of the start symbol as specified in the grammar file.

CitronErrorCaptureDelegate

This is typealiased to a protocol that the errorCaptureDelegate should conform to. The name and contents of the protocol are automatically generated from the grammar file.

See the CitronErrorCaptureDelegate page for more information.