Merge pull request #181 from nushell/string-arg

Make signatures a little more general
This commit is contained in:
Jonathan Turner
2019-07-16 09:40:36 +12:00
committed by GitHub
18 changed files with 292 additions and 194 deletions

View File

@ -8,8 +8,8 @@ use crate::parser::{Span, Spanned, Unit};
use derive_new::new;
use getset::Getters;
crate use baseline_parse::baseline_parse_single_token;
crate use baseline_parse_tokens::{baseline_parse_next_expr, ExpressionKindHint, TokensIterator};
crate use baseline_parse::{baseline_parse_single_token, baseline_parse_token_as_string};
crate use baseline_parse_tokens::{baseline_parse_next_expr, SyntaxType, TokensIterator};
crate use binary::Binary;
crate use named::NamedArguments;
crate use path::Path;

View File

@ -13,3 +13,16 @@ pub fn baseline_parse_single_token(token: &Token, source: &Text) -> hir::Express
RawToken::Bare => hir::Expression::bare(token.span),
}
}
pub fn baseline_parse_token_as_string(token: &Token, source: &Text) -> hir::Expression {
match *token.item() {
RawToken::Variable(span) if span.slice(source) == "it" => {
hir::Expression::it_variable(span, token.span)
}
RawToken::Variable(span) => hir::Expression::variable(span, token.span),
RawToken::Integer(_) => hir::Expression::bare(token.span),
RawToken::Size(int, unit) => hir::Expression::bare(token.span),
RawToken::Bare => hir::Expression::bare(token.span),
RawToken::String(span) => hir::Expression::string(span, token.span),
}
}

View File

@ -1,11 +1,14 @@
use crate::errors::ShellError;
use crate::parser::registry::CommandRegistry;
use crate::parser::{
hir, hir::baseline_parse_single_token, DelimitedNode, Delimiter, PathNode, RawToken, Span,
Spanned, TokenNode,
hir,
hir::{baseline_parse_single_token, baseline_parse_token_as_string},
DelimitedNode, Delimiter, PathNode, RawToken, Span, Spanned, TokenNode,
};
use crate::{SpannedItem, Text};
use derive_new::new;
use log::trace;
use serde_derive::{Deserialize, Serialize};
pub fn baseline_parse_tokens(
token_nodes: &mut TokensIterator<'_>,
@ -19,7 +22,7 @@ pub fn baseline_parse_tokens(
break;
}
let expr = baseline_parse_next_expr(token_nodes, registry, source, None)?;
let expr = baseline_parse_next_expr(token_nodes, registry, source, SyntaxType::Any)?;
exprs.push(expr);
}
@ -27,10 +30,12 @@ pub fn baseline_parse_tokens(
}
#[allow(unused)]
#[derive(Debug)]
pub enum ExpressionKindHint {
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum SyntaxType {
Any,
Literal,
Variable,
Path,
Binary,
Block,
Boolean,
@ -40,13 +45,31 @@ pub fn baseline_parse_next_expr(
tokens: &mut TokensIterator,
registry: &dyn CommandRegistry,
source: &Text,
coerce_hint: Option<ExpressionKindHint>,
syntax_type: SyntaxType,
) -> Result<hir::Expression, ShellError> {
let first = match tokens.next() {
None => return Err(ShellError::string("Expected token, found none")),
Some(token) => baseline_parse_semantic_token(token, registry, source)?,
let next = tokens
.next()
.ok_or_else(|| ShellError::string("Expected token, found none"))?;
trace!(target: "nu::parser::parse_one_expr", "syntax_type={:?}, token={:?}", syntax_type, next);
match (syntax_type, next) {
(SyntaxType::Path, TokenNode::Token(token)) => {
return Ok(baseline_parse_token_as_string(token, source))
}
(SyntaxType::Path, token) => {
return Err(ShellError::type_error(
"Path",
token.type_name().spanned(token.span()),
))
}
_ => {}
};
let first = baseline_parse_semantic_token(next, registry, source)?;
let possible_op = tokens.peek();
let op = match possible_op {
@ -69,8 +92,8 @@ pub fn baseline_parse_next_expr(
// We definitely have a binary expression here -- let's see if we should coerce it into a block
match coerce_hint {
None => {
match syntax_type {
SyntaxType::Any => {
let span = (first.span.start, second.span.end);
let binary = hir::Binary::new(first, op, second);
let binary = hir::RawExpression::Binary(Box::new(binary));
@ -79,74 +102,75 @@ pub fn baseline_parse_next_expr(
Ok(binary)
}
Some(hint) => match hint {
ExpressionKindHint::Block => {
let span = (first.span.start, second.span.end);
SyntaxType::Block => {
let span = (first.span.start, second.span.end);
let path: Spanned<hir::RawExpression> = match first {
let path: Spanned<hir::RawExpression> = match first {
Spanned {
item: hir::RawExpression::Literal(hir::Literal::Bare),
span,
} => {
let string = Spanned::from_item(span.slice(source).to_string(), span);
let path = hir::Path::new(
Spanned::from_item(
// TODO: Deal with synthetic nodes that have no representation at all in source
hir::RawExpression::Variable(hir::Variable::It(Span::from((0, 0)))),
(0, 0),
),
vec![string],
);
let path = hir::RawExpression::Path(Box::new(path));
Spanned {
item: hir::RawExpression::Literal(hir::Literal::Bare),
item: path,
span: first.span,
}
}
Spanned {
item: hir::RawExpression::Literal(hir::Literal::String(inner)),
span,
} => {
let string = Spanned::from_item(inner.slice(source).to_string(), span);
let path = hir::Path::new(
Spanned::from_item(
// TODO: Deal with synthetic nodes that have no representation at all in source
hir::RawExpression::Variable(hir::Variable::It(Span::from((0, 0)))),
(0, 0),
),
vec![string],
);
let path = hir::RawExpression::Path(Box::new(path));
Spanned {
item: path,
span: first.span,
}
}
Spanned {
item: hir::RawExpression::Variable(..),
..
} => first,
Spanned { span, item } => {
return Err(ShellError::labeled_error(
"The first part of an un-braced block must be a column name",
item.type_name(),
span,
} => {
let string = Spanned::from_item(span.slice(source).to_string(), span);
let path = hir::Path::new(
Spanned::from_item(
// TODO: Deal with synthetic nodes that have no representation at all in source
hir::RawExpression::Variable(hir::Variable::It(Span::from((0, 0)))),
(0, 0),
),
vec![string],
);
let path = hir::RawExpression::Path(Box::new(path));
Spanned {
item: path,
span: first.span,
}
}
Spanned {
item: hir::RawExpression::Literal(hir::Literal::String(inner)),
span,
} => {
let string = Spanned::from_item(inner.slice(source).to_string(), span);
let path = hir::Path::new(
Spanned::from_item(
// TODO: Deal with synthetic nodes that have no representation at all in source
hir::RawExpression::Variable(hir::Variable::It(Span::from((0, 0)))),
(0, 0),
),
vec![string],
);
let path = hir::RawExpression::Path(Box::new(path));
Spanned {
item: path,
span: first.span,
}
}
Spanned {
item: hir::RawExpression::Variable(..),
..
} => first,
Spanned { span, item } => {
return Err(ShellError::labeled_error(
"The first part of an un-braced block must be a column name",
item.type_name(),
span,
))
}
};
))
}
};
let binary = hir::Binary::new(path, op, second);
let binary = hir::RawExpression::Binary(Box::new(binary));
let binary = Spanned::from_item(binary, span);
let binary = hir::Binary::new(path, op, second);
let binary = hir::RawExpression::Binary(Box::new(binary));
let binary = Spanned::from_item(binary, span);
let block = hir::RawExpression::Block(vec![binary]);
let block = Spanned::from_item(block, span);
let block = hir::RawExpression::Block(vec![binary]);
let block = Spanned::from_item(block, span);
Ok(block)
}
Ok(block)
}
other => unimplemented!("coerce hint {:?}", other),
},
other => Err(ShellError::unimplemented(format!(
"coerce hint {:?}",
other
))),
}
}
@ -161,8 +185,10 @@ pub fn baseline_parse_semantic_token(
TokenNode::Delimited(delimited) => baseline_parse_delimited(delimited, registry, source),
TokenNode::Pipeline(_pipeline) => unimplemented!(),
TokenNode::Operator(_op) => unreachable!(),
TokenNode::Flag(_flag) => Err(ShellError::unimplemented("passing flags is not supported yet.")),
TokenNode::Identifier(_span) => unreachable!(),
TokenNode::Flag(_flag) => Err(ShellError::unimplemented(
"passing flags is not supported yet.",
)),
TokenNode::Member(_span) => unreachable!(),
TokenNode::Whitespace(_span) => unreachable!(),
TokenNode::Error(error) => Err(*error.item.clone()),
TokenNode::Path(path) => baseline_parse_path(path, registry, source),
@ -210,7 +236,7 @@ pub fn baseline_parse_path(
}
},
TokenNode::Identifier(span) => span.slice(source),
TokenNode::Member(span) => span.slice(source),
// TODO: Make this impossible
other => unreachable!("{:?}", other),

View File

@ -152,7 +152,7 @@ pub fn var(input: NomSpan) -> IResult<NomSpan, TokenNode> {
trace_step(input, "var", move |input| {
let start = input.offset;
let (input, _) = tag("$")(input)?;
let (input, bare) = identifier(input)?;
let (input, bare) = member(input)?;
let end = input.offset;
Ok((
@ -162,14 +162,7 @@ pub fn var(input: NomSpan) -> IResult<NomSpan, TokenNode> {
})
}
// let start = input.offset;
// let (input, _) = take_while1(is_start_bare_char)(input)?;
// let (input, _) = take_while(is_bare_char)(input)?;
// let end = input.offset;
// Ok((input, TokenTreeBuilder::spanned_bare((start, end))))
pub fn identifier(input: NomSpan) -> IResult<NomSpan, TokenNode> {
pub fn member(input: NomSpan) -> IResult<NomSpan, TokenNode> {
trace_step(input, "identifier", move |input| {
let start = input.offset;
let (input, _) = take_while1(is_id_start)(input)?;
@ -177,7 +170,7 @@ pub fn identifier(input: NomSpan) -> IResult<NomSpan, TokenNode> {
let end = input.offset;
Ok((input, TokenTreeBuilder::spanned_ident((start, end))))
Ok((input, TokenTreeBuilder::spanned_member((start, end))))
})
}
@ -430,7 +423,7 @@ pub fn path(input: NomSpan) -> IResult<NomSpan, TokenNode> {
let left = input.offset;
let (input, head) = node1(input)?;
let (input, _) = tag(".")(input)?;
let (input, tail) = separated_list(tag("."), alt((identifier, string)))(input)?;
let (input, tail) = separated_list(tag("."), alt((member, string)))(input)?;
let right = input.offset;
Ok((
@ -821,14 +814,14 @@ mod tests {
let _ = pretty_env_logger::try_init();
assert_eq!(
apply(node, "node", "$it.print"),
build_token(b::path(b::var("it"), vec![b::ident("print")]))
build_token(b::path(b::var("it"), vec![b::member("print")]))
);
assert_eq!(
apply(node, "node", "$head.part1.part2"),
build_token(b::path(
b::var("head"),
vec![b::ident("part1"), b::ident("part2")]
vec![b::member("part1"), b::member("part2")]
))
);
@ -836,7 +829,7 @@ mod tests {
apply(node, "node", "( hello ).world"),
build_token(b::path(
b::parens(vec![b::sp(), b::bare("hello"), b::sp()]),
vec![b::ident("world")]
vec![b::member("world")]
))
);
@ -862,7 +855,7 @@ mod tests {
b::sp(),
b::path(
b::var("it"),
vec![b::ident("is"), b::string("great news"), b::ident("right")]
vec![b::member("is"), b::string("great news"), b::member("right")]
),
b::sp(),
b::bare("yep"),
@ -991,7 +984,7 @@ mod tests {
vec![
b::sp(),
b::braced(vec![
b::path(b::var("it"), vec![b::ident("size")]),
b::path(b::var("it"), vec![b::member("size")]),
b::sp(),
b::op(">"),
b::sp(),

View File

@ -15,7 +15,7 @@ pub enum TokenNode {
Pipeline(Spanned<Pipeline>),
Operator(Spanned<Operator>),
Flag(Spanned<Flag>),
Identifier(Span),
Member(Span),
Whitespace(Span),
#[allow(unused)]
Error(Spanned<Box<ShellError>>),
@ -92,13 +92,29 @@ impl TokenNode {
TokenNode::Pipeline(s) => s.span,
TokenNode::Operator(s) => s.span,
TokenNode::Flag(s) => s.span,
TokenNode::Identifier(s) => *s,
TokenNode::Member(s) => *s,
TokenNode::Whitespace(s) => *s,
TokenNode::Error(s) => s.span,
TokenNode::Path(s) => s.span,
}
}
pub fn type_name(&self) -> String {
match self {
TokenNode::Token(t) => t.type_name(),
TokenNode::Call(s) => "command",
TokenNode::Delimited(d) => d.type_name(),
TokenNode::Pipeline(s) => "pipeline",
TokenNode::Operator(s) => "operator",
TokenNode::Flag(s) => "flag",
TokenNode::Member(s) => "member",
TokenNode::Whitespace(s) => "whitespace",
TokenNode::Error(s) => "error",
TokenNode::Path(s) => "path",
}
.to_string()
}
pub fn debug(&'a self, source: &'a Text) -> DebugTokenNode<'a> {
DebugTokenNode { node: self, source }
}
@ -147,6 +163,16 @@ pub struct DelimitedNode {
children: Vec<TokenNode>,
}
impl DelimitedNode {
pub fn type_name(&self) -> &'static str {
match self.delimiter {
Delimiter::Brace => "braced expression",
Delimiter::Paren => "parenthesized expression",
Delimiter::Square => "array literal or index operator",
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, FromStr)]
pub enum Delimiter {
Paren,

View File

@ -266,17 +266,17 @@ impl TokenTreeBuilder {
))
}
pub fn ident(input: impl Into<String>) -> CurriedToken {
pub fn member(input: impl Into<String>) -> CurriedToken {
let input = input.into();
Box::new(move |b| {
let (start, end) = b.consume(&input);
TokenTreeBuilder::spanned_ident((start, end))
TokenTreeBuilder::spanned_member((start, end))
})
}
pub fn spanned_ident(span: impl Into<Span>) -> TokenNode {
TokenNode::Identifier(span.into())
pub fn spanned_member(span: impl Into<Span>) -> TokenNode {
TokenNode::Member(span.into())
}
pub fn call(head: CurriedToken, input: Vec<CurriedToken>) -> CurriedCall {

View File

@ -86,7 +86,7 @@ fn parse_command_tail(
named.insert_switch(name, flag);
}
NamedType::Mandatory(kind) => {
NamedType::Mandatory(syntax_type) => {
match extract_mandatory(config, name, tail, source, command_span) {
Err(err) => return Err(err), // produce a correct diagnostic
Ok((pos, flag)) => {
@ -100,19 +100,15 @@ fn parse_command_tail(
));
}
let expr = hir::baseline_parse_next_expr(
tail,
registry,
source,
kind.to_coerce_hint(),
)?;
let expr =
hir::baseline_parse_next_expr(tail, registry, source, *syntax_type)?;
tail.restart();
named.insert_mandatory(name, expr);
}
}
}
NamedType::Optional(kind) => match extract_optional(name, tail, source) {
NamedType::Optional(syntax_type) => match extract_optional(name, tail, source) {
Err(err) => return Err(err), // produce a correct diagnostic
Ok(Some((pos, flag))) => {
tail.move_to(pos);
@ -125,12 +121,7 @@ fn parse_command_tail(
));
}
let expr = hir::baseline_parse_next_expr(
tail,
registry,
source,
kind.to_coerce_hint(),
)?;
let expr = hir::baseline_parse_next_expr(tail, registry, source, *syntax_type)?;
tail.restart();
named.insert_optional(name, Some(expr));
@ -169,7 +160,7 @@ fn parse_command_tail(
}
}
let result = hir::baseline_parse_next_expr(tail, registry, source, arg.to_coerce_hint())?;
let result = hir::baseline_parse_next_expr(tail, registry, source, arg.syntax_type())?;
positional.push(result);
}

View File

@ -1,5 +1,5 @@
use crate::evaluate::{evaluate_baseline_expr, Scope};
use crate::parser::{hir, hir::ExpressionKindHint, parse_command, CallNode, Spanned};
use crate::parser::{hir, hir::SyntaxType, parse_command, CallNode, Spanned};
use crate::prelude::*;
use derive_new::new;
use getset::Getters;
@ -12,55 +12,34 @@ use std::fmt;
#[derive(Debug, Serialize, Deserialize)]
pub enum NamedType {
Switch,
Mandatory(NamedValue),
Optional(NamedValue),
}
#[derive(Debug, Serialize, Deserialize)]
pub enum NamedValue {
Single,
#[allow(unused)]
Block,
}
impl NamedValue {
crate fn to_coerce_hint(&self) -> Option<ExpressionKindHint> {
match self {
NamedValue::Single => None,
NamedValue::Block => Some(ExpressionKindHint::Block),
}
}
Mandatory(SyntaxType),
Optional(SyntaxType),
}
#[allow(unused)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PositionalType {
Mandatory(String, PositionalValue),
Optional(String, PositionalValue),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PositionalValue {
Value,
Block,
Mandatory(String, SyntaxType),
Optional(String, SyntaxType),
}
impl PositionalType {
pub fn mandatory(name: &str) -> PositionalType {
PositionalType::Mandatory(name.to_string(), PositionalValue::Value)
pub fn mandatory(name: &str, ty: SyntaxType) -> PositionalType {
PositionalType::Mandatory(name.to_string(), ty)
}
pub fn mandatory_any(name: &str) -> PositionalType {
PositionalType::Mandatory(name.to_string(), SyntaxType::Any)
}
pub fn mandatory_block(name: &str) -> PositionalType {
PositionalType::Mandatory(name.to_string(), PositionalValue::Block)
PositionalType::Mandatory(name.to_string(), SyntaxType::Block)
}
crate fn to_coerce_hint(&self) -> Option<ExpressionKindHint> {
crate fn to_coerce_hint(&self) -> Option<SyntaxType> {
match self {
PositionalType::Mandatory(_, PositionalValue::Block)
| PositionalType::Optional(_, PositionalValue::Block) => {
Some(ExpressionKindHint::Block)
}
PositionalType::Mandatory(_, SyntaxType::Block)
| PositionalType::Optional(_, SyntaxType::Block) => Some(SyntaxType::Block),
_ => None,
}
}
@ -71,6 +50,13 @@ impl PositionalType {
PositionalType::Optional(s, _) => s,
}
}
crate fn syntax_type(&self) -> SyntaxType {
match *self {
PositionalType::Mandatory(_, t) => t,
PositionalType::Optional(_, t) => t,
}
}
}
#[derive(Debug, Getters, Serialize, Deserialize)]