Citron > Parsing interface

The Parsing Interface

Summary of the interface

Parser interface

The parser interface consists of these two methods which can be called on the parser class generated by Citron:

These methods are actually defined on the CitronParser protocol, which the Citron-generated parser class conforms to.

Lexer interface

Tokenization is a separate step that needs to drive the parsing. As the tokens are generated from the input string or data, the tokens should be passed to the parser’s consume(token:, code:) method.

Citron provides a simple rule-based lexer, CitronLexer, that can generate these tokens from an input string. The important methods it offers are:

Error handling interface


Both consume(token:, code:) and endParsing() are throwing methods. They throw when an input token at a certain position is inconsistent with the grammar, or when the input ends prematurely.


tokenize(string:, onFound:) throws on errors encountered during tokenization.

Error capturing

For more advanced error handling capabilities, see Error capturing.

Detailed API documentation


A few examples of how Citron is used for parsing can be found in the “examples” folder in the project repository.