From 5ea7e5fb7ab9f9ed762c8b5220ba01a29796a871 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Jan 2018 23:32:18 +0300 Subject: G: simplest use items --- src/parser/event_parser/grammar/items.rs | 8 +++++++- src/parser/event_parser/grammar/mod.rs | 9 ++------- src/parser/event_parser/grammar/paths.rs | 15 +++++++++++++++ src/syntax_kinds.rs | 12 +++++++++--- 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 src/parser/event_parser/grammar/paths.rs (limited to 'src') 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) { 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); 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::*; 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 @@ impl<'p> Parser<'p> { } } - 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 index 000000000..96966b380 --- /dev/null +++ b/src/parser/event_parser/grammar/paths.rs @@ -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 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); 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" }, @@ -147,6 +150,9 @@ static INFOS: [SyntaxInfo; 73] = [ 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" }, ]; -- cgit v1.2.3