Citron > Parser 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

Parsing

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.

Tokenization

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

API documentation

Here’s the API documentation for:

Examples

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