Move from using a Block to an Arc'd Block (#3289)

This commit is contained in:
Jonathan Turner
2021-04-09 20:12:25 +12:00
committed by GitHub
parent ac070ae942
commit b791d1ab0d
11 changed files with 81 additions and 54 deletions

View File

@ -1,4 +1,4 @@
use std::path::Path;
use std::{path::Path, sync::Arc};
use indexmap::IndexMap;
use log::trace;
@ -428,7 +428,9 @@ fn parse_invocation(
let (mut classified_block, err) = classify_block(&lite_block, scope);
scope.exit_scope();
classified_block.set_redirect(ExternalRedirection::Stdout);
if let Some(x) = std::sync::Arc::<nu_protocol::hir::Block>::get_mut(&mut classified_block) {
x.set_redirect(ExternalRedirection::Stdout);
}
(
SpannedExpression::new(Expression::Invocation(classified_block), lite_arg.span),
@ -638,12 +640,12 @@ fn parse_interpolated_string(
let group = Group::new(pipelines, lite_arg.span);
let call = SpannedExpression {
expr: Expression::Invocation(Block::new(
expr: Expression::Invocation(Arc::new(Block::new(
Signature::new("<invocation>"),
vec![group],
IndexMap::new(),
lite_arg.span,
)),
))),
span: lite_arg.span,
};
@ -1286,7 +1288,7 @@ fn parse_positional_argument(
span,
);
let arg = SpannedExpression::new(Expression::Block(block), span);
let arg = SpannedExpression::new(Expression::Block(Arc::new(block)), span);
idx = new_idx - 1;
if error.is_none() {
@ -1329,7 +1331,7 @@ fn parse_positional_argument(
span,
);
let arg = SpannedExpression::new(Expression::Block(block), span);
let arg = SpannedExpression::new(Expression::Block(Arc::new(block)), span);
idx = new_idx - 1;
if error.is_none() {
@ -1948,7 +1950,7 @@ fn parse_alias(call: &LiteCommand, scope: &dyn ParserScope) -> Option<ParseError
pub fn classify_block(
lite_block: &LiteBlock,
scope: &dyn ParserScope,
) -> (Block, Option<ParseError>) {
) -> (Arc<Block>, Option<ParseError>) {
let mut output = Block::basic();
let mut error = None;
@ -2012,7 +2014,7 @@ pub fn classify_block(
span: Span::new(vars.0.span.start(), vars.1.span.end()),
},
SpannedExpression {
expr: Expression::Block(block),
expr: Expression::Block(Arc::new(block)),
span,
},
]);
@ -2048,21 +2050,21 @@ pub fn classify_block(
}
output.infer_params();
(output, error)
(Arc::new(output), error)
}
pub fn parse(
input: &str,
span_offset: usize,
scope: &dyn ParserScope,
) -> (Block, Option<ParseError>) {
) -> (Arc<Block>, Option<ParseError>) {
let (output, error) = lex(input, span_offset);
if error.is_some() {
return (Block::basic(), error);
return (Arc::new(Block::basic()), error);
}
let (lite_block, error) = parse_block(output);
if error.is_some() {
return (Block::basic(), error);
return (Arc::new(Block::basic()), error);
}
classify_block(&lite_block, scope)

View File

@ -1,3 +1,5 @@
use std::sync::Arc;
use crate::{
lex::tokens::LiteCommand,
parse::{classify_block, util::trim_quotes},
@ -64,8 +66,11 @@ pub(crate) fn parse_definition(call: &LiteCommand, scope: &dyn ParserScope) -> O
let (mut block, err) = classify_block(&lite_block, scope);
scope.exit_scope();
block.params = signature;
block.params.name = name;
if let Some(block) = std::sync::Arc::<nu_protocol::hir::Block>::get_mut(&mut block)
{
block.params = signature;
block.params.name = name;
}
scope.add_definition(block);
@ -100,7 +105,12 @@ pub(crate) fn parse_definition_prototype(
err = error;
}
scope.add_definition(Block::new(signature, vec![], IndexMap::new(), call.span()));
scope.add_definition(Arc::new(Block::new(
signature,
vec![],
IndexMap::new(),
call.span(),
)));
err
}

View File

@ -1,15 +1,15 @@
use nu_protocol::hir::Block;
use nu_source::Spanned;
use std::fmt::Debug;
use std::{fmt::Debug, sync::Arc};
pub trait ParserScope: Debug {
fn get_signature(&self, name: &str) -> Option<nu_protocol::Signature>;
fn has_signature(&self, name: &str) -> bool;
fn add_definition(&self, block: Block);
fn add_definition(&self, block: Arc<Block>);
fn get_definitions(&self) -> Vec<Block>;
fn get_definitions(&self) -> Vec<Arc<Block>>;
fn get_alias(&self, name: &str) -> Option<Vec<Spanned<String>>>;