Skip to content

WIP: Add support for function #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crates/deno_task_shell/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ pipeline = !{ Bang? ~ pipe_sequence }
pipe_sequence = !{ command ~ ((StdoutStderr | Stdout) ~ linebreak ~ pipe_sequence)? }

command = !{
function_definition |
compound_command ~ redirect_list? |
simple_command |
function_definition
simple_command
}

compound_command = {
Expand Down Expand Up @@ -378,10 +378,14 @@ binary_posix_conditional_op = !{
while_clause = !{ While ~ conditional_expression ~ do_group }
until_clause = !{ Until ~ conditional_expression ~ do_group }

function_definition = !{ fname ~ "(" ~ ")" ~ linebreak ~ function_body }
function_definition = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prsabahrami Does this grammar look fine?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it looks good and adheres to the docs.
Thank you!

fname ~ "(" ~ ")" ~ linebreak ~ compound_list |
"function" ~ fname ~ ("(" ~ ")")? ~ linebreak ~ compound_list
}

function_body = !{ compound_command ~ redirect_list? }

fname = @{ RESERVED_WORD | NAME | ASSIGNMENT_WORD | UNQUOTED_PENDING_WORD }
fname = @{ NAME }
name = @{ NAME }

brace_group = !{ Lbrace ~ compound_list ~ Rbrace }
Expand Down
63 changes: 60 additions & 3 deletions crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ pub struct Command {
pub redirect: Option<Redirect>,
}

#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("Invalid function")]
pub struct Function {
pub name: String,
pub body: SequentialList,
}

#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
#[cfg_attr(
feature = "serialization",
Expand All @@ -170,6 +179,8 @@ pub enum CommandInner {
Case(CaseClause),
#[error("Invalid arithmetic expression")]
ArithmeticExpression(Arithmetic),
#[error("Invalid function definition")]
FunctionType(Function),
}

impl From<Command> for Sequence {
Expand Down Expand Up @@ -910,13 +921,59 @@ fn parse_command(pair: Pair<Rule>) -> Result<Command> {
match inner.as_rule() {
Rule::simple_command => parse_simple_command(inner),
Rule::compound_command => parse_compound_command(inner),
Rule::function_definition => {
Err(miette!("Function definitions are not supported yet"))
}
Rule::function_definition => parse_function_definition(inner),
_ => Err(miette!("Unexpected rule in command: {:?}", inner.as_rule())),
}
}

fn parse_function_definition(pair: Pair<Rule>) -> Result<Command> {
let mut inner = pair.into_inner();

// Handle both styles:
// 1. name() { body }
// 2. function name { body } or function name() { body }
let (name, body_pair) = if inner.peek().unwrap().as_rule() == Rule::fname {
// Style 1: name() { body }
let name = inner.next().unwrap().as_str().to_string();
// Skip the () part - these are just the parentheses tokens in the grammar
if inner.peek().is_some() {
let next = inner.peek().unwrap();
if next.as_str() == "(" || next.as_str() == ")" {
inner.next(); // skip (
inner.next(); // skip )
}
}
(name, inner.next().unwrap())
} else {
// Style 2: function name [()] { body }
// Skip "function" keyword
inner.next();
let name = inner.next().unwrap().as_str().to_string();
// Skip optional () - these are just the parentheses tokens
if inner.peek().is_some() {
let next = inner.peek().unwrap();
if next.as_str() == "(" || next.as_str() == ")" {
inner.next(); // skip (
inner.next(); // skip )
}
}
(name, inner.next().unwrap())
};

// Parse the function body
let mut body_items = Vec::new();
parse_compound_list(body_pair, &mut body_items)?;

Ok(Command {
inner: CommandInner::FunctionType(Function {
name,
body: SequentialList { items: body_items },
}),
redirect: None,
})
}


fn parse_simple_command(pair: Pair<Rule>) -> Result<Command> {
let mut env_vars = Vec::new();
let mut args = Vec::new();
Expand Down
1 change: 1 addition & 0 deletions crates/deno_task_shell/src/shell/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ async fn parse_shebang_args(
CommandInner::While(_) => return err_unsupported(text),
CommandInner::ArithmeticExpression(_) => return err_unsupported(text),
CommandInner::Case(_) => return err_unsupported(text),
CommandInner::FunctionType(_) => return err_unsupported(text),
};
if !cmd.env_vars.is_empty() {
return err_unsupported(text);
Expand Down
57 changes: 57 additions & 0 deletions crates/deno_task_shell/src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use crate::parser::IfClause;
use crate::parser::PipeSequence;
use crate::parser::PipeSequenceOperator;
use crate::parser::Pipeline;
use crate::parser::Function;
use crate::parser::PipelineInner;
use crate::parser::Redirect;
use crate::parser::RedirectFd;
Expand Down Expand Up @@ -667,6 +668,9 @@ async fn execute_command(
}
}
}
CommandInner::FunctionType(function) => {
execute_function(function, state, stdin, stdout, stderr).await
}
}
}

Expand Down Expand Up @@ -845,6 +849,38 @@ async fn execute_case_clause(
}
}

async fn execute_function(
func: Function,
state: ShellState,
stdin: ShellPipeReader,
stdout: ShellPipeWriter,
stderr: ShellPipeWriter,
) -> ExecuteResult {
// Store the function definition in the state
let mut state = state;
state.add_function(func.name.clone(), func.body.clone());

// Execute the function body
let result = execute_sequential_list(
func.body,
state,
stdin,
stdout,
stderr,
AsyncCommandBehavior::Yield,
)
.await;

match result {
ExecuteResult::Exit(code, env_changes, handles) => {
ExecuteResult::Continue(code, env_changes, handles)
}
ExecuteResult::Continue(code, env_changes, handles) => {
ExecuteResult::Continue(code, env_changes, handles)
}
}
}

async fn execute_for_clause(
for_clause: ForLoop,
state: &mut ShellState,
Expand Down Expand Up @@ -1587,6 +1623,27 @@ fn execute_command_args(
args.remove(0)
};

// Check if this is a function call
if let Some(body) = state.clone().get_function(&command_name) {
// Set $0 to function name and $1, $2, etc. to arguments
let mut changes = vec![EnvChange::SetShellVar("0".to_string(), command_name.clone())];
for (i, arg) in args.iter().enumerate() {
changes.push(EnvChange::SetShellVar((i + 1).to_string(), arg.clone()));
}

let mut state = state;
state.apply_changes(&changes);

return Box::pin(execute_sequential_list(
body.clone(),
state,
stdin,
stdout,
stderr,
AsyncCommandBehavior::Yield,
));
}

if state.token().is_cancelled() {
Box::pin(future::ready(ExecuteResult::for_cancellation()))
} else if let Some(stripped_name) = command_name.strip_prefix('!') {
Expand Down
11 changes: 11 additions & 0 deletions crates/deno_task_shell/src/shell/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::shell::fs_util;

use super::commands::builtin_commands;
use super::commands::ShellCommand;
use crate::parser::SequentialList;

#[derive(Clone)]
pub struct ShellState {
Expand Down Expand Up @@ -52,6 +53,7 @@ pub struct ShellState {
last_command_exit_code: i32, // Exit code of the last command
// The shell options to be modified using `set` command
shell_options: HashMap<ShellOptions, bool>,
functions: HashMap<String, SequentialList>,
}

#[allow(clippy::print_stdout)]
Expand Down Expand Up @@ -92,6 +94,7 @@ impl ShellState {
map.insert(ShellOptions::ExitOnError, true);
map
},
functions: HashMap::new(),
};
// ensure the data is normalized
for (name, value) in env_vars {
Expand Down Expand Up @@ -353,6 +356,14 @@ impl ShellState {
pub fn reset_cancellation_token(&mut self) {
self.token = CancellationToken::default();
}

pub fn add_function(&mut self, name: String, body: SequentialList) {
self.functions.insert(name, body);
}

pub fn get_function(&self, name: &str) -> Option<&SequentialList> {
self.functions.get(name)
}
}

#[derive(Debug, PartialEq, Eq, Clone, PartialOrd)]
Expand Down
Loading