Skip to content

Commit d963f89

Browse files
committed
better lexeme
1 parent d523ad0 commit d963f89

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

rust/rustell/src/lib.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use chumsky::container::OrderedSeq;
2-
use chumsky::extra::ParserExtra;
31
pub use chumsky::prelude::Parser;
42
use chumsky::prelude::*;
5-
use chumsky::primitive::Just;
6-
use chumsky::text::{Char, Padded};
3+
use chumsky::text::whitespace;
74

85
#[derive(Eq, PartialEq, Debug, Clone)]
96
pub enum Expr<'src> {
@@ -40,7 +37,7 @@ fn expr_use<'src>() -> impl Parser<
4037
Expr<'src>,
4138
extra::Err<Rich<'src, char>>,
4239
> {
43-
lexeme("use")
40+
just("use")
4441
.ignore_then(expr_use_rec())
4542
.then_ignore(lexeme(";").or_not())
4643
.map(Expr::Use)
@@ -72,10 +69,10 @@ fn expr_use_rec<'src>() -> impl Parser<
7269
});
7370

7471
let many = expr_use
75-
.separated_by(lexeme(','))
72+
.separated_by(lexeme(","))
7673
.allow_trailing()
7774
.collect::<Vec<_>>()
78-
.delimited_by(lexeme('{'), lexeme('}'))
75+
.delimited_by(lexeme("{"), lexeme("}"))
7976
.map(ExprUse::Many);
8077

8178
let glob = lexeme("*").map(|_| ExprUse::Glob);
@@ -99,12 +96,13 @@ fn expr_other<'src>() -> impl Parser<
9996
.map(Expr::Other)
10097
}
10198

102-
fn lexeme<'src, T, I, E>(seq: T) -> Padded<Just<T, I, E>>
103-
where
104-
I: Input<'src>,
105-
I::Token: Char,
106-
E: ParserExtra<'src, I>,
107-
T: OrderedSeq<'src, I::Token> + Clone,
108-
{
109-
just(seq).padded()
99+
fn lexeme<'src>(
100+
seq: &'static str,
101+
) -> impl Parser<
102+
'src,
103+
&'src str,
104+
&'src str,
105+
extra::Err<Rich<'src, char>>,
106+
> + Clone {
107+
whitespace().or_not().ignore_then(just(seq))
110108
}

rust/rustell/tests/integration.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ fn test_parser_other_then_use() {
147147
r#"
148148
fn test() {
149149
println!("Hello");
150-
}"#,
150+
}
151+
"#,
151152
),
152153
Expr::Use(ExprUse::Item {
153154
module: "crate",
@@ -174,6 +175,7 @@ fn test_parser_multiple() {
174175
"#;
175176
let lhs = expr().parse(src).into_result().unwrap();
176177
let rhs = vec![
178+
Expr::Other("\n "),
177179
Expr::Use(ExprUse::Item {
178180
module: "std",
179181
rename: None,
@@ -183,6 +185,7 @@ fn test_parser_multiple() {
183185
nested: None,
184186
})),
185187
}),
188+
Expr::Other("\n "),
186189
Expr::Use(ExprUse::Item {
187190
module: "std",
188191
rename: None,
@@ -192,6 +195,7 @@ fn test_parser_multiple() {
192195
nested: None,
193196
})),
194197
}),
198+
Expr::Other("\n "),
195199
];
196200
assert_eq!(lhs, rhs)
197201
}
@@ -207,6 +211,7 @@ fn test_parser_multiple_with_other() {
207211
"#;
208212
let lhs = expr().parse(src).into_result().unwrap();
209213
let rhs = vec![
214+
Expr::Other("\n "),
210215
Expr::Use(ExprUse::Item {
211216
module: "std",
212217
rename: None,
@@ -217,9 +222,11 @@ fn test_parser_multiple_with_other() {
217222
})),
218223
}),
219224
Expr::Other(
220-
r#"fn test() {
225+
r#"
226+
fn test() {
221227
println!("Hello");
222-
}"#,
228+
}
229+
"#,
223230
),
224231
Expr::Use(ExprUse::Item {
225232
module: "std",
@@ -230,6 +237,7 @@ fn test_parser_multiple_with_other() {
230237
nested: None,
231238
})),
232239
}),
240+
Expr::Other("\n "),
233241
];
234242
assert_eq!(lhs, rhs)
235243
}
@@ -249,6 +257,7 @@ fn test_parser_mixed_all_cases() {
249257
"#;
250258
let lhs = expr().parse(src).into_result().unwrap();
251259
let rhs = vec![
260+
Expr::Other("\n "),
252261
Expr::Use(ExprUse::Item {
253262
module: "std",
254263
rename: None,
@@ -278,6 +287,7 @@ fn test_parser_mixed_all_cases() {
278287
},
279288
]))),
280289
}),
290+
Expr::Other("\n "),
281291
Expr::Use(ExprUse::Item {
282292
module: "crate",
283293
rename: None,
@@ -292,7 +302,9 @@ fn test_parser_mixed_all_cases() {
292302
})),
293303
}),
294304
Expr::Other(
295-
r#"fn hello() {
305+
r#"
306+
307+
fn hello() {
296308
println!("Hello");
297309
}
298310
"#,

0 commit comments

Comments
 (0)