From cb362626f326a565aca34c1a11c95dcb7152b798 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 1 Jan 2018 18:58:46 +0300 Subject: Parser: guess what? Groundwork! --- src/parser/event_parser/grammar.rs | 62 +++++++++++++++++++++++++++++++++++++- src/parser/event_parser/parser.rs | 15 ++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) (limited to 'src/parser/event_parser') diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs index c3496cccd..5219ed535 100644 --- a/src/parser/event_parser/grammar.rs +++ b/src/parser/event_parser/grammar.rs @@ -3,8 +3,68 @@ use super::parser::Parser; use syntax_kinds::*; +// Items // + pub fn file(p: &mut Parser) { p.start(FILE); - //TODO: parse_shebang + shebang(p); + inner_attributes(p); + mod_items(p); + p.finish(); +} + +type Result = ::std::result::Result<(), ()>; +const OK: Result = Ok(()); +const ERR: Result = Err(()); + +fn shebang(_: &mut Parser) { + //TODO +} + +fn inner_attributes(_: &mut Parser) { + //TODO +} + +fn mod_items(p: &mut Parser) { + loop { + skip_until_item(p); + if p.is_eof() { + return; + } + if item(p).is_err() { + skip_one_token(p); + } + } +} + +fn item(p: &mut Parser) -> Result { + outer_attributes(p)?; + visibility(p)?; + ERR +} + + + +// Paths, types, attributes, and stuff // + +fn outer_attributes(_: &mut Parser) -> Result { + OK +} + +fn visibility(_: &mut Parser) -> Result { + OK +} + +// Expressions // + +// Error recovery and high-order utils // + +fn skip_until_item(_: &mut Parser) { + //TODO +} + +fn skip_one_token(p: &mut Parser) { + p.start(ERROR); + p.bump().unwrap(); p.finish(); } \ No newline at end of file diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index 9592b90c9..0e4d44b79 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs @@ -34,10 +34,14 @@ impl<'t> Parser<'t> { } pub(crate) fn into_events(self) -> Vec { - assert!(self.pos == self.non_ws_tokens.len()); + assert!(self.is_eof()); self.events } + pub(crate) fn is_eof(&self) -> bool { + self.pos == self.non_ws_tokens.len() + } + pub(crate) fn start(&mut self, kind: SyntaxKind) { self.event(Event::Start { kind }); } @@ -46,6 +50,15 @@ impl<'t> Parser<'t> { self.event(Event::Finish); } + pub(crate) fn bump(&mut self) -> Option { + if self.is_eof() { + return None; + } + let idx = self.non_ws_tokens[self.pos].0; + self.pos += 1; + Some(self.raw_tokens[idx].kind) + } + fn event(&mut self, event: Event) { self.events.push(event) } -- cgit v1.2.3