aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--grammar.ron3
-rw-r--r--src/parser/event_parser/grammar/items.rs8
-rw-r--r--src/parser/event_parser/grammar/mod.rs9
-rw-r--r--src/parser/event_parser/grammar/paths.rs15
-rw-r--r--src/syntax_kinds.rs12
-rw-r--r--tests/data/parser/ok/0009_use_item.rs2
-rw-r--r--tests/data/parser/ok/0009_use_item.txt17
7 files changed, 55 insertions, 11 deletions
diff --git a/grammar.ron b/grammar.ron
index 8871a1996..bb3c5f65e 100644
--- a/grammar.ron
+++ b/grammar.ron
@@ -75,6 +75,9 @@ Grammar(
75 "ATTR", 75 "ATTR",
76 "META_ITEM", 76 "META_ITEM",
77 "MOD_ITEM", 77 "MOD_ITEM",
78 "USE_ITEM",
79 "PATH",
80 "PATH_SEGMENT",
78 "LITERAL", 81 "LITERAL",
79 "ALIAS", 82 "ALIAS",
80 ] 83 ]
diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs
index e775db14b..950e02a4d 100644
--- a/src/parser/event_parser/grammar/items.rs
+++ b/src/parser/event_parser/grammar/items.rs
@@ -12,7 +12,7 @@ pub(super) fn mod_contents(p: &mut Parser) {
12 12
13fn item_first(p: &Parser) -> bool { 13fn item_first(p: &Parser) -> bool {
14 match p.current() { 14 match p.current() {
15 STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW => true, 15 STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW | USE_KW => true,
16 _ => false, 16 _ => false,
17 } 17 }
18} 18}
@@ -43,6 +43,7 @@ fn item(p: &mut Parser) -> bool {
43 // || node_if(p, TYPE_KW, TYPE_ITEM, type_item) 43 // || node_if(p, TYPE_KW, TYPE_ITEM, type_item)
44 node_if(p, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item) 44 node_if(p, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item)
45 || node_if(p, MOD_KW, MOD_ITEM, mod_item) 45 || node_if(p, MOD_KW, MOD_ITEM, mod_item)
46 || node_if(p, USE_KW, USE_ITEM, use_item)
46 || node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item) 47 || node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item)
47 || node_if(p, FN_KW, FN_ITEM, fn_item) 48 || node_if(p, FN_KW, FN_ITEM, fn_item)
48} 49}
@@ -66,6 +67,11 @@ fn mod_item(p: &mut Parser) {
66 p.curly_block(mod_contents); 67 p.curly_block(mod_contents);
67} 68}
68 69
70fn use_item(p: &mut Parser) {
71 paths::use_path(p);
72 p.expect(SEMI);
73}
74
69fn struct_field(p: &mut Parser) -> bool { 75fn struct_field(p: &mut Parser) -> bool {
70 node_if(p, IDENT, STRUCT_FIELD, |p| { 76 node_if(p, IDENT, STRUCT_FIELD, |p| {
71 p.expect(COLON) && p.expect(IDENT); 77 p.expect(COLON) && p.expect(IDENT);
diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs
index 6d1cd7ec3..60458ce70 100644
--- a/src/parser/event_parser/grammar/mod.rs
+++ b/src/parser/event_parser/grammar/mod.rs
@@ -6,10 +6,11 @@ use syntax_kinds::*;
6mod items; 6mod items;
7mod attributes; 7mod attributes;
8mod expressions; 8mod expressions;
9mod paths;
9 10
10pub(crate) fn file(p: &mut Parser) { 11pub(crate) fn file(p: &mut Parser) {
11 node(p, FILE, |p| { 12 node(p, FILE, |p| {
12 p.optional(SHEBANG); 13 p.eat(SHEBANG);
13 items::mod_contents(p); 14 items::mod_contents(p);
14 }) 15 })
15} 16}
@@ -99,12 +100,6 @@ impl<'p> Parser<'p> {
99 } 100 }
100 } 101 }
101 102
102 fn optional(&mut self, kind: SyntaxKind) {
103 if self.current() == kind {
104 self.bump();
105 }
106 }
107
108 fn eat(&mut self, kind: SyntaxKind) -> bool { 103 fn eat(&mut self, kind: SyntaxKind) -> bool {
109 self.current() == kind && { self.bump(); true } 104 self.current() == kind && { self.bump(); true }
110 } 105 }
diff --git a/src/parser/event_parser/grammar/paths.rs b/src/parser/event_parser/grammar/paths.rs
new file mode 100644
index 000000000..96966b380
--- /dev/null
+++ b/src/parser/event_parser/grammar/paths.rs
@@ -0,0 +1,15 @@
1use super::*;
2
3pub(crate) fn use_path(p: &mut Parser) {
4 if !AnyOf(&[IDENT, COLONCOLON]).is_ahead(p) {
5 return;
6 }
7 node(p, PATH, |p| {
8 p.eat(COLONCOLON);
9 path_segment(p);
10 })
11}
12
13fn path_segment(p: &mut Parser) -> bool {
14 node_if(p, IDENT, PATH_SEGMENT, |p| ())
15} \ No newline at end of file
diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs
index 26838d2d8..a86f203d7 100644
--- a/src/syntax_kinds.rs
+++ b/src/syntax_kinds.rs
@@ -72,10 +72,13 @@ pub const EXTERN_CRATE_ITEM: SyntaxKind = SyntaxKind(67);
72pub const ATTR: SyntaxKind = SyntaxKind(68); 72pub const ATTR: SyntaxKind = SyntaxKind(68);
73pub const META_ITEM: SyntaxKind = SyntaxKind(69); 73pub const META_ITEM: SyntaxKind = SyntaxKind(69);
74pub const MOD_ITEM: SyntaxKind = SyntaxKind(70); 74pub const MOD_ITEM: SyntaxKind = SyntaxKind(70);
75pub const LITERAL: SyntaxKind = SyntaxKind(71); 75pub const USE_ITEM: SyntaxKind = SyntaxKind(71);
76pub const ALIAS: SyntaxKind = SyntaxKind(72); 76pub const PATH: SyntaxKind = SyntaxKind(72);
77pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(73);
78pub const LITERAL: SyntaxKind = SyntaxKind(74);
79pub const ALIAS: SyntaxKind = SyntaxKind(75);
77 80
78static INFOS: [SyntaxInfo; 73] = [ 81static INFOS: [SyntaxInfo; 76] = [
79 SyntaxInfo { name: "USE_KW" }, 82 SyntaxInfo { name: "USE_KW" },
80 SyntaxInfo { name: "FN_KW" }, 83 SyntaxInfo { name: "FN_KW" },
81 SyntaxInfo { name: "STRUCT_KW" }, 84 SyntaxInfo { name: "STRUCT_KW" },
@@ -147,6 +150,9 @@ static INFOS: [SyntaxInfo; 73] = [
147 SyntaxInfo { name: "ATTR" }, 150 SyntaxInfo { name: "ATTR" },
148 SyntaxInfo { name: "META_ITEM" }, 151 SyntaxInfo { name: "META_ITEM" },
149 SyntaxInfo { name: "MOD_ITEM" }, 152 SyntaxInfo { name: "MOD_ITEM" },
153 SyntaxInfo { name: "USE_ITEM" },
154 SyntaxInfo { name: "PATH" },
155 SyntaxInfo { name: "PATH_SEGMENT" },
150 SyntaxInfo { name: "LITERAL" }, 156 SyntaxInfo { name: "LITERAL" },
151 SyntaxInfo { name: "ALIAS" }, 157 SyntaxInfo { name: "ALIAS" },
152]; 158];
diff --git a/tests/data/parser/ok/0009_use_item.rs b/tests/data/parser/ok/0009_use_item.rs
new file mode 100644
index 000000000..05a6aff83
--- /dev/null
+++ b/tests/data/parser/ok/0009_use_item.rs
@@ -0,0 +1,2 @@
1use foo;
2use ::bar; \ No newline at end of file
diff --git a/tests/data/parser/ok/0009_use_item.txt b/tests/data/parser/ok/0009_use_item.txt
new file mode 100644
index 000000000..f0b2981f2
--- /dev/null
+++ b/tests/data/parser/ok/0009_use_item.txt
@@ -0,0 +1,17 @@
1FILE@[0; 19)
2 USE_ITEM@[0; 9)
3 USE_KW@[0; 3)
4 PATH@[3; 7)
5 PATH_SEGMENT@[3; 7)
6 WHITESPACE@[3; 4)
7 IDENT@[4; 7)
8 SEMI@[7; 8)
9 WHITESPACE@[8; 9)
10 USE_ITEM@[9; 19)
11 USE_KW@[9; 12)
12 PATH@[12; 18)
13 WHITESPACE@[12; 13)
14 COLONCOLON@[13; 15)
15 PATH_SEGMENT@[15; 18)
16 IDENT@[15; 18)
17 SEMI@[18; 19)