rust/crates/ra_syntax/src/grammar/paths.rs

104 lines
2.5 KiB
Rust
Raw Normal View History

2018-01-09 21:32:18 +01:00
use super::*;
pub(super) const PATH_FIRST: TokenSet =
2018-09-29 13:53:23 +02:00
token_set![IDENT, SELF_KW, SUPER_KW, CRATE_KW, COLONCOLON, L_ANGLE];
2018-01-30 20:53:19 +01:00
pub(super) fn is_path_start(p: &Parser) -> bool {
2018-02-11 11:13:06 +01:00
match p.current() {
2018-09-29 13:53:23 +02:00
IDENT | SELF_KW | SUPER_KW | CRATE_KW | COLONCOLON => true,
2018-02-11 11:13:06 +01:00
_ => false,
}
2018-01-13 11:42:19 +01:00
}
2018-01-30 20:53:19 +01:00
pub(super) fn use_path(p: &mut Parser) {
2018-07-30 16:02:51 +02:00
path(p, Mode::Use)
2018-01-30 20:53:19 +01:00
}
pub(super) fn type_path(p: &mut Parser) {
2018-07-30 16:02:51 +02:00
path(p, Mode::Type)
2018-01-30 20:53:19 +01:00
}
2018-07-30 16:02:51 +02:00
pub(super) fn expr_path(p: &mut Parser) {
path(p, Mode::Expr)
}
#[derive(Clone, Copy, Eq, PartialEq)]
2018-07-31 14:40:40 +02:00
enum Mode {
Use,
Type,
Expr,
}
2018-07-30 16:02:51 +02:00
fn path(p: &mut Parser, mode: Mode) {
2018-01-20 21:25:34 +01:00
let path = p.start();
2018-07-30 16:02:51 +02:00
path_segment(p, mode, 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();
2018-07-30 16:02:51 +02:00
path_segment(p, mode, 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
}
2018-07-30 16:02:51 +02:00
fn path_segment(p: &mut Parser, mode: Mode, first: bool) {
2018-08-13 22:54:00 +02:00
let m = p.start();
// test qual_paths
// type X = <A as B>::Output;
// fn foo() { <usize as Default>::default(); }
if first && p.eat(L_ANGLE) {
types::type_(p);
if p.eat(AS_KW) {
if is_path_start(p) {
types::path_type(p);
} else {
p.error("expected a trait");
}
2018-07-31 14:40:40 +02:00
}
2018-08-13 22:54:00 +02:00
p.expect(R_ANGLE);
} else {
if first {
p.eat(COLONCOLON);
2018-02-09 20:55:50 +01:00
}
2018-08-13 22:54:00 +02:00
match p.current() {
IDENT => {
name_ref(p);
2018-08-24 01:14:10 +02:00
opt_path_type_args(p, mode);
2018-08-13 22:54:00 +02:00
}
2018-09-29 13:53:23 +02:00
// test crate_path
// use crate::foo;
SELF_KW | SUPER_KW | CRATE_KW => p.bump(),
2018-08-13 22:54:00 +02:00
_ => {
2018-11-21 16:34:20 +01:00
p.err_recover("expected identifier", items::ITEM_RECOVERY_SET);
2018-08-13 22:54:00 +02:00
}
};
}
m.complete(p, PATH_SEGMENT);
2018-01-20 19:49:58 +01:00
}
2018-07-30 16:02:51 +02:00
2018-08-24 01:14:10 +02:00
fn opt_path_type_args(p: &mut Parser, mode: Mode) {
2018-07-30 16:02:51 +02:00
match mode {
Mode::Use => return,
2018-08-08 13:43:14 +02:00
Mode::Type => {
// test path_fn_trait_args
// type F = Box<Fn(x: i32) -> ()>;
if p.at(L_PAREN) {
2018-08-08 17:34:26 +02:00
params::param_list_opt_patterns(p);
2018-08-24 01:14:10 +02:00
opt_fn_ret_type(p);
2018-08-08 13:43:14 +02:00
} else {
2018-08-24 01:14:10 +02:00
type_args::opt_type_arg_list(p, false)
2018-08-08 13:43:14 +02:00
}
}
2018-08-24 01:14:10 +02:00
Mode::Expr => type_args::opt_type_arg_list(p, true),
2018-07-30 16:02:51 +02:00
}
}