2022-08-04 21:51:02 +02:00
|
|
|
use fancy_regex::Regex;
|
2021-11-15 19:27:15 +01:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
|
|
use nu_protocol::{
|
2022-01-28 19:32:33 +01:00
|
|
|
Category, Example, ListStream, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
|
2022-11-09 22:55:05 +01:00
|
|
|
Type, Value,
|
2021-11-15 19:27:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Parse;
|
|
|
|
|
|
|
|
impl Command for Parse {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"parse"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse columns from string data using a simple pattern."
|
|
|
|
}
|
|
|
|
|
2022-06-06 15:47:09 +02:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["pattern", "match"]
|
|
|
|
}
|
|
|
|
|
2021-11-15 19:27:15 +01:00
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
|
|
Signature::build("parse")
|
|
|
|
.required(
|
|
|
|
"pattern",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the pattern to match. Eg) \"{foo}: {bar}\"",
|
|
|
|
)
|
2022-11-09 22:55:05 +01:00
|
|
|
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
|
2021-11-15 19:27:15 +01:00
|
|
|
.switch("regex", "use full regex syntax for patterns", Some('r'))
|
2021-11-17 05:22:37 +01:00
|
|
|
.category(Category::Strings)
|
2021-11-15 19:27:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
let result = Value::List {
|
|
|
|
vals: vec![Value::Record {
|
|
|
|
cols: vec!["foo".to_string(), "bar".to_string()],
|
|
|
|
vals: vec![Value::test_string("hi"), Value::test_string("there")],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-11-15 19:27:15 +01:00
|
|
|
}],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-11-15 19:27:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Parse a string into two named columns",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "\"hi there\" | parse \"{foo} {bar}\"",
|
2021-11-15 19:27:15 +01:00
|
|
|
result: Some(result.clone()),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Parse a string using regex pattern",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "\"hi there\" | parse -r '(?P<foo>\\w+) (?P<bar>\\w+)'",
|
2021-11-15 19:27:15 +01:00
|
|
|
result: Some(result),
|
|
|
|
},
|
2022-08-04 21:51:02 +02:00
|
|
|
Example {
|
|
|
|
description: "Parse a string using fancy-regex named capture group pattern",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "\"foo bar.\" | parse -r '\\s*(?<name>\\w+)(?=\\.)'",
|
2022-08-04 21:51:02 +02:00
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![Value::Record {
|
|
|
|
cols: vec!["name".to_string()],
|
|
|
|
vals: vec![Value::test_string("bar")],
|
2022-12-16 17:51:00 +01:00
|
|
|
span: Span::test_data(),
|
2022-08-04 21:51:02 +02:00
|
|
|
}],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Parse a string using fancy-regex capture group pattern",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "\"foo! bar.\" | parse -r '(\\w+)(?=\\.)|(\\w+)(?=!)'",
|
2022-08-04 21:51:02 +02:00
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::Record {
|
2023-01-29 14:34:34 +01:00
|
|
|
cols: vec!["capture0".to_string(), "capture1".to_string()],
|
2022-08-04 21:51:02 +02:00
|
|
|
vals: vec![Value::test_string(""), Value::test_string("foo")],
|
2022-12-16 17:51:00 +01:00
|
|
|
span: Span::test_data(),
|
2022-08-04 21:51:02 +02:00
|
|
|
},
|
|
|
|
Value::Record {
|
2023-01-29 14:34:34 +01:00
|
|
|
cols: vec!["capture0".to_string(), "capture1".to_string()],
|
2022-08-04 21:51:02 +02:00
|
|
|
vals: vec![Value::test_string("bar"), Value::test_string("")],
|
|
|
|
span: Span::test_data(),
|
2022-12-16 17:51:00 +01:00
|
|
|
},
|
|
|
|
],
|
2022-08-04 21:51:02 +02:00
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Parse a string using fancy-regex look behind pattern",
|
2022-12-16 17:51:00 +01:00
|
|
|
example:
|
|
|
|
"\" @another(foo bar) \" | parse -r '\\s*(?<=[() ])(@\\w+)(\\([^)]*\\))?\\s*'",
|
2022-08-04 21:51:02 +02:00
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![Value::Record {
|
2023-01-29 14:34:34 +01:00
|
|
|
cols: vec!["capture0".to_string(), "capture1".to_string()],
|
2022-12-16 17:51:00 +01:00
|
|
|
vals: vec![
|
|
|
|
Value::test_string("@another"),
|
|
|
|
Value::test_string("(foo bar)"),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
2022-08-04 21:51:02 +02:00
|
|
|
}],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Parse a string using fancy-regex look ahead atomic group pattern",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "\"abcd\" | parse -r '^a(bc(?=d)|b)cd$'",
|
2022-08-04 21:51:02 +02:00
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![Value::Record {
|
2023-01-29 14:34:34 +01:00
|
|
|
cols: vec!["capture0".to_string()],
|
2022-08-04 21:51:02 +02:00
|
|
|
vals: vec![Value::test_string("b")],
|
2022-12-16 17:51:00 +01:00
|
|
|
span: Span::test_data(),
|
2022-08-04 21:51:02 +02:00
|
|
|
}],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
2021-11-15 19:27:15 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
operate(engine_state, stack, call, input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn operate(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let head = call.head;
|
|
|
|
let pattern: Spanned<String> = call.req(engine_state, stack, 0)?;
|
|
|
|
let regex: bool = call.has_flag("regex");
|
2021-12-02 06:59:10 +01:00
|
|
|
let ctrlc = engine_state.ctrlc.clone();
|
2021-11-15 19:27:15 +01:00
|
|
|
|
|
|
|
let pattern_item = pattern.item;
|
|
|
|
let pattern_span = pattern.span;
|
|
|
|
|
|
|
|
let item_to_parse = if regex {
|
|
|
|
pattern_item
|
|
|
|
} else {
|
|
|
|
build_regex(&pattern_item, pattern_span)?
|
|
|
|
};
|
|
|
|
|
2022-08-04 21:51:02 +02:00
|
|
|
let regex_pattern = Regex::new(&item_to_parse).map_err(|err| {
|
|
|
|
ShellError::GenericError(
|
|
|
|
"Error with regular expression".into(),
|
|
|
|
err.to_string(),
|
|
|
|
Some(pattern_span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
)
|
|
|
|
})?;
|
2021-11-15 19:27:15 +01:00
|
|
|
|
|
|
|
let columns = column_names(®ex_pattern);
|
2023-02-09 03:59:02 +01:00
|
|
|
|
|
|
|
match input {
|
|
|
|
PipelineData::Empty => Ok(PipelineData::Empty),
|
|
|
|
PipelineData::Value(..) => {
|
|
|
|
let mut parsed: Vec<Value> = Vec::new();
|
|
|
|
|
|
|
|
for v in input {
|
|
|
|
match v.as_string() {
|
|
|
|
Ok(s) => {
|
|
|
|
let results = regex_pattern.captures_iter(&s);
|
|
|
|
|
|
|
|
for c in results {
|
|
|
|
let mut cols = Vec::with_capacity(columns.len());
|
|
|
|
let captures = match c {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => {
|
|
|
|
return Err(ShellError::GenericError(
|
|
|
|
"Error with regular expression captures".into(),
|
|
|
|
e.to_string(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mut vals = Vec::with_capacity(captures.len());
|
|
|
|
|
|
|
|
for (column_name, cap) in columns.iter().zip(captures.iter().skip(1)) {
|
|
|
|
let cap_string = cap.map(|v| v.as_str()).unwrap_or("").to_string();
|
|
|
|
cols.push(column_name.clone());
|
|
|
|
vals.push(Value::String {
|
|
|
|
val: cap_string,
|
|
|
|
span: v.span()?,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed.push(Value::Record {
|
|
|
|
cols,
|
|
|
|
vals,
|
|
|
|
span: head,
|
|
|
|
});
|
2022-08-04 21:51:02 +02:00
|
|
|
}
|
2021-11-15 19:27:15 +01:00
|
|
|
}
|
2023-02-09 03:59:02 +01:00
|
|
|
Err(_) => {
|
|
|
|
return Err(ShellError::PipelineMismatch(
|
|
|
|
"string".into(),
|
|
|
|
head,
|
|
|
|
v.span()?,
|
|
|
|
))
|
|
|
|
}
|
2021-11-15 19:27:15 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-09 03:59:02 +01:00
|
|
|
|
|
|
|
Ok(PipelineData::ListStream(
|
|
|
|
ListStream::from_stream(parsed.into_iter(), ctrlc),
|
|
|
|
None,
|
|
|
|
))
|
2021-11-15 19:27:15 +01:00
|
|
|
}
|
2023-02-09 03:59:02 +01:00
|
|
|
PipelineData::ListStream(stream, ..) => Ok(PipelineData::ListStream(
|
|
|
|
ListStream::from_stream(
|
|
|
|
ParseStreamer {
|
|
|
|
span: head,
|
|
|
|
excess: Vec::new(),
|
|
|
|
regex: regex_pattern,
|
|
|
|
columns,
|
|
|
|
stream: stream.stream,
|
|
|
|
},
|
|
|
|
ctrlc,
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
|
|
|
|
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::Empty),
|
2021-11-15 19:27:15 +01:00
|
|
|
|
2023-02-09 03:59:02 +01:00
|
|
|
PipelineData::ExternalStream {
|
|
|
|
stdout: Some(stream),
|
|
|
|
..
|
|
|
|
} => Ok(PipelineData::ListStream(
|
|
|
|
ListStream::from_stream(
|
|
|
|
ParseStreamerExternal {
|
|
|
|
span: head,
|
|
|
|
excess: Vec::new(),
|
|
|
|
regex: regex_pattern,
|
|
|
|
columns,
|
|
|
|
stream: stream.stream,
|
|
|
|
},
|
|
|
|
ctrlc,
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
}
|
2021-11-15 19:27:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_regex(input: &str, span: Span) -> Result<String, ShellError> {
|
|
|
|
let mut output = "(?s)\\A".to_string();
|
|
|
|
|
|
|
|
//let mut loop_input = input;
|
|
|
|
let mut loop_input = input.chars().peekable();
|
|
|
|
loop {
|
|
|
|
let mut before = String::new();
|
|
|
|
while let Some(c) = loop_input.next() {
|
|
|
|
if c == '{' {
|
|
|
|
// If '{{', still creating a plaintext parse command, but just for a single '{' char
|
|
|
|
if loop_input.peek() == Some(&'{') {
|
|
|
|
let _ = loop_input.next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
before.push(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !before.is_empty() {
|
2022-08-04 21:51:02 +02:00
|
|
|
output.push_str(&fancy_regex::escape(&before));
|
2021-11-15 19:27:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for column as we're now at one
|
|
|
|
let mut column = String::new();
|
|
|
|
while let Some(c) = loop_input.next() {
|
|
|
|
if c == '}' {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
column.push(c);
|
|
|
|
|
|
|
|
if loop_input.peek().is_none() {
|
|
|
|
return Err(ShellError::DelimiterError(
|
|
|
|
"Found opening `{` without an associated closing `}`".to_owned(),
|
|
|
|
span,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !column.is_empty() {
|
|
|
|
output.push_str("(?P<");
|
|
|
|
output.push_str(&column);
|
|
|
|
output.push_str(">.*?)");
|
|
|
|
}
|
|
|
|
|
|
|
|
if before.is_empty() && column.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push_str("\\z");
|
|
|
|
Ok(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn column_names(regex: &Regex) -> Vec<String> {
|
|
|
|
regex
|
|
|
|
.capture_names()
|
|
|
|
.enumerate()
|
|
|
|
.skip(1)
|
|
|
|
.map(|(i, name)| {
|
|
|
|
name.map(String::from)
|
2023-01-29 14:34:34 +01:00
|
|
|
.unwrap_or_else(|| format!("capture{}", i - 1))
|
2021-11-15 19:27:15 +01:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2023-02-09 03:59:02 +01:00
|
|
|
pub struct ParseStreamer {
|
|
|
|
span: Span,
|
|
|
|
excess: Vec<Value>,
|
|
|
|
regex: Regex,
|
|
|
|
columns: Vec<String>,
|
|
|
|
stream: Box<dyn Iterator<Item = Value> + Send + 'static>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for ParseStreamer {
|
|
|
|
type Item = Value;
|
|
|
|
fn next(&mut self) -> Option<Value> {
|
|
|
|
if !self.excess.is_empty() {
|
|
|
|
return Some(self.excess.remove(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.stream.next();
|
|
|
|
|
|
|
|
if let Some(v) = v {
|
|
|
|
match v.as_string() {
|
|
|
|
Ok(s) => stream_helper(
|
|
|
|
self.regex.clone(),
|
|
|
|
v.span().unwrap_or(self.span),
|
|
|
|
s,
|
|
|
|
self.columns.clone(),
|
|
|
|
&mut self.excess,
|
|
|
|
),
|
|
|
|
Err(_) => Some(Value::Error {
|
|
|
|
error: ShellError::PipelineMismatch(
|
|
|
|
"string".into(),
|
|
|
|
self.span,
|
|
|
|
v.span().unwrap_or(self.span),
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ParseStreamerExternal {
|
|
|
|
span: Span,
|
|
|
|
excess: Vec<Value>,
|
|
|
|
regex: Regex,
|
|
|
|
columns: Vec<String>,
|
|
|
|
stream: Box<dyn Iterator<Item = Result<Vec<u8>, ShellError>> + Send + 'static>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for ParseStreamerExternal {
|
|
|
|
type Item = Value;
|
|
|
|
fn next(&mut self) -> Option<Value> {
|
|
|
|
if !self.excess.is_empty() {
|
|
|
|
return Some(self.excess.remove(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = self.stream.next();
|
|
|
|
|
|
|
|
if let Some(Ok(v)) = v {
|
|
|
|
match String::from_utf8(v) {
|
|
|
|
Ok(s) => stream_helper(
|
|
|
|
self.regex.clone(),
|
|
|
|
self.span,
|
|
|
|
s,
|
|
|
|
self.columns.clone(),
|
|
|
|
&mut self.excess,
|
|
|
|
),
|
|
|
|
Err(_) => Some(Value::Error {
|
|
|
|
error: ShellError::PipelineMismatch("string".into(), self.span, self.span),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
} else if let Some(Err(err)) = v {
|
|
|
|
Some(Value::Error { error: err })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stream_helper(
|
|
|
|
regex: Regex,
|
|
|
|
span: Span,
|
|
|
|
s: String,
|
|
|
|
columns: Vec<String>,
|
|
|
|
excess: &mut Vec<Value>,
|
|
|
|
) -> Option<Value> {
|
|
|
|
let results = regex.captures_iter(&s);
|
|
|
|
|
|
|
|
for c in results {
|
|
|
|
let mut cols = Vec::with_capacity(columns.len());
|
|
|
|
let captures = match c {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => {
|
|
|
|
return Some(Value::Error {
|
|
|
|
error: ShellError::GenericError(
|
|
|
|
"Error with regular expression captures".into(),
|
|
|
|
e.to_string(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mut vals = Vec::with_capacity(captures.len());
|
|
|
|
|
|
|
|
for (column_name, cap) in columns.iter().zip(captures.iter().skip(1)) {
|
|
|
|
let cap_string = cap.map(|v| v.as_str()).unwrap_or("").to_string();
|
|
|
|
cols.push(column_name.clone());
|
|
|
|
vals.push(Value::String {
|
|
|
|
val: cap_string,
|
|
|
|
span,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
excess.push(Value::Record { cols, vals, span });
|
|
|
|
}
|
|
|
|
|
|
|
|
if !excess.is_empty() {
|
|
|
|
Some(excess.remove(0))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 19:27:15 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
crate::test_examples(Parse)
|
|
|
|
}
|
|
|
|
}
|