Merge pull request #25 from elferherrera/one-parse-fn

One parser function
This commit is contained in:
JT 2021-09-07 08:47:16 +12:00 committed by GitHub
commit f71b7e89e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 50 deletions

View File

@ -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())

View File

@ -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};

View File

@ -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<ParseError>) {
@ -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<ParseError>) {
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)
}

View File

@ -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());
}

View File

@ -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,
);