From 1671d0fbf3b5691f2db44883a0973ec858beaf94 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 5 Nov 2021 17:02:26 +0100 Subject: driver: replace regex with nom-based parser For now, the nom-based parser doesn't really reduce complexity, but we will need a more powerful parsing solution anyways when the task YML is replaced with a specialized language. --- crates/driver/Cargo.toml | 2 +- crates/driver/src/context.rs | 29 +++++++----------- crates/driver/src/main.rs | 1 + crates/driver/src/parse.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 crates/driver/src/parse.rs (limited to 'crates') diff --git a/crates/driver/Cargo.toml b/crates/driver/Cargo.toml index 36afe9f..9913a83 100644 --- a/crates/driver/Cargo.toml +++ b/crates/driver/Cargo.toml @@ -18,7 +18,7 @@ handlebars = "4.1.3" indoc = "1.0.3" lazy_static = "1.4.0" nix = "0.23.0" -regex = "1.5.4" +nom = "7.1.0" scoped-tls-hkt = "0.1.2" serde = { version = "1", features = ["derive", "rc"] } serde_yaml = "0.8" diff --git a/crates/driver/src/context.rs b/crates/driver/src/context.rs index ccb8581..bd8d985 100644 --- a/crates/driver/src/context.rs +++ b/crates/driver/src/context.rs @@ -8,16 +8,13 @@ use std::{ result, }; -use lazy_static::lazy_static; -use regex::Regex; - use common::{ error::{self, Contextualizable}, string_hash::ArchiveHash, types::TaskID, }; -use crate::{args::*, paths, pin::Pins, task::*}; +use crate::{args::*, parse, paths, pin::Pins, task::*}; #[derive(Debug, Clone, Copy)] pub enum ErrorKind<'ctx> { @@ -347,16 +344,10 @@ impl Context { } pub fn parse<'ctx>(&'ctx self, s: &str) -> error::Result { - lazy_static! { - static ref RE: Regex = Regex::new( - r"^(?P[[:word:]-]+):(?P[[:word:]-]+)(?:/(?P[[:word:]-]+)?(?::(?P[[:word:]-]+))?)?$", - ).unwrap(); - } - - let cap = RE.captures(s).context("Invalid task syntax")?; + let parsed = parse::parse_task(s).context("Invalid task syntax")?; - let recipe = cap["recipe"].to_string(); - let task = cap["task"].to_string(); + let recipe = parsed.recipe.to_string(); + let task = parsed.task.to_string(); let id = TaskID { recipe, task }; let (ctx_id, _) = self @@ -366,19 +357,19 @@ impl Context { let mut args = self.globals.clone(); - if let Some(host) = cap.name("host") { + if let Some(host) = parsed.host { let plat = self .platforms - .get(host.as_str()) - .with_context(|| format!("Platform '{}' not found", host.as_str()))?; + .get(host) + .with_context(|| format!("Platform '{}' not found", host))?; args.set("host", Some(plat)); args.set("target", Some(plat)); } - if let Some(target) = cap.name("target") { + if let Some(target) = parsed.target { let plat = self .platforms - .get(target.as_str()) - .with_context(|| format!("Platform '{}' not found", target.as_str()))?; + .get(target) + .with_context(|| format!("Platform '{}' not found", target))?; args.set("target", Some(plat)); } diff --git a/crates/driver/src/main.rs b/crates/driver/src/main.rs index fe9204a..0e5c629 100644 --- a/crates/driver/src/main.rs +++ b/crates/driver/src/main.rs @@ -1,6 +1,7 @@ mod args; mod context; mod driver; +mod parse; mod paths; mod pin; mod recipe; diff --git a/crates/driver/src/parse.rs b/crates/driver/src/parse.rs new file mode 100644 index 0000000..892b654 --- /dev/null +++ b/crates/driver/src/parse.rs @@ -0,0 +1,71 @@ +use nom::{ + bytes::complete::{tag, take_while1}, + combinator::{all_consuming, opt}, + error::ParseError, + Err, IResult, InputLength, Parser, +}; + +pub struct Output<'a> { + pub recipe: &'a str, + pub task: &'a str, + pub host: Option<&'a str>, + pub target: Option<&'a str>, +} + +fn is_name_char(c: char) -> bool { + matches!(c, 'a'..='z' | 'A' ..='Z' | '0'..='9' | '_' | '-') +} + +fn name(input: &str) -> IResult<&str, &str> { + take_while1(is_name_char)(input) +} + +fn task_id(input: &str) -> IResult<&str, (&str, &str)> { + let (input, recipe) = name(input)?; + let (input, _) = tag(":")(input)?; + let (input, task) = name(input)?; + Ok((input, (recipe, task))) +} + +fn task_arg_target(input: &str) -> IResult<&str, &str> { + let (input, _) = tag(":")(input)?; + let (input, target) = name(input)?; + Ok((input, target)) +} + +fn task_args(input: &str) -> IResult<&str, (Option<&str>, Option<&str>)> { + let (input, _) = tag("/")(input)?; + let (input, host) = opt(name)(input)?; + let (input, target) = opt(task_arg_target)(input)?; + + Ok((input, (host, target))) +} + +fn task_ref(input: &str) -> IResult<&str, Output> { + let (input, (recipe, task)) = task_id(input)?; + let (input, args) = opt(task_args)(input)?; + + let (host, target) = args.unwrap_or_default(); + + Ok(( + input, + Output { + recipe, + task, + host, + target, + }, + )) +} + +fn parse_all, F>(f: F, input: I) -> Result> +where + I: InputLength, + F: Parser, +{ + all_consuming(f)(input).map(|(_, result)| result) +} + +pub fn parse_task(input: &str) -> Option { + parse_all(task_ref, input).ok() +} -- cgit v1.2.3