From bd8479a1ebf3862c13909e270ddd6179d9a24591 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 16 Nov 2021 00:15:28 +0100 Subject: driver: parse: add support for + flag The + flag will be used to force running a task. --- crates/driver/src/context.rs | 2 +- crates/driver/src/parse.rs | 34 +++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/crates/driver/src/context.rs b/crates/driver/src/context.rs index b89b23c..3a40102 100644 --- a/crates/driver/src/context.rs +++ b/crates/driver/src/context.rs @@ -354,7 +354,7 @@ impl Context { } pub fn parse<'ctx>(&'ctx self, s: &str) -> error::Result { - let parsed = parse::parse_task(s).context("Invalid task syntax")?; + let (parsed, _flags) = parse::parse_task_with_flags(s).context("Invalid task syntax")?; let recipe = parsed.recipe.to_string(); let task = parsed.task.to_string(); diff --git a/crates/driver/src/parse.rs b/crates/driver/src/parse.rs index 892b654..aad9360 100644 --- a/crates/driver/src/parse.rs +++ b/crates/driver/src/parse.rs @@ -5,13 +5,19 @@ use nom::{ Err, IResult, InputLength, Parser, }; -pub struct Output<'a> { +#[derive(Debug, Clone, Copy)] +pub struct Task<'a> { pub recipe: &'a str, pub task: &'a str, pub host: Option<&'a str>, pub target: Option<&'a str>, } +#[derive(Debug, Clone, Copy)] +pub struct TaskFlags { + pub force_run: bool, +} + fn is_name_char(c: char) -> bool { matches!(c, 'a'..='z' | 'A' ..='Z' | '0'..='9' | '_' | '-') } @@ -41,7 +47,7 @@ fn task_args(input: &str) -> IResult<&str, (Option<&str>, Option<&str>)> { Ok((input, (host, target))) } -fn task_ref(input: &str) -> IResult<&str, Output> { +fn task(input: &str) -> IResult<&str, Task> { let (input, (recipe, task)) = task_id(input)?; let (input, args) = opt(task_args)(input)?; @@ -49,7 +55,7 @@ fn task_ref(input: &str) -> IResult<&str, Output> { Ok(( input, - Output { + Task { recipe, task, host, @@ -58,6 +64,24 @@ fn task_ref(input: &str) -> IResult<&str, Output> { )) } +fn task_flags(input: &str) -> IResult<&str, TaskFlags> { + let (input, force_run) = opt(tag("+"))(input)?; + + Ok(( + input, + TaskFlags { + force_run: force_run.is_some(), + }, + )) +} + +fn task_with_flags(input: &str) -> IResult<&str, (Task, TaskFlags)> { + let (input, task) = task(input)?; + let (input, flags) = task_flags(input)?; + + Ok((input, (task, flags))) +} + fn parse_all, F>(f: F, input: I) -> Result> where I: InputLength, @@ -66,6 +90,6 @@ where all_consuming(f)(input).map(|(_, result)| result) } -pub fn parse_task(input: &str) -> Option { - parse_all(task_ref, input).ok() +pub fn parse_task_with_flags(input: &str) -> Option<(Task, TaskFlags)> { + parse_all(task_with_flags, input).ok() } -- cgit v1.2.3