Skip to content

Commit 17c3e56

Browse files
committed
rust lexeme
1 parent 079ca09 commit 17c3e56

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

rust/rustell/rustfmt.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
edition = "2024"
2-
max_width = 88
2+
max_width = 60

rust/rustell/src/lib.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use chumsky::container::OrderedSeq;
2+
use chumsky::extra::ParserExtra;
13
pub use chumsky::prelude::Parser;
24
use chumsky::prelude::*;
5+
use chumsky::primitive::Just;
6+
use chumsky::text::{Char, Padded};
37

48
#[derive(Eq, PartialEq, Debug, Clone)]
59
pub enum Expr<'src> {
@@ -18,13 +22,16 @@ pub enum UseExpr<'src> {
1822
Glob,
1923
}
2024

21-
pub fn parser<'src>()
22-
-> impl Parser<'src, &'src str, Vec<Expr<'src>>, extra::Err<Rich<'src, char>>> {
25+
pub fn parser<'src>() -> impl Parser<
26+
'src,
27+
&'src str,
28+
Vec<Expr<'src>>,
29+
extra::Err<Rich<'src, char>>,
30+
> {
2331
let use_expr = || {
24-
just("use")
25-
.padded()
32+
lexeme("use")
2633
.ignore_then(use_parser())
27-
.then_ignore(just(";").padded().or_not())
34+
.then_ignore(lexeme(";").or_not())
2835
.map(Expr::Use)
2936
};
3037

@@ -39,28 +46,50 @@ pub fn parser<'src>()
3946
use_expr().or(other).repeated().collect::<Vec<_>>()
4047
}
4148

42-
fn use_parser<'src>()
43-
-> impl Parser<'src, &'src str, UseExpr<'src>, extra::Err<Rich<'src, char>>> {
49+
fn use_parser<'src>() -> impl Parser<
50+
'src,
51+
&'src str,
52+
UseExpr<'src>,
53+
extra::Err<Rich<'src, char>>,
54+
> {
4455
recursive(|use_parser| {
4556
let ident = || text::ascii::ident().padded();
4657
let path = ident()
47-
.then(just("as").padded().ignore_then(ident()).or_not())
48-
.then(just("::").padded().ignore_then(use_parser.clone()).or_not())
49-
.map(|((ident, rename), nested)| UseExpr::Path {
50-
ident,
51-
rename,
52-
nested: nested.map(Box::new),
58+
.then(
59+
lexeme("as").ignore_then(ident()).or_not(),
60+
)
61+
.then(
62+
lexeme("::")
63+
.ignore_then(use_parser.clone())
64+
.or_not(),
65+
)
66+
.map(|((ident, rename), nested)| {
67+
UseExpr::Path {
68+
ident,
69+
rename,
70+
nested: nested.map(Box::new),
71+
}
5372
});
5473

5574
let group = use_parser
56-
.separated_by(just(','))
75+
.separated_by(lexeme(','))
5776
.allow_trailing()
5877
.collect::<Vec<_>>()
59-
.delimited_by(just('{').padded(), just('}').padded())
78+
.delimited_by(lexeme('{'), lexeme('}'))
6079
.map(UseExpr::Group);
6180

62-
let glob = just("*").padded().map(|_| UseExpr::Glob);
81+
let glob = lexeme("*").map(|_| UseExpr::Glob);
6382

6483
path.or(group).or(glob)
6584
})
6685
}
86+
87+
fn lexeme<'src, T, I, E>(seq: T) -> Padded<Just<T, I, E>>
88+
where
89+
I: Input<'src>,
90+
I::Token: Char,
91+
E: ParserExtra<'src, I>,
92+
T: OrderedSeq<'src, I::Token> + Clone,
93+
{
94+
just(seq).padded()
95+
}

0 commit comments

Comments
 (0)