|
SciLex
A header-only C++20 lexer built on REAL
|
A token rule: a kind, the pattern that recognizes it, whether matches are discarded (whitespace, comments), and — for contextual lexing — the modes it is active in and an optional mode transition it fires when it wins. More...
#include <lexer.hpp>
Public Attributes | |
| int | kind |
| Kind assigned to tokens this rule produces. | |
| real::regex | pattern |
| The recognizer (a linear-time REAL regex; its flags are the author's — see above). | |
| bool | skip {false} |
| If true, matches are consumed but not emitted. | |
| indent_role | indent {indent_role::none} |
| Its part in the indentation (Indentation). | |
| extension | extend {extension::none} |
| How it extends below its line (Indentation). | |
| std::vector< std::string > | in_mode {} |
| Modes this rule is active in; empty ⇒ {"default"}. | |
| std::optional< mode_action > | action {} |
| std::vector< int > | after {} |
| Active only right after a token of one of these kinds (see The previous token). | |
| std::vector< int > | not_after {} |
| Active except right after a token of one of these kinds (see The previous token). | |
| std::optional< real::regex > | continuation {} |
| std::optional< bool > | at_line_start {} |
A token rule: a kind, the pattern that recognizes it, whether matches are discarded (whitespace, comments), and — for contextual lexing — the modes it is active in and an optional mode transition it fires when it wins.
in_mode empty means the rule is active in the implicit "default" mode only, so a plain {kind, pattern, skip} rule keeps working unchanged.
The pattern is a fully-formed real::regex, so the grammar author owns its flags.
This is a real trade-off, not a footnote. \w+ (or [^\W\d]\w*) with the default flags reads Unicode identifiers — café, 変数 — the faithful behaviour for a language like Python 3. But a Unicode \w expands into more UTF-8 byte transitions than a DFA is built from, and \b is a zero-width assertion no DFA represents, so a rule holding either stays on the general Pike engine while the mode's other rules take the DFA (same tokens). That rule is tried at every position its first byte allows, so it keeps a share of the per-rule cost. The narrower Unicode \d and \s expand and stay on the DFA. BENCHMARKS.md measures the cost per grammar: the python-unicode grammar, whose identifier rule stays on Pike, keeps a smaller part of the DFA's gain than python, whose rules all take the DFA — the Unicode identifier costs part of the fast path.
after and not_after make a rule depend on the token emitted before it, in any mode (a skip rule emits none; an error run counts, as scilex::error): with after, the rule is active only right after a token of one of those kinds; with not_after, everywhere else. Before the first token the previous kind is scilex::end_of_input. A rule has at most one of the two. This is how a / after an operand is a division and anywhere else opens a regular expression literal, without a mode for every context. The lexer gives a mode one DFA per distinct set of active rules, so the choice costs nothing per token.
at_line_start makes a rule depend on where its token begins: with true it is active only at the start of a line (the start of the text, or right after a \n), with false only past it. It says what a (?<![^\n]) or (?<=[^\n]) in front of the pattern says, but through the same units as the previous token, so the rule stays on the DFA where the lookbehind would send it to Pike, which every token then pays for. YAML's --- is a document marker only at the start of a line, and past it begins a scalar.
indent and extend (Layout Awareness Level B) let a token's extent depend on the indentation of the block it belongs to, which no pattern knows. The lexer keeps a stack of open blocks' columns, as YAML's scanner does, and updates it at each emitted token of a rule with a role or an extension, in a significant mode (a mode named insignificant, such as YAML's flow collections, takes no part): first the blocks deeper than the token's column close (all of them for indent_role::reset), then an indent_role::entry opens a block at its column, an indent_role::key is kept as the line's key candidate, and an indent_role::value opens a block at that candidate's column. The innermost open block is the reference an extension reads:
---, ...) at column 0 ends it. Columns are counted in the lexer's unit; an indentation counts spaces. A token so extended spans lines; the layout pass reads where it starts, so its body adds no structure.If your identifiers are ASCII by specification (JSON, SQL, C), pin (?a) inline in the pattern (or pass real::flags::ascii) to keep \w \d \s \b ASCII and small, DFA-representable, and fast — this is what the examples/ grammars do. If you want Unicode identifiers, write \w+ and accept the general-engine floor. The two spellings tokenize the same ASCII input identically; they differ only on non-ASCII input and on whether the mode can be a DFA.
| std::optional<mode_action> scilex::rule::action {} |
Mode transition fired when this rule wins.
| std::vector<int> scilex::rule::after {} |
Active only right after a token of one of these kinds (see The previous token).
| std::optional<bool> scilex::rule::at_line_start {} |
Active only at the start of a line (true) or only past it (false); empty ⇒ both (see The start of a line).
| std::optional<real::regex> scilex::rule::continuation {} |
What each line an extension::lines token continues on must hold at its first non-blank (matched within the line); empty ⇒ pattern.
| extension scilex::rule::extend {extension::none} |
How it extends below its line (Indentation).
| std::vector<std::string> scilex::rule::in_mode {} |
Modes this rule is active in; empty ⇒ {"default"}.
| indent_role scilex::rule::indent {indent_role::none} |
Its part in the indentation (Indentation).
| int scilex::rule::kind |
Kind assigned to tokens this rule produces.
| std::vector<int> scilex::rule::not_after {} |
Active except right after a token of one of these kinds (see The previous token).
| real::regex scilex::rule::pattern |
The recognizer (a linear-time REAL regex; its flags are the author's — see above).
| bool scilex::rule::skip {false} |
If true, matches are consumed but not emitted.