rust/src/parser/grammar/paths.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

2018-01-09 21:32:18 +01:00
use super::*;
2018-01-30 20:53:19 +01:00
pub(super) fn is_path_start(p: &Parser) -> bool {
2018-01-13 11:42:19 +01:00
AnyOf(&[IDENT, SELF_KW, SUPER_KW, COLONCOLON]).is_ahead(p)
}
2018-01-30 20:53:19 +01:00
pub(super) fn use_path(p: &mut Parser) {
path(p)
}
pub(super) fn type_path(p: &mut Parser) {
path(p)
}
fn path(p: &mut Parser) {
2018-01-13 11:42:19 +01:00
if !is_path_start(p) {
2018-01-09 21:32:18 +01:00
return;
}
2018-01-20 21:25:34 +01:00
let path = p.start();
2018-01-20 19:49:58 +01:00
path_segment(p, true);
2018-01-20 21:25:34 +01:00
let mut qual = path.complete(p, PATH);
2018-01-20 19:49:58 +01:00
loop {
let use_tree = match p.nth(1) {
STAR | L_CURLY => true,
_ => false,
};
if p.at(COLONCOLON) && !use_tree {
2018-01-20 21:25:34 +01:00
let path = qual.precede(p);
2018-01-20 19:49:58 +01:00
p.bump();
path_segment(p, false);
2018-01-20 21:25:34 +01:00
let path = path.complete(p, PATH);
qual = path;
2018-01-13 11:42:19 +01:00
} else {
2018-01-20 19:49:58 +01:00
break;
2018-01-13 11:42:19 +01:00
}
2018-01-20 19:49:58 +01:00
}
2018-01-09 21:32:18 +01:00
}
fn path_segment(p: &mut Parser, first: bool) {
2018-01-20 21:25:34 +01:00
let segment = p.start();
2018-01-20 19:49:58 +01:00
if first {
p.eat(COLONCOLON);
}
match p.current() {
2018-01-28 12:58:01 +01:00
IDENT | SELF_KW | SUPER_KW => p.bump(),
2018-02-09 20:44:50 +01:00
_ => {
p.error("expected identifier");
2018-02-09 20:55:50 +01:00
}
2018-01-20 19:49:58 +01:00
};
2018-01-20 21:25:34 +01:00
segment.complete(p, PATH_SEGMENT);
2018-01-20 19:49:58 +01:00
}