From 49ab44102496ac8c4a05b00c584adecf583f4d87 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Aug 2018 23:54:00 +0300 Subject: Qualified paths --- crates/libsyntax2/src/grammar/expressions/atom.rs | 2 +- crates/libsyntax2/src/grammar/expressions/mod.rs | 2 +- crates/libsyntax2/src/grammar/paths.rs | 44 ++++++++++++++--------- crates/libsyntax2/src/grammar/types.rs | 3 +- 4 files changed, 32 insertions(+), 19 deletions(-) (limited to 'crates/libsyntax2/src') diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs index e62b16654..4f03862b1 100644 --- a/crates/libsyntax2/src/grammar/expressions/atom.rs +++ b/crates/libsyntax2/src/grammar/expressions/atom.rs @@ -38,7 +38,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option return Some(m), None => (), } - if paths::is_path_start(p) { + if paths::is_path_start(p) || p.at(L_ANGLE) { return Some(path_expr(p, r)); } let la = p.nth(1); diff --git a/crates/libsyntax2/src/grammar/expressions/mod.rs b/crates/libsyntax2/src/grammar/expressions/mod.rs index 30a78d0c4..e56f3d30e 100644 --- a/crates/libsyntax2/src/grammar/expressions/mod.rs +++ b/crates/libsyntax2/src/grammar/expressions/mod.rs @@ -332,7 +332,7 @@ fn arg_list(p: &mut Parser) { // let _ = format!(); // } fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { - assert!(paths::is_path_start(p)); + assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); match p.current() { diff --git a/crates/libsyntax2/src/grammar/paths.rs b/crates/libsyntax2/src/grammar/paths.rs index aa5ecf4b8..97ab1880b 100644 --- a/crates/libsyntax2/src/grammar/paths.rs +++ b/crates/libsyntax2/src/grammar/paths.rs @@ -27,9 +27,6 @@ enum Mode { } 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); @@ -51,21 +48,36 @@ fn path(p: &mut Parser, mode: Mode) { } 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); + let m = p.start(); + // test qual_paths + // type X = ::Output; + // fn foo() { ::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"); + } } - SELF_KW | SUPER_KW => p.bump(), - _ => { - p.err_and_bump("expected identifier"); + p.expect(R_ANGLE); + } else { + if first { + p.eat(COLONCOLON); } - }; - segment.complete(p, PATH_SEGMENT); + match p.current() { + IDENT => { + name_ref(p); + path_generic_args(p, mode); + } + SELF_KW | SUPER_KW => p.bump(), + _ => { + p.err_and_bump("expected identifier"); + } + }; + } + m.complete(p, PATH_SEGMENT); } fn path_generic_args(p: &mut Parser, mode: Mode) { diff --git a/crates/libsyntax2/src/grammar/types.rs b/crates/libsyntax2/src/grammar/types.rs index 88631fefe..f58b545c7 100644 --- a/crates/libsyntax2/src/grammar/types.rs +++ b/crates/libsyntax2/src/grammar/types.rs @@ -12,6 +12,7 @@ pub(super) fn type_(p: &mut Parser) { FOR_KW => for_type(p), IMPL_KW => impl_trait_type(p), DYN_KW => dyn_trait_type(p), + L_ANGLE => path_type(p), _ if paths::is_path_start(p) => path_type(p), _ => { p.err_and_bump("expected type"); @@ -214,7 +215,7 @@ fn dyn_trait_type(p: &mut Parser) { // type C = self::Foo; // type D = super::Foo; pub(super) fn path_type(p: &mut Parser) { - assert!(paths::is_path_start(p)); + assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::type_path(p); // test path_type_with_bounds -- cgit v1.2.3