From c12450fb4e30c3418555e47d045bb9fd4318a10a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 29 Jul 2018 13:51:55 +0300 Subject: Introduce red-green syntax tree --- src/parser/event.rs | 8 ++++++-- src/parser/mod.rs | 29 +++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src/parser') diff --git a/src/parser/event.rs b/src/parser/event.rs index ac8a55de9..0fbfaeb9f 100644 --- a/src/parser/event.rs +++ b/src/parser/event.rs @@ -1,5 +1,5 @@ use { - ErrorMsg, File, FileBuilder, Sink, SyntaxKind, Token, + ErrorMsg, File, FileBuilder, Sink, SyntaxKind, Token, GreenBuilder, syntax_kinds::TOMBSTONE, }; use super::is_insignificant; @@ -69,6 +69,11 @@ pub(crate) enum Event { pub(super) fn to_file(text: String, tokens: &[Token], events: Vec) -> File { let mut builder = FileBuilder::new(text); + process(&mut builder, tokens, events); + builder.finish() +} + +pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec) { let mut idx = 0; let mut holes = Vec::new(); @@ -145,5 +150,4 @@ pub(super) fn to_file(text: String, tokens: &[Token], events: Vec) -> Fil &Event::Error { ref msg } => builder.error(ErrorMsg { msg: msg.clone() }), } } - builder.finish() } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 3814837e1..26fbb6e3d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,7 +1,3 @@ -use {File, SyntaxKind, Token}; - -use syntax_kinds::*; - #[macro_use] mod token_set; mod parser; @@ -9,6 +5,16 @@ mod input; mod event; mod grammar; +use std::sync::Arc; +use { + File, SyntaxKind, Token, + yellow::SyntaxNode, + syntax_kinds::* +}; +use GreenBuilder; +use parser::event::process; + + /// Parse a sequence of tokens into the representative node tree pub fn parse(text: String, tokens: &[Token]) -> File { let events = { @@ -21,6 +27,21 @@ pub fn parse(text: String, tokens: &[Token]) -> File { event::to_file(text, tokens, events) } +/// Parse a sequence of tokens into the representative node tree +pub fn parse_green(text: String, tokens: &[Token]) -> SyntaxNode { + let events = { + let input = input::ParserInput::new(&text, tokens); + let parser_impl = parser::imp::ParserImpl::new(&input); + let mut parser = parser::Parser(parser_impl); + grammar::file(&mut parser); + parser.0.into_events() + }; + let mut builder = GreenBuilder::new(text); + process(&mut builder, tokens, events); + let (green, errors) = builder.finish(); + SyntaxNode::new(Arc::new(green), errors) +} + fn is_insignificant(kind: SyntaxKind) -> bool { match kind { WHITESPACE | COMMENT => true, -- cgit v1.2.3