sipha-core 0.2.0

Core foundation types and traits for sipha - minimal dependencies, no parsing logic
Documentation

sipha-core

License: MIT Repository Crates.io docs.rs

Core parsing infrastructure for sipha - provides grammar-based parsing, CST construction, and error handling.

Overview

sipha-core is the foundation of the sipha parser ecosystem. It provides:

  • Grammar-based parsing with support for complex grammars
  • Pratt parsing for operator precedence handling
  • Concrete Syntax Tree (CST) construction preserving all syntax information
  • Memoization for efficient parsing
  • Error recovery with sync tokens
  • Rich error diagnostics with source code context
  • Visitor pattern for tree traversal
  • CST rewriting capabilities

Quick Start

Add sipha-core to your Cargo.toml:

[dependencies]
sipha-core = "0.1.1"

With optional features:

[dependencies]
sipha-core = { version = "0.1.1", features = ["serde", "color", "diagnostics"] }

Features

  • default: Includes serde, color, and diagnostics features
  • serde: Enables serialization of grammar rules and CST nodes
  • color: Adds color support for error output
  • diagnostics: Enables rich error diagnostics with miette integration

Example

use sipha_core::{
    cst::{NodeArena, RawNodeId},
    helpers,
    parser::Parser,
    span::Span,
    state::ParserState,
    token::Token,
    traits::TokenKind,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum MyToken {
    Ident,
    Plus,
}

impl TokenKind for MyToken {
    fn is_trivia(&self) -> bool {
        false
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum MyRule {
    Expr,
}

let mut parser = Parser::create();
parser.register_rule(
    MyRule::Expr,
    helpers::seq(vec![
        helpers::token(MyToken::Ident),
        helpers::token(MyToken::Plus),
        helpers::token(MyToken::Ident),
    ]),
);

let tokens = vec![
    Token::create(MyToken::Ident, Span::new(0..1), "a", Vec::new(), Vec::new()),
    Token::create(MyToken::Plus, Span::new(1..2), "+", Vec::new(), Vec::new()),
    Token::create(MyToken::Ident, Span::new(2..3), "b", Vec::new(), Vec::new()),
];

let mut arena: NodeArena<MyToken, MyRule, RawNodeId> = NodeArena::new();
let mut state = ParserState::new(&tokens, &mut arena, false, ());

match parser.parse_rule(MyRule::Expr, &mut state) {
    Ok(_) => println!("Parsed successfully!"),
    Err(e) => eprintln!("Parse error: {:?}", e),
}

Documentation

For more information, see:

License

This project is licensed under the MIT License - see the LICENSE file for details.

See Also