Crate langen

Source
Expand description

§Langen - A tool to create programming languages

use langen::{Grammar, NoErr, Span, Tokens};

#[derive(Tokens, Grammar, Debug)]
#[ignored(r"[ \t]")]
enum Math {
    #[token(r"\(")]
    LParen,

    #[token(r"\)")]
    RParen,

    #[token(r"\+")]
    Plus,

    #[token(r"-")]
    Minus,

    #[token(r"\*")]
    Mul,

    #[token(r"/")]
    Div,

    #[token(r"[0-9]+", |s: &str| {s.parse()})]
    IntLit(i32),

    #[rule(|_span: Span, expr: i32, term: i32| Ok::<i32, NoErr>(expr+term), Expression, Plus, Term)]
    #[rule(|_span: Span, expr: i32, term: i32| Ok::<i32, NoErr>(expr+term), Expression, Minus, Term)]
    #[rule(|_span: Span, term: i32| Ok::<i32, NoErr>(term), Term)]
    #[rule(|_span: Span, term: i32| Ok::<i32, NoErr>(-term), Minus, Factor)]
    Expression(i32),

    #[rule(|_span: Span, term: i32, factor: i32| Ok::<i32, NoErr>(term*factor), Term, Mul, Factor)]
    #[rule(|_span: Span, term: i32, factor: i32| Ok::<i32, NoErr>(term/factor), Term, Div, Factor)]
    #[rule(|_span: Span, factor: i32| Ok::<i32, NoErr>(factor), Factor)]
    Term(i32),

    #[rule(|_span: Span, intlit: i32| Ok::<i32, NoErr>(intlit), IntLit)]
    #[rule(|_span: Span, expr: i32| Ok::<i32, NoErr>(expr), LParen, Expression, RParen)]
    Factor(i32),
}

let input = "(33+5) * (-34)";
let tokens = Math::scan(input).unwrap();
for (t, s) in &tokens {
    println!("{}\t{:?}", s, t);
}
let result = Math::parse(tokens).unwrap();
println!("{:?}", result);

Re-exports§

pub use regex_automata;

Structs§

NoErr
A placeholder type that you can use in the type of process_funcs that can’t error
Span
A range of characters in the input file

Enums§

LexerError
Possible runtime errors during the lexical analysis
ParserError
Possible runtime errors during the syntactical analysis

Traits§

Grammar
Use the Grammar derive macro to generate a parser. See Grammar for more info.
Tokens
Use the Tokens derive macro to generate a lexer. See Tokens for more info.

Derive Macros§

Grammar
Derive macro for Grammar
Tokens
Derive macro for Tokens