Skip to content
al8nPublic

About

Blazing fast parser combinators with parse-while-lexing architecture (zero-copy), deterministic LALR-style parsing, and no hidden backtracking.

Resources

Stars

13 stars

Watchers

0 watching

Forks

Repository files navigation

Tokora

Deterministic parser combinators, with on-demand lexing, LALR-style dispatch, explicit backtracking, and configurable diagnostics.

github Build codecov

docs.rs crates.io crates.io

book Discord license

Introduction

Tokora is a parser-combinator library for Rust that lexes while it parses. A grammar is ordinary Rust: parser functions and combinators over an input handle that pulls each token from your lexer only when the parser asks for it. Choices are deterministic: a fixed lookahead window or a table of token kinds selects exactly one branch. Backtracking is explicit and transactional, so an abandoned branch leaves neither consumed input nor diagnostics behind. The same parser functions fail fast in a runtime or collect every diagnostic for a compiler or an editor, depending on the emitter they run with. Tokora builds for no_std without an allocator, lexes through a bundled Logos adapter or any lexer you write, and can build lossless syntax trees on Rowan.

Install

[dependencies]
tokora = { version = "0.13", features = ["logos"] }

The default features are std and the combinators umbrella. logos adds LogosLexer, the adapter for Logos-derived token enums, and re-exports Logos as tokora::logos (pinned: tokora::logos_0_16), so a derive can name it with #[logos(crate = tokora::logos)]. Features lists every flag, and Platform support covers no_std.

Quick start

A complete program: a Logos token enum, the Token impl that connects it to Tokora, and a parser written as a plain function. It needs the logos feature.

use tokora::{
  Emitter, InputRef, ParseContext, Token,
  lexer::LogosLexer,
  logos::{self, Logos},
  parse,
};

// The token vocabulary. It carries no payloads, so it serves as its own kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Logos)]
#[logos(crate = logos, skip r"[ \t\r\n]+")]
enum Tok {
  #[regex(r"[0-9]+")]
  Int,
  #[token("+")]
  Plus,
}

// Diagnostics print a kind through `Display`.
impl core::fmt::Display for Tok {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    f.write_str(match self {
      Tok::Int => "integer",
      Tok::Plus => "`+`",
    })
  }
}

impl Token<'_> for Tok {
  type Kind = Tok;
  type Error = (); // the Logos lexer's default error
  // A claim about how far past a token the lexer reads; only partial input consults it.
  const SCAN_LOOKAHEAD: tokora::ScanLookahead = tokora::ScanLookahead::Unbounded;

  fn kind(&self) -> Tok {
    *self
  }

  fn is_trivia(&self) -> bool {
    false
  }
}

type SumLexer<'a> = LogosLexer<'a, Tok>;

// A parser is a function over `InputRef`. It is generic over the parse context, so the caller
// picks the emitter; `()` is the error type to keep the example short.
fn sum<'inp, Ctx>(inp: &mut InputRef<'inp, '_, SumLexer<'inp>, Ctx>) -> Result<u64, ()>
where
  Ctx: ParseContext<'inp, SumLexer<'inp>>,
  Ctx::Emitter: Emitter<'inp, SumLexer<'inp>, Error = ()>,
{
  let mut total = 0;
  loop {
    // `try_expect` consumes the next token only if it matches; `slice` borrows its text.
    if inp.try_expect(|tok| matches!(tok.data(), Tok::Int))?.is_none() {
      return Err(());
    }
    total += inp.slice().parse::<u64>().map_err(|_| ())?;
    if inp.try_expect(|tok| matches!(tok.data(), Tok::Plus))?.is_none() {
      break;
    }
  }
  // `next` pulls one more token: anything after the sum is an error.
  match inp.next()? {
    None => Ok(total),
    Some(_) => Err(()),
  }
}

let total: Result<u64, ()> = parse(sum, "1 + 2 + 39");
assert_eq!(total, Ok(42));

let unfinished: Result<u64, ()> = parse(sum, "1 +");
assert_eq!(unfinished, Err(()));

parse runs a parser with the fail-fast Fatal emitter. parse_with takes a context you supply, such as a collecting emitter or a configured cache, and Parser configures one before the source exists. A real grammar defines its own error type and implements From for the structured errors it keeps; chapter 2 of the guide lists them, and the guide's tutorial builds a complete calculator from the same pieces.

What makes Tokora different

  • Parse while lexing. There is no token vector: tokens are lexed as the parser asks for them, and a bounded cache holds only what lookahead or backtracking needs, with a lookahead window of 1 to 32 tokens fixed at compile time.
  • Deterministic choice with exact expectations. A kind dispatch that matches nothing reports its whole table as "expected one of …".
  • Transactional backtracking. Speculation is opt-in and visible in the code, and a rollback undoes both the input the abandoned branch consumed and the diagnostics it emitted.
  • One grammar, any error policy. Parsers are generic over their parse context, and so over its emitter: fail fast, collect everything, stay silent, or implement only the emitter capabilities a grammar uses.
  • Recovery and partial input. Recovery is explicit, with nesting-aware resynchronization, and a parse over a buffer that is still growing reports Incomplete for an unfinished construct rather than an error.
  • Bounded recursion. Nesting past a configurable budget fails with a catchable error instead of overflowing the stack.
  • Pratt expressions through two drivers, token-level and AST-level, that share one precedence vocabulary.
  • Lossless syntax trees (feature rowan), built over the same backtracking, so a rollback also rewinds the half-built tree.
  • no_std without an allocator, down to core-only parsing over bounded caches and containers.
  • Tooling: a conformance kit for custom lexers, a deterministic fuzz harness for the backtracking machinery, and parse tracing that compiles away when it is off.

How Tokora parses

A grammar is ordinary Rust. Parser functions and combinators read through InputRef, which drives the Lexer and stages tokens in its cache when lookahead or backtracking needs them. peek_then_choice decides from a fixed lookahead window; dispatch_on_kind and fused_dispatch_on_kind route the next token's Token::Kind to exactly one branch, and the fused form hands the token it classified straight to that branch instead of round-tripping it through the cache.

That token-kind dispatch is what "LALR-style" means here, and it is local to a hand-written combinator grammar: Tokora does not accept an LALR grammar, generate LALR parse tables, or act as a parser generator.

Speculation is explicit. InputRef::attempt and InputRef::try_attempt keep a successful branch and roll back a declined or failed one, and input::Transaction exposes commit and rollback directly. A rollback restores the input position, span, lexer state, token cache, and the diagnostics emitted since the checkpoint. Side effects in your own code need their own transaction boundary.

Diagnostics and recovery

Parsers are generic over their parse context, which carries the emitter. parse and Parser::new() use Fatal, which turns the first diagnostic into the parse's error. emitter::Verbose (std or alloc) records every diagnostic together with its open labelled context, or the first n when it is built with Verbose::with_max_diagnostics(n), counting the errors it refuses past them, and lets a recovering grammar continue; Silent and Ignored discard diagnostics, keeping or erasing the error type. Structured lexer, token, separator, container, and Pratt errors convert into the application's error type through From implementations.

Recovery is explicit. recover rewinds to where the failed parser started and runs a recovery parser; inplace_recover continues from the failure position. sync_balanced and skip_then_retry skip to a synchronization point without crossing unbalanced delimiters, and Verbose records each non-empty skipped region once, alongside the other diagnostics. An Incomplete error is re-raised rather than recovered, so unfinished partial input is never discarded.

Recursion budget

Every InputRef::descend or InputRef::descending a grammar takes, and every frame of Tokora's two Pratt engines, draws on one shared budget. Input nested past it fails the parse with a catchable error::RecursionLimitReached instead of exhausting the native stack.

The default, RecursionLimiter::PARSE_DEFAULT_DEPTH, is 32 in every build profile. A release frame is roughly an order of magnitude cheaper than a debug one, but Tokora cannot see how the frames it bounds were compiled: debug_assertions is not opt-level, and it is per crate, while the recursion is spent in your own productions. Both profiles are therefore priced at the debug frame cost, and no profile combination can abort. The depth a fully optimised parse supports is published as RecursionLimiter::OPTIMIZED_PARSE_DEPTH; nothing installs it, and taking it is one call:

use tokora::state::RecursionLimiter;

// The default budget, the same in every build profile.
assert_eq!(RecursionLimiter::PARSE_DEFAULT_DEPTH, 32);

// The depth a fully optimised parse supports. Nothing installs it; a caller does.
assert_eq!(RecursionLimiter::OPTIMIZED_PARSE_DEPTH, 256);

let limiter = RecursionLimiter::with_limitation(RecursionLimiter::OPTIMIZED_PARSE_DEPTH);
assert_eq!(limiter.limitation(), 256);

Hand the limiter to ParserContext::with_recursion_limiter, or to input::InputContext::with_recursion_limiter for a driver that builds its own input context. Pass 256 only when every frame the budget bounds, Tokora's and your own productions alike, is compiled at opt-level = 3; under a per-package profile override, or a debug consumer against a release Tokora, the number that applies is the default. RecursionLimiter::unlimited() opts out for a grammar whose depth is bounded by something other than its input. With the stacker feature, RecursionLimiter::SEGMENTED_PRATT_DEPTH (1024) is the budget for a descent made entirely of Pratt frames; What stacker segments explains why it is a separate constant.

Module map

The crate root holds exactly the names a production needs in scope: the parsing vocabulary (ParseInput, TryParseInput, ParseAttempt, Accumulator, ParseChoice, ParseTokenChoice, Action), the parse context (ParseContext, ComposableParseContext, ParserContext, FatalContext, ErrorOf), the drivers (Parse, Parser, parse, parse_with, parse_with_state, parse_partial), the input and lexer vocabulary (InputRef, Lexer, ScanLookahead, ReadFrontier, Token, Source, Slice, Span, SimpleSpan), and the emitter (Emitter, EmitterView). Beside them sit the attempt!, keyword!, punctuator!, select!, syntax_kinds! and try_select! macros, and the logos / logos_0_16 and rowan / rowan_0_17 re-exports. Each of those names also lives in the module of its concept, and everything else is reached through its module. There is no prelude: a file imports what it uses.

Module Holds
parse The parsing traits and the types their contracts name, the parse-context vocabulary, and the drivers.
combinator Combinator constructors, the parser types they return, and the values they take: expect, delimited, node, labelled, Any, Recover, and the rest. No trait lives here.
pratt Pratt expression parsing: the typed pratt driver, binding powers, fold traits, and CST hooks. Feature pratt.
input InputRef, transactions and savepoints, the Complete / Partial typestate, and partial-input sessions.
lexer The Lexer contract, Lexed, and the LogosLexer adapter (feature logos).
token The Token trait and token capability traits such as KeywordToken and PrattToken.
emitter Emitter, its atomic capability traits, and the Fatal, Verbose, Silent and Ignored policies.
error Span-carrying structured errors: unexpected tokens and ends, missing syntax, delimiters, malformed lexemes, recursion limits.
diagnostic Diagnose, a renderer-neutral contract (code, severity, locations, labels, help) for an application's own errors.
cache Bounded token caches for lookahead and backtracking.
state Lexer state and limits: State, RecursionLimiter, TokenLimiter.
span Span, SimpleSpan, Spanned, Located.
source Source, the input a lexer reads: str, [u8], and the optional backends.
slice Slice, a zero-copy view of a source.
container Container, the collection a repetition fills: Vec, VecDeque, ArrayDeque, Option, and the optional backends.
delimiter Delimiter and TypedDelimiter for open/close pairs, and DelimiterKind.
punct Punctuator markers such as Comma and Semicolon, and the Punctuator trait.
dialect Dialect, one anchor type per language that names its lexer, so productions keep short where clauses.
types Reusable AST building blocks: identifiers, keywords, literals, and recovery status.
syntax Syntax, for syntax elements with a known set of components so every missing part can be reported, and the AstNode and Language traits.
cst Concrete syntax trees: the event vocabulary in every build, and the recording Sink and typed lossless trees with rowan.
utils Helpers: array deques and type-level numbers, display adapters, expected sets and messages.
conformance Test kits for custom lexers, caches and emitters. Feature conformance.
fuzz A deterministic fuzz harness for the input and backtracking machinery. Feature fuzz.
guide The Tokora Guide, rendered by rustdoc. Features std, logos and combinators.

Guide and examples

The Tokora Guide is a book in five parts, also published as the guide module on docs.rs:

  • Part I introduces Tokora and Calc, the small calculator language the tutorial builds.
  • Part II is the tutorial, ten chapters that build Calc end to end: tokens and the lexer, first parsers, composition, deterministic choice, Pratt expressions, backtracking, diagnostics, recovery, partial input, and testing.
  • Part III is the internals, for a reader who wants to know why an API has its shape: the parse-while-lexing engine, checkpoints and the LIFO rewind contract, the atomic emitter, the event-stream CST engine, and the Source / Slice storage backends.
  • Part IV applies it: the anatomy of a real parser, a custom-lexer recipe, walkthroughs of the four examples below, and lossless CSTs with Rowan.
  • Part V is the reference: combinators and atoms; errors, emitters and context; vocabulary, macros and feature flags; Pratt; types and syntax building blocks.

The examples are canonical complete programs, and the guide links to them rather than copying them into prose.

Program Focus Canonical source Run
calculator Token-level Pratt evaluator calculator.rs cargo run -p tokora --example calculator --features logos
s_expression Recursive descent and evaluation s_expression.rs cargo run -p tokora --example s_expression --features logos
json Borrowed values, delimiters, and tentative choice json.rs cargo run -p tokora --example json --features logos
c_expression AST-level Pratt parsing with postfix forms c_expression.rs cargo run -p tokora --example c_expression --features logos

Each has a lossless-CST twin (calculator_cst, s_expression_cst, json_cst, c_expression_cst), and expr_recovery parses malformed input to completion, producing an AST and a lossless tree whose holes are explicit error nodes. Those five need --features logos,rowan. The book source lives under tokora/src/guide, and all nine examples compile and run their tests with cargo test -p tokora --no-default-features --features std,logos,rowan,combinators --examples.

Features

An integration feature is the crate's own name plus its version, so a hyphenated crate keeps its hyphen (smol-bytes_0_2), and a crate without one is just the name and version (rowan_0_17). The unversioned name is an alias that selects the supported version, or the newest where two are supported, and a versioned feature enables the matching optional dependency.

Library tiers

Feature Effect
default Enables std and combinators.
std Enables standard-library support and the default features of applicable dependencies.
alloc Enables allocator-backed facilities without std, such as the Verbose emitter, Vec-collecting repetition, and stacked savepoints. Token caches stay bounded.

Combinator families

combinators turns on all thirteen families below. Each family is also its own feature, so a default-features = false build compiles only the combinators its grammar calls. What the families build on is unconditional: Parser, Parse and the parse drivers, the ParseInput, TryParseInput and ParseChoice traits, and the substrate combinators (expect, delimited, recover, select, opt, padded, node, labelled, and the rest).

Feature Effect
combinators Umbrella for every combinator family below. On by default.
any Any: accept one token of any kind, with its spanned, sliced and located shapes.
fail fail / fail_with.
filter filter, filter_with, filter_map, filter_map_with.
fold The fold drivers (fold_while, try_fold*, rfold*); implies many.
ident Ident::parse / try_parse and their _except twins.
keyword Keyword::parse / try_parse and their _exact / _sliced twins.
many The repetition family: repeated*, separated*, delim*, the delimiter handlers, the cardinality bounds, list and separated1.
map map / map_with.
peek peek_then*, peek_then_choice, peek_kind, dispatch_on_kind and its fused twin.
pratt Pratt expressions: the tokora::pratt module with the typed pratt driver, the token-level InputRef::pratt*, PrattToken, and the PrattEmitter channel.
punct The punctuator parsers (Comma::parse, …) and the parens / braces / brackets / angles delimited shapes built on them.
then then, then_ignore, ignore_then, then_value, and_then, and_then_with.
validate validate / validate_with.

Integrations

Feature Effect
logos Alias for logos_0_16.
logos_0_16 Enables the LogosLexer adapter over logos@0.16, the only supported Logos major, and the tokora::logos / tokora::logos_0_16 re-exports.
rowan Alias for rowan_0_17.
rowan_0_17 Enables the recording CST sink and the typed lossless tree over rowan@0.17, and the tokora::rowan / tokora::rowan_0_17 re-exports; implies std, not logos. Read the safety disclosure before enabling it.
bytes Alias for bytes_1.
bytes_1 Enables bytes::Bytes as a Source and Slice.
bstr Alias for bstr_1.
bstr_1 Enables bstr::BStr as a Source and Slice.
hipstr Alias for hipstr_0_8.
hipstr_0_8 Enables HipStr and HipByt from hipstr@0.8 as sources and slices.
smol-bytes Alias for smol-bytes_0_2.
smol-bytes_0_1 Enables the smol-bytes@0.1 types (shared::Bytes, compact::Bytes, Utf8Bytes, compact::Utf8Bytes) as sources and slices; requires smol-bytes ≥ 0.1.2.
smol-bytes_0_2 Enables the same four types from smol-bytes@0.2. Both versions can be enabled together.
smallvec Alias for smallvec_1.
smallvec_1 Enables SmallVec as a Container in a build with alloc or std, and turns on neither.
heapless Alias for heapless_0_9.
heapless_0_9 Enables heapless::Vec and heapless::Deque as containers.
tinyvec Alias for tinyvec_1.
tinyvec_1 Enables ArrayVec and SliceVec as containers, and TinyVec with alloc.

Tooling

Feature Effect
stacker Runs each Pratt frame prologue on a fresh heap stack segment when the native stack is nearly exhausted; implies std and pratt. It does not raise the recursion budget; see What stacker segments.
trace Turns combinator::traced into a wrapper that prints an indented enter, exit, and rollback transcript to stderr, and instruments Tokora's own combinators; implies std. Without it, traced is the identity.
unstable-raw Exposes the unstable raw checkpoint API: InputRef::save, restore and commit, and Checkpoint construction.
conformance Enables the tokora::conformance test kits for custom lexers, caches and emitters; implies alloc.
fuzz Enables the tokora::fuzz deterministic operation-script harness for the input and backtracking machinery; implies std.

What stacker segments

stacker puts a fresh heap stack segment under the frame prologue of Tokora's two Pratt engines, and under nothing else. Your own descend / descending frames are ordinary native frames and are untouched, so the feature does not move RecursionLimiter::PARSE_DEFAULT_DEPTH, the budget they share. RecursionLimiter::SEGMENTED_PRATT_DEPTH is the larger figure it does justify, for a caller whose whole descent is Pratt frames to opt into.

It is not a substitute for the recursion budget: a segment is an mmap, so a deep enough input still ends the process with nothing on any Result channel.

rowan and known upstream undefined behaviour

A lossless sink requires a lexer that surfaces trivia (Lexer::SURFACES_TRIVIA). Tokora re-exports Rowan as tokora::rowan (pinned: tokora::rowan_0_17), so implement tokora::rowan::Language without a rowan dependency of your own, and the tree types you name are the ones the cst module is built over.

Platform support

Tokora's MSRV is Rust 1.95. The core supports allocator-free no_std (no_std without alloc) and allocation-enabled no_std (no_std with alloc). Disable default features for core-only parsing over bounded caches and containers, and enable alloc when a parser, container or selected facility needs allocation. rowan_0_17 (and so its rowan alias), stacker, trace and fuzz require std; conformance requires alloc. smallvec turns on no tier, and its impls need alloc or std beside it.

Allocator-free no_std:

[dependencies]
tokora = { version = "0.13", default-features = false }

no_std with alloc:

[dependencies]
tokora = { version = "0.13", default-features = false, features = ["alloc"] }

Neither line enables a combinator family, because combinators is a default feature and both turn the defaults off. Add "combinators" for the umbrella, or list the families the grammar calls, as in features = ["alloc", "many", "map"].

CI checks both no_std builds on thumbv6m-none-eabi, and builds the default features for Linux, Android, Windows (GNU), and WebAssembly targets. bytes, hipstr and smol-bytes share buffers through atomic compare-and-swap, so they do not build on a target without it, such as thumbv6m-none-eabi; bstr does, and CI checks alloc,bstr_1 there.

Design notes

Priorities

  1. Performance: pull tokens from the lexer on demand, and offer fused dispatch where avoiding a peek-and-cache round trip matters.
  2. Predictability: prefer deterministic lookahead and token-kind dispatch; make speculation explicit and transactional.
  3. Composability: combine small parser functions and combinators; compose focused emitter traits into custom diagnostic strategies.
  4. Versatility: run the same parser functions with fail-fast, collecting, silent, or custom emitters.
  5. Flexibility: work through the generic Lexer and Token traits, with optional Logos input and Rowan CST integrations.
  6. Correctness: track spans and structured errors, rewind emitted diagnostics with parser rollbacks, and provide conformance and fuzz test kits.

Inspirations

Tokora takes inspiration from:

  • winnow, for ergonomic parser API design
  • chumsky, for composable parser-combinator patterns
  • logos, for high-performance lexing
  • rowan, for lossless syntax tree representation

Development

Useful repository checks:

cargo fmt --all --check
cargo test -p tokora --all-features
cargo test -p tokora --no-default-features --features std,logos,rowan,combinators --examples
RUSTDOCFLAGS="-D warnings" cargo test -p tokora --all-features --doc
python3 tokora/tools/validate_docs.py --source
(cd tokora && mdbook build)
python3 tokora/tools/validate_docs.py --book target/book

This README is the crate-root documentation, so its Rust blocks are doctests as well. The guide is validated both as rustdoc and as an mdBook, built with the mdBook version pages.yml pins, so API links, local links, chapter order, and the published pages stay aligned. The full pre-push gate set, and what each line in it protects, is the header of .github/workflows/ci.yml.

License

tokora is under the terms of both the MIT license and the Apache License (Version 2.0).

See the Apache License, Version 2.0 and the MIT license text for details.

Copyright (c) 2026 Al Liu.

About

Blazing fast parser combinators with parse-while-lexing architecture (zero-copy), deterministic LALR-style parsing, and no hidden backtracking.

Resources

Stars

13 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages

Generated from al8n/template-rs