use std::sync::Arc;
use swiftlet::grammar::Algorithm;
use swiftlet::{ParserConfig, Swiftlet};

fn main() {
    let grammar = r#"
        start: expr
        expr: expr "+" INT -> add
            | expr "-" INT -> sub
            | INT
        %import (WS, INT)
        %ignore WS
        "#;

    let text = "3 + 10 - 5 + 20";

    let conf = Arc::new(ParserConfig {
        algorithm: Algorithm::Earley,
        ..Default::default()
    });
    let parser = Swiftlet::from_str(grammar)
        .map(|grammar| grammar.parser(conf))
        .expect("failed to build parser");
    let parsed_text = parser.parse(&text);

    match parsed_text {
        Ok(tree) => {
            println!("AST");
            tree.pretty_print();
            /*
            start
              add
                sub
                  add
                    expr  3
                    +
                    10
                  -
                  5
                +
                20
             */
        }
        Err(e) => {
            println!("Error: {}", e);
        }
    }
}
