Skip to content

Commit 2269812

Browse files
committed
lite refactoring
1 parent 7e3415f commit 2269812

File tree

3 files changed

+102
-85
lines changed

3 files changed

+102
-85
lines changed

rust/rustell/src/lib.rs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,98 @@ use chumsky::text::{Char, Padded};
77

88
#[derive(Eq, PartialEq, Debug, Clone)]
99
pub enum Expr<'src> {
10-
Use(UseExpr<'src>),
10+
Use(ExprUse<'src>),
1111
Other(&'src str),
1212
}
1313

1414
#[derive(Eq, PartialEq, Debug, Clone)]
15-
pub enum UseExpr<'src> {
15+
pub enum ExprUse<'src> {
1616
Item {
1717
module: &'src str,
1818
rename: Option<&'src str>,
19-
nested: Option<Box<UseExpr<'src>>>,
19+
nested: Option<Box<ExprUse<'src>>>,
2020
},
21-
Many(Vec<UseExpr<'src>>),
21+
Many(Vec<ExprUse<'src>>),
2222
Glob,
2323
}
2424

25-
pub fn parser<'src>() -> impl Parser<
25+
pub fn expr<'src>() -> impl Parser<
2626
'src,
2727
&'src str,
2828
Vec<Expr<'src>>,
2929
extra::Err<Rich<'src, char>>,
3030
> {
31-
let use_expr = || {
32-
lexeme("use")
33-
.ignore_then(use_parser())
34-
.then_ignore(lexeme(";").or_not())
35-
.map(Expr::Use)
36-
};
37-
38-
let other = use_expr()
39-
.not()
40-
.ignore_then(any())
31+
expr_use()
32+
.or(expr_other())
4133
.repeated()
42-
.at_least(1)
43-
.to_slice()
44-
.map(Expr::Other);
34+
.collect::<Vec<_>>()
35+
}
4536

46-
use_expr().or(other).repeated().collect::<Vec<_>>()
37+
fn expr_use<'src>() -> impl Parser<
38+
'src,
39+
&'src str,
40+
Expr<'src>,
41+
extra::Err<Rich<'src, char>>,
42+
> {
43+
lexeme("use")
44+
.ignore_then(expr_use_rec())
45+
.then_ignore(lexeme(";").or_not())
46+
.map(Expr::Use)
4747
}
4848

49-
fn use_parser<'src>() -> impl Parser<
49+
fn expr_use_rec<'src>() -> impl Parser<
5050
'src,
5151
&'src str,
52-
UseExpr<'src>,
52+
ExprUse<'src>,
5353
extra::Err<Rich<'src, char>>,
5454
> {
55-
recursive(|use_parser| {
55+
recursive(|expr_use| {
5656
let ident = || text::ascii::ident().padded();
5757
let item = ident()
5858
.then(
5959
lexeme("as").ignore_then(ident()).or_not(),
6060
)
6161
.then(
6262
lexeme("::")
63-
.ignore_then(use_parser.clone())
63+
.ignore_then(expr_use.clone())
6464
.or_not(),
6565
)
6666
.map(|((module, rename), nested)| {
67-
UseExpr::Item {
67+
ExprUse::Item {
6868
module,
6969
rename,
7070
nested: nested.map(Box::new),
7171
}
7272
});
7373

74-
let many = use_parser
74+
let many = expr_use
7575
.separated_by(lexeme(','))
7676
.allow_trailing()
7777
.collect::<Vec<_>>()
7878
.delimited_by(lexeme('{'), lexeme('}'))
79-
.map(UseExpr::Many);
79+
.map(ExprUse::Many);
8080

81-
let glob = lexeme("*").map(|_| UseExpr::Glob);
81+
let glob = lexeme("*").map(|_| ExprUse::Glob);
8282

8383
item.or(many).or(glob)
8484
})
8585
}
8686

87+
fn expr_other<'src>() -> impl Parser<
88+
'src,
89+
&'src str,
90+
Expr<'src>,
91+
extra::Err<Rich<'src, char>>,
92+
> {
93+
expr_use()
94+
.not()
95+
.ignore_then(any())
96+
.repeated()
97+
.at_least(1)
98+
.to_slice()
99+
.map(Expr::Other)
100+
}
101+
87102
fn lexeme<'src, T, I, E>(seq: T) -> Padded<Just<T, I, E>>
88103
where
89104
I: Input<'src>,

rust/rustell/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ fn main() {
66
io::stdin()
77
.read_to_string(&mut src)
88
.expect("Failed to read stdin");
9-
match parser().parse(src.trim()).into_result() {
9+
match expr().parse(src.trim()).into_result() {
1010
Ok(ast) => println!("{:#?}", ast),
11-
Err(errs) => errs.into_iter().for_each(|e| println!("{e:?}")),
11+
Err(errs) => {
12+
errs.into_iter().for_each(|e| println!("{e:?}"))
13+
}
1214
};
1315
}

0 commit comments

Comments
 (0)