-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
indent.rs
48 lines (41 loc) · 1013 Bytes
/
indent.rs
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
use chumsky::prelude::*;
#[derive(Clone, Debug)]
enum Stmt {
Expr,
Loop(Vec<Stmt>),
}
fn parser<'a>() -> impl Parser<'a, &'a str, Vec<Stmt>> {
let expr = just("expr"); // TODO
let block = recursive(|block| {
let indent = just(' ')
.repeated()
.configure(|cfg, parent_indent| cfg.exactly(*parent_indent));
let expr_stmt = expr.then_ignore(text::newline()).to(Stmt::Expr);
let control_flow = just("loop:")
.then(text::newline())
.ignore_then(block)
.map(Stmt::Loop);
let stmt = expr_stmt.or(control_flow);
text::whitespace()
.count()
.then_with_ctx(stmt.separated_by(indent).collect())
});
block.with_ctx(0)
}
fn main() {
let stmts = parser().padded().parse(
r#"
expr
expr
loop:
expr
loop:
expr
expr
expr
expr
"#,
);
println!("{:#?}", stmts.output());
println!("{:?}", stmts.errors().collect::<Vec<_>>());
}