Add pattern matching (#8590)

# Description

This adds `match` and basic pattern matching.

An example:

```
match $x {
  1..10 => { print "Value is between 1 and 10" }
  { foo: $bar } => { print $"Value has a 'foo' field with value ($bar)" }
  [$a, $b] => { print $"Value is a list with two items: ($a) and ($b)" }
  _ => { print "Value is none of the above" }
}
```

Like the recent changes to `if` to allow it to be used as an expression,
`match` can also be used as an expression. This allows you to assign the
result to a variable, eg) `let xyz = match ...`

I've also included a short-hand pattern for matching records, as I think
it might help when doing a lot of record patterns: `{$foo}` which is
equivalent to `{foo: $foo}`.

There are still missing components, so consider this the first step in
full pattern matching support. Currently missing:
* Patterns for strings
* Or-patterns (like the `|` in Rust)
* Patterns for tables (unclear how we want to match a table, so it'll
need some design)
* Patterns for binary values
* And much more

# User-Facing Changes

[see above]

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
JT 2023-03-24 14:52:01 +13:00 committed by GitHub
parent be52f7fb07
commit 2c3aade057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 955 additions and 7 deletions

View File

@ -126,6 +126,7 @@ impl Highlighter for NuHighlighter {
FlatShape::Or => add_colored_token!(shape.1, next_token), FlatShape::Or => add_colored_token!(shape.1, next_token),
FlatShape::Redirection => add_colored_token!(shape.1, next_token), FlatShape::Redirection => add_colored_token!(shape.1, next_token),
FlatShape::Custom(..) => add_colored_token!(shape.1, next_token), FlatShape::Custom(..) => add_colored_token!(shape.1, next_token),
FlatShape::MatchPattern => add_colored_token!(shape.1, next_token),
} }
last_seen_span = shape.0.end; last_seen_span = shape.0.end;
} }
@ -308,6 +309,8 @@ fn find_matching_block_end_in_expr(
Expr::ImportPattern(_) => None, Expr::ImportPattern(_) => None,
Expr::Overlay(_) => None, Expr::Overlay(_) => None,
Expr::Signature(_) => None, Expr::Signature(_) => None,
Expr::MatchPattern(_) => None,
Expr::MatchBlock(_) => None,
Expr::Nothing => None, Expr::Nothing => None,
Expr::Garbage => None, Expr::Garbage => None,

View File

@ -0,0 +1,128 @@
use nu_engine::{eval_block, eval_expression_with_input, CallExt};
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EngineState, Matcher, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct Match;
impl Command for Match {
fn name(&self) -> &str {
"match"
}
fn usage(&self) -> &str {
"Conditionally run a block on a matched value."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("match")
.input_output_types(vec![(Type::Any, Type::Any)])
.required("value", SyntaxShape::Any, "value to check")
.required(
"match_block",
SyntaxShape::MatchBlock,
"block to run if check succeeds",
)
.category(Category::Core)
}
fn extra_usage(&self) -> &str {
r#"This command is a parser keyword. For details, check:
https://www.nushell.sh/book/thinking_in_nu.html"#
}
fn is_parser_keyword(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let value: Value = call.req(engine_state, stack, 0)?;
let block = call.positional_nth(1);
if let Some(Expression {
expr: Expr::MatchBlock(matches),
..
}) = block
{
for match_ in matches {
let mut match_variables = vec![];
if match_.0.match_value(&value, &mut match_variables) {
// This case does match, go ahead and return the evaluated expression
for match_variable in match_variables {
stack.add_var(match_variable.0, match_variable.1);
}
if let Some(block_id) = match_.1.as_block() {
let block = engine_state.get_block(block_id);
return eval_block(
engine_state,
stack,
block,
input,
call.redirect_stdout,
call.redirect_stderr,
);
} else {
return eval_expression_with_input(
engine_state,
stack,
&match_.1,
input,
call.redirect_stdout,
call.redirect_stderr,
)
.map(|x| x.0);
}
}
}
}
Ok(PipelineData::Empty)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Match on a value in range",
example: "match 3 { 1..10 => 'yes!' }",
result: Some(Value::test_string("yes!")),
},
Example {
description: "Match on a field in a record",
example: "match {a: 100} { {a: $my_value} => { $my_value } }",
result: Some(Value::test_int(100)),
},
Example {
description: "Match with a catch-all",
example: "match 3 { 1 => { 'yes!' }, _ => { 'no!' } }",
result: Some(Value::test_string("no!")),
},
Example {
description: "Match against a list",
example: "match [1, 2, 3] { [$a, $b, $c] => { $a + $b + $c }, _ => 0 }",
result: Some(Value::test_int(6)),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(Match {})
}
}

View File

@ -29,6 +29,7 @@ mod if_;
mod ignore; mod ignore;
mod let_; mod let_;
mod loop_; mod loop_;
mod match_;
mod module; mod module;
mod mut_; mod mut_;
pub(crate) mod overlay; pub(crate) mod overlay;
@ -69,6 +70,7 @@ pub use if_::If;
pub use ignore::Ignore; pub use ignore::Ignore;
pub use let_::Let; pub use let_::Let;
pub use loop_::Loop; pub use loop_::Loop;
pub use match_::Match;
pub use module::Module; pub use module::Module;
pub use mut_::Mut; pub use mut_::Mut;
pub use overlay::*; pub use overlay::*;

View File

@ -52,6 +52,7 @@ pub fn create_default_context() -> EngineState {
OverlayHide, OverlayHide,
Let, Let,
Loop, Loop,
Match,
Module, Module,
Mut, Mut,
Return, Return,

View File

@ -23,6 +23,7 @@ pub fn default_shape_color(shape: String) -> Style {
"shape_internalcall" => Style::new().fg(Color::Cyan).bold(), "shape_internalcall" => Style::new().fg(Color::Cyan).bold(),
"shape_list" => Style::new().fg(Color::Cyan).bold(), "shape_list" => Style::new().fg(Color::Cyan).bold(),
"shape_literal" => Style::new().fg(Color::Blue), "shape_literal" => Style::new().fg(Color::Blue),
"shape_match_pattern" => Style::new().fg(Color::Green),
"shape_nothing" => Style::new().fg(Color::LightCyan), "shape_nothing" => Style::new().fg(Color::LightCyan),
"shape_operator" => Style::new().fg(Color::Yellow), "shape_operator" => Style::new().fg(Color::Yellow),
"shape_or" => Style::new().fg(Color::Purple).bold(), "shape_or" => Style::new().fg(Color::Purple).bold(),

View File

@ -270,6 +270,7 @@ fn nu_value_to_string(value: Value, separator: &str) -> String {
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.into_string(), Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(), Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
} }
} }

View File

@ -328,5 +328,6 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.into_string(), Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(), Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
} }
} }

View File

@ -404,6 +404,7 @@ fn find_with_rest_and_highlight(
Err(_) => false, Err(_) => false,
}, },
Value::Binary { .. } => false, Value::Binary { .. } => false,
Value::MatchPattern { .. } => false,
}) != invert }) != invert
}, },
ctrlc, ctrlc,
@ -484,6 +485,7 @@ fn find_with_rest_and_highlight(
Err(_) => false, Err(_) => false,
}, },
Value::Binary { .. } => false, Value::Binary { .. } => false,
Value::MatchPattern { .. } => false,
}) != invert }) != invert
}), }),
ctrlc.clone(), ctrlc.clone(),

View File

@ -249,6 +249,12 @@ fn convert_to_value(
"extra tokens in input file".into(), "extra tokens in input file".into(),
expr.span, expr.span,
)), )),
Expr::MatchPattern(..) => Err(ShellError::OutsideSpannedLabeledError(
original_text.to_string(),
"Error when loading".into(),
"extra tokens in input file".into(),
expr.span,
)),
Expr::GlobPattern(val) => Ok(Value::String { val, span }), Expr::GlobPattern(val) => Ok(Value::String { val, span }),
Expr::ImportPattern(..) => Err(ShellError::OutsideSpannedLabeledError( Expr::ImportPattern(..) => Err(ShellError::OutsideSpannedLabeledError(
original_text.to_string(), original_text.to_string(),
@ -277,6 +283,12 @@ fn convert_to_value(
Ok(Value::List { vals: output, span }) Ok(Value::List { vals: output, span })
} }
Expr::MatchBlock(..) => Err(ShellError::OutsideSpannedLabeledError(
original_text.to_string(),
"Error when loading".into(),
"match blocks not supported in nuon".into(),
expr.span,
)),
Expr::Nothing => Ok(Value::Nothing { span }), Expr::Nothing => Ok(Value::Nothing { span }),
Expr::Operator(..) => Err(ShellError::OutsideSpannedLabeledError( Expr::Operator(..) => Err(ShellError::OutsideSpannedLabeledError(
original_text.to_string(), original_text.to_string(),

View File

@ -127,7 +127,10 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?), Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?),
Value::Error { error } => return Err(*error.clone()), Value::Error { error } => return Err(*error.clone()),
Value::Closure { .. } | Value::Block { .. } | Value::Range { .. } => nu_json::Value::Null, Value::Closure { .. }
| Value::Block { .. }
| Value::Range { .. }
| Value::MatchPattern { .. } => nu_json::Value::Null,
Value::Binary { val, .. } => { Value::Binary { val, .. } => {
nu_json::Value::Array(val.iter().map(|x| nu_json::Value::U64(*x as u64)).collect()) nu_json::Value::Array(val.iter().map(|x| nu_json::Value::U64(*x as u64)).collect())
} }

View File

@ -246,6 +246,12 @@ pub fn value_to_string(
)) ))
} }
} }
Value::MatchPattern { .. } => Err(ShellError::UnsupportedInput(
"match patterns are currently not nuon-compatible".to_string(),
"value originates from here".into(),
span,
v.expect_span(),
)),
Value::Nothing { .. } => Ok("null".to_string()), Value::Nothing { .. } => Ok("null".to_string()),
Value::Range { val, .. } => Ok(format!( Value::Range { val, .. } => Ok(format!(
"{}..{}{}", "{}..{}{}",

View File

@ -153,6 +153,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.into_string(), Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(), Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
} }
} }

View File

@ -93,6 +93,7 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
.collect::<Result<Vec<toml::Value>, ShellError>>()?, .collect::<Result<Vec<toml::Value>, ShellError>>()?,
), ),
Value::CustomValue { .. } => toml::Value::String("<Custom Value>".to_string()), Value::CustomValue { .. } => toml::Value::String("<Custom Value>".to_string()),
Value::MatchPattern { .. } => toml::Value::String("<Match Pattern>".to_string()),
}) })
} }

View File

@ -97,6 +97,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
.collect::<Result<Vec<serde_yaml::Value>, ShellError>>()?, .collect::<Result<Vec<serde_yaml::Value>, ShellError>>()?,
), ),
Value::CustomValue { .. } => serde_yaml::Value::Null, Value::CustomValue { .. } => serde_yaml::Value::Null,
Value::MatchPattern { .. } => serde_yaml::Value::Null,
}) })
} }

View File

@ -335,6 +335,11 @@ pub fn eval_expression(
span: expr.span, span: expr.span,
}), }),
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }), Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
Expr::MatchPattern(pattern) => Ok(Value::MatchPattern {
val: pattern.clone(),
span: expr.span,
}),
Expr::MatchBlock(_) => Ok(Value::Nothing { span: expr.span }), // match blocks are handled by `match`
Expr::UnaryNot(expr) => { Expr::UnaryNot(expr) => {
let lhs = eval_expression(engine_state, stack, expr)?; let lhs = eval_expression(engine_state, stack, expr)?;
match lhs { match lhs {

View File

@ -1,5 +1,6 @@
use nu_protocol::ast::{ use nu_protocol::ast::{
Block, Expr, Expression, ImportPatternMember, PathMember, Pipeline, PipelineElement, Block, Expr, Expression, ImportPatternMember, MatchPattern, PathMember, Pattern, Pipeline,
PipelineElement,
}; };
use nu_protocol::DeclId; use nu_protocol::DeclId;
use nu_protocol::{engine::StateWorkingSet, Span}; use nu_protocol::{engine::StateWorkingSet, Span};
@ -25,6 +26,7 @@ pub enum FlatShape {
InternalCall, InternalCall,
List, List,
Literal, Literal,
MatchPattern,
Nothing, Nothing,
Operator, Operator,
Or, Or,
@ -60,6 +62,7 @@ impl Display for FlatShape {
FlatShape::InternalCall => write!(f, "shape_internalcall"), FlatShape::InternalCall => write!(f, "shape_internalcall"),
FlatShape::List => write!(f, "shape_list"), FlatShape::List => write!(f, "shape_list"),
FlatShape::Literal => write!(f, "shape_literal"), FlatShape::Literal => write!(f, "shape_literal"),
FlatShape::MatchPattern => write!(f, "shape_match_pattern"),
FlatShape::Nothing => write!(f, "shape_nothing"), FlatShape::Nothing => write!(f, "shape_nothing"),
FlatShape::Operator => write!(f, "shape_operator"), FlatShape::Operator => write!(f, "shape_operator"),
FlatShape::Or => write!(f, "shape_or"), FlatShape::Or => write!(f, "shape_or"),
@ -212,6 +215,20 @@ pub fn flatten_expression(
Expr::Float(_) => { Expr::Float(_) => {
vec![(expr.span, FlatShape::Float)] vec![(expr.span, FlatShape::Float)]
} }
Expr::MatchPattern(pattern) => {
// FIXME: do nicer flattening later
flatten_pattern(pattern)
}
Expr::MatchBlock(matches) => {
let mut output = vec![];
for match_ in matches {
output.extend(flatten_pattern(&match_.0));
output.extend(flatten_expression(working_set, &match_.1));
}
output
}
Expr::ValueWithUnit(x, unit) => { Expr::ValueWithUnit(x, unit) => {
let mut output = flatten_expression(working_set, x); let mut output = flatten_expression(working_set, x);
output.push((unit.span, FlatShape::String)); output.push((unit.span, FlatShape::String));
@ -488,3 +505,60 @@ pub fn flatten_pipeline(
} }
output output
} }
pub fn flatten_pattern(match_pattern: &MatchPattern) -> Vec<(Span, FlatShape)> {
let mut output = vec![];
match &match_pattern.pattern {
Pattern::Garbage => {
output.push((match_pattern.span, FlatShape::Garbage));
}
Pattern::IgnoreValue => {
output.push((match_pattern.span, FlatShape::Nothing));
}
Pattern::List(items) => {
if let Some(first) = items.first() {
if let Some(last) = items.last() {
output.push((
Span::new(match_pattern.span.start, first.span.start),
FlatShape::MatchPattern,
));
for item in items {
output.extend(flatten_pattern(item));
}
output.push((
Span::new(last.span.end, match_pattern.span.end),
FlatShape::MatchPattern,
))
}
} else {
output.push((match_pattern.span, FlatShape::MatchPattern));
}
}
Pattern::Record(items) => {
if let Some(first) = items.first() {
if let Some(last) = items.last() {
output.push((
Span::new(match_pattern.span.start, first.1.span.start),
FlatShape::MatchPattern,
));
for item in items {
output.extend(flatten_pattern(&item.1));
}
output.push((
Span::new(last.1.span.end, match_pattern.span.end),
FlatShape::MatchPattern,
))
}
} else {
output.push((match_pattern.span, FlatShape::MatchPattern));
}
}
Pattern::Value(_) => {
output.push((match_pattern.span, FlatShape::MatchPattern));
}
Pattern::Variable(_) => {
output.push((match_pattern.span, FlatShape::Variable));
}
}
output
}

View File

@ -6,6 +6,7 @@ mod known_external;
mod lex; mod lex;
mod lite_parser; mod lite_parser;
mod parse_keywords; mod parse_keywords;
mod parse_patterns;
mod parser; mod parser;
mod type_check; mod type_check;

View File

@ -0,0 +1,248 @@
use nu_protocol::{
ast::{Expr, Expression, MatchPattern, Pattern},
engine::StateWorkingSet,
Span, SyntaxShape, Type,
};
use crate::{
lex, lite_parse,
parser::{is_variable, parse_value},
LiteElement, ParseError,
};
pub fn garbage(span: Span) -> MatchPattern {
MatchPattern {
pattern: Pattern::Garbage,
span,
}
}
pub fn parse_match_pattern(
working_set: &mut StateWorkingSet,
span: Span,
) -> (Expression, Option<ParseError>) {
working_set.enter_scope();
let (output, err) = parse_pattern(working_set, span);
working_set.exit_scope();
(
Expression {
expr: Expr::MatchPattern(Box::new(output)),
span,
ty: Type::Any,
custom_completion: None,
},
err,
)
}
pub fn parse_pattern(
working_set: &mut StateWorkingSet,
span: Span,
) -> (MatchPattern, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
if bytes.starts_with(b"$") {
// Variable pattern
parse_variable_pattern(working_set, span)
} else if bytes.starts_with(b"{") {
// Record pattern
parse_record_pattern(working_set, span)
} else if bytes.starts_with(b"[") {
// List pattern
parse_list_pattern(working_set, span)
} else if bytes == b"_" {
(
MatchPattern {
pattern: Pattern::IgnoreValue,
span,
},
None,
)
} else {
// Literal value
let (value, error) = parse_value(working_set, span, &SyntaxShape::Any, &[]);
(
MatchPattern {
pattern: Pattern::Value(value),
span,
},
error,
)
}
}
pub fn parse_variable_pattern(
working_set: &mut StateWorkingSet,
span: Span,
) -> (MatchPattern, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
if is_variable(bytes) {
if let Some(var_id) = working_set.find_variable(bytes) {
(
MatchPattern {
pattern: Pattern::Variable(var_id),
span,
},
None,
)
} else {
let var_id = working_set.add_variable(bytes.to_vec(), span, Type::Any, true);
(
MatchPattern {
pattern: Pattern::Variable(var_id),
span,
},
None,
)
}
} else {
(
garbage(span),
Some(ParseError::Expected("valid variable name".into(), span)),
)
}
}
pub fn parse_list_pattern(
working_set: &mut StateWorkingSet,
span: Span,
) -> (MatchPattern, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let mut error = None;
let mut start = span.start;
let mut end = span.end;
if bytes.starts_with(b"[") {
start += 1;
}
if bytes.ends_with(b"]") {
end -= 1;
} else {
error = error.or_else(|| Some(ParseError::Unclosed("]".into(), Span::new(end, end))));
}
let inner_span = Span::new(start, end);
let source = working_set.get_span_contents(inner_span);
let (output, err) = lex(source, inner_span.start, &[b'\n', b'\r', b','], &[], true);
error = error.or(err);
let (output, err) = lite_parse(&output);
error = error.or(err);
let mut args = vec![];
if !output.block.is_empty() {
for arg in &output.block[0].commands {
let mut spans_idx = 0;
if let LiteElement::Command(_, command) = arg {
while spans_idx < command.parts.len() {
let (arg, err) = parse_pattern(working_set, command.parts[spans_idx]);
error = error.or(err);
args.push(arg);
spans_idx += 1;
}
}
}
}
(
MatchPattern {
pattern: Pattern::List(args),
span,
},
error,
)
}
pub fn parse_record_pattern(
working_set: &mut StateWorkingSet,
span: Span,
) -> (MatchPattern, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let mut error = None;
let mut start = span.start;
let mut end = span.end;
if bytes.starts_with(b"{") {
start += 1;
} else {
error = error.or_else(|| {
Some(ParseError::Expected(
"{".into(),
Span::new(start, start + 1),
))
});
}
if bytes.ends_with(b"}") {
end -= 1;
} else {
error = error.or_else(|| Some(ParseError::Unclosed("}".into(), Span::new(end, end))));
}
let inner_span = Span::new(start, end);
let source = working_set.get_span_contents(inner_span);
let (tokens, err) = lex(source, start, &[b'\n', b'\r', b','], &[b':'], true);
error = error.or(err);
let mut output = vec![];
let mut idx = 0;
while idx < tokens.len() {
let bytes = working_set.get_span_contents(tokens[idx].span);
let (field, pattern) = if !bytes.is_empty() && bytes[0] == b'$' {
// If this is a variable, treat it as both the name of the field and the pattern
let field = String::from_utf8_lossy(&bytes[1..]).to_string();
let (pattern, err) = parse_variable_pattern(working_set, tokens[idx].span);
error = error.or(err);
(field, pattern)
} else {
let field = String::from_utf8_lossy(bytes).to_string();
idx += 1;
if idx == tokens.len() {
return (
garbage(span),
Some(ParseError::Expected("record".into(), span)),
);
}
let colon = working_set.get_span_contents(tokens[idx].span);
idx += 1;
if idx == tokens.len() || colon != b":" {
//FIXME: need better error
return (
garbage(span),
Some(ParseError::Expected("record".into(), span)),
);
}
let (pattern, err) = parse_pattern(working_set, tokens[idx].span);
error = error.or(err);
(field, pattern)
};
idx += 1;
output.push((field, pattern));
}
(
MatchPattern {
pattern: Pattern::Record(output),
span,
},
error,
)
}

View File

@ -3,6 +3,7 @@ use crate::{
lex, lex,
lite_parser::{lite_parse, LiteCommand, LiteElement}, lite_parser::{lite_parse, LiteCommand, LiteElement},
parse_mut, parse_mut,
parse_patterns::{parse_match_pattern, parse_pattern},
type_check::{math_result_type, type_compatible}, type_check::{math_result_type, type_compatible},
ParseError, Token, TokenContents, ParseError, Token, TokenContents,
}; };
@ -76,6 +77,7 @@ pub fn is_math_expression_like(
|| bytes == b"null" || bytes == b"null"
|| bytes == b"not" || bytes == b"not"
|| bytes == b"if" || bytes == b"if"
|| bytes == b"match"
{ {
return true; return true;
} }
@ -120,7 +122,7 @@ fn is_identifier(bytes: &[u8]) -> bool {
bytes.iter().all(|x| is_identifier_byte(*x)) bytes.iter().all(|x| is_identifier_byte(*x))
} }
fn is_variable(bytes: &[u8]) -> bool { pub fn is_variable(bytes: &[u8]) -> bool {
if bytes.len() > 1 && bytes[0] == b'$' { if bytes.len() > 1 && bytes[0] == b'$' {
is_identifier(&bytes[1..]) is_identifier(&bytes[1..])
} else { } else {
@ -1787,6 +1789,8 @@ pub fn parse_brace_expr(
parse_closure_expression(working_set, shape, span, expand_aliases_denylist, true) parse_closure_expression(working_set, shape, span, expand_aliases_denylist, true)
} else if matches!(shape, SyntaxShape::Block) { } else if matches!(shape, SyntaxShape::Block) {
parse_block_expression(working_set, span, expand_aliases_denylist) parse_block_expression(working_set, span, expand_aliases_denylist)
} else if matches!(shape, SyntaxShape::MatchBlock) {
parse_match_block_expression(working_set, span, expand_aliases_denylist)
} else { } else {
parse_record(working_set, span, expand_aliases_denylist) parse_record(working_set, span, expand_aliases_denylist)
} }
@ -1802,6 +1806,8 @@ pub fn parse_brace_expr(
parse_closure_expression(working_set, shape, span, expand_aliases_denylist, true) parse_closure_expression(working_set, shape, span, expand_aliases_denylist, true)
} else if matches!(shape, SyntaxShape::Block) { } else if matches!(shape, SyntaxShape::Block) {
parse_block_expression(working_set, span, expand_aliases_denylist) parse_block_expression(working_set, span, expand_aliases_denylist)
} else if matches!(shape, SyntaxShape::MatchBlock) {
parse_match_block_expression(working_set, span, expand_aliases_denylist)
} else { } else {
( (
Expression::garbage(span), Expression::garbage(span),
@ -4415,6 +4421,113 @@ pub fn parse_block_expression(
) )
} }
pub fn parse_match_block_expression(
working_set: &mut StateWorkingSet,
span: Span,
expand_aliases_denylist: &[usize],
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let mut error = None;
let mut start = span.start;
let mut end = span.end;
if bytes.starts_with(b"{") {
start += 1;
} else {
return (
garbage(span),
Some(ParseError::Expected("closure".into(), span)),
);
}
if bytes.ends_with(b"}") {
end -= 1;
} else {
error = error.or_else(|| Some(ParseError::Unclosed("}".into(), Span::new(end, end))));
}
let inner_span = Span::new(start, end);
let source = working_set.get_span_contents(inner_span);
let (output, err) = lex(source, start, &[b' ', b'\r', b'\n', b','], &[], false);
error = error.or(err);
let mut position = 0;
let mut output_matches = vec![];
while position < output.len() {
// Each match gets its own scope
working_set.enter_scope();
// First parse the pattern
let (pattern, err) = parse_pattern(working_set, output[position].span);
error = error.or(err);
position += 1;
if position >= output.len() {
error = error.or(Some(ParseError::Mismatch(
"=>".into(),
"end of input".into(),
Span::new(output[position - 1].span.end, output[position - 1].span.end),
)));
working_set.exit_scope();
break;
}
// Then the =>
let thick_arrow = working_set.get_span_contents(output[position].span);
if thick_arrow != b"=>" {
error = error.or(Some(ParseError::Mismatch(
"=>".into(),
"end of input".into(),
Span::new(output[position - 1].span.end, output[position - 1].span.end),
)));
}
// Finally, the value/expression/block that we will run to produce the result
position += 1;
if position >= output.len() {
error = error.or(Some(ParseError::Mismatch(
"match result".into(),
"end of input".into(),
Span::new(output[position - 1].span.end, output[position - 1].span.end),
)));
working_set.exit_scope();
break;
}
let (result, err) = parse_multispan_value(
working_set,
&[output[position].span],
&mut 0,
&SyntaxShape::OneOf(vec![SyntaxShape::Block, SyntaxShape::Expression]),
expand_aliases_denylist,
);
error = error.or(err);
position += 1;
working_set.exit_scope();
output_matches.push((pattern, result));
}
(
Expression {
expr: Expr::MatchBlock(output_matches),
span,
ty: Type::Any,
custom_completion: None,
},
error,
)
}
pub fn parse_closure_expression( pub fn parse_closure_expression(
working_set: &mut StateWorkingSet, working_set: &mut StateWorkingSet,
shape: &SyntaxShape, shape: &SyntaxShape,
@ -4650,6 +4763,10 @@ pub fn parse_value(
_ => {} _ => {}
} }
if matches!(shape, SyntaxShape::MatchPattern) {
return parse_match_pattern(working_set, span);
}
match bytes[0] { match bytes[0] {
b'$' => return parse_dollar_expr(working_set, span, expand_aliases_denylist), b'$' => return parse_dollar_expr(working_set, span, expand_aliases_denylist),
b'(' => return parse_paren_expr(working_set, span, shape, expand_aliases_denylist), b'(' => return parse_paren_expr(working_set, span, shape, expand_aliases_denylist),
@ -4688,6 +4805,7 @@ pub fn parse_value(
SyntaxShape::GlobPattern => parse_glob_pattern(working_set, span), SyntaxShape::GlobPattern => parse_glob_pattern(working_set, span),
SyntaxShape::String => parse_string(working_set, span, expand_aliases_denylist), SyntaxShape::String => parse_string(working_set, span, expand_aliases_denylist),
SyntaxShape::Binary => parse_binary(working_set, span), SyntaxShape::Binary => parse_binary(working_set, span),
SyntaxShape::MatchPattern => parse_match_pattern(working_set, span),
SyntaxShape::Signature => { SyntaxShape::Signature => {
if bytes.starts_with(b"[") { if bytes.starts_with(b"[") {
parse_signature(working_set, span, expand_aliases_denylist) parse_signature(working_set, span, expand_aliases_denylist)
@ -4998,7 +5116,7 @@ pub fn parse_math_expression(
let first_span = working_set.get_span_contents(spans[0]); let first_span = working_set.get_span_contents(spans[0]);
if first_span == b"if" { if first_span == b"if" || first_span == b"match" {
// If expression // If expression
if spans.len() > 1 { if spans.len() > 1 {
return parse_call(working_set, spans, spans[0], expand_aliases_denylist, false); return parse_call(working_set, spans, spans[0], expand_aliases_denylist, false);
@ -6085,6 +6203,13 @@ pub fn discover_captures_in_expr(
output.extend(&result); output.extend(&result);
} }
} }
Expr::MatchPattern(_) => {}
Expr::MatchBlock(match_block) => {
for match_ in match_block {
let result = discover_captures_in_expr(working_set, &match_.1, seen, seen_blocks)?;
output.extend(&result);
}
}
Expr::RowCondition(block_id) | Expr::Subexpression(block_id) => { Expr::RowCondition(block_id) | Expr::Subexpression(block_id) => {
let block = working_set.get_block(*block_id); let block = working_set.get_block(*block_id);
let results = { let results = {

View File

@ -1,7 +1,7 @@
use chrono::FixedOffset; use chrono::FixedOffset;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{Call, CellPath, Expression, FullCellPath, Operator, RangeOperator}; use super::{Call, CellPath, Expression, FullCellPath, MatchPattern, Operator, RangeOperator};
use crate::{ast::ImportPattern, BlockId, Signature, Span, Spanned, Unit, VarId}; use crate::{ast::ImportPattern, BlockId, Signature, Span, Spanned, Unit, VarId};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@ -27,6 +27,7 @@ pub enum Expr {
Subexpression(BlockId), Subexpression(BlockId),
Block(BlockId), Block(BlockId),
Closure(BlockId), Closure(BlockId),
MatchBlock(Vec<(MatchPattern, Expression)>),
List(Vec<Expression>), List(Vec<Expression>),
Table(Vec<Expression>, Vec<Vec<Expression>>), Table(Vec<Expression>, Vec<Vec<Expression>>),
Record(Vec<(Expression, Expression)>), Record(Vec<(Expression, Expression)>),
@ -43,6 +44,7 @@ pub enum Expr {
Overlay(Option<BlockId>), // block ID of the overlay's origin module Overlay(Option<BlockId>), // block ID of the overlay's origin module
Signature(Box<Signature>), Signature(Box<Signature>),
StringInterpolation(Vec<Expression>), StringInterpolation(Vec<Expression>),
MatchPattern(Box<MatchPattern>),
Nothing, Nothing,
Garbage, Garbage,
} }

View File

@ -221,7 +221,9 @@ impl Expression {
} }
false false
} }
Expr::MatchPattern(_) => false,
Expr::Operator(_) => false, Expr::Operator(_) => false,
Expr::MatchBlock(_) => false,
Expr::Range(left, middle, right, ..) => { Expr::Range(left, middle, right, ..) => {
if let Some(left) = &left { if let Some(left) = &left {
if left.has_in_variable(working_set) { if left.has_in_variable(working_set) {
@ -395,6 +397,8 @@ impl Expression {
Expr::Nothing => {} Expr::Nothing => {}
Expr::GlobPattern(_) => {} Expr::GlobPattern(_) => {}
Expr::Int(_) => {} Expr::Int(_) => {}
Expr::MatchPattern(_) => {}
Expr::MatchBlock(_) => {}
Expr::Keyword(_, _, expr) => expr.replace_in_variable(working_set, new_var_id), Expr::Keyword(_, _, expr) => expr.replace_in_variable(working_set, new_var_id),
Expr::List(list) => { Expr::List(list) => {
for l in list { for l in list {
@ -554,6 +558,8 @@ impl Expression {
Expr::Garbage => {} Expr::Garbage => {}
Expr::Nothing => {} Expr::Nothing => {}
Expr::GlobPattern(_) => {} Expr::GlobPattern(_) => {}
Expr::MatchPattern(_) => {}
Expr::MatchBlock(_) => {}
Expr::Int(_) => {} Expr::Int(_) => {}
Expr::Keyword(_, _, expr) => expr.replace_span(working_set, replaced, new_span), Expr::Keyword(_, _, expr) => expr.replace_span(working_set, replaced, new_span),
Expr::List(list) => { Expr::List(list) => {

View File

@ -0,0 +1,21 @@
use serde::{Deserialize, Serialize};
use crate::{Span, VarId};
use super::Expression;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MatchPattern {
pub pattern: Pattern,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Pattern {
Record(Vec<(String, MatchPattern)>),
List(Vec<MatchPattern>),
Value(Expression),
Variable(VarId),
IgnoreValue, // the _ pattern
Garbage,
}

View File

@ -4,6 +4,7 @@ mod cell_path;
mod expr; mod expr;
mod expression; mod expression;
mod import_pattern; mod import_pattern;
mod match_pattern;
mod operator; mod operator;
mod pipeline; mod pipeline;
@ -13,5 +14,6 @@ pub use cell_path::*;
pub use expr::*; pub use expr::*;
pub use expression::*; pub use expression::*;
pub use import_pattern::*; pub use import_pattern::*;
pub use match_pattern::*;
pub use operator::*; pub use operator::*;
pub use pipeline::*; pub use pipeline::*;

View File

@ -3,6 +3,7 @@ mod capture_block;
mod command; mod command;
mod engine_state; mod engine_state;
mod overlay; mod overlay;
mod pattern_match;
mod stack; mod stack;
pub use call_info::*; pub use call_info::*;
@ -10,4 +11,5 @@ pub use capture_block::*;
pub use command::*; pub use command::*;
pub use engine_state::*; pub use engine_state::*;
pub use overlay::*; pub use overlay::*;
pub use pattern_match::*;
pub use stack::*; pub use stack::*;

View File

@ -0,0 +1,201 @@
use crate::{
ast::{Expr, MatchPattern, Pattern, RangeInclusion},
Unit, Value, VarId,
};
pub trait Matcher {
fn match_value(&self, value: &Value, matches: &mut Vec<(VarId, Value)>) -> bool;
}
impl Matcher for MatchPattern {
fn match_value(&self, value: &Value, matches: &mut Vec<(VarId, Value)>) -> bool {
self.pattern.match_value(value, matches)
}
}
impl Matcher for Pattern {
fn match_value(&self, value: &Value, matches: &mut Vec<(VarId, Value)>) -> bool {
match self {
Pattern::Garbage => false,
Pattern::IgnoreValue => true,
Pattern::Record(field_patterns) => match value {
Value::Record { cols, vals, .. } => {
'top: for field_pattern in field_patterns {
for (col_idx, col) in cols.iter().enumerate() {
if col == &field_pattern.0 {
// We have found the field
let result = field_pattern.1.match_value(&vals[col_idx], matches);
if !result {
return false;
} else {
continue 'top;
}
}
}
return false;
}
true
}
_ => false,
},
Pattern::Variable(var_id) => {
// TODO: FIXME: This needs the span of this variable
matches.push((*var_id, value.clone()));
true
}
Pattern::List(items) => match &value {
Value::List { vals, .. } => {
if items.len() > vals.len() {
// We need more items in our pattern than are available in the Value
return false;
}
for (val_idx, val) in vals.iter().enumerate() {
// We require that the pattern and the value have the same number of items, or the pattern does not match
// The only exception is if the pattern includes a `..` pattern
if let Some(pattern) = items.get(val_idx) {
if !pattern.match_value(val, matches) {
return false;
}
} else {
return false;
}
}
true
}
_ => false,
},
Pattern::Value(pattern_value) => {
// TODO: Fill this out with the rest of them
match &pattern_value.expr {
Expr::Int(x) => {
if let Value::Int { val, .. } = &value {
x == val
} else {
false
}
}
Expr::Binary(x) => {
if let Value::Binary { val, .. } = &value {
x == val
} else {
false
}
}
Expr::Bool(x) => {
if let Value::Bool { val, .. } = &value {
x == val
} else {
false
}
}
Expr::ValueWithUnit(amount, unit) => {
if let Value::Filesize { val, .. } = &value {
// FIXME: we probably want this math in one place that both the
// pattern matcher and the eval engine can get to it
match &amount.expr {
Expr::Int(amount) => match &unit.item {
Unit::Byte => amount == val,
Unit::Kilobyte => *val == amount * 1000,
Unit::Megabyte => *val == amount * 1000 * 1000,
Unit::Gigabyte => *val == amount * 1000 * 1000 * 1000,
Unit::Petabyte => *val == amount * 1000 * 1000 * 1000 * 1000,
Unit::Exabyte => {
*val == amount * 1000 * 1000 * 1000 * 1000 * 1000
}
Unit::Zettabyte => {
*val == amount * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
}
Unit::Kibibyte => *val == amount * 1024,
Unit::Mebibyte => *val == amount * 1024 * 1024,
Unit::Gibibyte => *val == amount * 1024 * 1024 * 1024,
Unit::Pebibyte => *val == amount * 1024 * 1024 * 1024 * 1024,
Unit::Exbibyte => {
*val == amount * 1024 * 1024 * 1024 * 1024 * 1024
}
Unit::Zebibyte => {
*val == amount * 1024 * 1024 * 1024 * 1024 * 1024 * 1024
}
_ => false,
},
_ => false,
}
} else if let Value::Duration { val, .. } = &value {
// FIXME: we probably want this math in one place that both the
// pattern matcher and the eval engine can get to it
match &amount.expr {
Expr::Int(amount) => match &unit.item {
Unit::Nanosecond => val == amount,
Unit::Microsecond => *val == amount * 1000,
Unit::Millisecond => *val == amount * 1000 * 1000,
Unit::Second => *val == amount * 1000 * 1000 * 1000,
Unit::Minute => *val == amount * 1000 * 1000 * 1000 * 60,
Unit::Hour => *val == amount * 1000 * 1000 * 1000 * 60 * 60,
Unit::Day => *val == amount * 1000 * 1000 * 1000 * 60 * 60 * 24,
Unit::Week => {
*val == amount * 1000 * 1000 * 1000 * 60 * 60 * 24 * 7
}
_ => false,
},
_ => false,
}
} else {
false
}
}
Expr::Range(start, step, end, inclusion) => {
// TODO: Add support for floats
let start = if let Some(start) = &start {
match &start.expr {
Expr::Int(start) => *start,
_ => return false,
}
} else {
0
};
let end = if let Some(end) = &end {
match &end.expr {
Expr::Int(end) => *end,
_ => return false,
}
} else {
i64::MAX
};
let step = if let Some(step) = step {
match &step.expr {
Expr::Int(step) => *step - start,
_ => return false,
}
} else if end < start {
-1
} else {
1
};
let (start, end) = if end < start {
(end, start)
} else {
(start, end)
};
if let Value::Int { val, .. } = &value {
if matches!(inclusion.inclusion, RangeInclusion::RightExclusive) {
*val >= start && *val < end && ((*val - start) % step) == 0
} else {
*val >= start && *val <= end && ((*val - start) % step) == 0
}
} else {
false
}
}
_ => false,
}
}
}
}
}

View File

@ -73,6 +73,12 @@ pub enum SyntaxShape {
/// A general math expression, eg `1 + 2` /// A general math expression, eg `1 + 2`
MathExpression, MathExpression,
/// A block of matches, used by `match`
MatchBlock,
/// A match pattern, eg `{a: $foo}`
MatchPattern,
/// Nothing /// Nothing
Nothing, Nothing,
@ -137,6 +143,8 @@ impl SyntaxShape {
Type::List(Box::new(contents)) Type::List(Box::new(contents))
} }
SyntaxShape::Keyword(_, expr) => expr.to_type(), SyntaxShape::Keyword(_, expr) => expr.to_type(),
SyntaxShape::MatchBlock => Type::Any,
SyntaxShape::MatchPattern => Type::Any,
SyntaxShape::MathExpression => Type::Any, SyntaxShape::MathExpression => Type::Any,
SyntaxShape::Nothing => Type::Any, SyntaxShape::Nothing => Type::Any,
SyntaxShape::Number => Type::Number, SyntaxShape::Number => Type::Number,
@ -196,6 +204,8 @@ impl Display for SyntaxShape {
SyntaxShape::Variable => write!(f, "var"), SyntaxShape::Variable => write!(f, "var"),
SyntaxShape::VarWithOptType => write!(f, "vardecl"), SyntaxShape::VarWithOptType => write!(f, "vardecl"),
SyntaxShape::Signature => write!(f, "signature"), SyntaxShape::Signature => write!(f, "signature"),
SyntaxShape::MatchPattern => write!(f, "matchpattern"),
SyntaxShape::MatchBlock => write!(f, "matchblock"),
SyntaxShape::Expression => write!(f, "expression"), SyntaxShape::Expression => write!(f, "expression"),
SyntaxShape::Boolean => write!(f, "bool"), SyntaxShape::Boolean => write!(f, "bool"),
SyntaxShape::Error => write!(f, "error"), SyntaxShape::Error => write!(f, "error"),

View File

@ -22,6 +22,7 @@ pub enum Type {
Int, Int,
List(Box<Type>), List(Box<Type>),
ListStream, ListStream,
MatchPattern,
#[default] #[default]
Nothing, Nothing,
Number, Number,
@ -94,6 +95,7 @@ impl Type {
Type::Binary => SyntaxShape::Binary, Type::Binary => SyntaxShape::Binary,
Type::Custom(_) => SyntaxShape::Any, Type::Custom(_) => SyntaxShape::Any,
Type::Signature => SyntaxShape::Signature, Type::Signature => SyntaxShape::Signature,
Type::MatchPattern => SyntaxShape::MatchPattern,
} }
} }
@ -114,6 +116,7 @@ impl Type {
Type::Record(_) => String::from("record"), Type::Record(_) => String::from("record"),
Type::Table(_) => String::from("table"), Type::Table(_) => String::from("table"),
Type::List(_) => String::from("list"), Type::List(_) => String::from("list"),
Type::MatchPattern => String::from("match pattern"),
Type::Nothing => String::from("nothing"), Type::Nothing => String::from("nothing"),
Type::Number => String::from("number"), Type::Number => String::from("number"),
Type::String => String::from("string"), Type::String => String::from("string"),
@ -180,6 +183,7 @@ impl Display for Type {
Type::Binary => write!(f, "binary"), Type::Binary => write!(f, "binary"),
Type::Custom(custom) => write!(f, "{custom}"), Type::Custom(custom) => write!(f, "{custom}"),
Type::Signature => write!(f, "signature"), Type::Signature => write!(f, "signature"),
Type::MatchPattern => write!(f, "match pattern"),
} }
} }
} }

View File

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use crate::ast::{CellPath, PathMember}; use crate::ast::{CellPath, MatchPattern, PathMember};
use crate::engine::{Block, Closure}; use crate::engine::{Block, Closure};
use crate::ShellError; use crate::ShellError;
use crate::{Range, Spanned, Value}; use crate::{Range, Spanned, Value};
@ -563,3 +563,34 @@ impl FromValue for Spanned<Closure> {
} }
} }
} }
impl FromValue for Spanned<MatchPattern> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::MatchPattern { val, span } => Ok(Spanned {
item: *val.clone(),
span: *span,
}),
v => Err(ShellError::CantConvert {
to_type: "Match pattern".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}
impl FromValue for MatchPattern {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value::MatchPattern { val, .. } => Ok(*val.clone()),
v => Err(ShellError::CantConvert {
to_type: "Match pattern".into(),
from_type: v.get_type().to_string(),
span: v.span()?,
help: None,
}),
}
}
}

View File

@ -6,7 +6,7 @@ mod range;
mod stream; mod stream;
mod unit; mod unit;
use crate::ast::{Bits, Boolean, CellPath, Comparison, PathMember}; use crate::ast::{Bits, Boolean, CellPath, Comparison, MatchPattern, PathMember};
use crate::ast::{Math, Operator}; use crate::ast::{Math, Operator};
use crate::engine::EngineState; use crate::engine::EngineState;
use crate::ShellError; use crate::ShellError;
@ -113,6 +113,10 @@ pub enum Value {
val: Box<dyn LazyRecord>, val: Box<dyn LazyRecord>,
span: Span, span: Span,
}, },
MatchPattern {
val: Box<MatchPattern>,
span: Span,
},
} }
impl Clone for Value { impl Clone for Value {
@ -185,6 +189,10 @@ impl Clone for Value {
span: *span, span: *span,
}, },
Value::CustomValue { val, span } => val.clone_value(*span), Value::CustomValue { val, span } => val.clone_value(*span),
Value::MatchPattern { val, span } => Value::MatchPattern {
val: val.clone(),
span: *span,
},
} }
} }
} }
@ -387,6 +395,7 @@ impl Value {
Value::CellPath { span, .. } => Ok(*span), Value::CellPath { span, .. } => Ok(*span),
Value::CustomValue { span, .. } => Ok(*span), Value::CustomValue { span, .. } => Ok(*span),
Value::LazyRecord { span, .. } => Ok(*span), Value::LazyRecord { span, .. } => Ok(*span),
Value::MatchPattern { span, .. } => Ok(*span),
} }
} }
@ -418,6 +427,7 @@ impl Value {
Value::Binary { span, .. } => *span = new_span, Value::Binary { span, .. } => *span = new_span,
Value::CellPath { span, .. } => *span = new_span, Value::CellPath { span, .. } => *span = new_span,
Value::CustomValue { span, .. } => *span = new_span, Value::CustomValue { span, .. } => *span = new_span,
Value::MatchPattern { span, .. } => *span = new_span,
} }
self self
@ -475,6 +485,7 @@ impl Value {
Value::Binary { .. } => Type::Binary, Value::Binary { .. } => Type::Binary,
Value::CellPath { .. } => Type::CellPath, Value::CellPath { .. } => Type::CellPath,
Value::CustomValue { val, .. } => Type::Custom(val.typetag_name().into()), Value::CustomValue { val, .. } => Type::Custom(val.typetag_name().into()),
Value::MatchPattern { .. } => Type::MatchPattern,
} }
} }
@ -571,6 +582,7 @@ impl Value {
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.into_string(), Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(), Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("<Pattern: {:?}>", val),
} }
} }
@ -622,6 +634,7 @@ impl Value {
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.into_string(), Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(), Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { .. } => "<Pattern>".into(),
} }
} }
@ -673,6 +686,7 @@ impl Value {
Value::Binary { val, .. } => format!("{val:?}"), Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.into_string(), Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(), Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("<Pattern {:?}>", val),
} }
} }
@ -1720,6 +1734,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Int { val: lhs, .. }, rhs) => match rhs { (Value::Int { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1740,6 +1755,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Float { val: lhs, .. }, rhs) => match rhs { (Value::Float { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1760,6 +1776,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Filesize { val: lhs, .. }, rhs) => match rhs { (Value::Filesize { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1780,6 +1797,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Duration { val: lhs, .. }, rhs) => match rhs { (Value::Duration { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1800,6 +1818,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Date { val: lhs, .. }, rhs) => match rhs { (Value::Date { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1820,6 +1839,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Range { val: lhs, .. }, rhs) => match rhs { (Value::Range { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1840,6 +1860,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::String { val: lhs, .. }, rhs) => match rhs { (Value::String { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1860,6 +1881,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
( (
Value::Record { Value::Record {
@ -1912,6 +1934,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::List { vals: lhs, .. }, rhs) => match rhs { (Value::List { vals: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1932,6 +1955,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Block { val: lhs, .. }, rhs) => match rhs { (Value::Block { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1952,6 +1976,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Closure { val: lhs, .. }, rhs) => match rhs { (Value::Closure { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1972,6 +1997,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Nothing { .. }, rhs) => match rhs { (Value::Nothing { .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -1992,6 +2018,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Error { .. }, rhs) => match rhs { (Value::Error { .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -2012,6 +2039,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Less), Value::Binary { .. } => Some(Ordering::Less),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::Binary { val: lhs, .. }, rhs) => match rhs { (Value::Binary { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -2032,6 +2060,7 @@ impl PartialOrd for Value {
Value::Binary { val: rhs, .. } => lhs.partial_cmp(rhs), Value::Binary { val: rhs, .. } => lhs.partial_cmp(rhs),
Value::CellPath { .. } => Some(Ordering::Less), Value::CellPath { .. } => Some(Ordering::Less),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::CellPath { val: lhs, .. }, rhs) => match rhs { (Value::CellPath { val: lhs, .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater), Value::Bool { .. } => Some(Ordering::Greater),
@ -2052,6 +2081,7 @@ impl PartialOrd for Value {
Value::Binary { .. } => Some(Ordering::Greater), Value::Binary { .. } => Some(Ordering::Greater),
Value::CellPath { val: rhs, .. } => lhs.partial_cmp(rhs), Value::CellPath { val: rhs, .. } => lhs.partial_cmp(rhs),
Value::CustomValue { .. } => Some(Ordering::Less), Value::CustomValue { .. } => Some(Ordering::Less),
Value::MatchPattern { .. } => Some(Ordering::Less),
}, },
(Value::CustomValue { val: lhs, .. }, rhs) => lhs.partial_cmp(rhs), (Value::CustomValue { val: lhs, .. }, rhs) => lhs.partial_cmp(rhs),
(Value::LazyRecord { val, .. }, rhs) => { (Value::LazyRecord { val, .. }, rhs) => {
@ -2061,6 +2091,27 @@ impl PartialOrd for Value {
None None
} }
} }
(Value::MatchPattern { .. }, rhs) => match rhs {
Value::Bool { .. } => Some(Ordering::Greater),
Value::Int { .. } => Some(Ordering::Greater),
Value::Float { .. } => Some(Ordering::Greater),
Value::Filesize { .. } => Some(Ordering::Greater),
Value::Duration { .. } => Some(Ordering::Greater),
Value::Date { .. } => Some(Ordering::Greater),
Value::Range { .. } => Some(Ordering::Greater),
Value::String { .. } => Some(Ordering::Greater),
Value::Record { .. } => Some(Ordering::Greater),
Value::LazyRecord { .. } => Some(Ordering::Greater),
Value::List { .. } => Some(Ordering::Greater),
Value::Block { .. } => Some(Ordering::Greater),
Value::Closure { .. } => Some(Ordering::Greater),
Value::Nothing { .. } => Some(Ordering::Greater),
Value::Error { .. } => Some(Ordering::Greater),
Value::Binary { .. } => Some(Ordering::Greater),
Value::CellPath { .. } => Some(Ordering::Greater),
Value::CustomValue { .. } => Some(Ordering::Greater),
Value::MatchPattern { .. } => None,
},
} }
} }
} }

View File

@ -70,6 +70,7 @@ let dark_theme = {
shape_internalcall: cyan_bold shape_internalcall: cyan_bold
shape_list: cyan_bold shape_list: cyan_bold
shape_literal: blue shape_literal: blue
shape_match_pattern: green
shape_matching_brackets: { attr: u } shape_matching_brackets: { attr: u }
shape_nothing: light_cyan shape_nothing: light_cyan
shape_operator: yellow shape_operator: yellow
@ -151,6 +152,7 @@ let light_theme = {
shape_internalcall: cyan_bold shape_internalcall: cyan_bold
shape_list: cyan_bold shape_list: cyan_bold
shape_literal: blue shape_literal: blue
shape_match_pattern: green
shape_matching_brackets: { attr: u } shape_matching_brackets: { attr: u }
shape_nothing: light_cyan shape_nothing: light_cyan
shape_operator: yellow shape_operator: yellow