rust/src/parser/event_parser/grammar/paths.rs

47 lines
1.1 KiB
Rust
Raw Normal View History

2018-01-09 14:32:18 -06:00
use super::*;
2018-01-13 04:42:19 -06:00
pub (crate) fn is_path_start(p: &Parser) -> bool {
AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p)
}
2018-01-09 14:32:18 -06:00
pub(crate) fn use_path(p: &mut Parser) {
2018-01-13 04:42:19 -06:00
if !is_path_start(p) {
2018-01-09 14:32:18 -06:00
return;
}
2018-01-12 11:59:19 -06:00
let mut prev = p.mark();
2018-01-09 14:32:18 -06:00
node(p, PATH, |p| {
path_segment(p, true);
2018-01-11 11:55:08 -06:00
});
many(p, |p| {
2018-01-12 11:59:19 -06:00
let curr = p.mark();
2018-01-13 04:42:19 -06:00
if p.current() == COLONCOLON && !items::is_use_tree_start(p.raw_lookahead(1)) {
node(p, PATH, |p| {
p.bump();
path_segment(p, false);
p.forward_parent(prev, curr);
prev = curr;
});
true
} else {
false
}
2018-01-11 11:55:08 -06:00
});
2018-01-09 14:32:18 -06:00
}
fn path_segment(p: &mut Parser, first: bool) {
node(p, PATH_SEGMENT, |p| {
if first {
p.eat(COLONCOLON);
}
2018-01-13 02:55:03 -06:00
match p.current() {
IDENT | SELF_KW | SUPER_KW => {
p.bump();
},
_ => {
p.error()
.message("expected identifier")
.emit();
}
};
})
2018-01-09 14:32:18 -06:00
}