From 4cda3255309e7ed9c9d9094e12a8d91bdc1554ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 1 Jan 2018 16:05:46 +0300 Subject: Parser: even more groundwork --- src/parser/event_parser/grammar.rs | 9 +++++++ src/parser/event_parser/mod.rs | 20 +++++++++++++++ src/parser/event_parser/parser.rs | 52 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/parser/event_parser/grammar.rs create mode 100644 src/parser/event_parser/mod.rs create mode 100644 src/parser/event_parser/parser.rs (limited to 'src/parser/event_parser') diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs new file mode 100644 index 000000000..dd09061aa --- /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 000000000..14107720a --- /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/parser.rs b/src/parser/event_parser/parser.rs new file mode 100644 index 000000000..9592b90c9 --- /dev/null +++ b/src/parser/event_parser/parser.rs @@ -0,0 +1,52 @@ +use {Token, SyntaxKind, TextUnit}; +use super::Event; +use syntax_kinds::{WHITESPACE, COMMENT}; + +pub struct Parser<'t> { + text: &'t str, + raw_tokens: &'t [Token], + non_ws_tokens: Vec<(usize, TextUnit)>, + + pos: usize, + events: Vec, +} + +impl<'t> 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() { + match token.kind { + WHITESPACE | COMMENT => (), + _ => non_ws_tokens.push((idx, len)), + } + len += token.len; + } + + Parser { + text, + raw_tokens, + non_ws_tokens, + + pos: 0, + events: Vec::new(), + } + } + + 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 }); + } + + pub(crate) fn finish(&mut self) { + self.event(Event::Finish); + } + + fn event(&mut self, event: Event) { + self.events.push(event) + } +} \ No newline at end of file -- cgit v1.2.3