]> git.lizzy.rs Git - rust.git/commitdiff
G: simplest use items
authorAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 9 Jan 2018 20:32:18 +0000 (23:32 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Tue, 9 Jan 2018 20:32:18 +0000 (23:32 +0300)
grammar.ron
src/parser/event_parser/grammar/items.rs
src/parser/event_parser/grammar/mod.rs
src/parser/event_parser/grammar/paths.rs [new file with mode: 0644]
src/syntax_kinds.rs
tests/data/parser/ok/0009_use_item.rs [new file with mode: 0644]
tests/data/parser/ok/0009_use_item.txt [new file with mode: 0644]

index 8871a1996e45b7cbbea229758745b73e8da904ee..bb3c5f65e991eff551c3b744753146c99dfb1030 100644 (file)
@@ -75,6 +75,9 @@ Grammar(
         "ATTR",
         "META_ITEM",
         "MOD_ITEM",
+        "USE_ITEM",
+        "PATH",
+        "PATH_SEGMENT",
         "LITERAL",
         "ALIAS",
     ]
index e775db14b6c52b247d8da5bae8d79da649bb5049..950e02a4d48ecee6b5e5562074cb65b2f22dc784 100644 (file)
@@ -12,7 +12,7 @@ pub(super) fn mod_contents(p: &mut Parser) {
 
 fn item_first(p: &Parser) -> bool {
     match p.current() {
-        STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW => true,
+        STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW | USE_KW => true,
         _ => false,
     }
 }
@@ -43,6 +43,7 @@ fn item(p: &mut Parser) -> bool {
     // || node_if(p, TYPE_KW, TYPE_ITEM, type_item)
     node_if(p, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item)
         || node_if(p, MOD_KW, MOD_ITEM, mod_item)
+        || node_if(p, USE_KW, USE_ITEM, use_item)
         || node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item)
         || node_if(p, FN_KW, FN_ITEM, fn_item)
 }
@@ -66,6 +67,11 @@ fn mod_item(p: &mut Parser) {
     p.curly_block(mod_contents);
 }
 
+fn use_item(p: &mut Parser) {
+    paths::use_path(p);
+    p.expect(SEMI);
+}
+
 fn struct_field(p: &mut Parser) -> bool {
     node_if(p, IDENT, STRUCT_FIELD, |p| {
         p.expect(COLON) && p.expect(IDENT);
index 6d1cd7ec34e0b83322ebb46dbabb134f2ee295ac..60458ce7090b52ece24bd3acada94c69b97b009f 100644 (file)
@@ -6,10 +6,11 @@
 mod items;
 mod attributes;
 mod expressions;
+mod paths;
 
 pub(crate) fn file(p: &mut Parser) {
     node(p, FILE, |p| {
-        p.optional(SHEBANG);
+        p.eat(SHEBANG);
         items::mod_contents(p);
     })
 }
@@ -99,12 +100,6 @@ pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool {
         }
     }
 
-    fn optional(&mut self, kind: SyntaxKind) {
-        if self.current() == kind {
-            self.bump();
-        }
-    }
-
     fn eat(&mut self, kind: SyntaxKind) -> bool {
         self.current() == kind && { self.bump(); true }
     }
diff --git a/src/parser/event_parser/grammar/paths.rs b/src/parser/event_parser/grammar/paths.rs
new file mode 100644 (file)
index 0000000..96966b3
--- /dev/null
@@ -0,0 +1,15 @@
+use super::*;
+
+pub(crate) fn use_path(p: &mut Parser) {
+    if !AnyOf(&[IDENT, COLONCOLON]).is_ahead(p) {
+        return;
+    }
+    node(p, PATH, |p| {
+        p.eat(COLONCOLON);
+        path_segment(p);
+    })
+}
+
+fn path_segment(p: &mut Parser) -> bool {
+    node_if(p, IDENT, PATH_SEGMENT, |p| ())
+}
\ No newline at end of file
index 26838d2d8e4b9897948a3cbda7a51592c4d70912..a86f203d709ab52ef593337869ce9d6c0f7cadcf 100644 (file)
 pub const ATTR: SyntaxKind = SyntaxKind(68);
 pub const META_ITEM: SyntaxKind = SyntaxKind(69);
 pub const MOD_ITEM: SyntaxKind = SyntaxKind(70);
-pub const LITERAL: SyntaxKind = SyntaxKind(71);
-pub const ALIAS: SyntaxKind = SyntaxKind(72);
+pub const USE_ITEM: SyntaxKind = SyntaxKind(71);
+pub const PATH: SyntaxKind = SyntaxKind(72);
+pub const PATH_SEGMENT: SyntaxKind = SyntaxKind(73);
+pub const LITERAL: SyntaxKind = SyntaxKind(74);
+pub const ALIAS: SyntaxKind = SyntaxKind(75);
 
-static INFOS: [SyntaxInfo; 73] = [
+static INFOS: [SyntaxInfo; 76] = [
     SyntaxInfo { name: "USE_KW" },
     SyntaxInfo { name: "FN_KW" },
     SyntaxInfo { name: "STRUCT_KW" },
     SyntaxInfo { name: "ATTR" },
     SyntaxInfo { name: "META_ITEM" },
     SyntaxInfo { name: "MOD_ITEM" },
+    SyntaxInfo { name: "USE_ITEM" },
+    SyntaxInfo { name: "PATH" },
+    SyntaxInfo { name: "PATH_SEGMENT" },
     SyntaxInfo { name: "LITERAL" },
     SyntaxInfo { name: "ALIAS" },
 ];
diff --git a/tests/data/parser/ok/0009_use_item.rs b/tests/data/parser/ok/0009_use_item.rs
new file mode 100644 (file)
index 0000000..05a6aff
--- /dev/null
@@ -0,0 +1,2 @@
+use foo;
+use ::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 (file)
index 0000000..f0b2981
--- /dev/null
@@ -0,0 +1,17 @@
+FILE@[0; 19)
+  USE_ITEM@[0; 9)
+    USE_KW@[0; 3)
+    PATH@[3; 7)
+      PATH_SEGMENT@[3; 7)
+        WHITESPACE@[3; 4)
+        IDENT@[4; 7)
+    SEMI@[7; 8)
+    WHITESPACE@[8; 9)
+  USE_ITEM@[9; 19)
+    USE_KW@[9; 12)
+    PATH@[12; 18)
+      WHITESPACE@[12; 13)
+      COLONCOLON@[13; 15)
+      PATH_SEGMENT@[15; 18)
+        IDENT@[15; 18)
+    SEMI@[18; 19)