|
SciLex
A header-only C++20 lexer built on REAL
|
The .lex grammar format: rules written as text, parsed into scilex::rule lists.
More...
#include <algorithm>#include <cstddef>#include <fstream>#include <optional>#include <sstream>#include <stdexcept>#include <string>#include <string_view>#include <utility>#include <vector>#include <real/real.hpp>#include "lexer.hpp"Classes | |
| class | scilex::grammar_error |
Thrown for a malformed .lex grammar: where (origin, 1-based line, and 1-based byte column in the line when the cause has one) and why. More... | |
| struct | scilex::grammar |
| A parsed grammar: the rules in order, and each rule's name (index = the rule's kind). More... | |
| struct | scilex::detail::grammar_fields |
| The fields of one rule line, split on tabs, with the byte column each starts at. More... | |
| struct | scilex::detail::predicate_site |
An after= or not_after= option, its names resolved once every rule is read. More... | |
| struct | scilex::detail::line_extras |
| The options of a line that name another line, or make it a fragment, resolved once every line is read. More... | |
| struct | scilex::detail::transition_site |
Where a rule's push= or set= option is written. More... | |
| struct | scilex::detail::fragment |
A fragment line: a named pattern for continue=. More... | |
| struct | scilex::detail::continue_site |
A continue= option, its name resolved once every line is read. More... | |
| struct | scilex::detail::directive_site |
Where a insignificant directive names a mode. More... | |
Namespaces | |
| namespace | scilex |
| The SciLex public API (scilex::lexer, scilex::rule, scilex::token). | |
| namespace | scilex::detail |
Functions | |
| std::string | scilex::detail::quoting (std::string_view before, std::string_view word, std::string_view after) |
before, then word in quotes, then after: a cause naming the offending word. | |
| grammar_fields | scilex::detail::split_grammar_line (std::string_view line, std::size_t first) |
Splits line on tabs, from first (0-based), keeping each field's column. | |
| std::string_view | scilex::detail::checked_mode (std::string_view name, std::string_view word, const std::string &origin, std::size_t line, std::size_t column) |
name, refused when it is empty or holds a comma (which separates the modes of in=). | |
| void | scilex::detail::set_transition (rule &out, mode_action::op operation, std::string_view target, const std::string &origin, std::size_t line, std::size_t column) |
Gives out its transition, refusing a second one. | |
| std::size_t | scilex::detail::apply_grammar_options (std::string_view options, std::size_t column, const std::string &origin, std::size_t line, rule &out, std::size_t index, std::vector< predicate_site > &predicates, line_extras &extras) |
Applies the space-separated options of one rule to out. | |
| void | scilex::detail::check_transition_targets (const grammar &parsed, const std::vector< transition_site > &sites, const std::string &origin) |
Refuses a push= or set= whose mode no rule is active in, at the option. | |
| void | scilex::detail::resolve_predicates (grammar &parsed, const std::vector< predicate_site > &sites, const std::string &origin) |
Gives each rule of sites the kinds its names stand for: every rule bearing the name, and scilex::end_of_input for ^. | |
| void | scilex::detail::resolve_continuations (grammar &parsed, const std::vector< fragment > &fragments, const std::vector< continue_site > &sites, const std::string &origin) |
Gives each rule of sites the pattern its continue= names: the fragment of that name, else the first rule of that name. | |
| void | scilex::detail::parse_directive (std::string_view line, std::size_t first, const std::string &origin, std::size_t number, std::vector< directive_site > &sites) |
Parses the directive line (its % at 0-based first): insignificant m1,m2. | |
| void | scilex::detail::apply_directives (grammar &parsed, const std::vector< directive_site > &sites, const std::string &origin) |
Records the modes of sites in parsed, refusing one no rule is active in, at its column. | |
| grammar | scilex::parse_grammar (std::string_view text, const std::string &origin="<string>") |
Parses a .lex grammar from text (see the file documentation for the format). | |
| grammar | scilex::load_grammar (const std::string &path) |
Reads and parses the .lex grammar at path. | |
The .lex grammar format: rules written as text, parsed into scilex::rule lists.
Optional, and not included by scilex.hpp: the lexer itself takes C++ rule lists, and a program that builds its rules in code needs none of this. It exists so that a grammar kept in a file — the CLI's input, a user-supplied grammar, a Python caller's text — goes through one tested parser.
One rule per line: a name, a tab, the pattern, then optionally a tab and space-separated options. Blank lines and lines whose first non-blank character is # are ignored; a UTF-8 byte order mark that starts the text is dropped, a trailing \r too, and so are the spaces and tabs that end a line, so a pattern never ends in one: write a final space [ ] or \x20. The options are:
skip — matches are consumed but not emitted;in=m1,m2 — the modes the rule is active in (default: default only);push=m, set=m, pop — the mode transition fired when the rule wins (at most one); a mode that push= or set= enters must have a rule active in it;after=A,B or not_after=A,B — the rule is active only right after (or except right after) a token of the rules named A or B (scilex::rule::after): a name stands for every rule that bears it, earlier or later in the file, and ^ for the start of the input;line_start or not_line_start — the rule is active only at the start of a line (or only past it; scilex::rule::at_line_start);indent=entry, indent=key, indent=value or indent=reset — the rule's part in the indentation the lexer tracks, and extend=block or extend=lines — how its token extends below its line (Layout Awareness Level B, Indentation);continue=N — with extend=lines, what each continued line must hold: the pattern of the fragment (or else the first rule) named N, earlier or later in the file;fragment — the line names a pattern for continue=, not a rule: it takes no other option, has no kind, is not lexed, and shares its name with no other fragment.A line gives each option once.
A line whose first non-blank character is % is a directive, not a rule; the one directive is
insignificant m1,m2 — the modes whose tokens carry no layout structure (the lexer's insignificant_modes); each must have a rule active in it, and the directive may repeat.A rule's kind is its 0-based position among the rules, fragments not counted; scilex::grammar::names maps it back.