Module traits

Module traits 

Source
Expand description

Core traits for the synkit parsing framework.

This module defines the fundamental traits that enable synkit’s parsing capabilities. Implementations of these traits work together to provide a flexible, type-safe parsing infrastructure.

§Trait Hierarchy

TokenStream (stream interface)
    ├── parse::<T>() where T: Parse
    └── peek::<T>() where T: Peek

SpanLike (position tracking)
    └── SpannedLike<T> (value + span)

ToTokens (code generation)
    └── Printer (formatting)

Diagnostic (error reporting)
    └── SpannedError (error + span)

§Usage Patterns

§Parsing

use synkit::{TokenStream, Parse};

fn parse_expression(stream: &mut impl TokenStream) -> Result<Expr, Error> {
    // Peek to decide which production to use
    if stream.peek::<NumberToken>() {
        let num = stream.parse::<NumberLiteral>()?;
        Ok(Expr::Number(num))
    } else if stream.peek::<IdentToken>() {
        let ident = stream.parse::<Identifier>()?;
        Ok(Expr::Ident(ident))
    } else {
        Err(Error::unexpected_token())
    }
}

§Code Generation

use synkit::{ToTokens, Printer};

impl ToTokens for MyExpr {
    fn to_tokens(&self, printer: &mut impl Printer) {
        match self {
            MyExpr::Number(n) => printer.write(&n.to_string()),
            MyExpr::Ident(i) => printer.write(&i.name),
        }
    }
}

§Feature Flags

  • std: Enables std::error::Error implementations
  • serde: Enables serialization for span types

Traits§

Diagnostic
Diagnostic formatting for error messages.
Parse
Trait for types that can be parsed from a token stream.
Peek
Trait for lookahead without consuming tokens.
Printer
Trait for building formatted text output.
SpanLike
A span representing a source location range.
SpannedError
Error that can have a span attached.
SpannedLike
A value paired with its source location span.
ToTokens
Trait for converting AST nodes back to text.
TokenStream
A stream of tokens for parsing.