From f7a19d37c6f14704b5c6b376bad8c0a9eb2df158 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Mon, 6 Sep 2021 21:41:30 +0100 Subject: [PATCH] one parser function --- crates/nu-cli/src/syntax_highlight.rs | 4 +-- crates/nu-parser/src/lib.rs | 2 +- crates/nu-parser/src/parser.rs | 37 ++++++++------------------- crates/nu-parser/tests/test_parser.rs | 34 ++++++++++++------------ src/main.rs | 8 +++--- 5 files changed, 35 insertions(+), 50 deletions(-) diff --git a/crates/nu-cli/src/syntax_highlight.rs b/crates/nu-cli/src/syntax_highlight.rs index 4df2e42c97..3471b04128 100644 --- a/crates/nu-cli/src/syntax_highlight.rs +++ b/crates/nu-cli/src/syntax_highlight.rs @@ -1,5 +1,5 @@ use nu_ansi_term::Style; -use nu_parser::{flatten_block, parse_source, FlatShape}; +use nu_parser::{flatten_block, parse, FlatShape}; use nu_protocol::engine::{EngineState, StateWorkingSet}; use reedline::{Highlighter, StyledText}; use std::{cell::RefCell, rc::Rc}; @@ -13,7 +13,7 @@ impl Highlighter for NuHighlighter { let (shapes, global_span_offset) = { let engine_state = self.engine_state.borrow(); let mut working_set = StateWorkingSet::new(&*engine_state); - let (block, _) = parse_source(&mut working_set, line.as_bytes(), false); + let (block, _) = parse(&mut working_set, None, line.as_bytes(), false); let shapes = flatten_block(&working_set, &block); (shapes, engine_state.next_span_start()) diff --git a/crates/nu-parser/src/lib.rs b/crates/nu-parser/src/lib.rs index fb2d2e8e2e..cfd9535f70 100644 --- a/crates/nu-parser/src/lib.rs +++ b/crates/nu-parser/src/lib.rs @@ -9,4 +9,4 @@ pub use errors::ParseError; pub use flatten::{flatten_block, FlatShape}; pub use lex::{lex, Token, TokenContents}; pub use lite_parse::{lite_parse, LiteBlock}; -pub use parser::{parse_file, parse_source, Import, VarDecl}; +pub use parser::{parse, Import, VarDecl}; diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index a3adc73974..347629f7a8 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -2443,9 +2443,12 @@ pub fn parse_block( (block, error) } -pub fn parse_file( +// Parses a vector of u8 to create an AST Block. If a file name is given, then +// the name is stored in the working set. When parsing a source without a file +// name, the source of bytes is stored as "source" +pub fn parse( working_set: &mut StateWorkingSet, - fname: &str, + fname: Option<&str>, contents: &[u8], scoped: bool, ) -> (Block, Option) { @@ -2453,7 +2456,12 @@ pub fn parse_file( let span_offset = working_set.next_span_start(); - working_set.add_file(fname.into(), contents); + let name = match fname { + Some(fname) => fname.to_string(), + None => "source".to_string(), + }; + + working_set.add_file(name, contents); let (output, err) = lex(contents, span_offset, &[], &[]); error = error.or(err); @@ -2466,26 +2474,3 @@ pub fn parse_file( (output, error) } - -pub fn parse_source( - working_set: &mut StateWorkingSet, - source: &[u8], - scoped: bool, -) -> (Block, Option) { - let mut error = None; - - let span_offset = working_set.next_span_start(); - - working_set.add_file("source".into(), source); - - let (output, err) = lex(source, span_offset, &[], &[]); - error = error.or(err); - - let (output, err) = lite_parse(&output); - error = error.or(err); - - let (output, err) = parse_block(working_set, &output, scoped); - error = error.or(err); - - (output, error) -} diff --git a/crates/nu-parser/tests/test_parser.rs b/crates/nu-parser/tests/test_parser.rs index 781cfd9577..ef52f1fbcf 100644 --- a/crates/nu-parser/tests/test_parser.rs +++ b/crates/nu-parser/tests/test_parser.rs @@ -11,7 +11,7 @@ pub fn parse_int() { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"3", true); + let (block, err) = parse(&mut working_set, None, b"3", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -38,7 +38,7 @@ pub fn parse_call() { let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')); working_set.add_decl(sig.predeclare()); - let (block, err) = parse_source(&mut working_set, b"foo", true); + let (block, err) = parse(&mut working_set, None, b"foo", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -67,7 +67,7 @@ pub fn parse_call_missing_flag_arg() { let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')); working_set.add_decl(sig.predeclare()); - let (_, err) = parse_source(&mut working_set, b"foo --jazz", true); + let (_, err) = parse(&mut working_set, None, b"foo --jazz", true); assert!(matches!(err, Some(ParseError::MissingFlagParam(..)))); } @@ -79,7 +79,7 @@ pub fn parse_call_missing_short_flag_arg() { let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')); working_set.add_decl(sig.predeclare()); - let (_, err) = parse_source(&mut working_set, b"foo -j", true); + let (_, err) = parse(&mut working_set, None, b"foo -j", true); assert!(matches!(err, Some(ParseError::MissingFlagParam(..)))); } @@ -92,7 +92,7 @@ pub fn parse_call_too_many_shortflag_args() { .named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')) .named("--math", SyntaxShape::Int, "math!!", Some('m')); working_set.add_decl(sig.predeclare()); - let (_, err) = parse_source(&mut working_set, b"foo -mj", true); + let (_, err) = parse(&mut working_set, None, b"foo -mj", true); assert!(matches!( err, Some(ParseError::ShortFlagBatchCantTakeArg(..)) @@ -106,7 +106,7 @@ pub fn parse_call_unknown_shorthand() { let sig = Signature::build("foo").switch("--jazz", "jazz!!", Some('j')); working_set.add_decl(sig.predeclare()); - let (_, err) = parse_source(&mut working_set, b"foo -mj", true); + let (_, err) = parse(&mut working_set, None, b"foo -mj", true); assert!(matches!(err, Some(ParseError::UnknownFlag(..)))); } @@ -117,7 +117,7 @@ pub fn parse_call_extra_positional() { let sig = Signature::build("foo").switch("--jazz", "jazz!!", Some('j')); working_set.add_decl(sig.predeclare()); - let (_, err) = parse_source(&mut working_set, b"foo -j 100", true); + let (_, err) = parse(&mut working_set, None, b"foo -j 100", true); assert!(matches!(err, Some(ParseError::ExtraPositional(..)))); } @@ -128,7 +128,7 @@ pub fn parse_call_missing_req_positional() { let sig = Signature::build("foo").required("jazz", SyntaxShape::Int, "jazz!!"); working_set.add_decl(sig.predeclare()); - let (_, err) = parse_source(&mut working_set, b"foo", true); + let (_, err) = parse(&mut working_set, None, b"foo", true); assert!(matches!(err, Some(ParseError::MissingPositional(..)))); } @@ -139,7 +139,7 @@ pub fn parse_call_missing_req_flag() { let sig = Signature::build("foo").required_named("--jazz", SyntaxShape::Int, "jazz!!", None); working_set.add_decl(sig.predeclare()); - let (_, err) = parse_source(&mut working_set, b"foo", true); + let (_, err) = parse(&mut working_set, None, b"foo", true); assert!(matches!(err, Some(ParseError::MissingRequiredFlag(..)))); } @@ -152,7 +152,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"0..10", true); + let (block, err) = parse(&mut working_set, None, b"0..10", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -183,7 +183,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"0..<10", true); + let (block, err) = parse(&mut working_set, None, b"0..<10", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -214,7 +214,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"(3 - 3)..<(8 + 2)", true); + let (block, err) = parse(&mut working_set, None, b"(3 - 3)..<(8 + 2)", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -245,7 +245,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"let a = 2; $a..10", true); + let (block, err) = parse(&mut working_set, None, b"let a = 2; $a..10", true); assert!(err.is_none()); assert!(block.len() == 2); @@ -276,7 +276,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"let a = 2; $a..<($a + 10)", true); + let (block, err) = parse(&mut working_set, None, b"let a = 2; $a..<($a + 10)", true); assert!(err.is_none()); assert!(block.len() == 2); @@ -307,7 +307,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"0..", true); + let (block, err) = parse(&mut working_set, None, b"0..", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -338,7 +338,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = parse_source(&mut working_set, b"-10..-3", true); + let (block, err) = parse(&mut working_set, None, b"-10..-3", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -369,7 +369,7 @@ mod range { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (_, err) = parse_source(&mut working_set, b"(0)..\"a\"", true); + let (_, err) = parse(&mut working_set, None, b"(0)..\"a\"", true); assert!(err.is_some()); } diff --git a/src/main.rs b/src/main.rs index 0437eef936..58267f0f33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use nu_cli::{report_parsing_error, report_shell_error, NuHighlighter}; use nu_command::create_default_context; use nu_engine::eval_block; -use nu_parser::parse_file; +use nu_parser::parse; use nu_protocol::{ engine::{EngineState, EvaluationContext, StateWorkingSet}, Value, @@ -19,7 +19,7 @@ fn main() -> std::io::Result<()> { let (block, delta) = { let engine_state = engine_state.borrow(); let mut working_set = StateWorkingSet::new(&*engine_state); - let (output, err) = parse_file(&mut working_set, &path, &file, false); + let (output, err) = parse(&mut working_set, Some(&path), &file, false); if let Some(err) = err { let _ = report_parsing_error(&working_set, &err); @@ -89,9 +89,9 @@ fn main() -> std::io::Result<()> { let (block, delta) = { let engine_state = engine_state.borrow(); let mut working_set = StateWorkingSet::new(&*engine_state); - let (output, err) = parse_file( + let (output, err) = parse( &mut working_set, - &format!("line_{}", current_line), + Some(&format!("line_{}", current_line)), s.as_bytes(), false, );