Skip to content

Commit 0a3b0b2

Browse files
committed
wip
1 parent 92728a3 commit 0a3b0b2

File tree

2 files changed

+76
-29
lines changed

2 files changed

+76
-29
lines changed

rust/rustell/src/lib.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ fn expr_use_rec<'src>() -> impl Parser<
4949
ExprUse<'src>,
5050
extra::Err<Rich<'src, char>>,
5151
> {
52-
recursive(|expr_use| {
53-
let ident = || text::ascii::ident().padded();
52+
recursive(|expr_use_rec| {
53+
let ident = || token(text::ascii::ident());
5454
let item = ident()
5555
.then(
5656
lexeme("as").ignore_then(ident()).or_not(),
5757
)
5858
.then(
5959
lexeme("::")
60-
.ignore_then(expr_use.clone())
60+
.ignore_then(expr_use_rec.clone())
6161
.or_not(),
6262
)
6363
.map(|((module, rename), nested)| {
@@ -68,7 +68,7 @@ fn expr_use_rec<'src>() -> impl Parser<
6868
}
6969
});
7070

71-
let many = expr_use
71+
let many = expr_use_rec
7272
.separated_by(lexeme(","))
7373
.allow_trailing()
7474
.collect::<Vec<_>>()
@@ -96,6 +96,22 @@ fn expr_other<'src>() -> impl Parser<
9696
.map(Expr::Other)
9797
}
9898

99+
fn token<'src>(
100+
tok: impl Parser<
101+
'src,
102+
&'src str,
103+
&'src str,
104+
extra::Err<Rich<'src, char>>,
105+
> + Clone,
106+
) -> impl Parser<
107+
'src,
108+
&'src str,
109+
&'src str,
110+
extra::Err<Rich<'src, char>>,
111+
> + Clone {
112+
whitespace().or_not().ignore_then(tok)
113+
}
114+
99115
fn lexeme<'src>(
100116
seq: &'src str,
101117
) -> impl Parser<
@@ -104,5 +120,28 @@ fn lexeme<'src>(
104120
&'src str,
105121
extra::Err<Rich<'src, char>>,
106122
> + Clone {
107-
whitespace().or_not().ignore_then(just(seq))
123+
token(just(seq))
124+
}
125+
126+
fn keyword<'src>() -> impl Parser<
127+
'src,
128+
&'src str,
129+
&'src str,
130+
extra::Err<Rich<'src, char>>,
131+
> + Clone {
132+
choice(
133+
[
134+
"as", "break", "const", "continue", "crate",
135+
"else", "enum", "extern", "false", "fn", "for",
136+
"if", "impl", "in", "let", "loop", "match",
137+
"mod", "move", "mut", "pub", "ref", "return",
138+
"self", "Self", "static", "struct", "super",
139+
"trait", "true", "type", "unsafe", "use",
140+
"where", "while", "async", "await", "dyn",
141+
"abstract", "become", "box", "do", "final",
142+
"macro", "override", "priv", "typeof",
143+
"unsized", "virtual", "yield", "try", "gen",
144+
]
145+
.map(lexeme),
146+
)
108147
}

rust/rustell/tests/integration.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use rustell::*;
22

33
#[test]
44
fn test_parser() {
5-
let src = "use std::io::Read;";
6-
let lhs = expr().parse(src).into_result().unwrap();
5+
let lhs = "use std::io::Read;";
76
let rhs = vec![Expr::Use(ExprUse::Item {
87
module: "std",
98
rename: None,
@@ -17,13 +16,13 @@ fn test_parser() {
1716
})),
1817
})),
1918
})];
20-
assert_eq!(lhs, rhs)
19+
assert_eq!(parse(lhs), rhs);
20+
assert_eq!(parse(&sloppy(lhs)), rhs)
2121
}
2222

2323
#[test]
2424
fn test_parser_many() {
25-
let src = "use std::{io::Read, fs::File};";
26-
let lhs = expr().parse(src).into_result().unwrap();
25+
let lhs = "use std::{io::Read, fs::File};";
2726
let rhs = vec![Expr::Use(ExprUse::Item {
2827
module: "std",
2928
rename: None,
@@ -48,13 +47,13 @@ fn test_parser_many() {
4847
},
4948
]))),
5049
})];
51-
assert_eq!(lhs, rhs);
50+
assert_eq!(parse(lhs), rhs);
51+
assert_eq!(parse(&sloppy(lhs)), rhs)
5252
}
5353

5454
#[test]
5555
fn test_parser_glob() {
56-
let src = "use std::io::*;";
57-
let lhs = expr().parse(src).into_result().unwrap();
56+
let lhs = "use std::io::*;";
5857
let rhs = vec![Expr::Use(ExprUse::Item {
5958
module: "std",
6059
rename: None,
@@ -64,13 +63,13 @@ fn test_parser_glob() {
6463
nested: Some(Box::new(ExprUse::Glob)),
6564
})),
6665
})];
67-
assert_eq!(lhs, rhs)
66+
assert_eq!(parse(lhs), rhs);
67+
assert_eq!(parse(&sloppy(lhs)), rhs)
6868
}
6969

7070
#[test]
7171
fn test_parser_rename() {
72-
let src = "use std::io::Read as Readable;";
73-
let lhs = expr().parse(src).into_result().unwrap();
72+
let lhs = "use std::io::Read as Readable;";
7473
let rhs = vec![Expr::Use(ExprUse::Item {
7574
module: "std",
7675
rename: None,
@@ -84,13 +83,13 @@ fn test_parser_rename() {
8483
})),
8584
})),
8685
})];
87-
assert_eq!(lhs, rhs)
86+
assert_eq!(parse(lhs), rhs);
87+
assert_eq!(parse(&sloppy(lhs)), rhs)
8888
}
8989

9090
#[test]
9191
fn test_parser_complex() {
92-
let src = "use std::{io::Read as Readable, fs::*};";
93-
let lhs = expr().parse(src).into_result().unwrap();
92+
let lhs = "use std::{io::Read as Readable, fs::*};";
9493
let rhs = vec![Expr::Use(ExprUse::Item {
9594
module: "std",
9695
rename: None,
@@ -111,13 +110,13 @@ fn test_parser_complex() {
111110
},
112111
]))),
113112
})];
114-
assert_eq!(lhs, rhs)
113+
assert_eq!(parse(lhs), rhs);
114+
assert_eq!(parse(&sloppy(lhs)), rhs)
115115
}
116116

117117
#[test]
118118
fn test_parser_crate() {
119-
let src = "use crate::module::Type;";
120-
let lhs = expr().parse(src).into_result().unwrap();
119+
let lhs = "use crate::module::Type;";
121120
let rhs = vec![Expr::Use(ExprUse::Item {
122121
module: "crate",
123122
rename: None,
@@ -131,17 +130,17 @@ fn test_parser_crate() {
131130
})),
132131
})),
133132
})];
134-
assert_eq!(lhs, rhs)
133+
assert_eq!(parse(lhs), rhs);
134+
assert_eq!(parse(&sloppy(lhs)), rhs)
135135
}
136136

137137
#[test]
138138
fn test_parser_other_then_use() {
139-
let src = r#"
139+
let lhs = r#"
140140
fn test() {
141141
println!("Hello");
142142
}
143143
use crate::module::Type;"#;
144-
let lhs = expr().parse(src).into_result().unwrap();
145144
let rhs = vec![
146145
Expr::Other(
147146
r#"
@@ -164,16 +163,16 @@ fn test_parser_other_then_use() {
164163
})),
165164
}),
166165
];
167-
assert_eq!(lhs, rhs)
166+
assert_eq!(parse(lhs), rhs);
167+
// assert_eq!(parse(&sloppy(lhs)), rhs)
168168
}
169169

170170
#[test]
171171
fn test_parser_multiple() {
172-
let src = r#"
172+
let lhs = r#"
173173
use std::io;
174174
use std::fs;
175175
"#;
176-
let lhs = expr().parse(src).into_result().unwrap();
177176
let rhs = vec![
178177
Expr::Other("\n "),
179178
Expr::Use(ExprUse::Item {
@@ -197,7 +196,8 @@ fn test_parser_multiple() {
197196
}),
198197
Expr::Other("\n "),
199198
];
200-
assert_eq!(lhs, rhs)
199+
assert_eq!(parse(lhs), rhs);
200+
assert_eq!(parse(&sloppy(lhs)), rhs)
201201
}
202202

203203
#[test]
@@ -312,3 +312,11 @@ fn test_parser_mixed_all_cases() {
312312
];
313313
assert_eq!(lhs, rhs)
314314
}
315+
316+
fn sloppy(src: &str) -> String {
317+
src.replace(";", "")
318+
}
319+
320+
fn parse(src: &str) -> Vec<Expr> {
321+
expr().parse(src).into_result().unwrap()
322+
}

0 commit comments

Comments
 (0)