From 86905a0ad6fb37cb8ad4719ad9db80704bbf0de5 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Sat, 1 Feb 2025 21:05:21 +0000 Subject: [PATCH] Full execution syntax now uses the same syntax as rules. --- src/ast.rs | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 2efc26d..7feb7db 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -227,31 +227,28 @@ impl<'a> Parsable<'a> for Command<'a> { let mut cwd = None; let mut args = None; - type TokenBracketOpen<'a> = TokenBracesOpen<'a>; - type TokenBracketClose<'a> = TokenBracesClose<'a>; - - match parser.try_parse::>() { + match parser.try_parse::>() { // If it starts with a `{`, it's a full - Ok(_) => loop { - let key = parser.try_parse::>()?; - parser.parse::>()?; - - match key.0 { - "cwd" => cwd = Some(parser.parse::>()?), - "args" => args = Some(parser.parse::>>()?), - key => zutil_app_error::bail!("Unknown key: `{key:?}`"), - } - - match parser.parse::, TokenBracketClose<'a>>>()? { - // `,` or `,}` - AnyOf2::T0(_) => - if parser.try_parse::>().is_ok() { - break; + Ok(_) => + while parser.try_parse::>().is_err() { + match parser + .peek::, TokenArgs<'a>>>() + .context("Expected an alias, default or rule statement")? + { + AnyOf2::T0(_) => { + parser.parse::>()?; + zutil_app_error::ensure!(cwd.is_none(), "Working directory was already specified"); + cwd = Some(parser.parse::>()?); + parser.parse::>()?; }, - // `}` - AnyOf2::T1(_) => break, - } - }, + AnyOf2::T1(_) => { + parser.parse::>()?; + zutil_app_error::ensure!(args.is_none(), "Arguments were already specified"); + args = Some(parser.parse::>>()?); + parser.parse::>()?; + }, + } + }, // Otherwise, just parse an array of expressions Err(_) => { @@ -259,7 +256,7 @@ impl<'a> Parsable<'a> for Command<'a> { }, }; - let args = args.context("Missing `args:` for command")?; + let args = args.context("Missing command `args`")?; Ok(Self { cwd, args }) } @@ -367,7 +364,7 @@ impl<'a> ExprCmpt<'a> { pub fn parse_many(parser: &mut Parser<'a>, cmpts: &mut Vec) -> Result<(), AppError> { match parser .peek::, TokenDoubleQuote<'a>>>() - .context("Expected an identifier, or literal")? + .context("Expected an identifier or literal")? { // If we get a sole identifier, that's the only component AnyOf2::T0(_) => { @@ -458,6 +455,8 @@ pub macro decl_tokens($($TokenName:ident = $Token:expr;)*) { decl_tokens! { TokenAlias = "alias"; + TokenArgs = "args"; + TokenCwd = "cwd"; TokenDefault = "default"; TokenDep = "dep"; TokenDepsFile = "deps_file"; @@ -474,7 +473,6 @@ decl_tokens! { TokenBracketOpen = '['; TokenBracketClose = ']'; - TokenColon = ':'; TokenComma = ','; TokenDot = '.'; TokenDoubleQuote = '"';