From 7912189ec304b28c4df0030b5282cf3d21074154 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Jul 2018 23:38:19 +0300 Subject: reorganize --- src/grammar/paths.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/grammar/paths.rs (limited to 'src/grammar/paths.rs') diff --git a/src/grammar/paths.rs b/src/grammar/paths.rs new file mode 100644 index 000000000..fe69db096 --- /dev/null +++ b/src/grammar/paths.rs @@ -0,0 +1,77 @@ +use super::*; + +pub(super) fn is_path_start(p: &Parser) -> bool { + match p.current() { + IDENT | SELF_KW | SUPER_KW | COLONCOLON => true, + _ => false, + } +} + +pub(super) fn use_path(p: &mut Parser) { + path(p, Mode::Use) +} + +pub(super) fn type_path(p: &mut Parser) { + path(p, Mode::Type) +} + +pub(super) fn expr_path(p: &mut Parser) { + path(p, Mode::Expr) +} + +#[derive(Clone, Copy, Eq, PartialEq)] +enum Mode { + Use, + Type, + Expr, +} + +fn path(p: &mut Parser, mode: Mode) { + if !is_path_start(p) { + return; + } + let path = p.start(); + path_segment(p, mode, true); + let mut qual = path.complete(p, PATH); + loop { + let use_tree = match p.nth(1) { + STAR | L_CURLY => true, + _ => false, + }; + if p.at(COLONCOLON) && !use_tree { + let path = qual.precede(p); + p.bump(); + path_segment(p, mode, false); + let path = path.complete(p, PATH); + qual = path; + } else { + break; + } + } +} + +fn path_segment(p: &mut Parser, mode: Mode, first: bool) { + let segment = p.start(); + if first { + p.eat(COLONCOLON); + } + match p.current() { + IDENT => { + name_ref(p); + path_generic_args(p, mode); + } + SELF_KW | SUPER_KW => p.bump(), + _ => { + p.error("expected identifier"); + } + }; + segment.complete(p, PATH_SEGMENT); +} + +fn path_generic_args(p: &mut Parser, mode: Mode) { + match mode { + Mode::Use => return, + Mode::Type => type_args::type_arg_list(p, false), + Mode::Expr => type_args::type_arg_list(p, true), + } +} -- cgit v1.2.3