2023-03-24 01:52:01 +00:00
|
|
|
use crate::{
|
|
|
|
ast::{Expr, MatchPattern, Pattern, RangeInclusion},
|
2023-05-12 17:18:11 +00:00
|
|
|
Span, Value, VarId,
|
2023-03-24 01:52:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2023-03-30 22:08:53 +00:00
|
|
|
Pattern::IgnoreRest => false, // `..` and `..$foo` only match in specific contexts
|
|
|
|
Pattern::Rest(_) => false, // so we return false here and handle them elsewhere
|
2023-03-24 01:52:01 +00:00
|
|
|
Pattern::Record(field_patterns) => match value {
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
Value::Record { val, .. } => {
|
2023-03-24 01:52:01 +00:00
|
|
|
'top: for field_pattern in field_patterns {
|
2024-03-26 15:17:44 +00:00
|
|
|
for (col, val) in &**val {
|
2023-03-24 01:52:01 +00:00
|
|
|
if col == &field_pattern.0 {
|
|
|
|
// We have found the field
|
Create `Record` type (#10103)
# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
```rust
record! {
"key1" => some_value,
"key2" => Value::string("text", span),
"key3" => Value::int(optional_int.unwrap_or(0), span),
"key4" => Value::bool(config.setting, span),
}
```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.
Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.
# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
2023-08-24 19:50:29 +00:00
|
|
|
let result = field_pattern.1.match_value(val, matches);
|
2023-03-24 01:52:01 +00:00
|
|
|
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() {
|
2023-03-30 22:08:53 +00:00
|
|
|
// The only we we allow this is to have a rest pattern in the n+1 position
|
|
|
|
if items.len() == (vals.len() + 1) {
|
|
|
|
match &items[vals.len()].pattern {
|
|
|
|
Pattern::IgnoreRest => {}
|
|
|
|
Pattern::Rest(var_id) => {
|
|
|
|
matches.push((*var_id, Value::nothing(items[vals.len()].span)))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// There is a pattern which can't skip missing values, so we fail
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// There are patterns that can't be matches, so we fail
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
}
|
|
|
|
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) {
|
2023-03-30 22:08:53 +00:00
|
|
|
match &pattern.pattern {
|
|
|
|
Pattern::IgnoreRest => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Pattern::Rest(var_id) => {
|
|
|
|
let rest_vals = vals[val_idx..].to_vec();
|
2023-09-03 14:27:29 +00:00
|
|
|
matches.push((*var_id, Value::list(rest_vals, pattern.span)));
|
2023-03-30 22:08:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if !pattern.match_value(val, matches) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Pattern::Value(pattern_value) => {
|
|
|
|
// TODO: Fill this out with the rest of them
|
|
|
|
match &pattern_value.expr {
|
2023-10-24 22:30:45 +00:00
|
|
|
Expr::Nothing => {
|
|
|
|
matches!(value, Value::Nothing { .. })
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
Expr::Int(x) => {
|
|
|
|
if let Value::Int { val, .. } = &value {
|
|
|
|
x == val
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2023-03-25 04:30:35 +00:00
|
|
|
Expr::Float(x) => {
|
|
|
|
if let Value::Float { val, .. } = &value {
|
|
|
|
x == val
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2023-03-25 04:30:35 +00:00
|
|
|
Expr::String(x) => {
|
|
|
|
if let Value::String { val, .. } = &value {
|
|
|
|
x == val
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expr::DateTime(x) => {
|
|
|
|
if let Value::Date { val, .. } = &value {
|
|
|
|
x == val
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 15:46:35 +00:00
|
|
|
Expr::ValueWithUnit(val) => {
|
|
|
|
let span = val.unit.span;
|
2023-05-12 17:18:11 +00:00
|
|
|
|
2024-04-24 15:46:35 +00:00
|
|
|
if let Expr::Int(size) = val.expr.expr {
|
|
|
|
match &val.unit.item.build_value(size, span) {
|
2023-08-24 20:48:05 +00:00
|
|
|
Ok(v) => v == value,
|
|
|
|
_ => false,
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 15:46:35 +00:00
|
|
|
Expr::Range(range) => {
|
2023-03-24 01:52:01 +00:00
|
|
|
// TODO: Add support for floats
|
|
|
|
|
2024-04-24 15:46:35 +00:00
|
|
|
let start = if let Some(start) = &range.from {
|
2023-03-24 01:52:01 +00:00
|
|
|
match &start.expr {
|
|
|
|
Expr::Int(start) => *start,
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2024-04-24 15:46:35 +00:00
|
|
|
let end = if let Some(end) = &range.to {
|
2023-03-24 01:52:01 +00:00
|
|
|
match &end.expr {
|
|
|
|
Expr::Int(end) => *end,
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i64::MAX
|
|
|
|
};
|
|
|
|
|
2024-04-24 15:46:35 +00:00
|
|
|
let step = if let Some(step) = &range.next {
|
2023-03-24 01:52:01 +00:00
|
|
|
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 {
|
2024-04-24 15:46:35 +00:00
|
|
|
if matches!(range.operator.inclusion, RangeInclusion::RightExclusive) {
|
2023-03-24 01:52:01 +00:00
|
|
|
*val >= start && *val < end && ((*val - start) % step) == 0
|
|
|
|
} else {
|
|
|
|
*val >= start && *val <= end && ((*val - start) % step) == 0
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2023-03-26 22:31:57 +00:00
|
|
|
Pattern::Or(patterns) => {
|
|
|
|
let mut result = false;
|
|
|
|
|
|
|
|
for pattern in patterns {
|
|
|
|
let mut local_matches = vec![];
|
|
|
|
if !result {
|
|
|
|
if pattern.match_value(value, &mut local_matches) {
|
|
|
|
// TODO: do we need to replace previous variables that defaulted to nothing?
|
|
|
|
matches.append(&mut local_matches);
|
|
|
|
result = true;
|
|
|
|
} else {
|
|
|
|
// Create variables that don't match and assign them to null
|
|
|
|
let vars = pattern.variables();
|
|
|
|
for var in &vars {
|
|
|
|
let mut found = false;
|
|
|
|
for match_ in matches.iter() {
|
|
|
|
if match_.0 == *var {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
// FIXME: don't use Span::unknown()
|
|
|
|
matches.push((*var, Value::nothing(Span::unknown())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We already have a match, so ignore the remaining match variables
|
|
|
|
// And assign them to null
|
|
|
|
let vars = pattern.variables();
|
|
|
|
for var in &vars {
|
|
|
|
let mut found = false;
|
|
|
|
for match_ in matches.iter() {
|
|
|
|
if match_.0 == *var {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
// FIXME: don't use Span::unknown()
|
|
|
|
matches.push((*var, Value::nothing(Span::unknown())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2023-03-24 01:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|