Skip to content

Commit f72da47

Browse files
committed
rustell lite refactoring
1 parent fadcada commit f72da47

File tree

4 files changed

+166
-161
lines changed

4 files changed

+166
-161
lines changed

rust/rustell/src/decode.rs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
use super::*;
2+
use chumsky::prelude::Parser;
3+
use chumsky::prelude::*;
4+
use chumsky::text::whitespace;
5+
6+
pub fn expr<'src>() -> impl Parser<
7+
'src,
8+
&'src str,
9+
Vec<Expr<'src>>,
10+
extra::Err<Rich<'src, char>>,
11+
> {
12+
expr_use()
13+
.or(expr_other())
14+
.repeated()
15+
.collect::<Vec<_>>()
16+
}
17+
18+
fn expr_use<'src>() -> impl Parser<
19+
'src,
20+
&'src str,
21+
Expr<'src>,
22+
extra::Err<Rich<'src, char>>,
23+
> {
24+
just("use")
25+
.ignore_then(expr_use_rec())
26+
.then_ignore(lexeme(";").or_not())
27+
.map(Expr::Use)
28+
}
29+
30+
fn expr_use_rec<'src>() -> impl Parser<
31+
'src,
32+
&'src str,
33+
ExprUse<'src>,
34+
extra::Err<Rich<'src, char>>,
35+
> {
36+
recursive(|expr_use_rec| {
37+
let item = expr_use_tok()
38+
.then(
39+
lexeme("as")
40+
.ignore_then(expr_use_tok())
41+
.or_not(),
42+
)
43+
.then(
44+
lexeme("::")
45+
.ignore_then(expr_use_rec.clone())
46+
.or_not(),
47+
)
48+
.map(|((module, rename), nested)| {
49+
ExprUse::Item {
50+
module,
51+
rename,
52+
nested: nested.map(Box::new),
53+
}
54+
});
55+
56+
let many = expr_use_rec
57+
.separated_by(lexeme(","))
58+
.allow_trailing()
59+
.collect::<Vec<_>>()
60+
.delimited_by(lexeme("{"), lexeme("}"))
61+
.map(ExprUse::Many);
62+
63+
let glob = lexeme("*").map(|_| ExprUse::Glob);
64+
65+
item.or(many).or(glob)
66+
})
67+
}
68+
69+
fn expr_use_tok<'src>() -> impl Parser<
70+
'src,
71+
&'src str,
72+
&'src str,
73+
extra::Err<Rich<'src, char>>,
74+
> + Clone {
75+
token(text::ascii::ident()).and_is(
76+
keyword_except(&["crate", "super", "self", "Self"])
77+
.not(),
78+
)
79+
}
80+
81+
fn expr_other<'src>() -> impl Parser<
82+
'src,
83+
&'src str,
84+
Expr<'src>,
85+
extra::Err<Rich<'src, char>>,
86+
> {
87+
any()
88+
.and_is(expr_use().not())
89+
.repeated()
90+
.at_least(1)
91+
.to_slice()
92+
.map(Expr::Other)
93+
}
94+
95+
fn token<'src>(
96+
tok: impl Parser<
97+
'src,
98+
&'src str,
99+
&'src str,
100+
extra::Err<Rich<'src, char>>,
101+
> + Clone,
102+
) -> impl Parser<
103+
'src,
104+
&'src str,
105+
&'src str,
106+
extra::Err<Rich<'src, char>>,
107+
> + Clone {
108+
whitespace().or_not().ignore_then(tok)
109+
}
110+
111+
fn lexeme<'src>(
112+
seq: &'src str,
113+
) -> impl Parser<
114+
'src,
115+
&'src str,
116+
&'src str,
117+
extra::Err<Rich<'src, char>>,
118+
> + Clone {
119+
token(just(seq))
120+
}
121+
122+
fn keyword_except<'src>(
123+
except: &[&str],
124+
) -> impl Parser<
125+
'src,
126+
&'src str,
127+
&'src str,
128+
extra::Err<Rich<'src, char>>,
129+
> + Clone {
130+
choice(
131+
[
132+
"as", "break", "const", "continue", "crate",
133+
"else", "enum", "extern", "false", "fn", "for",
134+
"if", "impl", "in", "let", "loop", "match",
135+
"mod", "move", "mut", "pub", "ref", "return",
136+
"self", "Self", "static", "struct", "super",
137+
"trait", "true", "type", "unsafe", "use",
138+
"where", "while", "async", "await", "dyn",
139+
"abstract", "become", "box", "do", "final",
140+
"macro", "override", "priv", "typeof",
141+
"unsized", "virtual", "yield", "try", "gen",
142+
]
143+
.iter()
144+
.filter(|x| !except.contains(x))
145+
.map(|&x| {
146+
lexeme(x)
147+
.then_ignore(text::ascii::ident().not())
148+
})
149+
.collect::<Vec<_>>(),
150+
)
151+
}

rust/rustell/src/lib.rs

Lines changed: 1 addition & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
pub mod decode;
12
pub use chumsky::prelude::Parser;
2-
use chumsky::prelude::*;
3-
use chumsky::text::whitespace;
43

54
#[derive(Eq, PartialEq, Debug, Clone)]
65
pub enum Expr<'src> {
@@ -18,150 +17,3 @@ pub enum ExprUse<'src> {
1817
Many(Vec<ExprUse<'src>>),
1918
Glob,
2019
}
21-
22-
pub fn expr<'src>() -> impl Parser<
23-
'src,
24-
&'src str,
25-
Vec<Expr<'src>>,
26-
extra::Err<Rich<'src, char>>,
27-
> {
28-
expr_use()
29-
.or(expr_other())
30-
.repeated()
31-
.collect::<Vec<_>>()
32-
}
33-
34-
fn expr_use<'src>() -> impl Parser<
35-
'src,
36-
&'src str,
37-
Expr<'src>,
38-
extra::Err<Rich<'src, char>>,
39-
> {
40-
just("use")
41-
.ignore_then(expr_use_rec())
42-
.then_ignore(lexeme(";").or_not())
43-
.map(Expr::Use)
44-
}
45-
46-
fn expr_use_rec<'src>() -> impl Parser<
47-
'src,
48-
&'src str,
49-
ExprUse<'src>,
50-
extra::Err<Rich<'src, char>>,
51-
> {
52-
recursive(|expr_use_rec| {
53-
let item = expr_use_tok()
54-
.then(
55-
lexeme("as")
56-
.ignore_then(expr_use_tok())
57-
.or_not(),
58-
)
59-
.then(
60-
lexeme("::")
61-
.ignore_then(expr_use_rec.clone())
62-
.or_not(),
63-
)
64-
.map(|((module, rename), nested)| {
65-
ExprUse::Item {
66-
module,
67-
rename,
68-
nested: nested.map(Box::new),
69-
}
70-
});
71-
72-
let many = expr_use_rec
73-
.separated_by(lexeme(","))
74-
.allow_trailing()
75-
.collect::<Vec<_>>()
76-
.delimited_by(lexeme("{"), lexeme("}"))
77-
.map(ExprUse::Many);
78-
79-
let glob = lexeme("*").map(|_| ExprUse::Glob);
80-
81-
item.or(many).or(glob)
82-
})
83-
}
84-
85-
fn expr_use_tok<'src>() -> impl Parser<
86-
'src,
87-
&'src str,
88-
&'src str,
89-
extra::Err<Rich<'src, char>>,
90-
> + Clone {
91-
keyword_except(&["crate", "super", "self", "Self"])
92-
.not()
93-
.ignore_then(token(text::ascii::ident()))
94-
}
95-
96-
fn expr_other<'src>() -> impl Parser<
97-
'src,
98-
&'src str,
99-
Expr<'src>,
100-
extra::Err<Rich<'src, char>>,
101-
> {
102-
expr_use()
103-
.not()
104-
.ignore_then(any())
105-
.repeated()
106-
.at_least(1)
107-
.to_slice()
108-
.map(Expr::Other)
109-
}
110-
111-
fn token<'src>(
112-
tok: impl Parser<
113-
'src,
114-
&'src str,
115-
&'src str,
116-
extra::Err<Rich<'src, char>>,
117-
> + Clone,
118-
) -> impl Parser<
119-
'src,
120-
&'src str,
121-
&'src str,
122-
extra::Err<Rich<'src, char>>,
123-
> + Clone {
124-
whitespace().or_not().ignore_then(tok)
125-
}
126-
127-
fn lexeme<'src>(
128-
seq: &'src str,
129-
) -> impl Parser<
130-
'src,
131-
&'src str,
132-
&'src str,
133-
extra::Err<Rich<'src, char>>,
134-
> + Clone {
135-
token(just(seq))
136-
}
137-
138-
fn keyword_except<'src>(
139-
except: &[&str],
140-
) -> impl Parser<
141-
'src,
142-
&'src str,
143-
&'src str,
144-
extra::Err<Rich<'src, char>>,
145-
> + Clone {
146-
choice(
147-
[
148-
"as", "break", "const", "continue", "crate",
149-
"else", "enum", "extern", "false", "fn", "for",
150-
"if", "impl", "in", "let", "loop", "match",
151-
"mod", "move", "mut", "pub", "ref", "return",
152-
"self", "Self", "static", "struct", "super",
153-
"trait", "true", "type", "unsafe", "use",
154-
"where", "while", "async", "await", "dyn",
155-
"abstract", "become", "box", "do", "final",
156-
"macro", "override", "priv", "typeof",
157-
"unsized", "virtual", "yield", "try", "gen",
158-
]
159-
.iter()
160-
.filter(|x| !except.contains(x))
161-
.map(|&x| {
162-
lexeme(x)
163-
.then_ignore(text::ascii::ident().not())
164-
})
165-
.collect::<Vec<_>>(),
166-
)
167-
}

rust/rustell/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustell::decode;
12
use rustell::*;
23
use std::io::{self, Read};
34

@@ -6,7 +7,7 @@ fn main() {
67
io::stdin()
78
.read_to_string(&mut src)
89
.expect("Failed to read stdin");
9-
match expr().parse(src.trim()).into_result() {
10+
match decode::expr().parse(src.trim()).into_result() {
1011
Ok(ast) => println!("{:#?}", ast),
1112
Err(errs) => {
1213
errs.into_iter().for_each(|e| println!("{e:?}"))

0 commit comments

Comments
 (0)