From 07c22c7e817c072010ea2de8f50746d1c00575e8 Mon Sep 17 00:00:00 2001 From: JT Date: Thu, 22 Jul 2021 19:48:45 +1200 Subject: [PATCH] Start working on highlighter --- src/main.rs | 4 ++-- src/parser_state.rs | 8 +++++-- src/syntax_highlight.rs | 48 +++++++++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 385805e8c..e8eba65c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,7 +74,7 @@ fn main() -> std::io::Result<()> { .required("y", SyntaxShape::Int, "y value"); working_set.add_decl(sig.into()); - working_set.delta + working_set.render() }; { @@ -134,7 +134,7 @@ fn main() -> std::io::Result<()> { ); println!("{:#?}", output); println!("Error: {:?}", err); - working_set.delta + working_set.render() }; ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta); diff --git a/src/parser_state.rs b/src/parser_state.rs index 894687a11..59066e849 100644 --- a/src/parser_state.rs +++ b/src/parser_state.rs @@ -1,5 +1,5 @@ use crate::{parser::Block, Declaration, Span}; -use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc}; +use std::collections::HashMap; #[derive(Debug)] pub struct ParserState { @@ -327,6 +327,10 @@ impl<'a> ParserWorkingSet<'a> { self.delta.decls.get(decl_id - num_permanent_decls).cloned() } } + + pub fn render(self) -> ParserDelta { + self.delta + } } #[cfg(test)] @@ -362,7 +366,7 @@ mod parser_state_tests { let delta = { let mut working_set = ParserWorkingSet::new(&parser_state); working_set.add_file("child.nu".into(), &[]); - working_set.delta + working_set.render() }; ParserState::merge_delta(&mut parser_state, delta); diff --git a/src/syntax_highlight.rs b/src/syntax_highlight.rs index 22f31f63a..8cddcf2ef 100644 --- a/src/syntax_highlight.rs +++ b/src/syntax_highlight.rs @@ -1,24 +1,36 @@ -use std::sync::Arc; +use std::{cell::RefCell, rc::Rc}; use crate::{Block, Expr, Expression, ParserState, ParserWorkingSet, Statement}; -fn syntax_highlight<'a, 'b>(parser_state: &'a ParserState, input: &'b [u8]) { - // let mut working_set = ParserWorkingSet::new(parser_state); - - // let (block, _) = working_set.parse_source(input, false); - - // for stmt in &block.stmts { - // match stmt { - // Statement::Expression(expr) => { - - // } - // } - // } - // No merge at the end because this parse is speculative +struct Highlighter { + parser_state: Rc>, } -fn highlight_expression(expression: &Expression) { - // match &expression.expr { - // Expr::BinaryOp() - // } +impl Highlighter { + fn syntax_highlight(&self, input: &[u8]) { + let block = { + let parser_state = self.parser_state.borrow(); + let mut working_set = ParserWorkingSet::new(&*parser_state); + let (block, _) = working_set.parse_source(input, false); + + block + }; + + // let (block, _) = working_set.parse_source(input, false); + + // for stmt in &block.stmts { + // match stmt { + // Statement::Expression(expr) => { + + // } + // } + // } + // No merge at the end because this parse is speculative + } + + fn highlight_expression(expression: &Expression) { + // match &expression.expr { + // Expr::BinaryOp() + // } + } }