mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 22:47:43 +02:00
Custom command attributes (#14906)
# Description Add custom command attributes. - Attributes are placed before a command definition and start with a `@` character. - Attribute invocations consist of const command call. The command's name must start with "attr ", but this prefix is not used in the invocation. - A command named `attr example` is invoked as an attribute as `@example` - Several built-in attribute commands are provided as part of this PR - `attr example`: Attaches an example to the commands help text ```nushell # Double numbers @example "double an int" { 5 | double } --result 10 @example "double a float" { 0.5 | double } --result 1.0 def double []: [number -> number] { $in * 2 } ``` - `attr search-terms`: Adds search terms to a command - ~`attr env`: Equivalent to using `def --env`~ - ~`attr wrapped`: Equivalent to using `def --wrapped`~ shelved for later discussion - several testing related attributes in `std/testing` - If an attribute has no internal/special purpose, it's stored as command metadata that can be obtained with `scope commands`. - This allows having attributes like `@test` which can be used by test runners. - Used the `@example` attribute for `std` examples. - Updated the std tests and test runner to use `@test` attributes - Added completions for attributes # User-Facing Changes Users can add examples to their own command definitions, and add other arbitrary attributes. # Tests + Formatting - 🟢 toolkit fmt - 🟢 toolkit clippy - 🟢 toolkit test - 🟢 toolkit test stdlib # After Submitting - Add documentation about the attribute syntax and built-in attributes - `help attributes` --------- Co-authored-by: 132ikl <132@ikl.sh>
This commit is contained in:
@ -189,6 +189,12 @@ fn flatten_expression_into(
|
||||
}
|
||||
|
||||
match &expr.expr {
|
||||
Expr::AttributeBlock(ab) => {
|
||||
for attr in &ab.attributes {
|
||||
flatten_expression_into(working_set, &attr.expr, output);
|
||||
}
|
||||
flatten_expression_into(working_set, &ab.item, output);
|
||||
}
|
||||
Expr::BinaryOp(lhs, op, rhs) => {
|
||||
flatten_expression_into(working_set, lhs, output);
|
||||
flatten_expression_into(working_set, op, output);
|
||||
|
@ -3,11 +3,14 @@ use nu_protocol::{
|
||||
ast::{self, Expr, Expression},
|
||||
engine::{self, CallImpl, CommandType, UNKNOWN_SPAN_ID},
|
||||
ir::{self, DataSlice},
|
||||
CustomExample,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct KnownExternal {
|
||||
pub signature: Box<Signature>,
|
||||
pub attributes: Vec<(String, Value)>,
|
||||
pub examples: Vec<CustomExample>,
|
||||
}
|
||||
|
||||
impl Command for KnownExternal {
|
||||
@ -84,6 +87,17 @@ impl Command for KnownExternal {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn attributes(&self) -> Vec<(String, Value)> {
|
||||
self.attributes.clone()
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
self.examples
|
||||
.iter()
|
||||
.map(CustomExample::to_example)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Transform the args from an `ast::Call` onto a `run-external` call
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
use crate::{Token, TokenContents};
|
||||
use itertools::{Either, Itertools};
|
||||
use nu_protocol::{ast::RedirectionSource, ParseError, Span};
|
||||
use nu_protocol::{ast::RedirectionSource, engine::StateWorkingSet, ParseError, Span};
|
||||
use std::mem;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@ -65,6 +65,8 @@ pub struct LiteCommand {
|
||||
pub comments: Vec<Span>,
|
||||
pub parts: Vec<Span>,
|
||||
pub redirection: Option<LiteRedirection>,
|
||||
/// one past the end indices of attributes
|
||||
pub attribute_idx: Vec<usize>,
|
||||
}
|
||||
|
||||
impl LiteCommand {
|
||||
@ -146,6 +148,25 @@ impl LiteCommand {
|
||||
)
|
||||
.sorted_unstable_by_key(|a| (a.start, a.end))
|
||||
}
|
||||
|
||||
pub fn command_parts(&self) -> &[Span] {
|
||||
let command_start = self.attribute_idx.last().copied().unwrap_or(0);
|
||||
&self.parts[command_start..]
|
||||
}
|
||||
|
||||
pub fn has_attributes(&self) -> bool {
|
||||
!self.attribute_idx.is_empty()
|
||||
}
|
||||
|
||||
pub fn attribute_commands(&'_ self) -> impl Iterator<Item = LiteCommand> + '_ {
|
||||
std::iter::once(0)
|
||||
.chain(self.attribute_idx.iter().copied())
|
||||
.tuple_windows()
|
||||
.map(|(s, e)| LiteCommand {
|
||||
parts: self.parts[s..e].to_owned(),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
@ -188,7 +209,17 @@ fn last_non_comment_token(tokens: &[Token], cur_idx: usize) -> Option<TokenConte
|
||||
None
|
||||
}
|
||||
|
||||
pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Mode {
|
||||
Assignment,
|
||||
Attribute,
|
||||
Normal,
|
||||
}
|
||||
|
||||
pub fn lite_parse(
|
||||
tokens: &[Token],
|
||||
working_set: &StateWorkingSet,
|
||||
) -> (LiteBlock, Option<ParseError>) {
|
||||
if tokens.is_empty() {
|
||||
return (LiteBlock::default(), None);
|
||||
}
|
||||
@ -200,220 +231,263 @@ pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
||||
let mut last_token = TokenContents::Eol;
|
||||
let mut file_redirection = None;
|
||||
let mut curr_comment: Option<Vec<Span>> = None;
|
||||
let mut is_assignment = false;
|
||||
let mut mode = Mode::Normal;
|
||||
let mut error = None;
|
||||
|
||||
for (idx, token) in tokens.iter().enumerate() {
|
||||
if is_assignment {
|
||||
match &token.contents {
|
||||
// Consume until semicolon or terminating EOL. Assignments absorb pipelines and
|
||||
// redirections.
|
||||
TokenContents::Eol => {
|
||||
// Handle `[Command] [Pipe] ([Comment] | [Eol])+ [Command]`
|
||||
//
|
||||
// `[Eol]` branch checks if previous token is `[Pipe]` to construct pipeline
|
||||
// and so `[Comment] | [Eol]` should be ignore to make it work
|
||||
let actual_token = last_non_comment_token(tokens, idx);
|
||||
if actual_token != Some(TokenContents::Pipe) {
|
||||
is_assignment = false;
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
match mode {
|
||||
Mode::Attribute => {
|
||||
match &token.contents {
|
||||
// Consume until semicolon or terminating EOL. Attributes can't contain pipelines or redirections.
|
||||
TokenContents::Eol | TokenContents::Semicolon => {
|
||||
command.attribute_idx.push(command.parts.len());
|
||||
mode = Mode::Normal;
|
||||
if matches!(last_token, TokenContents::Eol | TokenContents::Semicolon) {
|
||||
// Clear out the comment as we're entering a new comment
|
||||
curr_comment = None;
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
}
|
||||
|
||||
if last_token == TokenContents::Eol {
|
||||
// Clear out the comment as we're entering a new comment
|
||||
curr_comment = None;
|
||||
}
|
||||
}
|
||||
TokenContents::Semicolon => {
|
||||
is_assignment = false;
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
TokenContents::Comment => {
|
||||
command.comments.push(token.span);
|
||||
curr_comment = None;
|
||||
}
|
||||
_ => command.push(token.span),
|
||||
}
|
||||
} else if let Some((source, append, span)) = file_redirection.take() {
|
||||
match &token.contents {
|
||||
TokenContents::PipePipe => {
|
||||
error = error.or(Some(ParseError::ShellOrOr(token.span)));
|
||||
command.push(span);
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::Item => {
|
||||
let target = LiteRedirectionTarget::File {
|
||||
connector: span,
|
||||
file: token.span,
|
||||
append,
|
||||
};
|
||||
if let Err(err) = command.try_add_redirection(source, target) {
|
||||
error = error.or(Some(err));
|
||||
command.push(span);
|
||||
command.push(token.span)
|
||||
}
|
||||
}
|
||||
TokenContents::AssignmentOperator => {
|
||||
error = error.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::OutGreaterThan
|
||||
| TokenContents::OutGreaterGreaterThan
|
||||
| TokenContents::ErrGreaterThan
|
||||
| TokenContents::ErrGreaterGreaterThan
|
||||
| TokenContents::OutErrGreaterThan
|
||||
| TokenContents::OutErrGreaterGreaterThan => {
|
||||
error = error.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::Pipe
|
||||
| TokenContents::ErrGreaterPipe
|
||||
| TokenContents::OutErrGreaterPipe => {
|
||||
error = error.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::Eol => {
|
||||
error = error.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
pipeline.push(&mut command);
|
||||
}
|
||||
TokenContents::Semicolon => {
|
||||
error = error.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
TokenContents::Comment => {
|
||||
error = error.or(Some(ParseError::Expected("redirection target", span)));
|
||||
command.push(span);
|
||||
command.comments.push(token.span);
|
||||
curr_comment = None;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match &token.contents {
|
||||
TokenContents::PipePipe => {
|
||||
error = error.or(Some(ParseError::ShellOrOr(token.span)));
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::Item => {
|
||||
// This is commented out to preserve old parser behavior,
|
||||
// but we should probably error here.
|
||||
//
|
||||
// if element.redirection.is_some() {
|
||||
// error = error.or(Some(ParseError::LabeledError(
|
||||
// "Unexpected positional".into(),
|
||||
// "cannot add positional arguments after output redirection".into(),
|
||||
// token.span,
|
||||
// )));
|
||||
// }
|
||||
//
|
||||
// For example, this is currently allowed: ^echo thing o> out.txt extra_arg
|
||||
|
||||
// If we have a comment, go ahead and attach it
|
||||
if let Some(curr_comment) = curr_comment.take() {
|
||||
command.comments = curr_comment;
|
||||
}
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::AssignmentOperator => {
|
||||
// When in assignment mode, we'll just consume pipes or redirections as part of
|
||||
// the command.
|
||||
is_assignment = true;
|
||||
if let Some(curr_comment) = curr_comment.take() {
|
||||
command.comments = curr_comment;
|
||||
}
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::OutGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stdout, false, token.span));
|
||||
}
|
||||
TokenContents::OutGreaterGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stdout, true, token.span));
|
||||
}
|
||||
TokenContents::ErrGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stderr, false, token.span));
|
||||
}
|
||||
TokenContents::ErrGreaterGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stderr, true, token.span));
|
||||
}
|
||||
TokenContents::OutErrGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection =
|
||||
Some((RedirectionSource::StdoutAndStderr, false, token.span));
|
||||
}
|
||||
TokenContents::OutErrGreaterGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::StdoutAndStderr, true, token.span));
|
||||
}
|
||||
TokenContents::ErrGreaterPipe => {
|
||||
let target = LiteRedirectionTarget::Pipe {
|
||||
connector: token.span,
|
||||
};
|
||||
if let Err(err) = command.try_add_redirection(RedirectionSource::Stderr, target)
|
||||
{
|
||||
error = error.or(Some(err));
|
||||
}
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::OutErrGreaterPipe => {
|
||||
let target = LiteRedirectionTarget::Pipe {
|
||||
connector: token.span,
|
||||
};
|
||||
if let Err(err) =
|
||||
command.try_add_redirection(RedirectionSource::StdoutAndStderr, target)
|
||||
{
|
||||
error = error.or(Some(err));
|
||||
}
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::Pipe => {
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::Eol => {
|
||||
// Handle `[Command] [Pipe] ([Comment] | [Eol])+ [Command]`
|
||||
//
|
||||
// `[Eol]` branch checks if previous token is `[Pipe]` to construct pipeline
|
||||
// and so `[Comment] | [Eol]` should be ignore to make it work
|
||||
let actual_token = last_non_comment_token(tokens, idx);
|
||||
if actual_token != Some(TokenContents::Pipe) {
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
|
||||
if last_token == TokenContents::Eol {
|
||||
// Clear out the comment as we're entering a new comment
|
||||
curr_comment = None;
|
||||
}
|
||||
}
|
||||
TokenContents::Semicolon => {
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
TokenContents::Comment => {
|
||||
// Comment is beside something
|
||||
if last_token != TokenContents::Eol {
|
||||
TokenContents::Comment => {
|
||||
command.comments.push(token.span);
|
||||
curr_comment = None;
|
||||
} else {
|
||||
// Comment precedes something
|
||||
if let Some(curr_comment) = &mut curr_comment {
|
||||
curr_comment.push(token.span);
|
||||
} else {
|
||||
curr_comment = Some(vec![token.span]);
|
||||
}
|
||||
_ => command.push(token.span),
|
||||
}
|
||||
}
|
||||
Mode::Assignment => {
|
||||
match &token.contents {
|
||||
// Consume until semicolon or terminating EOL. Assignments absorb pipelines and
|
||||
// redirections.
|
||||
TokenContents::Eol => {
|
||||
// Handle `[Command] [Pipe] ([Comment] | [Eol])+ [Command]`
|
||||
//
|
||||
// `[Eol]` branch checks if previous token is `[Pipe]` to construct pipeline
|
||||
// and so `[Comment] | [Eol]` should be ignore to make it work
|
||||
let actual_token = last_non_comment_token(tokens, idx);
|
||||
if actual_token != Some(TokenContents::Pipe) {
|
||||
mode = Mode::Normal;
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
|
||||
if last_token == TokenContents::Eol {
|
||||
// Clear out the comment as we're entering a new comment
|
||||
curr_comment = None;
|
||||
}
|
||||
}
|
||||
TokenContents::Semicolon => {
|
||||
mode = Mode::Normal;
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
TokenContents::Comment => {
|
||||
command.comments.push(token.span);
|
||||
curr_comment = None;
|
||||
}
|
||||
_ => command.push(token.span),
|
||||
}
|
||||
}
|
||||
Mode::Normal => {
|
||||
if let Some((source, append, span)) = file_redirection.take() {
|
||||
match &token.contents {
|
||||
TokenContents::PipePipe => {
|
||||
error = error.or(Some(ParseError::ShellOrOr(token.span)));
|
||||
command.push(span);
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::Item => {
|
||||
let target = LiteRedirectionTarget::File {
|
||||
connector: span,
|
||||
file: token.span,
|
||||
append,
|
||||
};
|
||||
if let Err(err) = command.try_add_redirection(source, target) {
|
||||
error = error.or(Some(err));
|
||||
command.push(span);
|
||||
command.push(token.span)
|
||||
}
|
||||
}
|
||||
TokenContents::AssignmentOperator => {
|
||||
error = error
|
||||
.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::OutGreaterThan
|
||||
| TokenContents::OutGreaterGreaterThan
|
||||
| TokenContents::ErrGreaterThan
|
||||
| TokenContents::ErrGreaterGreaterThan
|
||||
| TokenContents::OutErrGreaterThan
|
||||
| TokenContents::OutErrGreaterGreaterThan => {
|
||||
error = error
|
||||
.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::Pipe
|
||||
| TokenContents::ErrGreaterPipe
|
||||
| TokenContents::OutErrGreaterPipe => {
|
||||
error = error
|
||||
.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::Eol => {
|
||||
error = error
|
||||
.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
pipeline.push(&mut command);
|
||||
}
|
||||
TokenContents::Semicolon => {
|
||||
error = error
|
||||
.or(Some(ParseError::Expected("redirection target", token.span)));
|
||||
command.push(span);
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
TokenContents::Comment => {
|
||||
error =
|
||||
error.or(Some(ParseError::Expected("redirection target", span)));
|
||||
command.push(span);
|
||||
command.comments.push(token.span);
|
||||
curr_comment = None;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match &token.contents {
|
||||
TokenContents::PipePipe => {
|
||||
error = error.or(Some(ParseError::ShellOrOr(token.span)));
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::Item => {
|
||||
// FIXME: This is commented out to preserve old parser behavior,
|
||||
// but we should probably error here.
|
||||
//
|
||||
// if element.redirection.is_some() {
|
||||
// error = error.or(Some(ParseError::LabeledError(
|
||||
// "Unexpected positional".into(),
|
||||
// "cannot add positional arguments after output redirection".into(),
|
||||
// token.span,
|
||||
// )));
|
||||
// }
|
||||
//
|
||||
// For example, this is currently allowed: ^echo thing o> out.txt extra_arg
|
||||
|
||||
if working_set.get_span_contents(token.span).starts_with(b"@") {
|
||||
if matches!(
|
||||
last_token,
|
||||
TokenContents::Eol | TokenContents::Semicolon
|
||||
) {
|
||||
mode = Mode::Attribute;
|
||||
}
|
||||
command.push(token.span);
|
||||
} else {
|
||||
// If we have a comment, go ahead and attach it
|
||||
if let Some(curr_comment) = curr_comment.take() {
|
||||
command.comments = curr_comment;
|
||||
}
|
||||
command.push(token.span);
|
||||
}
|
||||
}
|
||||
TokenContents::AssignmentOperator => {
|
||||
// When in assignment mode, we'll just consume pipes or redirections as part of
|
||||
// the command.
|
||||
mode = Mode::Assignment;
|
||||
if let Some(curr_comment) = curr_comment.take() {
|
||||
command.comments = curr_comment;
|
||||
}
|
||||
command.push(token.span);
|
||||
}
|
||||
TokenContents::OutGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stdout, false, token.span));
|
||||
}
|
||||
TokenContents::OutGreaterGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stdout, true, token.span));
|
||||
}
|
||||
TokenContents::ErrGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stderr, false, token.span));
|
||||
}
|
||||
TokenContents::ErrGreaterGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection = Some((RedirectionSource::Stderr, true, token.span));
|
||||
}
|
||||
TokenContents::OutErrGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection =
|
||||
Some((RedirectionSource::StdoutAndStderr, false, token.span));
|
||||
}
|
||||
TokenContents::OutErrGreaterGreaterThan => {
|
||||
error = error.or(command.check_accepts_redirection(token.span));
|
||||
file_redirection =
|
||||
Some((RedirectionSource::StdoutAndStderr, true, token.span));
|
||||
}
|
||||
TokenContents::ErrGreaterPipe => {
|
||||
let target = LiteRedirectionTarget::Pipe {
|
||||
connector: token.span,
|
||||
};
|
||||
if let Err(err) =
|
||||
command.try_add_redirection(RedirectionSource::Stderr, target)
|
||||
{
|
||||
error = error.or(Some(err));
|
||||
}
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::OutErrGreaterPipe => {
|
||||
let target = LiteRedirectionTarget::Pipe {
|
||||
connector: token.span,
|
||||
};
|
||||
if let Err(err) = command
|
||||
.try_add_redirection(RedirectionSource::StdoutAndStderr, target)
|
||||
{
|
||||
error = error.or(Some(err));
|
||||
}
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::Pipe => {
|
||||
pipeline.push(&mut command);
|
||||
command.pipe = Some(token.span);
|
||||
}
|
||||
TokenContents::Eol => {
|
||||
// Handle `[Command] [Pipe] ([Comment] | [Eol])+ [Command]`
|
||||
//
|
||||
// `[Eol]` branch checks if previous token is `[Pipe]` to construct pipeline
|
||||
// and so `[Comment] | [Eol]` should be ignore to make it work
|
||||
let actual_token = last_non_comment_token(tokens, idx);
|
||||
if actual_token != Some(TokenContents::Pipe) {
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
|
||||
if last_token == TokenContents::Eol {
|
||||
// Clear out the comment as we're entering a new comment
|
||||
curr_comment = None;
|
||||
}
|
||||
}
|
||||
TokenContents::Semicolon => {
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
}
|
||||
TokenContents::Comment => {
|
||||
// Comment is beside something
|
||||
if last_token != TokenContents::Eol {
|
||||
command.comments.push(token.span);
|
||||
curr_comment = None;
|
||||
} else {
|
||||
// Comment precedes something
|
||||
if let Some(curr_comment) = &mut curr_comment {
|
||||
curr_comment.push(token.span);
|
||||
} else {
|
||||
curr_comment = Some(vec![token.span]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -428,6 +502,10 @@ pub fn lite_parse(tokens: &[Token]) -> (LiteBlock, Option<ParseError>) {
|
||||
error = error.or(Some(ParseError::Expected("redirection target", span)));
|
||||
}
|
||||
|
||||
if let Mode::Attribute = mode {
|
||||
command.attribute_idx.push(command.parts.len());
|
||||
}
|
||||
|
||||
pipeline.push(&mut command);
|
||||
block.push(&mut pipeline);
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
exportable::Exportable,
|
||||
parse_block,
|
||||
parser::{parse_redirection, redirecting_builtin_error},
|
||||
parser::{parse_attribute, parse_redirection, redirecting_builtin_error},
|
||||
type_check::{check_block_input_output, type_compatible},
|
||||
};
|
||||
use itertools::Itertools;
|
||||
@ -9,14 +9,14 @@ use log::trace;
|
||||
use nu_path::canonicalize_with;
|
||||
use nu_protocol::{
|
||||
ast::{
|
||||
Argument, Block, Call, Expr, Expression, ImportPattern, ImportPatternHead,
|
||||
Argument, AttributeBlock, Block, Call, Expr, Expression, ImportPattern, ImportPatternHead,
|
||||
ImportPatternMember, Pipeline, PipelineElement,
|
||||
},
|
||||
engine::{StateWorkingSet, DEFAULT_OVERLAY_NAME},
|
||||
eval_const::eval_constant,
|
||||
parser_path::ParserPath,
|
||||
Alias, BlockId, DeclId, Module, ModuleId, ParseError, PositionalArg, ResolvedImportPattern,
|
||||
Span, Spanned, SyntaxShape, Type, Value, VarId,
|
||||
Alias, BlockId, CustomExample, DeclId, FromValue, Module, ModuleId, ParseError, PositionalArg,
|
||||
ResolvedImportPattern, ShellError, Span, Spanned, SyntaxShape, Type, Value, VarId,
|
||||
};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
@ -363,16 +363,166 @@ fn verify_not_reserved_variable_name(working_set: &mut StateWorkingSet, name: &s
|
||||
}
|
||||
}
|
||||
|
||||
// This is meant for parsing attribute blocks without an accompanying `def` or `extern`. It's
|
||||
// necessary to provide consistent syntax highlighting, completions, and helpful errors
|
||||
//
|
||||
// There is no need to run the const evaluation here
|
||||
pub fn parse_attribute_block(
|
||||
working_set: &mut StateWorkingSet,
|
||||
lite_command: &LiteCommand,
|
||||
) -> Pipeline {
|
||||
let attributes = lite_command
|
||||
.attribute_commands()
|
||||
.map(|cmd| parse_attribute(working_set, &cmd).0)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let last_attr_span = attributes
|
||||
.last()
|
||||
.expect("Attribute block must contain at least one attribute")
|
||||
.expr
|
||||
.span;
|
||||
|
||||
working_set.error(ParseError::AttributeRequiresDefinition(last_attr_span));
|
||||
let cmd_span = if lite_command.command_parts().is_empty() {
|
||||
last_attr_span.past()
|
||||
} else {
|
||||
Span::concat(lite_command.command_parts())
|
||||
};
|
||||
let cmd_expr = garbage(working_set, cmd_span);
|
||||
let ty = cmd_expr.ty.clone();
|
||||
|
||||
let attr_block_span = Span::merge_many(
|
||||
attributes
|
||||
.first()
|
||||
.map(|x| x.expr.span)
|
||||
.into_iter()
|
||||
.chain(Some(cmd_span)),
|
||||
);
|
||||
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::AttributeBlock(AttributeBlock {
|
||||
attributes,
|
||||
item: Box::new(cmd_expr),
|
||||
}),
|
||||
attr_block_span,
|
||||
ty,
|
||||
)])
|
||||
}
|
||||
|
||||
// Returns also the parsed command name and ID
|
||||
pub fn parse_def(
|
||||
working_set: &mut StateWorkingSet,
|
||||
lite_command: &LiteCommand,
|
||||
module_name: Option<&[u8]>,
|
||||
) -> (Pipeline, Option<(Vec<u8>, DeclId)>) {
|
||||
let spans = &lite_command.parts[..];
|
||||
let mut attributes = vec![];
|
||||
let mut attribute_vals = vec![];
|
||||
|
||||
for attr_cmd in lite_command.attribute_commands() {
|
||||
let (attr, name) = parse_attribute(working_set, &attr_cmd);
|
||||
if let Some(name) = name {
|
||||
let val = eval_constant(working_set, &attr.expr);
|
||||
match val {
|
||||
Ok(val) => attribute_vals.push((name, val)),
|
||||
Err(e) => working_set.error(e.wrap(working_set, attr.expr.span)),
|
||||
}
|
||||
}
|
||||
attributes.push(attr);
|
||||
}
|
||||
|
||||
let (expr, decl) = parse_def_inner(working_set, attribute_vals, lite_command, module_name);
|
||||
|
||||
let ty = expr.ty.clone();
|
||||
|
||||
let attr_block_span = Span::merge_many(
|
||||
attributes
|
||||
.first()
|
||||
.map(|x| x.expr.span)
|
||||
.into_iter()
|
||||
.chain(Some(expr.span)),
|
||||
);
|
||||
|
||||
let expr = if attributes.is_empty() {
|
||||
expr
|
||||
} else {
|
||||
Expression::new(
|
||||
working_set,
|
||||
Expr::AttributeBlock(AttributeBlock {
|
||||
attributes,
|
||||
item: Box::new(expr),
|
||||
}),
|
||||
attr_block_span,
|
||||
ty,
|
||||
)
|
||||
};
|
||||
|
||||
(Pipeline::from_vec(vec![expr]), decl)
|
||||
}
|
||||
|
||||
pub fn parse_extern(
|
||||
working_set: &mut StateWorkingSet,
|
||||
lite_command: &LiteCommand,
|
||||
module_name: Option<&[u8]>,
|
||||
) -> Pipeline {
|
||||
let mut attributes = vec![];
|
||||
let mut attribute_vals = vec![];
|
||||
|
||||
for attr_cmd in lite_command.attribute_commands() {
|
||||
let (attr, name) = parse_attribute(working_set, &attr_cmd);
|
||||
if let Some(name) = name {
|
||||
let val = eval_constant(working_set, &attr.expr);
|
||||
match val {
|
||||
Ok(val) => attribute_vals.push((name, val)),
|
||||
Err(e) => working_set.error(e.wrap(working_set, attr.expr.span)),
|
||||
}
|
||||
}
|
||||
attributes.push(attr);
|
||||
}
|
||||
|
||||
let expr = parse_extern_inner(working_set, attribute_vals, lite_command, module_name);
|
||||
|
||||
let ty = expr.ty.clone();
|
||||
|
||||
let attr_block_span = Span::merge_many(
|
||||
attributes
|
||||
.first()
|
||||
.map(|x| x.expr.span)
|
||||
.into_iter()
|
||||
.chain(Some(expr.span)),
|
||||
);
|
||||
|
||||
let expr = if attributes.is_empty() {
|
||||
expr
|
||||
} else {
|
||||
Expression::new(
|
||||
working_set,
|
||||
Expr::AttributeBlock(AttributeBlock {
|
||||
attributes,
|
||||
item: Box::new(expr),
|
||||
}),
|
||||
attr_block_span,
|
||||
ty,
|
||||
)
|
||||
};
|
||||
|
||||
Pipeline::from_vec(vec![expr])
|
||||
}
|
||||
|
||||
// Returns also the parsed command name and ID
|
||||
fn parse_def_inner(
|
||||
working_set: &mut StateWorkingSet,
|
||||
attributes: Vec<(String, Value)>,
|
||||
lite_command: &LiteCommand,
|
||||
module_name: Option<&[u8]>,
|
||||
) -> (Expression, Option<(Vec<u8>, DeclId)>) {
|
||||
let spans = lite_command.command_parts();
|
||||
|
||||
let (desc, extra_desc) = working_set.build_desc(&lite_command.comments);
|
||||
|
||||
let (attribute_vals, examples, search_terms) =
|
||||
handle_special_attributes(attributes, working_set);
|
||||
|
||||
// Checking that the function is used with the correct name
|
||||
// Maybe this is not necessary but it is a sanity check
|
||||
// Note: "export def" is treated the same as "def"
|
||||
@ -390,11 +540,11 @@ pub fn parse_def(
|
||||
"internal error: Wrong call name for def function".into(),
|
||||
Span::concat(spans),
|
||||
));
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
}
|
||||
if let Some(redirection) = lite_command.redirection.as_ref() {
|
||||
working_set.error(redirecting_builtin_error("def", redirection));
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
}
|
||||
|
||||
// Parsing the spans and checking that they match the register signature
|
||||
@ -406,7 +556,7 @@ pub fn parse_def(
|
||||
"internal error: def declaration not found".into(),
|
||||
Span::concat(spans),
|
||||
));
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
}
|
||||
Some(decl_id) => {
|
||||
working_set.enter_scope();
|
||||
@ -430,7 +580,7 @@ pub fn parse_def(
|
||||
String::from_utf8_lossy(&def_call).as_ref(),
|
||||
) {
|
||||
working_set.error(err);
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
}
|
||||
}
|
||||
|
||||
@ -474,17 +624,12 @@ pub fn parse_def(
|
||||
working_set.parse_errors.append(&mut new_errors);
|
||||
|
||||
let Ok(is_help) = has_flag_const(working_set, &call, "help") else {
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
};
|
||||
|
||||
if starting_error_count != working_set.parse_errors.len() || is_help {
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
output,
|
||||
)]),
|
||||
Expression::new(working_set, Expr::Call(call), call_span, output),
|
||||
None,
|
||||
);
|
||||
}
|
||||
@ -494,10 +639,10 @@ pub fn parse_def(
|
||||
};
|
||||
|
||||
let Ok(has_env) = has_flag_const(working_set, &call, "env") else {
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
};
|
||||
let Ok(has_wrapped) = has_flag_const(working_set, &call, "wrapped") else {
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
};
|
||||
|
||||
// All positional arguments must be in the call positional vector by this point
|
||||
@ -517,12 +662,7 @@ pub fn parse_def(
|
||||
name_expr_span,
|
||||
));
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
Type::Any,
|
||||
)]),
|
||||
Expression::new(working_set, Expr::Call(call), call_span, Type::Any),
|
||||
None,
|
||||
);
|
||||
}
|
||||
@ -534,7 +674,7 @@ pub fn parse_def(
|
||||
"Could not get string from string expression".into(),
|
||||
name_expr.span,
|
||||
));
|
||||
return (garbage_pipeline(working_set, spans), None);
|
||||
return (garbage(working_set, Span::concat(spans)), None);
|
||||
};
|
||||
|
||||
let mut result = None;
|
||||
@ -567,12 +707,7 @@ pub fn parse_def(
|
||||
format!("...rest-like positional argument used in 'def --wrapped' supports only strings. Change the type annotation of ...{} to 'string'.", &rest.name)));
|
||||
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
Type::Any,
|
||||
)]),
|
||||
Expression::new(working_set, Expr::Call(call), call_span, Type::Any),
|
||||
result,
|
||||
);
|
||||
}
|
||||
@ -581,12 +716,7 @@ pub fn parse_def(
|
||||
working_set.error(ParseError::MissingPositional("...rest-like positional argument".to_string(), name_expr.span, "def --wrapped must have a ...rest-like positional argument. Add '...rest: string' to the command's signature.".to_string()));
|
||||
|
||||
return (
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
Type::Any,
|
||||
)]),
|
||||
Expression::new(working_set, Expr::Call(call), call_span, Type::Any),
|
||||
result,
|
||||
);
|
||||
}
|
||||
@ -602,8 +732,11 @@ pub fn parse_def(
|
||||
signature.description = desc;
|
||||
signature.extra_description = extra_desc;
|
||||
signature.allows_unknown_args = has_wrapped;
|
||||
signature.search_terms = search_terms;
|
||||
|
||||
*declaration = signature.clone().into_block_command(block_id);
|
||||
*declaration = signature
|
||||
.clone()
|
||||
.into_block_command(block_id, attribute_vals, examples);
|
||||
|
||||
let block = working_set.get_block_mut(block_id);
|
||||
block.signature = signature;
|
||||
@ -637,25 +770,25 @@ pub fn parse_def(
|
||||
working_set.merge_predecl(name.as_bytes());
|
||||
|
||||
(
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
Type::Any,
|
||||
)]),
|
||||
Expression::new(working_set, Expr::Call(call), call_span, Type::Any),
|
||||
result,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_extern(
|
||||
fn parse_extern_inner(
|
||||
working_set: &mut StateWorkingSet,
|
||||
attributes: Vec<(String, Value)>,
|
||||
lite_command: &LiteCommand,
|
||||
module_name: Option<&[u8]>,
|
||||
) -> Pipeline {
|
||||
let spans = &lite_command.parts;
|
||||
) -> Expression {
|
||||
let spans = lite_command.command_parts();
|
||||
let concat_span = Span::concat(spans);
|
||||
|
||||
let (description, extra_description) = working_set.build_desc(&lite_command.comments);
|
||||
|
||||
let (attribute_vals, examples, search_terms) =
|
||||
handle_special_attributes(attributes, working_set);
|
||||
|
||||
// Checking that the function is used with the correct name
|
||||
// Maybe this is not necessary but it is a sanity check
|
||||
|
||||
@ -672,11 +805,11 @@ pub fn parse_extern(
|
||||
"internal error: Wrong call name for extern command".into(),
|
||||
Span::concat(spans),
|
||||
));
|
||||
return garbage_pipeline(working_set, spans);
|
||||
return garbage(working_set, concat_span);
|
||||
}
|
||||
if let Some(redirection) = lite_command.redirection.as_ref() {
|
||||
working_set.error(redirecting_builtin_error("extern", redirection));
|
||||
return garbage_pipeline(working_set, spans);
|
||||
return garbage(working_set, concat_span);
|
||||
}
|
||||
|
||||
// Parsing the spans and checking that they match the register signature
|
||||
@ -688,7 +821,7 @@ pub fn parse_extern(
|
||||
"internal error: def declaration not found".into(),
|
||||
Span::concat(spans),
|
||||
));
|
||||
return garbage_pipeline(working_set, spans);
|
||||
return garbage(working_set, concat_span);
|
||||
}
|
||||
Some(decl_id) => {
|
||||
working_set.enter_scope();
|
||||
@ -702,7 +835,7 @@ pub fn parse_extern(
|
||||
String::from_utf8_lossy(&extern_call).as_ref(),
|
||||
) {
|
||||
working_set.error(err);
|
||||
return garbage_pipeline(working_set, spans);
|
||||
return garbage(working_set, concat_span);
|
||||
}
|
||||
}
|
||||
|
||||
@ -736,12 +869,7 @@ pub fn parse_extern(
|
||||
"main".to_string(),
|
||||
name_expr_span,
|
||||
));
|
||||
return Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
Type::Any,
|
||||
)]);
|
||||
return Expression::new(working_set, Expr::Call(call), call_span, Type::Any);
|
||||
}
|
||||
}
|
||||
|
||||
@ -761,6 +889,7 @@ pub fn parse_extern(
|
||||
signature.name = external_name;
|
||||
signature.description = description;
|
||||
signature.extra_description = extra_description;
|
||||
signature.search_terms = search_terms;
|
||||
signature.allows_unknown_args = true;
|
||||
|
||||
if let Some(block_id) = body.and_then(|x| x.as_block()) {
|
||||
@ -770,7 +899,11 @@ pub fn parse_extern(
|
||||
name_expr.span,
|
||||
));
|
||||
} else {
|
||||
*declaration = signature.clone().into_block_command(block_id);
|
||||
*declaration = signature.clone().into_block_command(
|
||||
block_id,
|
||||
attribute_vals,
|
||||
examples,
|
||||
);
|
||||
|
||||
working_set.get_block_mut(block_id).signature = signature;
|
||||
}
|
||||
@ -785,7 +918,11 @@ pub fn parse_extern(
|
||||
);
|
||||
}
|
||||
|
||||
let decl = KnownExternal { signature };
|
||||
let decl = KnownExternal {
|
||||
signature,
|
||||
attributes: attribute_vals,
|
||||
examples,
|
||||
};
|
||||
|
||||
*declaration = Box::new(decl);
|
||||
}
|
||||
@ -807,12 +944,54 @@ pub fn parse_extern(
|
||||
}
|
||||
}
|
||||
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
Type::Any,
|
||||
)])
|
||||
Expression::new(working_set, Expr::Call(call), call_span, Type::Any)
|
||||
}
|
||||
|
||||
fn handle_special_attributes(
|
||||
attributes: Vec<(String, Value)>,
|
||||
working_set: &mut StateWorkingSet<'_>,
|
||||
) -> (Vec<(String, Value)>, Vec<CustomExample>, Vec<String>) {
|
||||
let mut attribute_vals = vec![];
|
||||
let mut examples = vec![];
|
||||
let mut search_terms = vec![];
|
||||
|
||||
for (name, value) in attributes {
|
||||
let val_span = value.span();
|
||||
match name.as_str() {
|
||||
"example" => match CustomExample::from_value(value) {
|
||||
Ok(example) => examples.push(example),
|
||||
Err(_) => {
|
||||
let e = ShellError::GenericError {
|
||||
error: "nu::shell::invalid_example".into(),
|
||||
msg: "Value couldn't be converted to an example".into(),
|
||||
span: Some(val_span),
|
||||
help: Some("Is `attr example` shadowed?".into()),
|
||||
inner: vec![],
|
||||
};
|
||||
working_set.error(e.wrap(working_set, val_span));
|
||||
}
|
||||
},
|
||||
"search-terms" => match <Vec<String>>::from_value(value) {
|
||||
Ok(mut terms) => {
|
||||
search_terms.append(&mut terms);
|
||||
}
|
||||
Err(_) => {
|
||||
let e = ShellError::GenericError {
|
||||
error: "nu::shell::invalid_search_terms".into(),
|
||||
msg: "Value couldn't be converted to search-terms".into(),
|
||||
span: Some(val_span),
|
||||
help: Some("Is `attr search-terms` shadowed?".into()),
|
||||
inner: vec![],
|
||||
};
|
||||
working_set.error(e.wrap(working_set, val_span));
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
attribute_vals.push((name, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
(attribute_vals, examples, search_terms)
|
||||
}
|
||||
|
||||
fn check_alias_name<'a>(working_set: &mut StateWorkingSet, spans: &'a [Span]) -> Option<&'a Span> {
|
||||
@ -1126,8 +1305,9 @@ pub fn parse_export_in_block(
|
||||
) -> Pipeline {
|
||||
let call_span = Span::concat(&lite_command.parts);
|
||||
|
||||
let full_name = if lite_command.parts.len() > 1 {
|
||||
let sub = working_set.get_span_contents(lite_command.parts[1]);
|
||||
let parts = lite_command.command_parts();
|
||||
let full_name = if parts.len() > 1 {
|
||||
let sub = working_set.get_span_contents(parts[1]);
|
||||
match sub {
|
||||
b"alias" => "export alias",
|
||||
b"def" => "export def",
|
||||
@ -1146,64 +1326,62 @@ pub fn parse_export_in_block(
|
||||
return garbage_pipeline(working_set, &lite_command.parts);
|
||||
}
|
||||
|
||||
if let Some(decl_id) = working_set.find_decl(full_name.as_bytes()) {
|
||||
let starting_error_count = working_set.parse_errors.len();
|
||||
let ParsedInternalCall { call, output, .. } = parse_internal_call(
|
||||
working_set,
|
||||
if full_name == "export" {
|
||||
lite_command.parts[0]
|
||||
} else {
|
||||
Span::concat(&lite_command.parts[0..2])
|
||||
},
|
||||
if full_name == "export" {
|
||||
&lite_command.parts[1..]
|
||||
} else {
|
||||
&lite_command.parts[2..]
|
||||
},
|
||||
decl_id,
|
||||
);
|
||||
// don't need errors generated by parse_internal_call
|
||||
// further error will be generated by detail `parse_xxx` function.
|
||||
working_set.parse_errors.truncate(starting_error_count);
|
||||
// No need to care for this when attributes are present, parse_attribute_block will throw the
|
||||
// necessary error
|
||||
if !lite_command.has_attributes() {
|
||||
if let Some(decl_id) = working_set.find_decl(full_name.as_bytes()) {
|
||||
let starting_error_count = working_set.parse_errors.len();
|
||||
let ParsedInternalCall { call, output, .. } = parse_internal_call(
|
||||
working_set,
|
||||
if full_name == "export" {
|
||||
parts[0]
|
||||
} else {
|
||||
Span::concat(&parts[0..2])
|
||||
},
|
||||
if full_name == "export" {
|
||||
&parts[1..]
|
||||
} else {
|
||||
&parts[2..]
|
||||
},
|
||||
decl_id,
|
||||
);
|
||||
// don't need errors generated by parse_internal_call
|
||||
// further error will be generated by detail `parse_xxx` function.
|
||||
working_set.parse_errors.truncate(starting_error_count);
|
||||
|
||||
let decl = working_set.get_decl(decl_id);
|
||||
check_call(working_set, call_span, &decl.signature(), &call);
|
||||
let Ok(is_help) = has_flag_const(working_set, &call, "help") else {
|
||||
let decl = working_set.get_decl(decl_id);
|
||||
check_call(working_set, call_span, &decl.signature(), &call);
|
||||
let Ok(is_help) = has_flag_const(working_set, &call, "help") else {
|
||||
return garbage_pipeline(working_set, &lite_command.parts);
|
||||
};
|
||||
|
||||
if starting_error_count != working_set.parse_errors.len() || is_help {
|
||||
return Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
output,
|
||||
)]);
|
||||
}
|
||||
} else {
|
||||
working_set.error(ParseError::UnknownState(
|
||||
format!("internal error: '{full_name}' declaration not found",),
|
||||
Span::concat(&lite_command.parts),
|
||||
));
|
||||
return garbage_pipeline(working_set, &lite_command.parts);
|
||||
};
|
||||
|
||||
if starting_error_count != working_set.parse_errors.len() || is_help {
|
||||
return Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
call_span,
|
||||
output,
|
||||
)]);
|
||||
}
|
||||
} else {
|
||||
working_set.error(ParseError::UnknownState(
|
||||
format!("internal error: '{full_name}' declaration not found",),
|
||||
Span::concat(&lite_command.parts),
|
||||
));
|
||||
return garbage_pipeline(working_set, &lite_command.parts);
|
||||
};
|
||||
|
||||
if full_name == "export" {
|
||||
// export by itself is meaningless
|
||||
working_set.error(ParseError::UnexpectedKeyword(
|
||||
"export".into(),
|
||||
lite_command.parts[0],
|
||||
));
|
||||
return garbage_pipeline(working_set, &lite_command.parts);
|
||||
}
|
||||
|
||||
match full_name {
|
||||
"export alias" => parse_alias(working_set, lite_command, None),
|
||||
// `parse_def` and `parse_extern` work both with and without attributes
|
||||
"export def" => parse_def(working_set, lite_command, None).0,
|
||||
"export extern" => parse_extern(working_set, lite_command, None),
|
||||
// Other definitions can't have attributes, so we handle attributes here with parse_attribute_block
|
||||
_ if lite_command.has_attributes() => parse_attribute_block(working_set, lite_command),
|
||||
"export alias" => parse_alias(working_set, lite_command, None),
|
||||
"export const" => parse_const(working_set, &lite_command.parts[1..]).0,
|
||||
"export use" => parse_use(working_set, lite_command, None).0,
|
||||
"export module" => parse_module(working_set, lite_command, None).0,
|
||||
"export extern" => parse_extern(working_set, lite_command, None),
|
||||
_ => {
|
||||
working_set.error(ParseError::UnexpectedKeyword(
|
||||
full_name.into(),
|
||||
@ -1222,7 +1400,7 @@ pub fn parse_export_in_module(
|
||||
module_name: &[u8],
|
||||
parent_module: &mut Module,
|
||||
) -> (Pipeline, Vec<Exportable>) {
|
||||
let spans = &lite_command.parts[..];
|
||||
let spans = lite_command.command_parts();
|
||||
|
||||
let export_span = if let Some(sp) = spans.first() {
|
||||
if working_set.get_span_contents(*sp) != b"export" {
|
||||
@ -1259,18 +1437,15 @@ pub fn parse_export_in_module(
|
||||
parser_info: HashMap::new(),
|
||||
});
|
||||
|
||||
let mut out_pipeline = None;
|
||||
|
||||
let exportables = if let Some(kw_span) = spans.get(1) {
|
||||
let kw_name = working_set.get_span_contents(*kw_span);
|
||||
match kw_name {
|
||||
// `parse_def` and `parse_extern` work both with and without attributes
|
||||
b"def" => {
|
||||
let lite_command = LiteCommand {
|
||||
comments: lite_command.comments.clone(),
|
||||
parts: spans[1..].to_vec(),
|
||||
pipe: lite_command.pipe,
|
||||
redirection: lite_command.redirection.clone(),
|
||||
};
|
||||
let (pipeline, cmd_result) =
|
||||
parse_def(working_set, &lite_command, Some(module_name));
|
||||
let (mut pipeline, cmd_result) =
|
||||
parse_def(working_set, lite_command, Some(module_name));
|
||||
|
||||
let mut result = vec![];
|
||||
|
||||
@ -1292,30 +1467,37 @@ pub fn parse_export_in_module(
|
||||
};
|
||||
|
||||
// Trying to warp the 'def' call into the 'export def' in a very clumsy way
|
||||
if let Some(Expr::Call(def_call)) = pipeline.elements.first().map(|e| &e.expr.expr)
|
||||
{
|
||||
call.clone_from(def_call);
|
||||
call.head = Span::concat(&spans[0..=1]);
|
||||
call.decl_id = export_def_decl_id;
|
||||
} else {
|
||||
// TODO: Rather than this, handle `export def` correctly in `parse_def`
|
||||
'warp: {
|
||||
match pipeline.elements.first_mut().map(|e| &mut e.expr.expr) {
|
||||
Some(Expr::Call(def_call)) => {
|
||||
def_call.head = Span::concat(&spans[0..=1]);
|
||||
def_call.decl_id = export_def_decl_id;
|
||||
break 'warp;
|
||||
}
|
||||
Some(Expr::AttributeBlock(ab)) => {
|
||||
if let Expr::Call(def_call) = &mut ab.item.expr {
|
||||
def_call.head = Span::concat(&spans[0..=1]);
|
||||
def_call.decl_id = export_def_decl_id;
|
||||
break 'warp;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
working_set.error(ParseError::InternalError(
|
||||
"unexpected output from parsing a definition".into(),
|
||||
Span::concat(&spans[1..]),
|
||||
));
|
||||
};
|
||||
}
|
||||
|
||||
out_pipeline = Some(pipeline);
|
||||
result
|
||||
}
|
||||
b"extern" => {
|
||||
let lite_command = LiteCommand {
|
||||
comments: lite_command.comments.clone(),
|
||||
parts: spans[1..].to_vec(),
|
||||
pipe: lite_command.pipe,
|
||||
redirection: lite_command.redirection.clone(),
|
||||
};
|
||||
let extern_name = [b"export ", kw_name].concat();
|
||||
|
||||
let pipeline = parse_extern(working_set, &lite_command, Some(module_name));
|
||||
let mut pipeline = parse_extern(working_set, lite_command, Some(module_name));
|
||||
|
||||
let export_def_decl_id = if let Some(id) = working_set.find_decl(&extern_name) {
|
||||
id
|
||||
@ -1327,18 +1509,30 @@ pub fn parse_export_in_module(
|
||||
return (garbage_pipeline(working_set, spans), vec![]);
|
||||
};
|
||||
|
||||
// Trying to warp the 'def' call into the 'export def' in a very clumsy way
|
||||
if let Some(Expr::Call(def_call)) = pipeline.elements.first().map(|e| &e.expr.expr)
|
||||
{
|
||||
call.clone_from(def_call);
|
||||
call.head = Span::concat(&spans[0..=1]);
|
||||
call.decl_id = export_def_decl_id;
|
||||
} else {
|
||||
// Trying to warp the 'extern' call into the 'export extern' in a very clumsy way
|
||||
// TODO: Rather than this, handle `export extern` correctly in `parse_extern`
|
||||
'warp: {
|
||||
match pipeline.elements.first_mut().map(|e| &mut e.expr.expr) {
|
||||
Some(Expr::Call(def_call)) => {
|
||||
def_call.head = Span::concat(&spans[0..=1]);
|
||||
def_call.decl_id = export_def_decl_id;
|
||||
break 'warp;
|
||||
}
|
||||
Some(Expr::AttributeBlock(ab)) => {
|
||||
if let Expr::Call(def_call) = &mut ab.item.expr {
|
||||
def_call.head = Span::concat(&spans[0..=1]);
|
||||
def_call.decl_id = export_def_decl_id;
|
||||
break 'warp;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
working_set.error(ParseError::InternalError(
|
||||
"unexpected output from parsing a definition".into(),
|
||||
Span::concat(&spans[1..]),
|
||||
));
|
||||
};
|
||||
}
|
||||
|
||||
let mut result = vec![];
|
||||
|
||||
@ -1360,14 +1554,21 @@ pub fn parse_export_in_module(
|
||||
));
|
||||
}
|
||||
|
||||
out_pipeline = Some(pipeline);
|
||||
result
|
||||
}
|
||||
// Other definitions can't have attributes, so we handle attributes here with parse_attribute_block
|
||||
_ if lite_command.has_attributes() => {
|
||||
out_pipeline = Some(parse_attribute_block(working_set, lite_command));
|
||||
vec![]
|
||||
}
|
||||
b"alias" => {
|
||||
let lite_command = LiteCommand {
|
||||
comments: lite_command.comments.clone(),
|
||||
parts: spans[1..].to_vec(),
|
||||
pipe: lite_command.pipe,
|
||||
redirection: lite_command.redirection.clone(),
|
||||
attribute_idx: vec![],
|
||||
};
|
||||
let pipeline = parse_alias(working_set, &lite_command, Some(module_name));
|
||||
|
||||
@ -1425,6 +1626,7 @@ pub fn parse_export_in_module(
|
||||
parts: spans[1..].to_vec(),
|
||||
pipe: lite_command.pipe,
|
||||
redirection: lite_command.redirection.clone(),
|
||||
attribute_idx: vec![],
|
||||
};
|
||||
let (pipeline, exportables) =
|
||||
parse_use(working_set, &lite_command, Some(parent_module));
|
||||
@ -1580,15 +1782,13 @@ pub fn parse_export_in_module(
|
||||
vec![]
|
||||
};
|
||||
|
||||
(
|
||||
Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
Span::concat(spans),
|
||||
Type::Any,
|
||||
)]),
|
||||
exportables,
|
||||
)
|
||||
let out_pipeline = out_pipeline.unwrap_or(Pipeline::from_vec(vec![Expression::new(
|
||||
working_set,
|
||||
Expr::Call(call),
|
||||
Span::concat(spans),
|
||||
Type::Any,
|
||||
)]));
|
||||
(out_pipeline, exportables)
|
||||
}
|
||||
|
||||
pub fn parse_export_env(
|
||||
@ -1724,14 +1924,14 @@ pub fn parse_module_block(
|
||||
|
||||
let module_comments = collect_first_comments(&output);
|
||||
|
||||
let (output, err) = lite_parse(&output);
|
||||
let (output, err) = lite_parse(&output, working_set);
|
||||
if let Some(err) = err {
|
||||
working_set.error(err)
|
||||
}
|
||||
|
||||
for pipeline in &output.block {
|
||||
if pipeline.commands.len() == 1 {
|
||||
parse_def_predecl(working_set, &pipeline.commands[0].parts);
|
||||
parse_def_predecl(working_set, pipeline.commands[0].command_parts());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1744,9 +1944,14 @@ pub fn parse_module_block(
|
||||
if pipeline.commands.len() == 1 {
|
||||
let command = &pipeline.commands[0];
|
||||
|
||||
let name = working_set.get_span_contents(command.parts[0]);
|
||||
let name = command
|
||||
.command_parts()
|
||||
.first()
|
||||
.map(|s| working_set.get_span_contents(*s))
|
||||
.unwrap_or(b"");
|
||||
|
||||
match name {
|
||||
// `parse_def` and `parse_extern` work both with and without attributes
|
||||
b"def" => {
|
||||
block.pipelines.push(
|
||||
parse_def(
|
||||
@ -1757,33 +1962,10 @@ pub fn parse_module_block(
|
||||
.0,
|
||||
)
|
||||
}
|
||||
b"const" => block
|
||||
.pipelines
|
||||
.push(parse_const(working_set, &command.parts).0),
|
||||
b"extern" => block
|
||||
.pipelines
|
||||
.push(parse_extern(working_set, command, None)),
|
||||
b"alias" => {
|
||||
block.pipelines.push(parse_alias(
|
||||
working_set,
|
||||
command,
|
||||
None, // using aliases named as the module locally is OK
|
||||
))
|
||||
}
|
||||
b"use" => {
|
||||
let (pipeline, _) = parse_use(working_set, command, Some(&mut module));
|
||||
|
||||
block.pipelines.push(pipeline)
|
||||
}
|
||||
b"module" => {
|
||||
let (pipeline, _) = parse_module(
|
||||
working_set,
|
||||
command,
|
||||
None, // using modules named as the module locally is OK
|
||||
);
|
||||
|
||||
block.pipelines.push(pipeline)
|
||||
}
|
||||
// `parse_export_in_module` also handles attributes by itself
|
||||
b"export" => {
|
||||
let (pipe, exportables) =
|
||||
parse_export_in_module(working_set, command, module_name, &mut module);
|
||||
@ -1864,6 +2046,34 @@ pub fn parse_module_block(
|
||||
|
||||
block.pipelines.push(pipe)
|
||||
}
|
||||
// Other definitions can't have attributes, so we handle attributes here with parse_attribute_block
|
||||
_ if command.has_attributes() => block
|
||||
.pipelines
|
||||
.push(parse_attribute_block(working_set, command)),
|
||||
b"const" => block
|
||||
.pipelines
|
||||
.push(parse_const(working_set, &command.parts).0),
|
||||
b"alias" => {
|
||||
block.pipelines.push(parse_alias(
|
||||
working_set,
|
||||
command,
|
||||
None, // using aliases named as the module locally is OK
|
||||
))
|
||||
}
|
||||
b"use" => {
|
||||
let (pipeline, _) = parse_use(working_set, command, Some(&mut module));
|
||||
|
||||
block.pipelines.push(pipeline)
|
||||
}
|
||||
b"module" => {
|
||||
let (pipeline, _) = parse_module(
|
||||
working_set,
|
||||
command,
|
||||
None, // using modules named as the module locally is OK
|
||||
);
|
||||
|
||||
block.pipelines.push(pipeline)
|
||||
}
|
||||
b"export-env" => {
|
||||
let (pipe, maybe_env_block) = parse_export_env(working_set, &command.parts);
|
||||
|
||||
|
@ -99,7 +99,7 @@ pub fn parse_list_pattern(working_set: &mut StateWorkingSet, span: Span) -> Matc
|
||||
working_set.error(err);
|
||||
}
|
||||
|
||||
let (output, err) = lite_parse(&output);
|
||||
let (output, err) = lite_parse(&output, working_set);
|
||||
if let Some(err) = err {
|
||||
working_set.error(err);
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ use log::trace;
|
||||
use nu_engine::DIR_VAR_PARSER_INFO;
|
||||
use nu_protocol::{
|
||||
ast::*, engine::StateWorkingSet, eval_const::eval_constant, BlockId, DeclId, DidYouMean,
|
||||
FilesizeUnit, Flag, ParseError, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type,
|
||||
Value, VarId, ENV_VARIABLE_ID, IN_VARIABLE_ID,
|
||||
FilesizeUnit, Flag, ParseError, PositionalArg, ShellError, Signature, Span, Spanned,
|
||||
SyntaxShape, Type, Value, VarId, ENV_VARIABLE_ID, IN_VARIABLE_ID,
|
||||
};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
@ -1283,51 +1283,7 @@ pub fn parse_call(working_set: &mut StateWorkingSet, spans: &[Span], head: Span)
|
||||
return garbage(working_set, head);
|
||||
}
|
||||
|
||||
let mut pos = 0;
|
||||
let cmd_start = pos;
|
||||
let mut name_spans = vec![];
|
||||
let mut name = vec![];
|
||||
|
||||
for word_span in spans[cmd_start..].iter() {
|
||||
// Find the longest group of words that could form a command
|
||||
|
||||
name_spans.push(*word_span);
|
||||
|
||||
let name_part = working_set.get_span_contents(*word_span);
|
||||
if name.is_empty() {
|
||||
name.extend(name_part);
|
||||
} else {
|
||||
name.push(b' ');
|
||||
name.extend(name_part);
|
||||
}
|
||||
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
let mut maybe_decl_id = working_set.find_decl(&name);
|
||||
|
||||
while maybe_decl_id.is_none() {
|
||||
// Find the longest command match
|
||||
if name_spans.len() <= 1 {
|
||||
// Keep the first word even if it does not match -- could be external command
|
||||
break;
|
||||
}
|
||||
|
||||
name_spans.pop();
|
||||
pos -= 1;
|
||||
|
||||
let mut name = vec![];
|
||||
for name_span in &name_spans {
|
||||
let name_part = working_set.get_span_contents(*name_span);
|
||||
if name.is_empty() {
|
||||
name.extend(name_part);
|
||||
} else {
|
||||
name.push(b' ');
|
||||
name.extend(name_part);
|
||||
}
|
||||
}
|
||||
maybe_decl_id = working_set.find_decl(&name);
|
||||
}
|
||||
let (cmd_start, pos, _name, maybe_decl_id) = find_longest_decl(working_set, spans);
|
||||
|
||||
if let Some(decl_id) = maybe_decl_id {
|
||||
// Before the internal parsing we check if there is no let or alias declarations
|
||||
@ -1424,6 +1380,172 @@ pub fn parse_call(working_set: &mut StateWorkingSet, spans: &[Span], head: Span)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_longest_decl(
|
||||
working_set: &mut StateWorkingSet<'_>,
|
||||
spans: &[Span],
|
||||
) -> (
|
||||
usize,
|
||||
usize,
|
||||
Vec<u8>,
|
||||
Option<nu_protocol::Id<nu_protocol::marker::Decl>>,
|
||||
) {
|
||||
find_longest_decl_with_prefix(working_set, spans, b"")
|
||||
}
|
||||
|
||||
pub fn find_longest_decl_with_prefix(
|
||||
working_set: &mut StateWorkingSet<'_>,
|
||||
spans: &[Span],
|
||||
prefix: &[u8],
|
||||
) -> (
|
||||
usize,
|
||||
usize,
|
||||
Vec<u8>,
|
||||
Option<nu_protocol::Id<nu_protocol::marker::Decl>>,
|
||||
) {
|
||||
let mut pos = 0;
|
||||
let cmd_start = pos;
|
||||
let mut name_spans = vec![];
|
||||
let mut name = vec![];
|
||||
name.extend(prefix);
|
||||
|
||||
for word_span in spans[cmd_start..].iter() {
|
||||
// Find the longest group of words that could form a command
|
||||
|
||||
name_spans.push(*word_span);
|
||||
|
||||
let name_part = working_set.get_span_contents(*word_span);
|
||||
if name.is_empty() {
|
||||
name.extend(name_part);
|
||||
} else {
|
||||
name.push(b' ');
|
||||
name.extend(name_part);
|
||||
}
|
||||
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
let mut maybe_decl_id = working_set.find_decl(&name);
|
||||
|
||||
while maybe_decl_id.is_none() {
|
||||
// Find the longest command match
|
||||
if name_spans.len() <= 1 {
|
||||
// Keep the first word even if it does not match -- could be external command
|
||||
break;
|
||||
}
|
||||
|
||||
name_spans.pop();
|
||||
pos -= 1;
|
||||
|
||||
// TODO: Refactor to avoid recreating name with an inner loop.
|
||||
name.clear();
|
||||
name.extend(prefix);
|
||||
for name_span in &name_spans {
|
||||
let name_part = working_set.get_span_contents(*name_span);
|
||||
if name.is_empty() {
|
||||
name.extend(name_part);
|
||||
} else {
|
||||
name.push(b' ');
|
||||
name.extend(name_part);
|
||||
}
|
||||
}
|
||||
maybe_decl_id = working_set.find_decl(&name);
|
||||
}
|
||||
(cmd_start, pos, name, maybe_decl_id)
|
||||
}
|
||||
|
||||
pub fn parse_attribute(
|
||||
working_set: &mut StateWorkingSet,
|
||||
lite_command: &LiteCommand,
|
||||
) -> (Attribute, Option<String>) {
|
||||
let _ = lite_command
|
||||
.parts
|
||||
.first()
|
||||
.filter(|s| working_set.get_span_contents(**s).starts_with(b"@"))
|
||||
.expect("Attributes always start with an `@`");
|
||||
|
||||
assert!(
|
||||
lite_command.attribute_idx.is_empty(),
|
||||
"attributes can't have attributes"
|
||||
);
|
||||
|
||||
let mut spans = lite_command.parts.clone();
|
||||
if let Some(first) = spans.first_mut() {
|
||||
first.start += 1;
|
||||
}
|
||||
let spans = spans.as_slice();
|
||||
let attr_span = Span::concat(spans);
|
||||
|
||||
let (cmd_start, cmd_end, mut name, decl_id) =
|
||||
find_longest_decl_with_prefix(working_set, spans, b"attr");
|
||||
|
||||
debug_assert!(name.starts_with(b"attr "));
|
||||
let _ = name.drain(..(b"attr ".len()));
|
||||
|
||||
let name_span = Span::concat(&spans[cmd_start..cmd_end]);
|
||||
|
||||
let Ok(name) = String::from_utf8(name) else {
|
||||
working_set.error(ParseError::NonUtf8(name_span));
|
||||
return (
|
||||
Attribute {
|
||||
expr: garbage(working_set, attr_span),
|
||||
},
|
||||
None,
|
||||
);
|
||||
};
|
||||
|
||||
let Some(decl_id) = decl_id else {
|
||||
working_set.error(ParseError::UnknownCommand(name_span));
|
||||
return (
|
||||
Attribute {
|
||||
expr: garbage(working_set, attr_span),
|
||||
},
|
||||
None,
|
||||
);
|
||||
};
|
||||
|
||||
let decl = working_set.get_decl(decl_id);
|
||||
|
||||
let parsed_call = match decl.as_alias() {
|
||||
// TODO: Once `const def` is available, we should either disallow aliases as attributes OR
|
||||
// allow them but rather than using the aliases' name, use the name of the aliased command
|
||||
Some(alias) => match &alias.clone().wrapped_call {
|
||||
Expression {
|
||||
expr: Expr::ExternalCall(..),
|
||||
..
|
||||
} => {
|
||||
let shell_error = ShellError::NotAConstCommand { span: name_span };
|
||||
working_set.error(shell_error.wrap(working_set, attr_span));
|
||||
return (
|
||||
Attribute {
|
||||
expr: garbage(working_set, Span::concat(spans)),
|
||||
},
|
||||
None,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
trace!("parsing: alias of internal call");
|
||||
parse_internal_call(working_set, name_span, &spans[cmd_end..], decl_id)
|
||||
}
|
||||
},
|
||||
None => {
|
||||
trace!("parsing: internal call");
|
||||
parse_internal_call(working_set, name_span, &spans[cmd_end..], decl_id)
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
Attribute {
|
||||
expr: Expression::new(
|
||||
working_set,
|
||||
Expr::Call(parsed_call.call),
|
||||
Span::concat(spans),
|
||||
parsed_call.output,
|
||||
),
|
||||
},
|
||||
Some(name),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_binary(working_set: &mut StateWorkingSet, span: Span) -> Expression {
|
||||
trace!("parsing: binary");
|
||||
let contents = working_set.get_span_contents(span);
|
||||
@ -4166,7 +4288,7 @@ pub fn parse_list_expression(
|
||||
working_set.error(err)
|
||||
}
|
||||
|
||||
let (mut output, err) = lite_parse(&output);
|
||||
let (mut output, err) = lite_parse(&output, working_set);
|
||||
if let Some(err) = err {
|
||||
working_set.error(err)
|
||||
}
|
||||
@ -5728,11 +5850,20 @@ pub fn parse_builtin_commands(
|
||||
}
|
||||
|
||||
trace!("parsing: checking for keywords");
|
||||
let name = working_set.get_span_contents(lite_command.parts[0]);
|
||||
let name = lite_command
|
||||
.command_parts()
|
||||
.first()
|
||||
.map(|s| working_set.get_span_contents(*s))
|
||||
.unwrap_or(b"");
|
||||
|
||||
match name {
|
||||
// `parse_def` and `parse_extern` work both with and without attributes
|
||||
b"def" => parse_def(working_set, lite_command, None).0,
|
||||
b"extern" => parse_extern(working_set, lite_command, None),
|
||||
// `parse_export_in_block` also handles attributes by itself
|
||||
b"export" => parse_export_in_block(working_set, lite_command),
|
||||
// Other definitions can't have attributes, so we handle attributes here with parse_attribute_block
|
||||
_ if lite_command.has_attributes() => parse_attribute_block(working_set, lite_command),
|
||||
b"let" => parse_let(
|
||||
working_set,
|
||||
&lite_command
|
||||
@ -5761,7 +5892,6 @@ pub fn parse_builtin_commands(
|
||||
parse_keyword(working_set, lite_command)
|
||||
}
|
||||
b"source" | b"source-env" => parse_source(working_set, lite_command),
|
||||
b"export" => parse_export_in_block(working_set, lite_command),
|
||||
b"hide" => parse_hide(working_set, lite_command),
|
||||
b"where" => parse_where(working_set, lite_command),
|
||||
// Only "plugin use" is a keyword
|
||||
@ -6154,7 +6284,7 @@ pub fn parse_block(
|
||||
scoped: bool,
|
||||
is_subexpression: bool,
|
||||
) -> Block {
|
||||
let (lite_block, err) = lite_parse(tokens);
|
||||
let (lite_block, err) = lite_parse(tokens, working_set);
|
||||
if let Some(err) = err {
|
||||
working_set.error(err);
|
||||
}
|
||||
@ -6169,7 +6299,7 @@ pub fn parse_block(
|
||||
// that share the same block can see each other
|
||||
for pipeline in &lite_block.block {
|
||||
if pipeline.commands.len() == 1 {
|
||||
parse_def_predecl(working_set, &pipeline.commands[0].parts)
|
||||
parse_def_predecl(working_set, pipeline.commands[0].command_parts())
|
||||
}
|
||||
}
|
||||
|
||||
@ -6354,6 +6484,9 @@ pub fn discover_captures_in_expr(
|
||||
output: &mut Vec<(VarId, Span)>,
|
||||
) -> Result<(), ParseError> {
|
||||
match &expr.expr {
|
||||
Expr::AttributeBlock(ab) => {
|
||||
discover_captures_in_expr(working_set, &ab.item, seen, seen_blocks, output)?;
|
||||
}
|
||||
Expr::BinaryOp(lhs, _, rhs) => {
|
||||
discover_captures_in_expr(working_set, lhs, seen, seen_blocks, output)?;
|
||||
discover_captures_in_expr(working_set, rhs, seen, seen_blocks, output)?;
|
||||
|
Reference in New Issue
Block a user