Skip to content

Commit fadcada

Browse files
committed
rustell parser wip
1 parent 0a3b0b2 commit fadcada

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

rust/rustell/src/lib.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ fn expr_use_rec<'src>() -> impl Parser<
5050
extra::Err<Rich<'src, char>>,
5151
> {
5252
recursive(|expr_use_rec| {
53-
let ident = || token(text::ascii::ident());
54-
let item = ident()
53+
let item = expr_use_tok()
5554
.then(
56-
lexeme("as").ignore_then(ident()).or_not(),
55+
lexeme("as")
56+
.ignore_then(expr_use_tok())
57+
.or_not(),
5758
)
5859
.then(
5960
lexeme("::")
@@ -81,6 +82,17 @@ fn expr_use_rec<'src>() -> impl Parser<
8182
})
8283
}
8384

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+
8496
fn expr_other<'src>() -> impl Parser<
8597
'src,
8698
&'src str,
@@ -123,7 +135,9 @@ fn lexeme<'src>(
123135
token(just(seq))
124136
}
125137

126-
fn keyword<'src>() -> impl Parser<
138+
fn keyword_except<'src>(
139+
except: &[&str],
140+
) -> impl Parser<
127141
'src,
128142
&'src str,
129143
&'src str,
@@ -142,6 +156,12 @@ fn keyword<'src>() -> impl Parser<
142156
"macro", "override", "priv", "typeof",
143157
"unsized", "virtual", "yield", "try", "gen",
144158
]
145-
.map(lexeme),
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<_>>(),
146166
)
147167
}

rust/rustell/tests/integration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ fn test_parser_crate() {
138138
fn test_parser_other_then_use() {
139139
let lhs = r#"
140140
fn test() {
141-
println!("Hello");
141+
println!("Hello")
142142
}
143143
use crate::module::Type;"#;
144144
let rhs = vec![
145145
Expr::Other(
146146
r#"
147147
fn test() {
148-
println!("Hello");
148+
println!("Hello")
149149
}
150150
"#,
151151
),
@@ -164,7 +164,7 @@ fn test_parser_other_then_use() {
164164
}),
165165
];
166166
assert_eq!(parse(lhs), rhs);
167-
// assert_eq!(parse(&sloppy(lhs)), rhs)
167+
assert_eq!(parse(&sloppy(lhs)), rhs)
168168
}
169169

170170
#[test]

0 commit comments

Comments
 (0)