mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 07:16:05 +02:00
Add support for math-like externals (#4606)
This commit is contained in:
@ -13,7 +13,9 @@ pub use known_external::KnownExternal;
|
||||
pub use lex::{lex, Token, TokenContents};
|
||||
pub use lite_parse::{lite_parse, LiteBlock};
|
||||
|
||||
pub use parser::{parse, parse_block, parse_external_call, trim_quotes, Import};
|
||||
pub use parser::{
|
||||
is_math_expression_like, parse, parse_block, parse_external_call, trim_quotes, Import,
|
||||
};
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub use parse_keywords::parse_register;
|
||||
|
@ -41,7 +41,13 @@ fn is_identifier_byte(b: u8) -> bool {
|
||||
b != b'.' && b != b'[' && b != b'(' && b != b'{'
|
||||
}
|
||||
|
||||
fn is_math_expression_byte(b: u8) -> bool {
|
||||
pub fn is_math_expression_like(bytes: &[u8]) -> bool {
|
||||
if bytes.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let b = bytes[0];
|
||||
|
||||
b == b'0'
|
||||
|| b == b'1'
|
||||
|| b == b'2'
|
||||
@ -826,7 +832,13 @@ pub fn parse_call(
|
||||
// Find the longest group of words that could form a command
|
||||
let bytes = working_set.get_span_contents(*word_span);
|
||||
|
||||
if is_math_expression_byte(bytes[0]) {
|
||||
if is_math_expression_like(bytes)
|
||||
&& !working_set
|
||||
.permanent_state
|
||||
.external_exceptions
|
||||
.iter()
|
||||
.any(|x| x == bytes)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
@ -3446,7 +3458,13 @@ pub fn parse_expression(
|
||||
|
||||
let bytes = working_set.get_span_contents(spans[pos]);
|
||||
|
||||
let (output, err) = if is_math_expression_byte(bytes[0]) {
|
||||
let (output, err) = if is_math_expression_like(bytes)
|
||||
&& !working_set
|
||||
.permanent_state
|
||||
.external_exceptions
|
||||
.iter()
|
||||
.any(|x| x == bytes)
|
||||
{
|
||||
parse_math_expression(working_set, &spans[pos..], None)
|
||||
} else {
|
||||
// For now, check for special parses of certain keywords
|
||||
|
@ -169,6 +169,9 @@ pub struct EngineState {
|
||||
pub env_vars: im::HashMap<String, Value>,
|
||||
#[cfg(feature = "plugin")]
|
||||
pub plugin_signatures: Option<PathBuf>,
|
||||
|
||||
// A list of external commands that look like math expressions
|
||||
pub external_exceptions: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
pub const NU_VARIABLE_ID: usize = 0;
|
||||
@ -199,6 +202,7 @@ impl EngineState {
|
||||
env_vars: im::HashMap::new(),
|
||||
#[cfg(feature = "plugin")]
|
||||
plugin_signatures: None,
|
||||
external_exceptions: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@ -633,6 +637,7 @@ impl Default for EngineState {
|
||||
pub struct StateWorkingSet<'a> {
|
||||
pub permanent_state: &'a EngineState,
|
||||
pub delta: StateDelta,
|
||||
pub external_commands: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
/// A delta (or change set) between the current global state and a possible future global state. Deltas
|
||||
@ -707,6 +712,7 @@ impl<'a> StateWorkingSet<'a> {
|
||||
Self {
|
||||
delta: StateDelta::new(),
|
||||
permanent_state,
|
||||
external_commands: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user