|
SciLex
A header-only C++20 lexer built on REAL
|
The lexer: maximal-munch tokenization over a set of REAL patterns. More...
#include <algorithm>#include <array>#include <cstddef>#include <cstdint>#include <iterator>#include <limits>#include <map>#include <numeric>#include <memory>#include <optional>#include <ranges>#include <span>#include <stdexcept>#include <string>#include <string_view>#include <type_traits>#include <utility>#include <vector>#include <real/dfa.hpp>#include <real/real.hpp>#include "token.hpp"Classes | |
| struct | scilex::mode_action |
| A mode transition, fired when its rule wins, acting on the scan's mode stack: enter a nested mode, leave the current one, or replace it. More... | |
| struct | scilex::rule |
| 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... | |
| class | scilex::lex_error |
| Thrown when no rule matches at a position (a lexical error). More... | |
| struct | scilex::frame |
| One entry on the per-scan mode stack: the active mode and where it was entered (the entry position feeds the unterminated/diagnostic messages). More... | |
| struct | scilex::indent_state |
| The open blocks of a scan (Indentation), one per scan as its mode stack is. More... | |
| class | scilex::lexer |
| A lexer built from an ordered list of rules. More... | |
| class | scilex::token_iterator |
| Forward (single-pass) iterator yielding one token at a time. More... | |
| class | scilex::token_range |
| A lazy range of tokens, returned by lexer::scan. More... | |
| class | scilex::token_stream |
| Text fed in pieces, lexed as it arrives, with exactly the tokens lexer::tokenize gives the whole text: the same munches, modes, positions and errors. More... | |
Namespaces | |
| namespace | scilex |
| The SciLex public API (scilex::lexer, scilex::rule, scilex::token). | |
Enumerations | |
| enum class | scilex::eof_policy { scilex::omit , scilex::append } |
| Whether tokenization appends a synthetic end-of-input token. More... | |
| enum class | scilex::indent_role : std::uint8_t { scilex::none , scilex::entry , scilex::key , scilex::value , scilex::reset } |
| A rule's part in the indentation the lexer tracks, Layout Awareness Level B (see Indentation). The roles are those of a block language whose collections open at a column: YAML's sequence entries, mapping keys and values, and document markers. More... | |
| enum class | scilex::extension : std::uint8_t { scilex::none , scilex::block , scilex::lines } |
| How a token extends over the lines below its own, bounded by the indentation the lexer tracks (see Indentation). More... | |
| enum class | scilex::dfa_policy : std::uint8_t { scilex::automatic , scilex::requested } |
Which modes a lexer tries to accelerate with a real::dfa. More... | |
| enum class | scilex::error_policy { scilex::raise , scilex::token } |
| What a lexer does when it reaches a byte that no rule in the active mode can begin. More... | |
| enum class | scilex::column_unit { scilex::bytes , scilex::codepoints , scilex::utf16 } |
The unit a token's position::column is counted in. More... | |
Functions | |
| void | scilex::apply_transition (const rule &r, position start, std::vector< frame > &stack) |
Applies rule r's mode transition (if any) to stack — the per-scan mode-stack mutation, kept pure so the lexer and the fuzz oracle share it verbatim. | |
| bool | scilex::takes_part (const rule &r) noexcept |
Whether a token of r takes part in the indentation (a role or an extension). | |
| std::ptrdiff_t | scilex::indent_reference (const rule &r, position start, const indent_state &state) noexcept |
The innermost open block once the token of r at start has updated state (apply_indent), state itself left as it is: the reference the token's extension reads, decided before the update is, as a stream waiting for more text must not have made it. | |
| void | scilex::apply_indent (const rule &r, position start, indent_state &state) |
Updates state for the token of r at start (Indentation): the blocks deeper than its column close (every block for a reset), then an entry opens one at its column, a key is kept as the line's candidate if it is the first since the line began or since an entry or a value, and a value opens one at that candidate's column. Pure, so the lexer and the fuzz oracle share it. | |
| std::ptrdiff_t | scilex::leading_spaces (std::string_view source, std::size_t at) noexcept |
The spaces that begin the text at at of source. | |
| std::size_t | scilex::past_blanks (std::string_view source, std::size_t at) noexcept |
Where the spaces, tabs and carriage returns from at in source end (a loop: a search for a set of bytes tests each byte against each of them). | |
| bool | scilex::blank_between (std::string_view source, std::size_t first, std::size_t last, std::string_view blanks) noexcept |
Whether source holds only bytes of blanks from first to last. | |
| std::optional< std::size_t > | scilex::block_extent (std::string_view source, std::size_t end, std::string_view match, std::ptrdiff_t reference, bool final) |
Where an extension::block token matched up to end of source (match its match) ends, with reference the innermost open block (Indentation); none while the text past source may still change it (final false: a stream's buffer). | |
| std::optional< std::size_t > | scilex::lines_extent (std::string_view source, std::size_t end, const real::regex &continuation, std::ptrdiff_t reference, bool final) |
Where an extension::lines token matched up to end of source ends, each continued line read by continuation (Indentation), with reference the innermost open block; none while the text past source may still change it (final false). | |
Variables | |
| constexpr std::size_t | scilex::max_mode_depth {65536} |
The deepest the mode stack may grow. A push beyond it is a lex_error under either error policy: each frame costs memory, and without a bound an input of openers alone (16 MiB of ( under the python grammar) grew the stack past a gigabyte. 65 536 frames is ~2 MiB, far beyond any nesting a real grammar reaches. | |
The lexer: maximal-munch tokenization over a set of REAL patterns.
SciLex is a thin layer over REAL. Each scilex::rule pairs a token kind with a real::regex; the lexer scans the source left to right, and at each position picks the rule with the longest anchored match (maximal munch), breaking ties by rule order (earlier rules have priority). Because REAL is a linear-time engine, every match is linear in what it scans and ReDoS-safe by construction — no token rule can make the scanner backtrack catastrophically. The scan as a whole is linear wherever a mode's rules run on its DFA (each walk is memoized over the source), and quadratic in the worst case only through a rule left on Pike that scans far past the token that wins at every position (see the spec's complexity section).
Two ways to consume tokens: scilex::lexer::tokenize materializes them all into a vector, while scilex::lexer::scan returns a lazy range that produces one token at a time (the parser-friendly access pattern — no token vector is allocated).