diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 20:33:29 +0100 |
commit | 7c67612b8a894187fa3b64725531a5459f9211bf (patch) | |
tree | 9e2a536efa0c880d921fd8d4d74423afc9451fd4 /crates/libsyntax2/src/grammar/paths.rs | |
parent | 26262aaf05983c5b7f41cc438e287523268fe1eb (diff) |
organizize
Diffstat (limited to 'crates/libsyntax2/src/grammar/paths.rs')
-rw-r--r-- | crates/libsyntax2/src/grammar/paths.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/crates/libsyntax2/src/grammar/paths.rs b/crates/libsyntax2/src/grammar/paths.rs new file mode 100644 index 000000000..c277e2a6b --- /dev/null +++ b/crates/libsyntax2/src/grammar/paths.rs | |||
@@ -0,0 +1,86 @@ | |||
1 | use super::*; | ||
2 | |||
3 | pub(super) fn is_path_start(p: &Parser) -> bool { | ||
4 | match p.current() { | ||
5 | IDENT | SELF_KW | SUPER_KW | COLONCOLON => true, | ||
6 | _ => false, | ||
7 | } | ||
8 | } | ||
9 | |||
10 | pub(super) fn use_path(p: &mut Parser) { | ||
11 | path(p, Mode::Use) | ||
12 | } | ||
13 | |||
14 | pub(super) fn type_path(p: &mut Parser) { | ||
15 | path(p, Mode::Type) | ||
16 | } | ||
17 | |||
18 | pub(super) fn expr_path(p: &mut Parser) { | ||
19 | path(p, Mode::Expr) | ||
20 | } | ||
21 | |||
22 | #[derive(Clone, Copy, Eq, PartialEq)] | ||
23 | enum Mode { | ||
24 | Use, | ||
25 | Type, | ||
26 | Expr, | ||
27 | } | ||
28 | |||
29 | fn path(p: &mut Parser, mode: Mode) { | ||
30 | if !is_path_start(p) { | ||
31 | return; | ||
32 | } | ||
33 | let path = p.start(); | ||
34 | path_segment(p, mode, true); | ||
35 | let mut qual = path.complete(p, PATH); | ||
36 | loop { | ||
37 | let use_tree = match p.nth(1) { | ||
38 | STAR | L_CURLY => true, | ||
39 | _ => false, | ||
40 | }; | ||
41 | if p.at(COLONCOLON) && !use_tree { | ||
42 | let path = qual.precede(p); | ||
43 | p.bump(); | ||
44 | path_segment(p, mode, false); | ||
45 | let path = path.complete(p, PATH); | ||
46 | qual = path; | ||
47 | } else { | ||
48 | break; | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
53 | fn path_segment(p: &mut Parser, mode: Mode, first: bool) { | ||
54 | let segment = p.start(); | ||
55 | if first { | ||
56 | p.eat(COLONCOLON); | ||
57 | } | ||
58 | match p.current() { | ||
59 | IDENT => { | ||
60 | name_ref(p); | ||
61 | path_generic_args(p, mode); | ||
62 | } | ||
63 | SELF_KW | SUPER_KW => p.bump(), | ||
64 | _ => { | ||
65 | p.error("expected identifier"); | ||
66 | } | ||
67 | }; | ||
68 | segment.complete(p, PATH_SEGMENT); | ||
69 | } | ||
70 | |||
71 | fn path_generic_args(p: &mut Parser, mode: Mode) { | ||
72 | match mode { | ||
73 | Mode::Use => return, | ||
74 | Mode::Type => { | ||
75 | // test path_fn_trait_args | ||
76 | // type F = Box<Fn(x: i32) -> ()>; | ||
77 | if p.at(L_PAREN) { | ||
78 | params::param_list_opt_patterns(p); | ||
79 | fn_ret_type(p); | ||
80 | } else { | ||
81 | type_args::type_arg_list(p, false) | ||
82 | } | ||
83 | }, | ||
84 | Mode::Expr => type_args::type_arg_list(p, true), | ||
85 | } | ||
86 | } | ||