summaryrefslogtreecommitdiffstats
path: root/crates/rebel-parse/examples/parse-string.rs
blob: 190d0fa32972a877e8a2d3528cd6f861bf447298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::{fmt::Debug, process, time::Instant};

use clap::{Parser, ValueEnum};

use rebel_parse::{recipe, tokenize};

#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)]
enum Rule {
	Tokenize,
	Recipe,
	RecipeStmt,
	Body,
	BodyStmt,
	Expr,
}

#[derive(Clone, Debug, Parser)]
struct Opts {
	rule: Rule,
	input: String,
}

fn main() {
	let opts: Opts = Opts::parse();
	let input = opts.input.trim();

	fn as_debug<'a>(v: impl Debug + 'a) -> Box<dyn Debug + 'a> {
		Box::new(v)
	}

	let start = Instant::now();
	let result = tokenize::token_stream(input);
	let dur = Instant::now().duration_since(start);
	println!("Tokenization took {} µs", dur.as_micros());

	let tokens = match result {
		Ok(value) => value,
		Err(err) => {
			println!("{err}");
			process::exit(1);
		}
	};

	let start = Instant::now();
	let result = match opts.rule {
		Rule::Tokenize => Ok(as_debug(tokens)),
		Rule::Recipe => recipe::recipe(&tokens).map(as_debug),
		Rule::RecipeStmt => recipe::recipe_stmt(&tokens).map(as_debug),
		Rule::Body => recipe::body(&tokens).map(as_debug),
		Rule::BodyStmt => recipe::body_stmt(&tokens).map(as_debug),
		Rule::Expr => recipe::expr(&tokens).map(as_debug),
	};
	if opts.rule != Rule::Tokenize {
		let dur = Instant::now().duration_since(start);
		println!("Parsing took {} µs", dur.as_micros());
	}

	match result {
		Ok(value) => {
			println!("{value:#?}");
		}
		Err(err) => {
			println!("{err}");
		}
	};
}