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 +++++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 src/parser/event_parser/grammar/paths.rs (limited to 'src/parser/event_parser/grammar') 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 -- cgit v1.2.3