From 4cda3255309e7ed9c9d9094e12a8d91bdc1554ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 1 Jan 2018 16:05:46 +0300 Subject: [PATCH] Parser: even more groundwork --- src/parser/event_parser/grammar.rs | 9 ++++ src/parser/event_parser/mod.rs | 20 +++++++++ .../parser.rs} | 42 +++++++------------ 3 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 src/parser/event_parser/grammar.rs create mode 100644 src/parser/event_parser/mod.rs rename src/parser/{event_parser.rs => event_parser/parser.rs} (57%) diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs new file mode 100644 index 0000000000..dd09061aae --- /dev/null +++ b/src/parser/event_parser/grammar.rs @@ -0,0 +1,9 @@ +use super::Event; +use super::parser::Parser; + +use syntax_kinds::*; + +pub fn parse_file(p: &mut Parser) { + p.start(FILE); + p.finish(); +} \ No newline at end of file diff --git a/src/parser/event_parser/mod.rs b/src/parser/event_parser/mod.rs new file mode 100644 index 0000000000..14107720ad --- /dev/null +++ b/src/parser/event_parser/mod.rs @@ -0,0 +1,20 @@ +use {Token, TextUnit, SyntaxKind}; + +use syntax_kinds::*; +mod grammar; +mod parser; + +pub(crate) enum Event { + Start { kind: SyntaxKind }, + Finish, + Token { + kind: SyntaxKind, + n_raw_tokens: u8, + } +} + +pub(crate) fn parse<'t>(text: &'t str, raw_tokens: &'t [Token]) -> Vec { + let mut parser = parser::Parser::new(text, raw_tokens); + grammar::parse_file(&mut parser); + parser.into_events() +} \ No newline at end of file diff --git a/src/parser/event_parser.rs b/src/parser/event_parser/parser.rs similarity index 57% rename from src/parser/event_parser.rs rename to src/parser/event_parser/parser.rs index c6aacfefb0..9592b90c9d 100644 --- a/src/parser/event_parser.rs +++ b/src/parser/event_parser/parser.rs @@ -1,24 +1,8 @@ -use {Token, TextUnit, SyntaxKind}; +use {Token, SyntaxKind, TextUnit}; +use super::Event; +use syntax_kinds::{WHITESPACE, COMMENT}; -use syntax_kinds::*; - - -pub(crate) enum Event { - Start { kind: SyntaxKind }, - Finish, - Token { - kind: SyntaxKind, - n_raw_tokens: u8, - } -} - -pub(crate) fn parse<'t>(text: &'t str, raw_tokens: &'t [Token]) -> Vec { - let mut parser = Parser::new(text, raw_tokens); - parse_file(&mut parser); - parser.events -} - -struct Parser<'t> { +pub struct Parser<'t> { text: &'t str, raw_tokens: &'t [Token], non_ws_tokens: Vec<(usize, TextUnit)>, @@ -28,7 +12,7 @@ struct Parser<'t> { } impl<'t> Parser<'t> { - fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { + pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { let mut non_ws_tokens = Vec::new(); let mut len = TextUnit::new(0); for (idx, &token) in raw_tokens.iter().enumerate() { @@ -49,18 +33,20 @@ impl<'t> Parser<'t> { } } - fn start(&mut self, kind: SyntaxKind) { + pub(crate) fn into_events(self) -> Vec { + assert!(self.pos == self.non_ws_tokens.len()); + self.events + } + + pub(crate) fn start(&mut self, kind: SyntaxKind) { self.event(Event::Start { kind }); } - fn finish(&mut self) { + + pub(crate) fn finish(&mut self) { self.event(Event::Finish); } + fn event(&mut self, event: Event) { self.events.push(event) } -} - -fn parse_file(p: &mut Parser) { - p.start(FILE); - p.finish(); } \ No newline at end of file