2 stable releases

2.0.0 Nov 18, 2024
1.0.0 Mar 3, 2023

#1602 in Procedural macros

Download history 7/week @ 2024-09-20 1/week @ 2024-09-27 2/week @ 2024-10-11 130/week @ 2024-11-15 25/week @ 2024-11-22 5/week @ 2024-11-29 14/week @ 2024-12-06

174 downloads per month
Used in langen

MIT license

42KB
865 lines

Langen

A tool to create programming languages

Please depend on langen instead of langen_macro for this to work correctly!

The crate provides a set of derive macros to generate a lexer and parser. All the heavy computations are done during compilation.

Look at the docs for more information on how to use this.

Usage

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);

License

This project is licensed under the MIT license

Dependencies

~1.4–2.4MB
~35K SLoC