From 9a8e9bc4c6339051ef260f7794603481b6ff0bf2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Jan 2018 23:01:12 +0300 Subject: G: item outer attributes --- src/parser/event_parser/grammar/attributes.rs | 18 +++++++++++------- src/parser/event_parser/grammar/items.rs | 2 +- src/parser/event_parser/grammar/mod.rs | 10 +++++++++- src/parser/event_parser/parser.rs | 4 ++++ 4 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src/parser/event_parser') diff --git a/src/parser/event_parser/grammar/attributes.rs b/src/parser/event_parser/grammar/attributes.rs index 52210ccad..d774f8827 100644 --- a/src/parser/event_parser/grammar/attributes.rs +++ b/src/parser/event_parser/grammar/attributes.rs @@ -1,22 +1,26 @@ use super::*; +enum AttrKind { + Inner, Outer +} + pub(super) fn inner_attributes(p: &mut Parser) { - many(p, |p| attribute(p, true)) + many(p, |p| attribute(p, AttrKind::Inner)) } -pub(super) fn outer_attributes(_: &mut Parser) { +pub(super) fn outer_attributes(p: &mut Parser) { + many(p, |p| attribute(p, AttrKind::Outer)) } -fn attribute(p: &mut Parser, inner: bool) -> bool { +fn attribute(p: &mut Parser, kind: AttrKind) -> bool { fn attr_tail(p: &mut Parser) { meta_item(p) && p.expect(R_BRACK); } - if inner { - node_if(p, [POUND, EXCL, L_BRACK], ATTR, attr_tail) - } else { - node_if(p, [POUND, L_BRACK], ATTR, attr_tail) + match kind { + AttrKind::Inner => node_if(p, [POUND, EXCL, L_BRACK], ATTR, attr_tail), + AttrKind::Outer => node_if(p, [POUND, L_BRACK], ATTR, attr_tail), } } diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index 950e02a4d..522986ed0 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 | USE_KW => true, + STRUCT_KW | FN_KW | EXTERN_KW | MOD_KW | USE_KW | POUND => true, _ => false, } } diff --git a/src/parser/event_parser/grammar/mod.rs b/src/parser/event_parser/grammar/mod.rs index 60458ce70..76f62b714 100644 --- a/src/parser/event_parser/grammar/mod.rs +++ b/src/parser/event_parser/grammar/mod.rs @@ -41,7 +41,15 @@ fn node(p: &mut Parser, node_kind: SyntaxKind, rest: F) } fn many bool>(p: &mut Parser, f: F) { - while f(p) { } + loop { + let pos = p.pos(); + if !f(p) { + return + } + if pos == p.pos() { + panic!("Infinite loop in parser") + } + } } fn comma_list bool>(p: &mut Parser, end: SyntaxKind, f: F) { diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index a1a0ebfea..d7d24fa27 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs @@ -44,6 +44,10 @@ impl<'t> Parser<'t> { } } + pub(crate) fn pos(&self) -> usize { + self.pos + } + pub(crate) fn into_events(self) -> Vec { assert!(self.curly_limit.is_none()); assert!(self.current() == EOF); -- cgit v1.2.3