2016-10-22 23:18:26 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2016-10-02 22:30:28 -07:00
|
|
|
extern crate regex;
|
2016-10-07 17:56:52 -07:00
|
|
|
extern crate tempdir;
|
2016-10-30 16:15:18 -07:00
|
|
|
extern crate itertools;
|
2016-11-07 21:01:27 -08:00
|
|
|
extern crate ansi_term;
|
2016-11-11 15:18:42 -08:00
|
|
|
extern crate unicode_width;
|
2016-11-12 12:36:12 -08:00
|
|
|
extern crate edit_distance;
|
2017-04-21 22:11:18 -07:00
|
|
|
extern crate libc;
|
2017-04-23 15:37:17 -07:00
|
|
|
extern crate brev;
|
2016-10-02 22:30:28 -07:00
|
|
|
|
2017-05-07 19:48:07 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_utils;
|
|
|
|
|
2016-11-16 21:06:51 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod unit;
|
2016-10-02 22:30:28 -07:00
|
|
|
|
2016-11-16 21:06:51 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod integration;
|
|
|
|
|
2017-04-22 16:15:15 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod search;
|
|
|
|
|
2017-04-21 22:20:13 -07:00
|
|
|
mod platform;
|
|
|
|
|
2016-11-16 21:06:51 -08:00
|
|
|
mod app;
|
|
|
|
|
2017-06-01 18:01:35 -07:00
|
|
|
mod color;
|
|
|
|
|
2017-04-21 22:11:18 -07:00
|
|
|
mod prelude {
|
2017-04-21 22:20:13 -07:00
|
|
|
pub use libc::{EXIT_FAILURE, EXIT_SUCCESS};
|
|
|
|
pub use regex::Regex;
|
2017-06-01 18:01:35 -07:00
|
|
|
pub use std::io::prelude::*;
|
2017-04-22 21:39:29 -07:00
|
|
|
pub use std::path::{Path, PathBuf};
|
2017-04-21 22:11:18 -07:00
|
|
|
pub use std::{cmp, env, fs, fmt, io, iter, process};
|
2017-06-01 18:01:35 -07:00
|
|
|
|
|
|
|
pub fn default<T: Default>() -> T {
|
|
|
|
Default::default()
|
|
|
|
}
|
2017-04-21 22:11:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
use prelude::*;
|
|
|
|
|
2016-11-16 21:06:51 -08:00
|
|
|
pub use app::app;
|
|
|
|
|
2017-04-23 15:37:17 -07:00
|
|
|
use brev::{output, OutputError};
|
2017-06-01 18:01:35 -07:00
|
|
|
use color::Color;
|
2017-04-23 15:37:17 -07:00
|
|
|
use platform::{Platform, PlatformInterface};
|
2016-11-18 07:03:34 -08:00
|
|
|
use std::borrow::Cow;
|
2016-10-30 14:37:03 -07:00
|
|
|
use std::collections::{BTreeMap as Map, BTreeSet as Set};
|
2016-11-16 21:06:51 -08:00
|
|
|
use std::fmt::Display;
|
|
|
|
use std::ops::Range;
|
2016-10-07 17:56:52 -07:00
|
|
|
|
2016-10-02 22:30:28 -07:00
|
|
|
macro_rules! warn {
|
|
|
|
($($arg:tt)*) => {{
|
|
|
|
extern crate std;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
let _ = writeln!(&mut std::io::stderr(), $($arg)*);
|
|
|
|
}};
|
|
|
|
}
|
2016-10-28 00:06:36 -07:00
|
|
|
|
2016-10-02 22:30:28 -07:00
|
|
|
macro_rules! die {
|
|
|
|
($($arg:tt)*) => {{
|
|
|
|
extern crate std;
|
|
|
|
warn!($($arg)*);
|
2017-04-21 22:20:13 -07:00
|
|
|
process::exit(EXIT_FAILURE)
|
2016-10-02 22:30:28 -07:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2017-04-20 23:06:03 -07:00
|
|
|
const DEFAULT_SHELL: &'static str = "sh";
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
trait Slurp {
|
2016-10-02 22:30:28 -07:00
|
|
|
fn slurp(&mut self) -> Result<String, std::io::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Slurp for fs::File {
|
|
|
|
fn slurp(&mut self) -> Result<String, std::io::Error> {
|
|
|
|
let mut destination = String::new();
|
2016-11-11 21:37:16 -08:00
|
|
|
self.read_to_string(&mut destination)?;
|
2016-10-02 22:30:28 -07:00
|
|
|
Ok(destination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:20:13 -07:00
|
|
|
/// Split a shebang line into a command and an optional argument
|
|
|
|
fn split_shebang(shebang: &str) -> Option<(&str, Option<&str>)> {
|
|
|
|
lazy_static! {
|
|
|
|
static ref EMPTY: Regex = re(r"^#!\s*$");
|
|
|
|
static ref SIMPLE: Regex = re(r"^#!(\S+)\s*$");
|
|
|
|
static ref ARGUMENT: Regex = re(r"^#!(\S+)\s+(\S.*?)?\s*$");
|
|
|
|
}
|
|
|
|
|
|
|
|
if EMPTY.is_match(shebang) {
|
|
|
|
Some(("", None))
|
|
|
|
} else if let Some(captures) = SIMPLE.captures(shebang) {
|
|
|
|
Some((captures.at(1).unwrap(), None))
|
|
|
|
} else if let Some(captures) = ARGUMENT.captures(shebang) {
|
|
|
|
Some((captures.at(1).unwrap(), Some(captures.at(2).unwrap())))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 22:30:28 -07:00
|
|
|
fn re(pattern: &str) -> Regex {
|
|
|
|
Regex::new(pattern).unwrap()
|
|
|
|
}
|
|
|
|
|
2016-11-12 23:31:19 -08:00
|
|
|
fn empty<T, C: iter::FromIterator<T>>() -> C {
|
|
|
|
iter::empty().collect()
|
|
|
|
}
|
|
|
|
|
2017-05-12 19:09:47 -07:00
|
|
|
fn contains<T: PartialOrd + Copy>(range: &Range<T>, i: T) -> bool {
|
2016-11-12 09:15:13 -08:00
|
|
|
i >= range.start && i < range.end
|
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
struct Recipe<'a> {
|
2016-10-31 19:11:27 -07:00
|
|
|
dependencies: Vec<&'a str>,
|
|
|
|
dependency_tokens: Vec<Token<'a>>,
|
2016-11-18 07:03:34 -08:00
|
|
|
doc: Option<&'a str>,
|
|
|
|
line_number: usize,
|
|
|
|
lines: Vec<Vec<Fragment<'a>>>,
|
|
|
|
name: &'a str,
|
2016-11-12 09:15:13 -08:00
|
|
|
parameters: Vec<Parameter<'a>>,
|
2016-11-12 16:12:00 -08:00
|
|
|
quiet: bool,
|
2016-11-18 07:03:34 -08:00
|
|
|
shebang: bool,
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 09:15:13 -08:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
struct Parameter<'a> {
|
2016-11-18 07:03:34 -08:00
|
|
|
default: Option<String>,
|
|
|
|
name: &'a str,
|
|
|
|
token: Token<'a>,
|
|
|
|
variadic: bool,
|
2016-11-12 09:15:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display for Parameter<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2017-06-01 18:01:35 -07:00
|
|
|
let color = Color::fmt(f);
|
2016-11-18 07:03:34 -08:00
|
|
|
if self.variadic {
|
2017-06-01 18:01:35 -07:00
|
|
|
write!(f, "{}", color.annotation().paint("+"))?;
|
2016-11-18 07:03:34 -08:00
|
|
|
}
|
2017-06-01 18:01:35 -07:00
|
|
|
write!(f, "{}", color.parameter().paint(self.name))?;
|
2016-11-12 09:15:13 -08:00
|
|
|
if let Some(ref default) = self.default {
|
2016-11-12 11:40:52 -08:00
|
|
|
let escaped = default.chars().flat_map(char::escape_default).collect::<String>();;
|
2017-06-01 18:01:35 -07:00
|
|
|
write!(f, r#"='{}'"#, color.string().paint(&escaped))?;
|
2016-11-12 09:15:13 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 23:38:49 -07:00
|
|
|
#[derive(PartialEq, Debug)]
|
2016-10-27 09:44:07 -07:00
|
|
|
enum Fragment<'a> {
|
2016-10-27 00:13:10 -07:00
|
|
|
Text{text: Token<'a>},
|
2016-10-29 22:56:47 -07:00
|
|
|
Expression{expression: Expression<'a>},
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:45:12 -08:00
|
|
|
impl<'a> Fragment<'a> {
|
|
|
|
fn continuation(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Fragment::Text{ref text} => text.lexeme.ends_with('\\'),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 00:13:10 -07:00
|
|
|
#[derive(PartialEq, Debug)]
|
2016-10-25 19:11:58 -07:00
|
|
|
enum Expression<'a> {
|
2016-10-26 20:54:44 -07:00
|
|
|
Variable{name: &'a str, token: Token<'a>},
|
2016-11-12 09:15:13 -08:00
|
|
|
String{cooked_string: CookedString<'a>},
|
2016-10-29 23:39:12 -07:00
|
|
|
Backtick{raw: &'a str, token: Token<'a>},
|
2016-10-25 19:11:58 -07:00
|
|
|
Concatination{lhs: Box<Expression<'a>>, rhs: Box<Expression<'a>>},
|
|
|
|
}
|
|
|
|
|
2016-10-27 00:13:10 -07:00
|
|
|
impl<'a> Expression<'a> {
|
|
|
|
fn variables(&'a self) -> Variables<'a> {
|
|
|
|
Variables {
|
|
|
|
stack: vec![self],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Variables<'a> {
|
|
|
|
stack: Vec<&'a Expression<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for Variables<'a> {
|
|
|
|
type Item = &'a Token<'a>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<&'a Token<'a>> {
|
|
|
|
match self.stack.pop() {
|
2016-10-29 01:58:30 -07:00
|
|
|
None | Some(&Expression::String{..}) | Some(&Expression::Backtick{..}) => None,
|
2016-10-27 00:13:10 -07:00
|
|
|
Some(&Expression::Variable{ref token,..}) => Some(token),
|
|
|
|
Some(&Expression::Concatination{ref lhs, ref rhs}) => {
|
|
|
|
self.stack.push(lhs);
|
|
|
|
self.stack.push(rhs);
|
|
|
|
self.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 19:11:58 -07:00
|
|
|
impl<'a> Display for Expression<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
match *self {
|
2016-11-12 09:15:13 -08:00
|
|
|
Expression::Backtick {raw, .. } => write!(f, "`{}`", raw)?,
|
|
|
|
Expression::Concatination{ref lhs, ref rhs } => write!(f, "{} + {}", lhs, rhs)?,
|
|
|
|
Expression::String {ref cooked_string} => write!(f, "\"{}\"", cooked_string.raw)?,
|
|
|
|
Expression::Variable {name, .. } => write!(f, "{}", name)?,
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 19:09:47 -07:00
|
|
|
/// Return a `RunError::Signal` if the process was terminated by a signal,
|
|
|
|
/// otherwise return an `RunError::UnknownFailure`
|
2016-11-16 22:18:55 -08:00
|
|
|
fn error_from_signal(
|
|
|
|
recipe: &str,
|
|
|
|
line_number: Option<usize>,
|
|
|
|
exit_status: process::ExitStatus
|
|
|
|
) -> RunError {
|
2017-04-21 22:20:13 -07:00
|
|
|
match Platform::signal_from_exit_status(exit_status) {
|
2016-11-16 22:18:55 -08:00
|
|
|
Some(signal) => RunError::Signal{recipe: recipe, line_number: line_number, signal: signal},
|
|
|
|
None => RunError::UnknownFailure{recipe: recipe, line_number: line_number},
|
2016-10-05 13:58:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 16:15:18 -07:00
|
|
|
fn export_env<'a>(
|
|
|
|
command: &mut process::Command,
|
|
|
|
scope: &Map<&'a str, String>,
|
|
|
|
exports: &Set<&'a str>,
|
|
|
|
) -> Result<(), RunError<'a>> {
|
|
|
|
for name in exports {
|
|
|
|
if let Some(value) = scope.get(name) {
|
|
|
|
command.env(name, value);
|
|
|
|
} else {
|
|
|
|
return Err(RunError::InternalError {
|
|
|
|
message: format!("scope does not contain exported variable `{}`", name),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-10-29 23:39:12 -07:00
|
|
|
|
2016-10-30 16:15:18 -07:00
|
|
|
fn run_backtick<'a>(
|
|
|
|
raw: &str,
|
|
|
|
token: &Token<'a>,
|
|
|
|
scope: &Map<&'a str, String>,
|
|
|
|
exports: &Set<&'a str>,
|
2016-11-05 01:01:43 -07:00
|
|
|
quiet: bool,
|
2016-10-30 16:15:18 -07:00
|
|
|
) -> Result<String, RunError<'a>> {
|
2017-04-20 23:06:03 -07:00
|
|
|
let mut cmd = process::Command::new(DEFAULT_SHELL);
|
2016-10-30 16:15:18 -07:00
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
export_env(&mut cmd, scope, exports)?;
|
2016-10-30 16:15:18 -07:00
|
|
|
|
|
|
|
cmd.arg("-cu")
|
2016-11-05 01:01:43 -07:00
|
|
|
.arg(raw);
|
|
|
|
|
|
|
|
cmd.stderr(if quiet {
|
|
|
|
process::Stdio::null()
|
|
|
|
} else {
|
|
|
|
process::Stdio::inherit()
|
|
|
|
});
|
2016-10-30 16:15:18 -07:00
|
|
|
|
2017-05-12 19:09:47 -07:00
|
|
|
output(cmd).map_err(|output_error| RunError::Backtick{token: token.clone(), output_error})
|
2016-10-29 22:56:47 -07:00
|
|
|
}
|
|
|
|
|
2016-10-03 23:55:55 -07:00
|
|
|
impl<'a> Recipe<'a> {
|
2016-11-12 09:15:13 -08:00
|
|
|
fn argument_range(&self) -> Range<usize> {
|
2017-05-07 14:45:23 -07:00
|
|
|
self.min_arguments()..self.max_arguments() + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn min_arguments(&self) -> usize {
|
2016-11-12 09:15:13 -08:00
|
|
|
self.parameters.iter().filter(|p| !p.default.is_some()).count()
|
2017-05-07 14:45:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn max_arguments(&self) -> usize {
|
2016-11-18 07:03:34 -08:00
|
|
|
if self.parameters.iter().any(|p| p.variadic) {
|
2017-05-07 14:45:23 -07:00
|
|
|
std::usize::MAX - 1
|
2016-11-18 07:03:34 -08:00
|
|
|
} else {
|
2017-05-07 14:45:23 -07:00
|
|
|
self.parameters.len()
|
2016-11-18 07:03:34 -08:00
|
|
|
}
|
2016-11-12 09:15:13 -08:00
|
|
|
}
|
|
|
|
|
2016-10-29 21:51:39 -07:00
|
|
|
fn run(
|
2016-10-30 00:20:29 -07:00
|
|
|
&self,
|
2016-10-29 21:51:39 -07:00
|
|
|
arguments: &[&'a str],
|
2016-10-30 14:37:03 -07:00
|
|
|
scope: &Map<&'a str, String>,
|
2016-10-30 16:15:18 -07:00
|
|
|
exports: &Set<&'a str>,
|
2016-11-05 01:01:43 -07:00
|
|
|
options: &RunOptions,
|
2016-10-29 21:51:39 -07:00
|
|
|
) -> Result<(), RunError<'a>> {
|
2016-11-16 21:06:51 -08:00
|
|
|
if options.verbose {
|
2017-06-01 18:01:35 -07:00
|
|
|
let color = options.color.stderr().banner();
|
|
|
|
warn!("{}===> Running recipe `{}`...{}", color.prefix(), self.name, color.suffix());
|
2016-11-16 21:06:51 -08:00
|
|
|
}
|
|
|
|
|
2016-11-18 07:03:34 -08:00
|
|
|
let mut argument_map = Map::new();
|
|
|
|
|
|
|
|
let mut rest = arguments;
|
|
|
|
for parameter in &self.parameters {
|
|
|
|
let value = if rest.is_empty() {
|
|
|
|
match parameter.default {
|
|
|
|
Some(ref default) => Cow::Borrowed(default.as_str()),
|
|
|
|
None => return Err(RunError::InternalError{
|
|
|
|
message: "missing parameter without default".to_string()
|
|
|
|
}),
|
|
|
|
}
|
2016-11-18 07:07:20 -08:00
|
|
|
} else if parameter.variadic {
|
|
|
|
let value = Cow::Owned(rest.to_vec().join(" "));
|
|
|
|
rest = &[];
|
|
|
|
value
|
2016-11-12 09:15:13 -08:00
|
|
|
} else {
|
2016-11-18 07:07:20 -08:00
|
|
|
let value = Cow::Borrowed(rest[0]);
|
|
|
|
rest = &rest[1..];
|
|
|
|
value
|
2016-11-18 07:03:34 -08:00
|
|
|
};
|
|
|
|
argument_map.insert(parameter.name, value);
|
|
|
|
}
|
2016-10-29 00:55:47 -07:00
|
|
|
|
2016-10-29 22:56:47 -07:00
|
|
|
let mut evaluator = Evaluator {
|
2016-11-12 23:31:19 -08:00
|
|
|
evaluated: empty(),
|
2016-10-29 22:56:47 -07:00
|
|
|
scope: scope,
|
2016-10-30 16:15:18 -07:00
|
|
|
exports: exports,
|
2016-11-12 23:31:19 -08:00
|
|
|
assignments: &empty(),
|
|
|
|
overrides: &empty(),
|
2016-11-05 01:01:43 -07:00
|
|
|
quiet: options.quiet,
|
2016-10-29 22:56:47 -07:00
|
|
|
};
|
2016-10-29 00:14:41 -07:00
|
|
|
|
2016-10-07 17:56:52 -07:00
|
|
|
if self.shebang {
|
2016-10-29 22:56:47 -07:00
|
|
|
let mut evaluated_lines = vec![];
|
|
|
|
for line in &self.lines {
|
2016-11-12 09:33:38 -08:00
|
|
|
evaluated_lines.push(evaluator.evaluate_line(line, &argument_map)?);
|
2016-10-29 22:56:47 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 16:12:00 -08:00
|
|
|
if options.dry_run || self.quiet {
|
|
|
|
for line in &evaluated_lines {
|
2016-10-30 13:14:39 -07:00
|
|
|
warn!("{}", line);
|
|
|
|
}
|
2016-11-12 16:12:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.dry_run {
|
2016-10-30 13:14:39 -07:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
let tmp = tempdir::TempDir::new("just")
|
|
|
|
.map_err(|error| RunError::TmpdirIoError{recipe: self.name, io_error: error})?;
|
2016-10-07 17:56:52 -07:00
|
|
|
let mut path = tmp.path().to_path_buf();
|
|
|
|
path.push(self.name);
|
|
|
|
{
|
2016-11-11 21:37:16 -08:00
|
|
|
let mut f = fs::File::create(&path)
|
|
|
|
.map_err(|error| RunError::TmpdirIoError{recipe: self.name, io_error: error})?;
|
2016-10-07 17:56:52 -07:00
|
|
|
let mut text = String::new();
|
|
|
|
// add the shebang
|
2016-10-29 00:14:41 -07:00
|
|
|
text += &evaluated_lines[0];
|
2016-10-07 17:56:52 -07:00
|
|
|
text += "\n";
|
|
|
|
// add blank lines so that lines in the generated script
|
|
|
|
// have the same line number as the corresponding lines
|
|
|
|
// in the justfile
|
|
|
|
for _ in 1..(self.line_number + 2) {
|
|
|
|
text += "\n"
|
|
|
|
}
|
2016-10-29 00:14:41 -07:00
|
|
|
for line in &evaluated_lines[1..] {
|
2016-10-28 00:06:36 -07:00
|
|
|
text += line;
|
2016-10-07 17:56:52 -07:00
|
|
|
text += "\n";
|
|
|
|
}
|
2016-11-11 21:37:16 -08:00
|
|
|
f.write_all(text.as_bytes())
|
|
|
|
.map_err(|error| RunError::TmpdirIoError{recipe: self.name, io_error: error})?;
|
2016-10-05 13:58:18 -07:00
|
|
|
}
|
2016-10-07 17:56:52 -07:00
|
|
|
|
|
|
|
// make the script executable
|
2017-04-21 22:20:13 -07:00
|
|
|
Platform::set_execute_permission(&path)
|
2016-11-11 21:37:16 -08:00
|
|
|
.map_err(|error| RunError::TmpdirIoError{recipe: self.name, io_error: error})?;
|
2016-10-07 17:56:52 -07:00
|
|
|
|
2017-04-21 22:20:13 -07:00
|
|
|
let shebang_line = evaluated_lines.first()
|
|
|
|
.ok_or_else(|| RunError::InternalError {
|
|
|
|
message: "evaluated_lines was empty".to_string()
|
|
|
|
})?;
|
2016-10-30 16:15:18 -07:00
|
|
|
|
2017-04-21 22:20:13 -07:00
|
|
|
let (shebang_command, shebang_argument) = split_shebang(shebang_line)
|
|
|
|
.ok_or_else(|| RunError::InternalError {
|
|
|
|
message: format!("bad shebang line: {}", shebang_line)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// create a command to run the script
|
2017-04-23 16:09:34 -07:00
|
|
|
let mut command = Platform::make_shebang_command(&path, shebang_command, shebang_argument)
|
|
|
|
.map_err(|output_error| RunError::Cygpath{recipe: self.name, output_error: output_error})?;
|
2017-04-21 22:20:13 -07:00
|
|
|
|
|
|
|
// export environment variables
|
2016-11-11 21:37:16 -08:00
|
|
|
export_env(&mut command, scope, exports)?;
|
2016-10-30 16:15:18 -07:00
|
|
|
|
2017-04-21 22:20:13 -07:00
|
|
|
// run it!
|
2016-11-11 21:37:16 -08:00
|
|
|
match command.status() {
|
2016-10-05 13:58:18 -07:00
|
|
|
Ok(exit_status) => if let Some(code) = exit_status.code() {
|
2016-11-11 21:37:16 -08:00
|
|
|
if code != 0 {
|
2016-11-16 22:18:55 -08:00
|
|
|
return Err(RunError::Code{recipe: self.name, line_number: None, code: code})
|
2016-10-05 13:58:18 -07:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-16 22:18:55 -08:00
|
|
|
return Err(error_from_signal(self.name, None, exit_status))
|
2016-10-05 13:58:18 -07:00
|
|
|
},
|
2017-04-22 19:05:51 -07:00
|
|
|
Err(io_error) => return Err(RunError::Shebang {
|
|
|
|
recipe: self.name,
|
|
|
|
command: shebang_command.to_string(),
|
|
|
|
argument: shebang_argument.map(String::from),
|
|
|
|
io_error: io_error
|
|
|
|
})
|
2016-11-11 21:37:16 -08:00
|
|
|
};
|
2016-10-07 17:56:52 -07:00
|
|
|
} else {
|
2016-11-12 15:45:12 -08:00
|
|
|
let mut lines = self.lines.iter().peekable();
|
2016-11-16 22:18:55 -08:00
|
|
|
let mut line_number = self.line_number + 1;
|
2016-11-12 15:45:12 -08:00
|
|
|
loop {
|
|
|
|
if lines.peek().is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let mut evaluated = String::new();
|
|
|
|
loop {
|
|
|
|
if lines.peek().is_none() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let line = lines.next().unwrap();
|
2016-11-16 22:18:55 -08:00
|
|
|
line_number += 1;
|
2016-11-12 15:45:12 -08:00
|
|
|
evaluated += &evaluator.evaluate_line(line, &argument_map)?;
|
|
|
|
if line.last().map(Fragment::continuation).unwrap_or(false) {
|
|
|
|
evaluated.pop();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 22:56:47 -07:00
|
|
|
let mut command = evaluated.as_str();
|
2016-11-05 01:01:43 -07:00
|
|
|
let quiet_command = command.starts_with('@');
|
|
|
|
if quiet_command {
|
2016-10-30 13:14:39 -07:00
|
|
|
command = &command[1..];
|
|
|
|
}
|
2016-11-16 21:37:43 -08:00
|
|
|
|
|
|
|
if command == "" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:01:35 -07:00
|
|
|
if options.dry_run || options.verbose || !((quiet_command ^ self.quiet) || options.quiet) {
|
|
|
|
let color = if options.highlight {
|
|
|
|
options.color.command()
|
|
|
|
} else {
|
|
|
|
options.color
|
|
|
|
};
|
|
|
|
warn!("{}", color.stderr().paint(command));
|
2016-10-30 13:14:39 -07:00
|
|
|
}
|
2017-04-25 23:39:34 -07:00
|
|
|
|
2016-11-05 01:01:43 -07:00
|
|
|
if options.dry_run {
|
2016-10-30 13:14:39 -07:00
|
|
|
continue;
|
2016-10-07 17:56:52 -07:00
|
|
|
}
|
2016-10-30 16:15:18 -07:00
|
|
|
|
2017-04-20 23:06:03 -07:00
|
|
|
let mut cmd = process::Command::new(options.shell.unwrap_or(DEFAULT_SHELL));
|
2016-10-30 16:15:18 -07:00
|
|
|
|
|
|
|
cmd.arg("-cu").arg(command);
|
|
|
|
|
2016-11-05 01:01:43 -07:00
|
|
|
if options.quiet {
|
|
|
|
cmd.stderr(process::Stdio::null());
|
|
|
|
cmd.stdout(process::Stdio::null());
|
|
|
|
}
|
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
export_env(&mut cmd, scope, exports)?;
|
2016-10-30 16:15:18 -07:00
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
match cmd.status() {
|
2016-10-07 17:56:52 -07:00
|
|
|
Ok(exit_status) => if let Some(code) = exit_status.code() {
|
2016-11-11 21:37:16 -08:00
|
|
|
if code != 0 {
|
2016-11-16 22:18:55 -08:00
|
|
|
return Err(RunError::Code{
|
|
|
|
recipe: self.name, line_number: Some(line_number), code: code
|
|
|
|
});
|
2016-10-07 17:56:52 -07:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-16 22:18:55 -08:00
|
|
|
return Err(error_from_signal(self.name, Some(line_number), exit_status));
|
2016-10-07 17:56:52 -07:00
|
|
|
},
|
2016-11-11 21:37:16 -08:00
|
|
|
Err(io_error) => return Err(RunError::IoError{
|
|
|
|
recipe: self.name, io_error: io_error}),
|
|
|
|
};
|
2016-10-07 17:56:52 -07:00
|
|
|
}
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
impl<'a> Display for Recipe<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2016-11-12 23:31:19 -08:00
|
|
|
if let Some(doc) = self.doc {
|
|
|
|
writeln!(f, "# {}", doc)?;
|
|
|
|
}
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "{}", self.name)?;
|
2016-10-31 19:11:27 -07:00
|
|
|
for parameter in &self.parameters {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, " {}", parameter)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, ":")?;
|
2016-10-23 16:43:52 -07:00
|
|
|
for dependency in &self.dependencies {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, " {}", dependency)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-10-23 23:38:49 -07:00
|
|
|
|
2016-10-27 09:44:07 -07:00
|
|
|
for (i, pieces) in self.lines.iter().enumerate() {
|
2016-10-23 16:43:52 -07:00
|
|
|
if i == 0 {
|
2016-11-11 21:37:16 -08:00
|
|
|
writeln!(f, "")?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-10-27 00:13:10 -07:00
|
|
|
for (j, piece) in pieces.iter().enumerate() {
|
2016-10-23 23:38:49 -07:00
|
|
|
if j == 0 {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, " ")?;
|
2016-10-23 23:38:49 -07:00
|
|
|
}
|
2016-10-29 21:51:39 -07:00
|
|
|
match *piece {
|
2016-11-11 21:37:16 -08:00
|
|
|
Fragment::Text{ref text} => write!(f, "{}", text.lexeme)?,
|
2017-04-02 20:39:23 -07:00
|
|
|
Fragment::Expression{ref expression, ..} =>
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "{}{}{}", "{{", expression, "}}")?,
|
2016-10-23 23:38:49 -07:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 09:44:07 -07:00
|
|
|
if i + 1 < self.lines.len() {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "\n")?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 20:39:21 -07:00
|
|
|
fn resolve_recipes<'a>(
|
2016-10-30 14:37:03 -07:00
|
|
|
recipes: &Map<&'a str, Recipe<'a>>,
|
|
|
|
assignments: &Map<&'a str, Expression<'a>>,
|
2016-10-29 20:39:21 -07:00
|
|
|
text: &'a str,
|
2016-11-12 14:24:33 -08:00
|
|
|
) -> Result<(), CompileError<'a>> {
|
2016-10-25 19:11:58 -07:00
|
|
|
let mut resolver = Resolver {
|
2016-11-12 23:31:19 -08:00
|
|
|
seen: empty(),
|
|
|
|
stack: empty(),
|
|
|
|
resolved: empty(),
|
2016-10-29 20:39:21 -07:00
|
|
|
recipes: recipes,
|
2016-10-25 19:11:58 -07:00
|
|
|
};
|
2017-04-02 20:39:23 -07:00
|
|
|
|
2016-10-25 19:11:58 -07:00
|
|
|
for recipe in recipes.values() {
|
2016-11-12 09:33:38 -08:00
|
|
|
resolver.resolve(recipe)?;
|
2016-11-13 00:01:42 -08:00
|
|
|
resolver.seen = empty();
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:39:21 -07:00
|
|
|
for recipe in recipes.values() {
|
|
|
|
for line in &recipe.lines {
|
|
|
|
for fragment in line {
|
|
|
|
if let Fragment::Expression{ref expression, ..} = *fragment {
|
|
|
|
for variable in expression.variables() {
|
|
|
|
let name = variable.lexeme;
|
2017-04-02 20:39:23 -07:00
|
|
|
let undefined = !assignments.contains_key(name)
|
2016-11-12 09:15:13 -08:00
|
|
|
&& !recipe.parameters.iter().any(|p| p.name == name);
|
|
|
|
if undefined {
|
2016-10-29 20:39:21 -07:00
|
|
|
// There's a borrow issue here that seems too difficult to solve.
|
|
|
|
// The error derived from the variable token has too short a lifetime,
|
|
|
|
// so we create a new error from its contents, which do live long
|
|
|
|
// enough.
|
|
|
|
//
|
|
|
|
// I suspect the solution here is to give recipes, pieces, and expressions
|
|
|
|
// two lifetime parameters instead of one, with one being the lifetime
|
|
|
|
// of the struct, and the second being the lifetime of the tokens
|
|
|
|
// that it contains
|
2016-10-29 22:56:47 -07:00
|
|
|
let error = variable.error(ErrorKind::UndefinedVariable{variable: name});
|
2016-11-12 14:24:33 -08:00
|
|
|
return Err(CompileError {
|
2016-10-29 20:39:21 -07:00
|
|
|
text: text,
|
|
|
|
index: error.index,
|
|
|
|
line: error.line,
|
|
|
|
column: error.column,
|
|
|
|
width: error.width,
|
2016-10-29 22:56:47 -07:00
|
|
|
kind: ErrorKind::UndefinedVariable {
|
2016-10-29 20:39:21 -07:00
|
|
|
variable: &text[error.index..error.index + error.width.unwrap()],
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 19:11:58 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Resolver<'a: 'b, 'b> {
|
|
|
|
stack: Vec<&'a str>,
|
2016-10-30 14:37:03 -07:00
|
|
|
seen: Set<&'a str>,
|
|
|
|
resolved: Set<&'a str>,
|
|
|
|
recipes: &'b Map<&'a str, Recipe<'a>>,
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Resolver<'a, 'b> {
|
2016-11-12 14:24:33 -08:00
|
|
|
fn resolve(&mut self, recipe: &Recipe<'a>) -> Result<(), CompileError<'a>> {
|
2016-10-25 19:11:58 -07:00
|
|
|
if self.resolved.contains(recipe.name) {
|
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
self.stack.push(recipe.name);
|
|
|
|
self.seen.insert(recipe.name);
|
|
|
|
for dependency_token in &recipe.dependency_tokens {
|
|
|
|
match self.recipes.get(dependency_token.lexeme) {
|
|
|
|
Some(dependency) => if !self.resolved.contains(dependency.name) {
|
|
|
|
if self.seen.contains(dependency.name) {
|
|
|
|
let first = self.stack[0];
|
|
|
|
self.stack.push(first);
|
|
|
|
return Err(dependency_token.error(ErrorKind::CircularRecipeDependency {
|
|
|
|
recipe: recipe.name,
|
|
|
|
circle: self.stack.iter()
|
|
|
|
.skip_while(|name| **name != dependency.name)
|
|
|
|
.cloned().collect()
|
|
|
|
}));
|
|
|
|
}
|
2017-01-18 20:32:55 -08:00
|
|
|
self.resolve(dependency)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
},
|
|
|
|
None => return Err(dependency_token.error(ErrorKind::UnknownDependency {
|
|
|
|
recipe: recipe.name,
|
|
|
|
unknown: dependency_token.lexeme
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.resolved.insert(recipe.name);
|
|
|
|
self.stack.pop();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 20:39:21 -07:00
|
|
|
fn resolve_assignments<'a>(
|
2016-10-30 14:37:03 -07:00
|
|
|
assignments: &Map<&'a str, Expression<'a>>,
|
|
|
|
assignment_tokens: &Map<&'a str, Token<'a>>,
|
2016-11-12 14:24:33 -08:00
|
|
|
) -> Result<(), CompileError<'a>> {
|
2016-10-29 20:39:21 -07:00
|
|
|
|
|
|
|
let mut resolver = AssignmentResolver {
|
|
|
|
assignments: assignments,
|
|
|
|
assignment_tokens: assignment_tokens,
|
2016-11-12 23:31:19 -08:00
|
|
|
stack: empty(),
|
|
|
|
seen: empty(),
|
|
|
|
evaluated: empty(),
|
2016-10-29 20:39:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
for name in assignments.keys() {
|
2016-11-11 21:37:16 -08:00
|
|
|
resolver.resolve_assignment(name)?;
|
2016-10-29 20:39:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AssignmentResolver<'a: 'b, 'b> {
|
2016-10-30 14:37:03 -07:00
|
|
|
assignments: &'b Map<&'a str, Expression<'a>>,
|
|
|
|
assignment_tokens: &'b Map<&'a str, Token<'a>>,
|
2016-10-29 20:39:21 -07:00
|
|
|
stack: Vec<&'a str>,
|
2016-10-30 14:37:03 -07:00
|
|
|
seen: Set<&'a str>,
|
|
|
|
evaluated: Set<&'a str>,
|
2016-10-29 20:39:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a: 'b, 'b> AssignmentResolver<'a, 'b> {
|
2016-11-12 14:24:33 -08:00
|
|
|
fn resolve_assignment(&mut self, name: &'a str) -> Result<(), CompileError<'a>> {
|
2016-10-29 20:39:21 -07:00
|
|
|
if self.evaluated.contains(name) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.seen.insert(name);
|
|
|
|
self.stack.push(name);
|
|
|
|
|
|
|
|
if let Some(expression) = self.assignments.get(name) {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.resolve_expression(expression)?;
|
2016-10-29 20:39:21 -07:00
|
|
|
self.evaluated.insert(name);
|
|
|
|
} else {
|
2016-10-29 22:56:47 -07:00
|
|
|
return Err(internal_error(format!("attempted to resolve unknown assignment `{}`", name)));
|
2016-10-29 20:39:21 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
fn resolve_expression(&mut self, expression: &Expression<'a>) -> Result<(), CompileError<'a>> {
|
2016-10-29 20:39:21 -07:00
|
|
|
match *expression {
|
|
|
|
Expression::Variable{name, ref token} => {
|
|
|
|
if self.evaluated.contains(name) {
|
|
|
|
return Ok(());
|
|
|
|
} else if self.seen.contains(name) {
|
2016-10-29 22:56:47 -07:00
|
|
|
let token = &self.assignment_tokens[name];
|
2016-10-29 20:39:21 -07:00
|
|
|
self.stack.push(name);
|
|
|
|
return Err(token.error(ErrorKind::CircularVariableDependency {
|
|
|
|
variable: name,
|
|
|
|
circle: self.stack.clone(),
|
|
|
|
}));
|
|
|
|
} else if self.assignments.contains_key(name) {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.resolve_assignment(name)?;
|
2016-10-29 20:39:21 -07:00
|
|
|
} else {
|
2016-10-29 22:56:47 -07:00
|
|
|
return Err(token.error(ErrorKind::UndefinedVariable{variable: name}));
|
2016-10-29 20:39:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Expression::Concatination{ref lhs, ref rhs} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.resolve_expression(lhs)?;
|
|
|
|
self.resolve_expression(rhs)?;
|
2016-10-29 20:39:21 -07:00
|
|
|
}
|
2016-10-29 23:39:12 -07:00
|
|
|
Expression::String{..} | Expression::Backtick{..} => {}
|
2016-10-29 20:39:21 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 21:51:39 -07:00
|
|
|
fn evaluate_assignments<'a>(
|
2016-10-30 14:37:03 -07:00
|
|
|
assignments: &Map<&'a str, Expression<'a>>,
|
|
|
|
overrides: &Map<&str, &str>,
|
2016-11-05 01:01:43 -07:00
|
|
|
quiet: bool,
|
2016-10-30 14:37:03 -07:00
|
|
|
) -> Result<Map<&'a str, String>, RunError<'a>> {
|
2016-10-29 21:51:39 -07:00
|
|
|
let mut evaluator = Evaluator {
|
2016-11-05 01:01:43 -07:00
|
|
|
assignments: assignments,
|
2016-11-12 23:31:19 -08:00
|
|
|
evaluated: empty(),
|
|
|
|
exports: &empty(),
|
2016-10-30 03:08:28 -07:00
|
|
|
overrides: overrides,
|
2016-11-05 01:01:43 -07:00
|
|
|
quiet: quiet,
|
2016-11-12 23:31:19 -08:00
|
|
|
scope: &empty(),
|
2016-10-29 21:51:39 -07:00
|
|
|
};
|
2016-10-29 00:14:41 -07:00
|
|
|
|
2016-10-29 21:51:39 -07:00
|
|
|
for name in assignments.keys() {
|
2016-11-11 21:37:16 -08:00
|
|
|
evaluator.evaluate_assignment(name)?;
|
2016-10-29 00:14:41 -07:00
|
|
|
}
|
|
|
|
|
2016-10-29 21:51:39 -07:00
|
|
|
Ok(evaluator.evaluated)
|
2016-10-29 00:14:41 -07:00
|
|
|
}
|
|
|
|
|
2016-10-25 19:11:58 -07:00
|
|
|
struct Evaluator<'a: 'b, 'b> {
|
2016-11-05 01:01:43 -07:00
|
|
|
assignments: &'b Map<&'a str, Expression<'a>>,
|
2016-10-30 14:37:03 -07:00
|
|
|
evaluated: Map<&'a str, String>,
|
2016-10-30 16:15:18 -07:00
|
|
|
exports: &'b Set<&'a str>,
|
2016-10-30 14:37:03 -07:00
|
|
|
overrides: &'b Map<&'b str, &'b str>,
|
2016-11-05 01:01:43 -07:00
|
|
|
quiet: bool,
|
|
|
|
scope: &'b Map<&'a str, String>,
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Evaluator<'a, 'b> {
|
2016-10-29 22:56:47 -07:00
|
|
|
fn evaluate_line(
|
|
|
|
&mut self,
|
2016-10-30 00:20:29 -07:00
|
|
|
line: &[Fragment<'a>],
|
2016-11-18 07:03:34 -08:00
|
|
|
arguments: &Map<&str, Cow<str>>
|
2016-10-29 22:56:47 -07:00
|
|
|
) -> Result<String, RunError<'a>> {
|
|
|
|
let mut evaluated = String::new();
|
|
|
|
for fragment in line {
|
|
|
|
match *fragment {
|
|
|
|
Fragment::Text{ref text} => evaluated += text.lexeme,
|
|
|
|
Fragment::Expression{ref expression} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
evaluated += &self.evaluate_expression(expression, arguments)?;
|
2016-10-29 22:56:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(evaluated)
|
|
|
|
}
|
|
|
|
|
2016-10-29 21:51:39 -07:00
|
|
|
fn evaluate_assignment(&mut self, name: &'a str) -> Result<(), RunError<'a>> {
|
2016-10-25 19:11:58 -07:00
|
|
|
if self.evaluated.contains_key(name) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(expression) = self.assignments.get(name) {
|
2016-10-30 03:08:28 -07:00
|
|
|
if let Some(value) = self.overrides.get(name) {
|
|
|
|
self.evaluated.insert(name, value.to_string());
|
|
|
|
} else {
|
2016-11-12 23:31:19 -08:00
|
|
|
let value = self.evaluate_expression(expression, &empty())?;
|
2016-10-30 03:08:28 -07:00
|
|
|
self.evaluated.insert(name, value);
|
|
|
|
}
|
2016-10-25 19:11:58 -07:00
|
|
|
} else {
|
2017-04-02 20:39:23 -07:00
|
|
|
return Err(RunError::InternalError {
|
2016-10-29 22:56:47 -07:00
|
|
|
message: format!("attempted to evaluated unknown assignment {}", name)
|
|
|
|
});
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
|
2016-10-29 00:14:41 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn evaluate_expression(
|
|
|
|
&mut self,
|
2016-10-30 00:20:29 -07:00
|
|
|
expression: &Expression<'a>,
|
2016-11-18 07:03:34 -08:00
|
|
|
arguments: &Map<&str, Cow<str>>
|
2016-10-29 21:51:39 -07:00
|
|
|
) -> Result<String, RunError<'a>> {
|
2016-10-25 19:11:58 -07:00
|
|
|
Ok(match *expression {
|
2016-10-29 21:51:39 -07:00
|
|
|
Expression::Variable{name, ..} => {
|
2016-10-25 19:11:58 -07:00
|
|
|
if self.evaluated.contains_key(name) {
|
2016-10-29 22:56:47 -07:00
|
|
|
self.evaluated[name].clone()
|
2016-10-29 00:14:41 -07:00
|
|
|
} else if self.scope.contains_key(name) {
|
2016-10-29 22:56:47 -07:00
|
|
|
self.scope[name].clone()
|
2016-10-29 00:14:41 -07:00
|
|
|
} else if self.assignments.contains_key(name) {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.evaluate_assignment(name)?;
|
2016-10-29 22:56:47 -07:00
|
|
|
self.evaluated[name].clone()
|
2016-10-29 00:14:41 -07:00
|
|
|
} else if arguments.contains_key(name) {
|
2016-10-29 22:56:47 -07:00
|
|
|
arguments[name].to_string()
|
2016-10-29 00:14:41 -07:00
|
|
|
} else {
|
2017-04-02 20:39:23 -07:00
|
|
|
return Err(RunError::InternalError {
|
2016-10-29 22:56:47 -07:00
|
|
|
message: format!("attempted to evaluate undefined variable `{}`", name)
|
|
|
|
});
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
2016-11-12 09:15:13 -08:00
|
|
|
Expression::String{ref cooked_string} => cooked_string.cooked.clone(),
|
2016-10-30 16:15:18 -07:00
|
|
|
Expression::Backtick{raw, ref token} => {
|
2016-11-12 09:33:38 -08:00
|
|
|
run_backtick(raw, token, self.scope, self.exports, self.quiet)?
|
2016-10-30 16:15:18 -07:00
|
|
|
}
|
2016-10-25 19:11:58 -07:00
|
|
|
Expression::Concatination{ref lhs, ref rhs} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.evaluate_expression(lhs, arguments)?
|
2016-10-29 21:51:39 -07:00
|
|
|
+
|
2016-11-11 21:37:16 -08:00
|
|
|
&self.evaluate_expression(rhs, arguments)?
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
})
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 23:18:26 -07:00
|
|
|
#[derive(Debug, PartialEq)]
|
2016-11-12 14:24:33 -08:00
|
|
|
struct CompileError<'a> {
|
2016-10-22 23:18:26 -07:00
|
|
|
text: &'a str,
|
|
|
|
index: usize,
|
|
|
|
line: usize,
|
|
|
|
column: usize,
|
2016-10-23 16:43:52 -07:00
|
|
|
width: Option<usize>,
|
2016-10-22 23:18:26 -07:00
|
|
|
kind: ErrorKind<'a>,
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum ErrorKind<'a> {
|
2016-10-25 19:11:58 -07:00
|
|
|
CircularRecipeDependency{recipe: &'a str, circle: Vec<&'a str>},
|
|
|
|
CircularVariableDependency{variable: &'a str, circle: Vec<&'a str>},
|
2016-10-31 19:11:27 -07:00
|
|
|
DependencyHasParameters{recipe: &'a str, dependency: &'a str},
|
2016-10-28 00:06:36 -07:00
|
|
|
DuplicateDependency{recipe: &'a str, dependency: &'a str},
|
2016-10-31 19:11:27 -07:00
|
|
|
DuplicateParameter{recipe: &'a str, parameter: &'a str},
|
2016-10-23 16:43:52 -07:00
|
|
|
DuplicateRecipe{recipe: &'a str, first: usize},
|
2016-10-25 19:11:58 -07:00
|
|
|
DuplicateVariable{variable: &'a str},
|
2016-10-23 16:43:52 -07:00
|
|
|
ExtraLeadingWhitespace,
|
2016-10-02 22:30:28 -07:00
|
|
|
InconsistentLeadingWhitespace{expected: &'a str, found: &'a str},
|
2016-10-28 00:06:36 -07:00
|
|
|
InternalError{message: String},
|
|
|
|
InvalidEscapeSequence{character: char},
|
|
|
|
MixedLeadingWhitespace{whitespace: &'a str},
|
2016-10-06 17:43:30 -07:00
|
|
|
OuterShebang,
|
2016-10-31 19:11:27 -07:00
|
|
|
ParameterShadowsVariable{parameter: &'a str},
|
2016-11-12 09:15:13 -08:00
|
|
|
RequiredParameterFollowsDefaultParameter{parameter: &'a str},
|
2016-11-18 07:03:34 -08:00
|
|
|
ParameterFollowsVariadicParameter{parameter: &'a str},
|
2016-10-31 19:11:27 -07:00
|
|
|
UndefinedVariable{variable: &'a str},
|
2016-10-28 00:06:36 -07:00
|
|
|
UnexpectedToken{expected: Vec<TokenKind>, found: TokenKind},
|
2016-10-23 16:43:52 -07:00
|
|
|
UnknownDependency{recipe: &'a str, unknown: &'a str},
|
2016-10-16 18:59:49 -07:00
|
|
|
UnknownStartOfToken,
|
2016-10-28 00:06:36 -07:00
|
|
|
UnterminatedString,
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
fn internal_error(message: String) -> CompileError<'static> {
|
|
|
|
CompileError {
|
2016-10-29 22:56:47 -07:00
|
|
|
text: "",
|
|
|
|
index: 0,
|
|
|
|
line: 0,
|
|
|
|
column: 0,
|
|
|
|
width: None,
|
|
|
|
kind: ErrorKind::InternalError { message: message }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 22:30:28 -07:00
|
|
|
fn show_whitespace(text: &str) -> String {
|
2017-01-08 19:01:48 -08:00
|
|
|
text.chars().map(|c| match c { '\t' => '␉', ' ' => '␠', _ => c }).collect()
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
fn mixed_whitespace(text: &str) -> bool {
|
2016-10-02 22:30:28 -07:00
|
|
|
!(text.chars().all(|c| c == ' ') || text.chars().all(|c| c == '\t'))
|
|
|
|
}
|
|
|
|
|
2016-11-11 14:33:17 -08:00
|
|
|
fn maybe_s(n: usize) -> &'static str {
|
|
|
|
if n == 1 {
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
"s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Tick<'a, T: 'a + Display>(&'a T);
|
|
|
|
|
|
|
|
impl<'a, T: Display> Display for Tick<'a, T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(f, "`{}`", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 19:12:44 -08:00
|
|
|
fn ticks<T: Display>(ts: &[T]) -> Vec<Tick<T>> {
|
2016-11-11 14:33:17 -08:00
|
|
|
ts.iter().map(Tick).collect()
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:15:13 -08:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
struct CookedString<'a> {
|
|
|
|
raw: &'a str,
|
|
|
|
cooked: String,
|
|
|
|
}
|
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
fn cook_string<'a>(token: &Token<'a>) -> Result<CookedString<'a>, CompileError<'a>> {
|
2016-11-12 09:15:13 -08:00
|
|
|
let raw = &token.lexeme[1..token.lexeme.len()-1];
|
|
|
|
|
|
|
|
if let RawString = token.kind {
|
|
|
|
Ok(CookedString{raw: raw, cooked: raw.to_string()})
|
|
|
|
} else if let StringToken = token.kind {
|
|
|
|
let mut cooked = String::new();
|
|
|
|
let mut escape = false;
|
|
|
|
for c in raw.chars() {
|
|
|
|
if escape {
|
|
|
|
match c {
|
|
|
|
'n' => cooked.push('\n'),
|
|
|
|
'r' => cooked.push('\r'),
|
|
|
|
't' => cooked.push('\t'),
|
|
|
|
'\\' => cooked.push('\\'),
|
|
|
|
'"' => cooked.push('"'),
|
|
|
|
other => return Err(token.error(ErrorKind::InvalidEscapeSequence {
|
|
|
|
character: other,
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
escape = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if c == '\\' {
|
|
|
|
escape = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cooked.push(c);
|
|
|
|
}
|
|
|
|
Ok(CookedString{raw: raw, cooked: cooked})
|
|
|
|
} else {
|
|
|
|
Err(token.error(ErrorKind::InternalError{
|
|
|
|
message: "cook_string() called on non-string token".to_string()
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 03:08:28 -07:00
|
|
|
struct And<'a, T: 'a + Display>(&'a [T]);
|
|
|
|
struct Or <'a, T: 'a + Display>(&'a [T]);
|
|
|
|
|
|
|
|
impl<'a, T: Display> Display for And<'a, T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
conjoin(f, self.0, "and")
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
|
|
|
|
impl<'a, T: Display> Display for Or<'a, T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2016-10-30 03:08:28 -07:00
|
|
|
conjoin(f, self.0, "or")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn conjoin<T: Display>(
|
|
|
|
f: &mut fmt::Formatter,
|
|
|
|
values: &[T],
|
|
|
|
conjunction: &str,
|
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
match values.len() {
|
2016-10-23 16:43:52 -07:00
|
|
|
0 => {},
|
2016-11-11 21:37:16 -08:00
|
|
|
1 => write!(f, "{}", values[0])?,
|
|
|
|
2 => write!(f, "{} {} {}", values[0], conjunction, values[1])?,
|
2016-10-30 03:08:28 -07:00
|
|
|
_ => for (i, item) in values.iter().enumerate() {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "{}", item)?;
|
2016-10-30 03:08:28 -07:00
|
|
|
if i == values.len() - 1 {
|
|
|
|
} else if i == values.len() - 2 {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, ", {} ", conjunction)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
} else {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, ", ")?
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-10-02 22:30:28 -07:00
|
|
|
},
|
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
Ok(())
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
2016-11-07 21:01:27 -08:00
|
|
|
fn write_error_context(
|
|
|
|
f: &mut fmt::Formatter,
|
|
|
|
text: &str,
|
|
|
|
index: usize,
|
|
|
|
line: usize,
|
|
|
|
column: usize,
|
|
|
|
width: Option<usize>,
|
|
|
|
) -> Result<(), fmt::Error> {
|
|
|
|
let line_number = line + 1;
|
2017-06-01 18:01:35 -07:00
|
|
|
let red = Color::fmt(f).error();
|
2016-11-07 21:01:27 -08:00
|
|
|
match text.lines().nth(line) {
|
|
|
|
Some(line) => {
|
2016-11-11 15:18:42 -08:00
|
|
|
let mut i = 0;
|
|
|
|
let mut space_column = 0;
|
|
|
|
let mut space_line = String::new();
|
|
|
|
let mut space_width = 0;
|
|
|
|
for c in line.chars() {
|
|
|
|
if c == '\t' {
|
|
|
|
space_line.push_str(" ");
|
|
|
|
if i < column {
|
|
|
|
space_column += 4;
|
|
|
|
}
|
|
|
|
if i >= column && i < column + width.unwrap_or(1) {
|
|
|
|
space_width += 4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if i < column {
|
|
|
|
space_column += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
if i >= column && i < column + width.unwrap_or(1) {
|
|
|
|
space_width += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
|
|
|
|
}
|
|
|
|
space_line.push(c);
|
|
|
|
}
|
|
|
|
i += c.len_utf8();
|
|
|
|
}
|
2016-11-07 21:01:27 -08:00
|
|
|
let line_number_width = line_number.to_string().len();
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "{0:1$} |\n", "", line_number_width)?;
|
|
|
|
write!(f, "{} | {}\n", line_number, space_line)?;
|
|
|
|
write!(f, "{0:1$} |", "", line_number_width)?;
|
2016-11-11 15:18:42 -08:00
|
|
|
if width == None {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, " {0:1$}{2}^{3}", "", space_column, red.prefix(), red.suffix())?;
|
2016-11-11 15:18:42 -08:00
|
|
|
} else {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, " {0:1$}{2}{3:^<4$}{5}", "", space_column,
|
|
|
|
red.prefix(), "", space_width, red.suffix())?;
|
2016-11-11 15:18:42 -08:00
|
|
|
}
|
2016-11-07 21:01:27 -08:00
|
|
|
},
|
|
|
|
None => if index != text.len() {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "internal error: Error has invalid line number: {}", line_number)?
|
2016-11-07 21:01:27 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_token_error_context(f: &mut fmt::Formatter, token: &Token) -> Result<(), fmt::Error> {
|
|
|
|
write_error_context(
|
|
|
|
f,
|
|
|
|
token.text,
|
|
|
|
token.index,
|
|
|
|
token.line,
|
|
|
|
token.column + token.prefix.len(),
|
|
|
|
Some(token.lexeme.len())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
impl<'a> Display for CompileError<'a> {
|
2016-10-02 22:30:28 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2016-11-12 14:24:33 -08:00
|
|
|
use ErrorKind::*;
|
2017-06-01 18:01:35 -07:00
|
|
|
let error = Color::fmt(f).error();
|
|
|
|
let message = Color::fmt(f).message();
|
2016-11-07 21:01:27 -08:00
|
|
|
|
2017-06-01 18:01:35 -07:00
|
|
|
write!(f, "{} {}", error.paint("error:"), message.prefix())?;
|
2017-04-02 20:39:23 -07:00
|
|
|
|
2016-10-02 22:30:28 -07:00
|
|
|
match self.kind {
|
2016-11-12 14:24:33 -08:00
|
|
|
CircularRecipeDependency{recipe, ref circle} => {
|
2016-10-25 19:11:58 -07:00
|
|
|
if circle.len() == 2 {
|
2017-01-08 19:01:48 -08:00
|
|
|
writeln!(f, "Recipe `{}` depends on itself", recipe)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
} else {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Recipe `{}` has circular dependency `{}`",
|
2016-11-11 21:37:16 -08:00
|
|
|
recipe, circle.join(" -> "))?;
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
CircularVariableDependency{variable, ref circle} => {
|
2016-11-11 17:09:51 -08:00
|
|
|
if circle.len() == 2 {
|
2017-01-08 19:01:48 -08:00
|
|
|
writeln!(f, "Variable `{}` is defined in terms of itself", variable)?;
|
2016-11-11 17:09:51 -08:00
|
|
|
} else {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Variable `{}` depends on its own value: `{}`",
|
2016-11-11 21:37:16 -08:00
|
|
|
variable, circle.join(" -> "))?;
|
2016-11-11 17:09:51 -08:00
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
InvalidEscapeSequence{character} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
writeln!(f, "`\\{}` is not a valid escape sequence",
|
|
|
|
character.escape_default().collect::<String>())?;
|
2016-10-28 00:06:36 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
DuplicateParameter{recipe, parameter} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Recipe `{}` has duplicate parameter `{}`", recipe, parameter)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
DuplicateVariable{variable} => {
|
2017-01-08 19:01:48 -08:00
|
|
|
writeln!(f, "Variable `{}` has multiple definitions", variable)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
UnexpectedToken{ref expected, found} => {
|
2017-01-08 19:01:48 -08:00
|
|
|
writeln!(f, "Expected {}, but found {}", Or(expected), found)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
DuplicateDependency{recipe, dependency} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Recipe `{}` has duplicate dependency `{}`", recipe, dependency)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
DuplicateRecipe{recipe, first} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Recipe `{}` first defined on line {} is redefined on line {}",
|
2017-01-08 19:01:48 -08:00
|
|
|
recipe, first + 1, self.line + 1)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
DependencyHasParameters{recipe, dependency} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Recipe `{}` depends on `{}` which requires arguments. \
|
2017-01-08 19:01:48 -08:00
|
|
|
Dependencies may not require arguments", recipe, dependency)?;
|
2016-10-29 00:14:41 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
ParameterShadowsVariable{parameter} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Parameter `{}` shadows variable of the same name", parameter)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
RequiredParameterFollowsDefaultParameter{parameter} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Non-default parameter `{}` follows default parameter", parameter)?;
|
2016-11-12 09:15:13 -08:00
|
|
|
}
|
2016-11-18 07:03:34 -08:00
|
|
|
ParameterFollowsVariadicParameter{parameter} => {
|
2017-01-08 19:01:48 -08:00
|
|
|
writeln!(f, "Parameter `{}` follows variadic parameter", parameter)?;
|
2016-11-18 07:03:34 -08:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
MixedLeadingWhitespace{whitespace} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
writeln!(f,
|
2017-01-08 17:26:34 -08:00
|
|
|
"Found a mix of tabs and spaces in leading whitespace: `{}`\n\
|
2017-01-08 19:01:48 -08:00
|
|
|
Leading whitespace may consist of tabs or spaces, but not both",
|
2016-10-23 16:43:52 -07:00
|
|
|
show_whitespace(whitespace)
|
2016-11-11 21:37:16 -08:00
|
|
|
)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
ExtraLeadingWhitespace => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Recipe line has extra leading whitespace")?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
InconsistentLeadingWhitespace{expected, found} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
writeln!(f,
|
2017-05-12 19:09:47 -07:00
|
|
|
"Recipe line has inconsistent leading whitespace. \
|
|
|
|
Recipe started with `{}` but found line with `{}`",
|
2016-10-02 22:30:28 -07:00
|
|
|
show_whitespace(expected), show_whitespace(found)
|
2016-11-11 21:37:16 -08:00
|
|
|
)?;
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
OuterShebang => {
|
2017-01-08 19:01:48 -08:00
|
|
|
writeln!(f, "`#!` is reserved syntax outside of recipes")?;
|
2016-10-06 17:43:30 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
UnknownDependency{recipe, unknown} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Recipe `{}` has unknown dependency `{}`", recipe, unknown)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
UndefinedVariable{variable} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Variable `{}` not defined", variable)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
UnknownStartOfToken => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Unknown start of token:")?;
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
UnterminatedString => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Unterminated string")?;
|
2016-10-28 00:06:36 -07:00
|
|
|
}
|
2016-11-12 14:24:33 -08:00
|
|
|
InternalError{ref message} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
writeln!(f, "Internal error, this may indicate a bug in just: {}\n\
|
2016-11-11 21:37:16 -08:00
|
|
|
consider filing an issue: https://github.com/casey/just/issues/new",
|
|
|
|
message)?;
|
2016-10-22 23:18:26 -07:00
|
|
|
}
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
2017-06-01 18:01:35 -07:00
|
|
|
write!(f, "{}", message.suffix())?;
|
2016-10-02 22:30:28 -07:00
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
write_error_context(f, self.text, self.index, self.line, self.column, self.width)
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
struct Justfile<'a> {
|
2016-10-30 14:37:03 -07:00
|
|
|
recipes: Map<&'a str, Recipe<'a>>,
|
|
|
|
assignments: Map<&'a str, Expression<'a>>,
|
2016-10-30 16:15:18 -07:00
|
|
|
exports: Set<&'a str>,
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
2016-11-05 00:31:38 -07:00
|
|
|
#[derive(Default)]
|
|
|
|
struct RunOptions<'a> {
|
|
|
|
dry_run: bool,
|
|
|
|
evaluate: bool,
|
2017-04-25 23:39:34 -07:00
|
|
|
highlight: bool,
|
2016-11-05 00:31:38 -07:00
|
|
|
overrides: Map<&'a str, &'a str>,
|
2016-11-05 01:01:43 -07:00
|
|
|
quiet: bool,
|
2017-04-20 23:06:03 -07:00
|
|
|
shell: Option<&'a str>,
|
2017-06-01 18:01:35 -07:00
|
|
|
color: Color,
|
2016-11-16 21:06:51 -08:00
|
|
|
verbose: bool,
|
2016-11-05 00:31:38 -07:00
|
|
|
}
|
|
|
|
|
2016-10-29 00:14:41 -07:00
|
|
|
impl<'a, 'b> Justfile<'a> where 'a: 'b {
|
2017-05-07 15:11:10 -07:00
|
|
|
fn first(&self) -> Option<&Recipe> {
|
|
|
|
let mut first: Option<&Recipe> = None;
|
2016-10-23 20:39:50 -07:00
|
|
|
for recipe in self.recipes.values() {
|
2016-10-02 22:30:28 -07:00
|
|
|
if let Some(first_recipe) = first {
|
2016-10-06 17:43:30 -07:00
|
|
|
if recipe.line_number < first_recipe.line_number {
|
2016-10-02 22:30:28 -07:00
|
|
|
first = Some(recipe)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
first = Some(recipe);
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 15:11:10 -07:00
|
|
|
first
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
fn count(&self) -> usize {
|
2016-10-03 23:55:55 -07:00
|
|
|
self.recipes.len()
|
|
|
|
}
|
|
|
|
|
2016-11-12 12:36:12 -08:00
|
|
|
fn suggest(&self, name: &str) -> Option<&'a str> {
|
|
|
|
let mut suggestions = self.recipes.keys()
|
|
|
|
.map(|suggestion| (edit_distance::edit_distance(suggestion, name), suggestion))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
suggestions.sort();
|
|
|
|
if let Some(&(distance, suggestion)) = suggestions.first() {
|
|
|
|
if distance < 3 {
|
|
|
|
return Some(suggestion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-10-30 03:08:28 -07:00
|
|
|
fn run(
|
|
|
|
&'a self,
|
2016-10-30 13:14:39 -07:00
|
|
|
arguments: &[&'a str],
|
2016-11-05 01:01:43 -07:00
|
|
|
options: &RunOptions<'a>,
|
2016-10-30 03:08:28 -07:00
|
|
|
) -> Result<(), RunError<'a>> {
|
2016-11-05 00:31:38 -07:00
|
|
|
let unknown_overrides = options.overrides.keys().cloned()
|
2016-10-30 03:08:28 -07:00
|
|
|
.filter(|name| !self.assignments.contains_key(name))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if !unknown_overrides.is_empty() {
|
|
|
|
return Err(RunError::UnknownOverrides{overrides: unknown_overrides});
|
|
|
|
}
|
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
let scope = evaluate_assignments(&self.assignments, &options.overrides, options.quiet)?;
|
2016-11-05 00:31:38 -07:00
|
|
|
if options.evaluate {
|
2016-11-12 13:12:43 -08:00
|
|
|
let mut width = 0;
|
|
|
|
for name in scope.keys() {
|
|
|
|
width = cmp::max(name.len(), width);
|
|
|
|
}
|
|
|
|
|
2016-10-30 13:14:39 -07:00
|
|
|
for (name, value) in scope {
|
2016-11-12 13:12:43 -08:00
|
|
|
println!("{0:1$} = \"{2}\"", name, width, value);
|
2016-10-30 13:14:39 -07:00
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2016-12-10 16:35:52 -08:00
|
|
|
let mut missing = vec![];
|
|
|
|
let mut grouped = vec![];
|
|
|
|
let mut rest = arguments;
|
2016-10-29 21:51:39 -07:00
|
|
|
|
2016-12-10 16:35:52 -08:00
|
|
|
while let Some((argument, mut tail)) = rest.split_first() {
|
2016-10-29 00:14:41 -07:00
|
|
|
if let Some(recipe) = self.recipes.get(argument) {
|
2016-12-10 16:35:52 -08:00
|
|
|
if recipe.parameters.is_empty() {
|
|
|
|
grouped.push((recipe, &tail[0..0]));
|
|
|
|
} else {
|
2016-11-12 09:15:13 -08:00
|
|
|
let argument_range = recipe.argument_range();
|
2017-05-07 14:45:23 -07:00
|
|
|
let argument_count = cmp::min(tail.len(), recipe.max_arguments());
|
2016-12-10 16:35:52 -08:00
|
|
|
if !contains(&argument_range, argument_count) {
|
2016-10-29 00:14:41 -07:00
|
|
|
return Err(RunError::ArgumentCountMismatch {
|
|
|
|
recipe: recipe.name,
|
2016-12-10 16:35:52 -08:00
|
|
|
found: tail.len(),
|
2017-05-07 14:45:23 -07:00
|
|
|
min: recipe.min_arguments(),
|
|
|
|
max: recipe.max_arguments(),
|
2016-10-29 00:14:41 -07:00
|
|
|
});
|
|
|
|
}
|
2016-12-10 16:35:52 -08:00
|
|
|
grouped.push((recipe, &tail[0..argument_count]));
|
|
|
|
tail = &tail[argument_count..];
|
2016-10-29 00:14:41 -07:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-10 16:35:52 -08:00
|
|
|
missing.push(*argument);
|
2016-10-29 00:14:41 -07:00
|
|
|
}
|
2016-12-10 16:35:52 -08:00
|
|
|
rest = tail;
|
2016-10-29 00:14:41 -07:00
|
|
|
}
|
|
|
|
|
2016-10-23 20:39:50 -07:00
|
|
|
if !missing.is_empty() {
|
2016-11-12 12:36:12 -08:00
|
|
|
let suggestion = if missing.len() == 1 {
|
|
|
|
self.suggest(missing.first().unwrap())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
return Err(RunError::UnknownRecipes{recipes: missing, suggestion: suggestion});
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
2016-12-10 16:35:52 -08:00
|
|
|
|
|
|
|
let mut ran = empty();
|
|
|
|
for (recipe, arguments) in grouped {
|
|
|
|
self.run_recipe(recipe, arguments, &scope, &mut ran, options)?
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
2016-12-10 16:35:52 -08:00
|
|
|
|
2016-10-03 23:55:55 -07:00
|
|
|
Ok(())
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
2016-10-05 16:03:11 -07:00
|
|
|
|
2016-10-29 22:56:47 -07:00
|
|
|
fn run_recipe<'c>(
|
|
|
|
&'c self,
|
2016-10-30 00:20:29 -07:00
|
|
|
recipe: &Recipe<'a>,
|
2016-10-29 21:51:39 -07:00
|
|
|
arguments: &[&'a str],
|
2016-10-30 14:37:03 -07:00
|
|
|
scope: &Map<&'c str, String>,
|
|
|
|
ran: &mut Set<&'a str>,
|
2016-11-05 01:01:43 -07:00
|
|
|
options: &RunOptions<'a>,
|
2016-10-29 21:51:39 -07:00
|
|
|
) -> Result<(), RunError> {
|
|
|
|
for dependency_name in &recipe.dependencies {
|
|
|
|
if !ran.contains(dependency_name) {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.run_recipe(&self.recipes[dependency_name], &[], scope, ran, options)?;
|
2016-10-29 21:51:39 -07:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 09:33:38 -08:00
|
|
|
recipe.run(arguments, scope, &self.exports, options)?;
|
2016-10-29 21:51:39 -07:00
|
|
|
ran.insert(recipe.name);
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
2016-10-02 22:30:28 -07:00
|
|
|
|
2016-10-25 19:11:58 -07:00
|
|
|
impl<'a> Display for Justfile<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
let mut items = self.recipes.len() + self.assignments.len();
|
|
|
|
for (name, expression) in &self.assignments {
|
2016-10-30 16:15:18 -07:00
|
|
|
if self.exports.contains(name) {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "export ")?;
|
2016-10-30 16:15:18 -07:00
|
|
|
}
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "{} = {}", name, expression)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
items -= 1;
|
|
|
|
if items != 0 {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "\n\n")?;
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for recipe in self.recipes.values() {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "{}", recipe)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
items -= 1;
|
|
|
|
if items != 0 {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "\n\n")?;
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 13:58:18 -07:00
|
|
|
#[derive(Debug)]
|
2016-10-23 16:43:52 -07:00
|
|
|
enum RunError<'a> {
|
2016-11-12 09:15:13 -08:00
|
|
|
ArgumentCountMismatch{recipe: &'a str, found: usize, min: usize, max: usize},
|
2017-04-23 14:21:21 -07:00
|
|
|
Backtick{token: Token<'a>, output_error: OutputError},
|
2016-11-16 22:18:55 -08:00
|
|
|
Code{recipe: &'a str, line_number: Option<usize>, code: i32},
|
2017-04-23 16:09:34 -07:00
|
|
|
Cygpath{recipe: &'a str, output_error: OutputError},
|
2016-10-29 01:58:30 -07:00
|
|
|
InternalError{message: String},
|
2016-10-05 13:58:18 -07:00
|
|
|
IoError{recipe: &'a str, io_error: io::Error},
|
2017-04-22 19:05:51 -07:00
|
|
|
Shebang{recipe: &'a str, command: String, argument: Option<String>, io_error: io::Error},
|
2016-11-16 22:18:55 -08:00
|
|
|
Signal{recipe: &'a str, line_number: Option<usize>, signal: i32},
|
2016-10-07 17:56:52 -07:00
|
|
|
TmpdirIoError{recipe: &'a str, io_error: io::Error},
|
2016-11-16 22:18:55 -08:00
|
|
|
UnknownFailure{recipe: &'a str, line_number: Option<usize>},
|
2016-10-30 03:08:28 -07:00
|
|
|
UnknownOverrides{overrides: Vec<&'a str>},
|
2017-04-22 19:05:51 -07:00
|
|
|
UnknownRecipes{recipes: Vec<&'a str>, suggestion: Option<&'a str>},
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
|
|
|
|
2017-04-23 14:21:21 -07:00
|
|
|
impl<'a> RunError<'a> {
|
|
|
|
fn code(&self) -> Option<i32> {
|
|
|
|
use RunError::*;
|
|
|
|
match *self {
|
|
|
|
Code{code, ..} | Backtick{output_error: OutputError::Code(code), ..} => Some(code),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 23:55:55 -07:00
|
|
|
impl<'a> Display for RunError<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2016-11-12 14:24:33 -08:00
|
|
|
use RunError::*;
|
2017-06-01 18:01:35 -07:00
|
|
|
let color = if f.alternate() { Color::always() } else { Color::never() };
|
|
|
|
let error = color.error();
|
|
|
|
let message = color.message();
|
|
|
|
write!(f, "{} {}", error.paint("error:"), message.prefix())?;
|
2016-11-11 15:18:42 -08:00
|
|
|
|
|
|
|
let mut error_token = None;
|
2016-11-07 21:01:27 -08:00
|
|
|
|
2016-10-23 20:39:50 -07:00
|
|
|
match *self {
|
2016-11-12 14:24:33 -08:00
|
|
|
UnknownRecipes{ref recipes, ref suggestion} => {
|
2016-11-12 12:36:12 -08:00
|
|
|
write!(f, "Justfile does not contain recipe{} {}.",
|
2017-04-21 22:11:18 -07:00
|
|
|
maybe_s(recipes.len()), Or(&ticks(recipes)))?;
|
2016-11-12 12:36:12 -08:00
|
|
|
if let Some(suggestion) = *suggestion {
|
|
|
|
write!(f, "\nDid you mean `{}`?", suggestion)?;
|
|
|
|
}
|
2016-10-03 23:55:55 -07:00
|
|
|
},
|
2016-11-12 14:24:33 -08:00
|
|
|
UnknownOverrides{ref overrides} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "Variable{} {} overridden on the command line but not present in justfile",
|
|
|
|
maybe_s(overrides.len()),
|
|
|
|
And(&overrides.iter().map(Tick).collect::<Vec<_>>()))?;
|
2016-10-30 03:08:28 -07:00
|
|
|
},
|
2016-11-12 14:24:33 -08:00
|
|
|
ArgumentCountMismatch{recipe, found, min, max} => {
|
2016-11-12 09:15:13 -08:00
|
|
|
if min == max {
|
|
|
|
let expected = min;
|
|
|
|
write!(f, "Recipe `{}` got {} argument{} but {}takes {}",
|
|
|
|
recipe, found, maybe_s(found),
|
|
|
|
if expected < found { "only " } else { "" }, expected)?;
|
|
|
|
} else if found < min {
|
|
|
|
write!(f, "Recipe `{}` got {} argument{} but takes at least {}",
|
|
|
|
recipe, found, maybe_s(found), min)?;
|
|
|
|
} else if found > max {
|
|
|
|
write!(f, "Recipe `{}` got {} argument{} but takes at most {}",
|
|
|
|
recipe, found, maybe_s(found), max)?;
|
|
|
|
}
|
2016-10-27 00:31:50 -07:00
|
|
|
},
|
2016-11-16 22:18:55 -08:00
|
|
|
Code{recipe, line_number, code} => {
|
|
|
|
if let Some(n) = line_number {
|
|
|
|
write!(f, "Recipe `{}` failed on line {} with exit code {}", recipe, n, code)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "Recipe `{}` failed with exit code {}", recipe, code)?;
|
|
|
|
}
|
2016-10-03 23:55:55 -07:00
|
|
|
},
|
2017-04-23 16:09:34 -07:00
|
|
|
Cygpath{recipe, ref output_error} => match *output_error {
|
|
|
|
OutputError::Code(code) => {
|
|
|
|
write!(f, "Cygpath failed with exit code {} while translating recipe `{}` \
|
|
|
|
shebang interpreter path", code, recipe)?;
|
|
|
|
}
|
|
|
|
OutputError::Signal(signal) => {
|
|
|
|
write!(f, "Cygpath terminated by signal {} while translating recipe `{}` \
|
|
|
|
shebang interpreter path", signal, recipe)?;
|
|
|
|
}
|
|
|
|
OutputError::Unknown => {
|
|
|
|
write!(f, "Cygpath experienced an unknown failure while translating recipe `{}` \
|
|
|
|
shebang interpreter path", recipe)?;
|
|
|
|
}
|
|
|
|
OutputError::Io(ref io_error) => {
|
|
|
|
match io_error.kind() {
|
|
|
|
io::ErrorKind::NotFound => write!(
|
|
|
|
f, "Could not find `cygpath` executable to translate recipe `{}` \
|
|
|
|
shebang interpreter path:\n{}", recipe, io_error),
|
|
|
|
io::ErrorKind::PermissionDenied => write!(
|
|
|
|
f, "Could not run `cygpath` executable to translate recipe `{}` \
|
|
|
|
shebang interpreter path:\n{}", recipe, io_error),
|
|
|
|
_ => write!(f, "Could not run `cygpath` executable:\n{}", io_error),
|
|
|
|
}?;
|
|
|
|
}
|
|
|
|
OutputError::Utf8(ref utf8_error) => {
|
|
|
|
write!(f, "Cygpath successfully translated recipe `{}` shebang interpreter path, \
|
|
|
|
but output was not utf8: {}", recipe, utf8_error)?;
|
|
|
|
}
|
|
|
|
},
|
2017-04-22 19:05:51 -07:00
|
|
|
Shebang{recipe, ref command, ref argument, ref io_error} => {
|
|
|
|
if let Some(ref argument) = *argument {
|
|
|
|
write!(f, "Recipe `{}` with shebang `#!{} {}` execution error: {}",
|
|
|
|
recipe, command, argument, io_error)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "Recipe `{}` with shebang `#!{}` execution error: {}",
|
|
|
|
recipe, command, io_error)?;
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 22:18:55 -08:00
|
|
|
Signal{recipe, line_number, signal} => {
|
|
|
|
if let Some(n) = line_number {
|
|
|
|
write!(f, "Recipe `{}` was terminated on line {} by signal {}", recipe, n, signal)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "Recipe `{}` was terminated by signal {}", recipe, signal)?;
|
|
|
|
}
|
2016-10-05 13:58:18 -07:00
|
|
|
}
|
2016-11-16 22:18:55 -08:00
|
|
|
UnknownFailure{recipe, line_number} => {
|
|
|
|
if let Some(n) = line_number {
|
|
|
|
write!(f, "Recipe `{}` failed on line {} for an unknown reason", recipe, n)?;
|
|
|
|
} else {
|
|
|
|
}
|
2016-10-05 13:58:18 -07:00
|
|
|
},
|
2016-11-12 14:24:33 -08:00
|
|
|
IoError{recipe, ref io_error} => {
|
2016-11-11 21:37:16 -08:00
|
|
|
match io_error.kind() {
|
2016-11-07 21:01:27 -08:00
|
|
|
io::ErrorKind::NotFound => write!(f,
|
2017-04-23 16:09:34 -07:00
|
|
|
"Recipe `{}` could not be run because just could not find `sh`:\n{}",
|
2016-11-07 21:01:27 -08:00
|
|
|
recipe, io_error),
|
|
|
|
io::ErrorKind::PermissionDenied => write!(
|
|
|
|
f, "Recipe `{}` could not be run because just could not run `sh`:\n{}",
|
|
|
|
recipe, io_error),
|
|
|
|
_ => write!(f, "Recipe `{}` could not be run because of an IO error while \
|
|
|
|
launching `sh`:\n{}", recipe, io_error),
|
2016-11-11 21:37:16 -08:00
|
|
|
}?;
|
2016-10-05 13:58:18 -07:00
|
|
|
},
|
2016-11-12 14:24:33 -08:00
|
|
|
TmpdirIoError{recipe, ref io_error} =>
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "Recipe `{}` could not be run because of an IO error while trying \
|
|
|
|
to create a temporary directory or write a file to that directory`:\n{}",
|
|
|
|
recipe, io_error)?,
|
2017-04-23 14:21:21 -07:00
|
|
|
Backtick{ref token, ref output_error} => match *output_error {
|
|
|
|
OutputError::Code(code) => {
|
|
|
|
write!(f, "Backtick failed with exit code {}\n", code)?;
|
|
|
|
error_token = Some(token);
|
|
|
|
}
|
|
|
|
OutputError::Signal(signal) => {
|
|
|
|
write!(f, "Backtick was terminated by signal {}", signal)?;
|
|
|
|
error_token = Some(token);
|
|
|
|
}
|
|
|
|
OutputError::Unknown => {
|
|
|
|
write!(f, "Backtick failed for an unknown reason")?;
|
|
|
|
error_token = Some(token);
|
|
|
|
}
|
|
|
|
OutputError::Io(ref io_error) => {
|
|
|
|
match io_error.kind() {
|
|
|
|
io::ErrorKind::NotFound => write!(
|
2017-04-23 16:09:34 -07:00
|
|
|
f, "Backtick could not be run because just could not find `sh`:\n{}",
|
2017-04-23 14:21:21 -07:00
|
|
|
io_error),
|
|
|
|
io::ErrorKind::PermissionDenied => write!(
|
|
|
|
f, "Backtick could not be run because just could not run `sh`:\n{}", io_error),
|
|
|
|
_ => write!(f, "Backtick could not be run because of an IO \
|
|
|
|
error while launching `sh`:\n{}", io_error),
|
|
|
|
}?;
|
|
|
|
error_token = Some(token);
|
|
|
|
}
|
|
|
|
OutputError::Utf8(ref utf8_error) => {
|
|
|
|
write!(f, "Backtick succeeded but stdout was not utf8: {}", utf8_error)?;
|
|
|
|
error_token = Some(token);
|
|
|
|
}
|
|
|
|
},
|
2016-11-12 14:24:33 -08:00
|
|
|
InternalError{ref message} => {
|
2017-01-08 17:26:34 -08:00
|
|
|
write!(f, "Internal error, this may indicate a bug in just: {} \
|
2016-11-11 21:37:16 -08:00
|
|
|
consider filing an issue: https://github.com/casey/just/issues/new",
|
|
|
|
message)?;
|
2016-10-29 00:55:47 -07:00
|
|
|
}
|
2016-10-03 23:55:55 -07:00
|
|
|
}
|
2016-10-30 01:27:05 -07:00
|
|
|
|
2017-06-01 18:01:35 -07:00
|
|
|
write!(f, "{}", message.suffix())?;
|
2016-11-11 15:18:42 -08:00
|
|
|
|
|
|
|
if let Some(token) = error_token {
|
2016-11-11 21:37:16 -08:00
|
|
|
write_token_error_context(f, token)?;
|
2016-11-11 15:18:42 -08:00
|
|
|
}
|
|
|
|
|
2016-10-03 23:55:55 -07:00
|
|
|
Ok(())
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:27:05 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-10-16 18:59:49 -07:00
|
|
|
struct Token<'a> {
|
2016-10-22 23:18:26 -07:00
|
|
|
index: usize,
|
2016-10-16 18:59:49 -07:00
|
|
|
line: usize,
|
2016-10-22 23:18:26 -07:00
|
|
|
column: usize,
|
2016-10-23 16:43:52 -07:00
|
|
|
text: &'a str,
|
2016-10-16 18:59:49 -07:00
|
|
|
prefix: &'a str,
|
|
|
|
lexeme: &'a str,
|
2016-10-27 18:48:55 -07:00
|
|
|
kind: TokenKind,
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-10-22 23:18:26 -07:00
|
|
|
impl<'a> Token<'a> {
|
2016-11-12 14:24:33 -08:00
|
|
|
fn error(&self, kind: ErrorKind<'a>) -> CompileError<'a> {
|
|
|
|
CompileError {
|
2016-10-23 16:43:52 -07:00
|
|
|
text: self.text,
|
|
|
|
index: self.index + self.prefix.len(),
|
2016-10-22 23:18:26 -07:00
|
|
|
line: self.line,
|
2016-10-23 16:43:52 -07:00
|
|
|
column: self.column + self.prefix.len(),
|
|
|
|
width: Some(self.lexeme.len()),
|
2016-10-22 23:18:26 -07:00
|
|
|
kind: kind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 18:59:49 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
2016-10-23 18:46:04 -07:00
|
|
|
enum TokenKind {
|
2016-11-12 16:12:00 -08:00
|
|
|
At,
|
2016-10-29 01:58:30 -07:00
|
|
|
Backtick,
|
2016-10-16 18:59:49 -07:00
|
|
|
Colon,
|
|
|
|
Comment,
|
|
|
|
Dedent,
|
2016-10-29 01:58:30 -07:00
|
|
|
Eof,
|
|
|
|
Eol,
|
|
|
|
Equals,
|
|
|
|
Indent,
|
2016-10-26 20:54:44 -07:00
|
|
|
InterpolationEnd,
|
2016-10-29 01:58:30 -07:00
|
|
|
InterpolationStart,
|
2016-10-26 20:54:44 -07:00
|
|
|
Line,
|
2016-10-29 01:58:30 -07:00
|
|
|
Name,
|
|
|
|
Plus,
|
2016-10-30 16:56:22 -07:00
|
|
|
RawString,
|
2016-11-12 16:12:00 -08:00
|
|
|
StringToken,
|
2016-10-29 01:58:30 -07:00
|
|
|
Text,
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-10-23 18:46:04 -07:00
|
|
|
impl Display for TokenKind {
|
2016-10-23 16:43:52 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2016-11-11 21:37:16 -08:00
|
|
|
write!(f, "{}", match *self {
|
2016-10-29 01:58:30 -07:00
|
|
|
Backtick => "backtick",
|
2016-10-26 20:54:44 -07:00
|
|
|
Colon => "\":\"",
|
|
|
|
Comment => "comment",
|
|
|
|
Dedent => "dedent",
|
|
|
|
Eof => "end of file",
|
2016-10-29 01:58:30 -07:00
|
|
|
Eol => "end of line",
|
|
|
|
Equals => "\"=\"",
|
|
|
|
Indent => "indent",
|
|
|
|
InterpolationEnd => "}}",
|
|
|
|
InterpolationStart => "{{",
|
|
|
|
Line => "command",
|
|
|
|
Name => "name",
|
|
|
|
Plus => "\"+\"",
|
2016-11-12 16:12:00 -08:00
|
|
|
At => "\"@\"",
|
2016-10-29 01:58:30 -07:00
|
|
|
StringToken => "string",
|
2016-10-30 16:56:22 -07:00
|
|
|
RawString => "raw string",
|
2016-10-29 01:58:30 -07:00
|
|
|
Text => "command text",
|
2016-11-11 21:37:16 -08:00
|
|
|
})
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 18:46:04 -07:00
|
|
|
use TokenKind::*;
|
2016-10-16 18:59:49 -07:00
|
|
|
|
|
|
|
fn token(pattern: &str) -> Regex {
|
|
|
|
let mut s = String::new();
|
|
|
|
s += r"^(?m)([ \t]*)(";
|
|
|
|
s += pattern;
|
|
|
|
s += ")";
|
|
|
|
re(&s)
|
|
|
|
}
|
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
fn tokenize(text: &str) -> Result<Vec<Token>, CompileError> {
|
2016-10-22 23:18:26 -07:00
|
|
|
lazy_static! {
|
2016-10-30 14:37:03 -07:00
|
|
|
static ref BACKTICK: Regex = token(r"`[^`\n\r]*`" );
|
|
|
|
static ref COLON: Regex = token(r":" );
|
2016-11-12 16:12:00 -08:00
|
|
|
static ref AT: Regex = token(r"@" );
|
2016-10-30 14:37:03 -07:00
|
|
|
static ref COMMENT: Regex = token(r"#([^!].*)?$" );
|
|
|
|
static ref EOF: Regex = token(r"(?-m)$" );
|
|
|
|
static ref EOL: Regex = token(r"\n|\r\n" );
|
|
|
|
static ref EQUALS: Regex = token(r"=" );
|
|
|
|
static ref INTERPOLATION_END: Regex = token(r"[}][}]" );
|
|
|
|
static ref INTERPOLATION_START_TOKEN: Regex = token(r"[{][{]" );
|
2017-02-10 20:15:25 -08:00
|
|
|
static ref NAME: Regex = token(r"([a-zA-Z_][a-zA-Z0-9_-]*)" );
|
2016-10-30 14:37:03 -07:00
|
|
|
static ref PLUS: Regex = token(r"[+]" );
|
|
|
|
static ref STRING: Regex = token("\"" );
|
2016-11-13 14:04:20 -08:00
|
|
|
static ref RAW_STRING: Regex = token(r#"'[^']*'"# );
|
|
|
|
static ref UNTERMINATED_RAW_STRING: Regex = token(r#"'[^']*"# );
|
2016-10-27 09:44:07 -07:00
|
|
|
static ref INDENT: Regex = re(r"^([ \t]*)[^ \t\n\r]" );
|
|
|
|
static ref INTERPOLATION_START: Regex = re(r"^[{][{]" );
|
|
|
|
static ref LEADING_TEXT: Regex = re(r"^(?m)(.+?)[{][{]" );
|
2016-10-29 01:58:30 -07:00
|
|
|
static ref LINE: Regex = re(r"^(?m)[ \t]+[^ \t\n\r].*$");
|
2016-10-27 09:44:07 -07:00
|
|
|
static ref TEXT: Regex = re(r"^(?m)(.+)" );
|
2016-10-26 20:54:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum State<'a> {
|
|
|
|
Start,
|
|
|
|
Indent(&'a str),
|
|
|
|
Text,
|
|
|
|
Interpolation,
|
|
|
|
}
|
|
|
|
|
2016-10-16 18:59:49 -07:00
|
|
|
fn indentation(text: &str) -> Option<&str> {
|
2016-10-22 23:18:26 -07:00
|
|
|
INDENT.captures(text).map(|captures| captures.at(1).unwrap())
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-10-27 09:44:07 -07:00
|
|
|
let mut tokens = vec![];
|
|
|
|
let mut rest = text;
|
|
|
|
let mut index = 0;
|
|
|
|
let mut line = 0;
|
|
|
|
let mut column = 0;
|
|
|
|
let mut state = vec![State::Start];
|
2016-10-22 23:18:26 -07:00
|
|
|
|
|
|
|
macro_rules! error {
|
|
|
|
($kind:expr) => {{
|
2016-11-12 14:24:33 -08:00
|
|
|
Err(CompileError {
|
2016-10-22 23:18:26 -07:00
|
|
|
text: text,
|
|
|
|
index: index,
|
|
|
|
line: line,
|
|
|
|
column: column,
|
2016-10-23 16:43:52 -07:00
|
|
|
width: None,
|
2016-10-22 23:18:26 -07:00
|
|
|
kind: $kind,
|
|
|
|
})
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2016-10-16 18:59:49 -07:00
|
|
|
loop {
|
2016-10-22 23:18:26 -07:00
|
|
|
if column == 0 {
|
2016-10-27 18:48:55 -07:00
|
|
|
if let Some(kind) = match (state.last().unwrap(), indentation(rest)) {
|
2016-10-22 23:18:26 -07:00
|
|
|
// ignore: was no indentation and there still isn't
|
2016-10-23 20:39:50 -07:00
|
|
|
// or current line is blank
|
2016-10-26 20:54:44 -07:00
|
|
|
(&State::Start, Some("")) | (_, None) => {
|
2016-10-22 23:18:26 -07:00
|
|
|
None
|
|
|
|
}
|
|
|
|
// indent: was no indentation, now there is
|
2016-10-26 20:54:44 -07:00
|
|
|
(&State::Start, Some(current)) => {
|
2016-10-23 16:43:52 -07:00
|
|
|
if mixed_whitespace(current) {
|
|
|
|
return error!(ErrorKind::MixedLeadingWhitespace{whitespace: current})
|
|
|
|
}
|
2016-10-26 20:54:44 -07:00
|
|
|
//indent = Some(current);
|
|
|
|
state.push(State::Indent(current));
|
2016-10-16 18:59:49 -07:00
|
|
|
Some(Indent)
|
|
|
|
}
|
2016-10-22 23:18:26 -07:00
|
|
|
// dedent: there was indentation and now there isn't
|
2016-10-26 20:54:44 -07:00
|
|
|
(&State::Indent(_), Some("")) => {
|
|
|
|
// indent = None;
|
|
|
|
state.pop();
|
2016-10-22 23:18:26 -07:00
|
|
|
Some(Dedent)
|
|
|
|
}
|
|
|
|
// was indentation and still is, check if the new indentation matches
|
2016-10-26 20:54:44 -07:00
|
|
|
(&State::Indent(previous), Some(current)) => {
|
2016-10-16 18:59:49 -07:00
|
|
|
if !current.starts_with(previous) {
|
2016-10-22 23:18:26 -07:00
|
|
|
return error!(ErrorKind::InconsistentLeadingWhitespace{
|
|
|
|
expected: previous,
|
|
|
|
found: current
|
|
|
|
});
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2016-10-26 20:54:44 -07:00
|
|
|
// at column 0 in some other state: this should never happen
|
|
|
|
(&State::Text, _) | (&State::Interpolation, _) => {
|
|
|
|
return error!(ErrorKind::InternalError{
|
|
|
|
message: "unexpected state at column 0".to_string()
|
|
|
|
});
|
|
|
|
}
|
2016-10-16 18:59:49 -07:00
|
|
|
} {
|
|
|
|
tokens.push(Token {
|
2016-10-22 23:18:26 -07:00
|
|
|
index: index,
|
2016-10-16 18:59:49 -07:00
|
|
|
line: line,
|
2016-10-22 23:18:26 -07:00
|
|
|
column: column,
|
2016-10-23 16:43:52 -07:00
|
|
|
text: text,
|
2016-10-16 18:59:49 -07:00
|
|
|
prefix: "",
|
|
|
|
lexeme: "",
|
2016-10-27 18:48:55 -07:00
|
|
|
kind: kind,
|
2016-10-16 18:59:49 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
// insert a dedent if we're indented and we hit the end of the file
|
2016-10-28 00:06:36 -07:00
|
|
|
if &State::Start != state.last().unwrap() && EOF.is_match(rest) {
|
|
|
|
tokens.push(Token {
|
|
|
|
index: index,
|
|
|
|
line: line,
|
|
|
|
column: column,
|
|
|
|
text: text,
|
|
|
|
prefix: "",
|
|
|
|
lexeme: "",
|
|
|
|
kind: Dedent,
|
|
|
|
});
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
|
2017-04-02 20:39:23 -07:00
|
|
|
let (prefix, lexeme, kind) =
|
|
|
|
if let (0, &State::Indent(indent), Some(captures)) =
|
2016-11-07 21:01:27 -08:00
|
|
|
(column, state.last().unwrap(), LINE.captures(rest)) {
|
2016-10-16 18:59:49 -07:00
|
|
|
let line = captures.at(0).unwrap();
|
|
|
|
if !line.starts_with(indent) {
|
2016-10-22 23:18:26 -07:00
|
|
|
return error!(ErrorKind::InternalError{message: "unexpected indent".to_string()});
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
2016-10-26 20:54:44 -07:00
|
|
|
state.push(State::Text);
|
|
|
|
(&line[0..indent.len()], "", Line)
|
|
|
|
} else if let Some(captures) = EOF.captures(rest) {
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Eof)
|
2016-10-28 00:06:36 -07:00
|
|
|
} else if let State::Text = *state.last().unwrap() {
|
2016-10-26 20:54:44 -07:00
|
|
|
if let Some(captures) = INTERPOLATION_START.captures(rest) {
|
|
|
|
state.push(State::Interpolation);
|
|
|
|
("", captures.at(0).unwrap(), InterpolationStart)
|
|
|
|
} else if let Some(captures) = LEADING_TEXT.captures(rest) {
|
|
|
|
("", captures.at(1).unwrap(), Text)
|
|
|
|
} else if let Some(captures) = TEXT.captures(rest) {
|
|
|
|
("", captures.at(1).unwrap(), Text)
|
|
|
|
} else if let Some(captures) = EOL.captures(rest) {
|
|
|
|
state.pop();
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Eol)
|
|
|
|
} else {
|
|
|
|
return error!(ErrorKind::InternalError{
|
|
|
|
message: format!("Could not match token in text state: \"{}\"", rest)
|
|
|
|
});
|
|
|
|
}
|
2016-10-27 09:44:07 -07:00
|
|
|
} else if let Some(captures) = INTERPOLATION_START_TOKEN.captures(rest) {
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), InterpolationStart)
|
2016-10-26 20:54:44 -07:00
|
|
|
} else if let Some(captures) = INTERPOLATION_END.captures(rest) {
|
2016-10-27 09:44:07 -07:00
|
|
|
if state.last().unwrap() == &State::Interpolation {
|
|
|
|
state.pop();
|
2016-10-26 20:54:44 -07:00
|
|
|
}
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), InterpolationEnd)
|
2016-10-22 23:18:26 -07:00
|
|
|
} else if let Some(captures) = NAME.captures(rest) {
|
2016-10-16 18:59:49 -07:00
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Name)
|
2016-10-22 23:18:26 -07:00
|
|
|
} else if let Some(captures) = EOL.captures(rest) {
|
2016-10-26 20:54:44 -07:00
|
|
|
if state.last().unwrap() == &State::Interpolation {
|
2017-04-02 20:39:23 -07:00
|
|
|
return error!(ErrorKind::InternalError {
|
2016-10-29 22:56:47 -07:00
|
|
|
message: "hit EOL while still in interpolation state".to_string()
|
|
|
|
});
|
2016-10-26 20:54:44 -07:00
|
|
|
}
|
2016-10-16 18:59:49 -07:00
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Eol)
|
2016-10-29 01:58:30 -07:00
|
|
|
} else if let Some(captures) = BACKTICK.captures(rest) {
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Backtick)
|
2016-10-22 23:18:26 -07:00
|
|
|
} else if let Some(captures) = COLON.captures(rest) {
|
2016-10-16 18:59:49 -07:00
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Colon)
|
2016-11-12 16:12:00 -08:00
|
|
|
} else if let Some(captures) = AT.captures(rest) {
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), At)
|
2016-10-25 19:11:58 -07:00
|
|
|
} else if let Some(captures) = PLUS.captures(rest) {
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Plus)
|
2016-10-22 23:18:26 -07:00
|
|
|
} else if let Some(captures) = EQUALS.captures(rest) {
|
2016-10-16 18:59:49 -07:00
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Equals)
|
2016-10-22 23:18:26 -07:00
|
|
|
} else if let Some(captures) = COMMENT.captures(rest) {
|
2016-10-16 18:59:49 -07:00
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), Comment)
|
2016-10-30 16:56:22 -07:00
|
|
|
} else if let Some(captures) = RAW_STRING.captures(rest) {
|
|
|
|
(captures.at(1).unwrap(), captures.at(2).unwrap(), RawString)
|
2016-11-13 14:04:20 -08:00
|
|
|
} else if UNTERMINATED_RAW_STRING.is_match(rest) {
|
|
|
|
return error!(ErrorKind::UnterminatedString);
|
2016-10-25 19:11:58 -07:00
|
|
|
} else if let Some(captures) = STRING.captures(rest) {
|
2016-10-28 00:06:36 -07:00
|
|
|
let prefix = captures.at(1).unwrap();
|
|
|
|
let contents = &rest[prefix.len()+1..];
|
|
|
|
if contents.is_empty() {
|
|
|
|
return error!(ErrorKind::UnterminatedString);
|
|
|
|
}
|
|
|
|
let mut len = 0;
|
|
|
|
let mut escape = false;
|
2017-04-02 20:39:23 -07:00
|
|
|
for c in contents.chars() {
|
2016-10-28 00:06:36 -07:00
|
|
|
if c == '\n' || c == '\r' {
|
|
|
|
return error!(ErrorKind::UnterminatedString);
|
|
|
|
} else if !escape && c == '"' {
|
|
|
|
break;
|
|
|
|
} else if !escape && c == '\\' {
|
|
|
|
escape = true;
|
|
|
|
} else if escape {
|
|
|
|
escape = false;
|
|
|
|
}
|
|
|
|
len += c.len_utf8();
|
|
|
|
}
|
|
|
|
let start = prefix.len();
|
|
|
|
let content_end = start + len + 1;
|
|
|
|
if escape || content_end >= rest.len() {
|
|
|
|
return error!(ErrorKind::UnterminatedString);
|
|
|
|
}
|
|
|
|
(prefix, &rest[start..content_end + 1], StringToken)
|
2016-10-25 19:11:58 -07:00
|
|
|
} else if rest.starts_with("#!") {
|
|
|
|
return error!(ErrorKind::OuterShebang)
|
2016-10-16 18:59:49 -07:00
|
|
|
} else {
|
2016-10-25 19:11:58 -07:00
|
|
|
return error!(ErrorKind::UnknownStartOfToken)
|
2016-10-16 18:59:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
tokens.push(Token {
|
2016-10-22 23:18:26 -07:00
|
|
|
index: index,
|
|
|
|
line: line,
|
|
|
|
column: column,
|
2016-10-16 18:59:49 -07:00
|
|
|
prefix: prefix,
|
2016-10-23 16:43:52 -07:00
|
|
|
text: text,
|
2016-10-16 18:59:49 -07:00
|
|
|
lexeme: lexeme,
|
2016-10-27 18:48:55 -07:00
|
|
|
kind: kind,
|
2016-10-16 18:59:49 -07:00
|
|
|
});
|
|
|
|
|
2016-11-13 14:04:20 -08:00
|
|
|
let len = prefix.len() + lexeme.len();
|
|
|
|
|
2016-10-26 20:54:44 -07:00
|
|
|
if len == 0 {
|
2016-10-29 22:56:47 -07:00
|
|
|
let last = tokens.last().unwrap();
|
|
|
|
match last.kind {
|
2016-10-26 20:54:44 -07:00
|
|
|
Eof => {},
|
2016-10-29 22:56:47 -07:00
|
|
|
_ => return Err(last.error(ErrorKind::InternalError{
|
|
|
|
message: format!("zero length token: {:?}", last)
|
|
|
|
})),
|
2016-10-26 20:54:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:48:55 -07:00
|
|
|
match tokens.last().unwrap().kind {
|
2016-10-16 18:59:49 -07:00
|
|
|
Eol => {
|
|
|
|
line += 1;
|
2016-10-22 23:18:26 -07:00
|
|
|
column = 0;
|
2016-11-13 14:04:20 -08:00
|
|
|
}
|
2016-10-16 18:59:49 -07:00
|
|
|
Eof => {
|
|
|
|
break;
|
2016-11-13 14:04:20 -08:00
|
|
|
}
|
|
|
|
RawString => {
|
|
|
|
let lexeme_lines = lexeme.lines().count();
|
|
|
|
line += lexeme_lines - 1;
|
|
|
|
if lexeme_lines == 1 {
|
|
|
|
column += len;
|
|
|
|
} else {
|
|
|
|
column = lexeme.lines().last().unwrap().len();
|
|
|
|
}
|
|
|
|
}
|
2016-10-16 18:59:49 -07:00
|
|
|
_ => {
|
2016-10-22 23:18:26 -07:00
|
|
|
column += len;
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rest = &rest[len..];
|
2016-10-22 23:18:26 -07:00
|
|
|
index += len;
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tokens)
|
|
|
|
}
|
|
|
|
|
2016-11-16 20:17:24 -08:00
|
|
|
fn compile(text: &str) -> Result<Justfile, CompileError> {
|
2016-11-11 21:37:16 -08:00
|
|
|
let tokens = tokenize(text)?;
|
2016-10-30 16:15:18 -07:00
|
|
|
let parser = Parser {
|
2016-10-30 16:34:39 -07:00
|
|
|
text: text,
|
2016-11-12 23:31:19 -08:00
|
|
|
tokens: itertools::put_back(tokens),
|
|
|
|
recipes: empty(),
|
|
|
|
assignments: empty(),
|
|
|
|
assignment_tokens: empty(),
|
|
|
|
exports: empty(),
|
2016-10-22 23:18:26 -07:00
|
|
|
};
|
2016-11-16 20:17:24 -08:00
|
|
|
parser.justfile()
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-10-22 23:18:26 -07:00
|
|
|
struct Parser<'a> {
|
2016-10-30 16:34:39 -07:00
|
|
|
text: &'a str,
|
|
|
|
tokens: itertools::PutBack<std::vec::IntoIter<Token<'a>>>,
|
|
|
|
recipes: Map<&'a str, Recipe<'a>>,
|
|
|
|
assignments: Map<&'a str, Expression<'a>>,
|
|
|
|
assignment_tokens: Map<&'a str, Token<'a>>,
|
|
|
|
exports: Set<&'a str>,
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-10-22 23:18:26 -07:00
|
|
|
impl<'a> Parser<'a> {
|
2016-10-27 18:48:55 -07:00
|
|
|
fn peek(&mut self, kind: TokenKind) -> bool {
|
2016-10-30 16:15:18 -07:00
|
|
|
let next = self.tokens.next().unwrap();
|
|
|
|
let result = next.kind == kind;
|
|
|
|
self.tokens.put_back(next);
|
|
|
|
result
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
|
2016-10-27 18:48:55 -07:00
|
|
|
fn accept(&mut self, kind: TokenKind) -> Option<Token<'a>> {
|
|
|
|
if self.peek(kind) {
|
2016-10-22 23:18:26 -07:00
|
|
|
self.tokens.next()
|
2016-10-16 18:59:49 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:15:13 -08:00
|
|
|
fn accept_any(&mut self, kinds: &[TokenKind]) -> Option<Token<'a>> {
|
|
|
|
for kind in kinds {
|
|
|
|
if self.peek(*kind) {
|
|
|
|
return self.tokens.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:48:55 -07:00
|
|
|
fn accepted(&mut self, kind: TokenKind) -> bool {
|
|
|
|
self.accept(kind).is_some()
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-10-27 18:48:55 -07:00
|
|
|
fn expect(&mut self, kind: TokenKind) -> Option<Token<'a>> {
|
|
|
|
if self.peek(kind) {
|
2016-10-23 16:43:52 -07:00
|
|
|
self.tokens.next();
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.tokens.next()
|
|
|
|
}
|
2016-10-22 23:18:26 -07:00
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
fn expect_eol(&mut self) -> Option<Token<'a>> {
|
2016-11-12 23:31:19 -08:00
|
|
|
self.accepted(Comment);
|
2016-10-23 16:43:52 -07:00
|
|
|
if self.peek(Eol) {
|
|
|
|
self.accept(Eol);
|
|
|
|
None
|
|
|
|
} else if self.peek(Eof) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.tokens.next()
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
fn unexpected_token(&self, found: &Token<'a>, expected: &[TokenKind]) -> CompileError<'a> {
|
2016-10-25 19:11:58 -07:00
|
|
|
found.error(ErrorKind::UnexpectedToken {
|
|
|
|
expected: expected.to_vec(),
|
2016-10-27 18:48:55 -07:00
|
|
|
found: found.kind,
|
2016-10-25 19:11:58 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-12 23:31:19 -08:00
|
|
|
fn recipe(
|
|
|
|
&mut self,
|
|
|
|
name: Token<'a>,
|
|
|
|
doc: Option<Token<'a>>,
|
|
|
|
quiet: bool,
|
|
|
|
) -> Result<(), CompileError<'a>> {
|
2016-10-30 16:34:39 -07:00
|
|
|
if let Some(recipe) = self.recipes.get(name.lexeme) {
|
|
|
|
return Err(name.error(ErrorKind::DuplicateRecipe {
|
|
|
|
recipe: recipe.name,
|
|
|
|
first: recipe.line_number
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:15:13 -08:00
|
|
|
let mut parsed_parameter_with_default = false;
|
2016-11-18 07:03:34 -08:00
|
|
|
let mut parsed_variadic_parameter = false;
|
2016-11-12 09:15:13 -08:00
|
|
|
let mut parameters: Vec<Parameter> = vec![];
|
2016-11-18 07:03:34 -08:00
|
|
|
loop {
|
|
|
|
let plus = self.accept(Plus);
|
|
|
|
|
|
|
|
let parameter = match self.accept(Name) {
|
|
|
|
Some(parameter) => parameter,
|
|
|
|
None => if let Some(plus) = plus {
|
|
|
|
return Err(self.unexpected_token(&plus, &[Name]));
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let variadic = plus.is_some();
|
|
|
|
|
|
|
|
if parsed_variadic_parameter {
|
|
|
|
return Err(parameter.error(ErrorKind::ParameterFollowsVariadicParameter {
|
|
|
|
parameter: parameter.lexeme,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:15:13 -08:00
|
|
|
if parameters.iter().any(|p| p.name == parameter.lexeme) {
|
2016-10-31 19:11:27 -07:00
|
|
|
return Err(parameter.error(ErrorKind::DuplicateParameter {
|
|
|
|
recipe: name.lexeme, parameter: parameter.lexeme
|
2016-10-23 16:43:52 -07:00
|
|
|
}));
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
2017-04-02 20:39:23 -07:00
|
|
|
|
2016-11-12 09:15:13 -08:00
|
|
|
let default;
|
|
|
|
if self.accepted(Equals) {
|
|
|
|
if let Some(string) = self.accept_any(&[StringToken, RawString]) {
|
|
|
|
default = Some(cook_string(&string)?.cooked);
|
|
|
|
} else {
|
|
|
|
let unexpected = self.tokens.next().unwrap();
|
|
|
|
return Err(self.unexpected_token(&unexpected, &[StringToken, RawString]));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
default = None
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsed_parameter_with_default && default.is_none() {
|
|
|
|
return Err(parameter.error(ErrorKind::RequiredParameterFollowsDefaultParameter{
|
|
|
|
parameter: parameter.lexeme,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed_parameter_with_default |= default.is_some();
|
2016-11-18 07:03:34 -08:00
|
|
|
parsed_variadic_parameter = variadic;
|
2016-11-12 09:15:13 -08:00
|
|
|
|
|
|
|
parameters.push(Parameter {
|
2016-11-18 07:03:34 -08:00
|
|
|
default: default,
|
|
|
|
name: parameter.lexeme,
|
|
|
|
token: parameter,
|
|
|
|
variadic: variadic,
|
2016-11-12 09:15:13 -08:00
|
|
|
});
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
if let Some(token) = self.expect(Colon) {
|
2016-10-31 19:11:27 -07:00
|
|
|
// if we haven't accepted any parameters, an equals
|
2016-10-26 20:54:44 -07:00
|
|
|
// would have been fine as part of an assignment
|
2016-10-31 19:11:27 -07:00
|
|
|
if parameters.is_empty() {
|
2016-11-18 07:03:34 -08:00
|
|
|
return Err(self.unexpected_token(&token, &[Name, Plus, Colon, Equals]));
|
2016-10-25 19:11:58 -07:00
|
|
|
} else {
|
2016-11-18 07:03:34 -08:00
|
|
|
return Err(self.unexpected_token(&token, &[Name, Plus, Colon]));
|
2016-10-25 19:11:58 -07:00
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-10-16 18:59:49 -07:00
|
|
|
|
|
|
|
let mut dependencies = vec![];
|
2016-10-23 16:43:52 -07:00
|
|
|
let mut dependency_tokens = vec![];
|
|
|
|
while let Some(dependency) = self.accept(Name) {
|
|
|
|
if dependencies.contains(&dependency.lexeme) {
|
|
|
|
return Err(dependency.error(ErrorKind::DuplicateDependency {
|
2016-10-30 16:34:39 -07:00
|
|
|
recipe: name.lexeme,
|
2016-10-23 16:43:52 -07:00
|
|
|
dependency: dependency.lexeme
|
|
|
|
}));
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
dependencies.push(dependency.lexeme);
|
|
|
|
dependency_tokens.push(dependency);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(token) = self.expect_eol() {
|
|
|
|
return Err(self.unexpected_token(&token, &[Name, Eol, Eof]));
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:45:12 -08:00
|
|
|
let mut lines: Vec<Vec<Fragment>> = vec![];
|
2016-10-27 00:13:10 -07:00
|
|
|
let mut shebang = false;
|
2016-10-26 22:04:12 -07:00
|
|
|
|
|
|
|
if self.accepted(Indent) {
|
|
|
|
while !self.accepted(Dedent) {
|
2016-10-27 00:13:10 -07:00
|
|
|
if self.accepted(Eol) {
|
2016-11-16 21:37:43 -08:00
|
|
|
lines.push(vec![]);
|
2016-10-27 00:13:10 -07:00
|
|
|
continue;
|
|
|
|
}
|
2016-10-26 22:04:12 -07:00
|
|
|
if let Some(token) = self.expect(Line) {
|
|
|
|
return Err(token.error(ErrorKind::InternalError{
|
2016-10-27 18:48:55 -07:00
|
|
|
message: format!("Expected a line but got {}", token.kind)
|
2016-10-26 22:04:12 -07:00
|
|
|
}))
|
|
|
|
}
|
2016-11-12 15:45:12 -08:00
|
|
|
let mut fragments = vec![];
|
2016-10-26 22:04:12 -07:00
|
|
|
|
2016-10-27 00:13:10 -07:00
|
|
|
while !(self.accepted(Eol) || self.peek(Dedent)) {
|
2016-10-26 22:04:12 -07:00
|
|
|
if let Some(token) = self.accept(Text) {
|
2016-11-12 15:45:12 -08:00
|
|
|
if fragments.is_empty() {
|
2016-10-27 09:44:07 -07:00
|
|
|
if lines.is_empty() {
|
2016-10-27 00:13:10 -07:00
|
|
|
if token.lexeme.starts_with("#!") {
|
|
|
|
shebang = true;
|
|
|
|
}
|
2017-04-02 20:39:23 -07:00
|
|
|
} else if !shebang
|
2016-11-12 15:45:12 -08:00
|
|
|
&& !lines.last().and_then(|line| line.last())
|
|
|
|
.map(Fragment::continuation).unwrap_or(false)
|
|
|
|
&& (token.lexeme.starts_with(' ') || token.lexeme.starts_with('\t')) {
|
2016-10-27 00:13:10 -07:00
|
|
|
return Err(token.error(ErrorKind::ExtraLeadingWhitespace));
|
|
|
|
}
|
|
|
|
}
|
2016-11-12 15:45:12 -08:00
|
|
|
fragments.push(Fragment::Text{text: token});
|
2016-10-26 22:04:12 -07:00
|
|
|
} else if let Some(token) = self.expect(InterpolationStart) {
|
|
|
|
return Err(self.unexpected_token(&token, &[Text, InterpolationStart, Eol]));
|
|
|
|
} else {
|
2016-11-12 15:45:12 -08:00
|
|
|
fragments.push(Fragment::Expression{
|
2016-11-11 21:37:16 -08:00
|
|
|
expression: self.expression(true)?
|
2016-10-27 18:01:07 -07:00
|
|
|
});
|
2016-10-26 22:04:12 -07:00
|
|
|
if let Some(token) = self.expect(InterpolationEnd) {
|
|
|
|
return Err(self.unexpected_token(&token, &[InterpolationEnd]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 15:45:12 -08:00
|
|
|
lines.push(fragments);
|
2016-10-26 22:04:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 16:34:39 -07:00
|
|
|
self.recipes.insert(name.lexeme, Recipe {
|
|
|
|
line_number: name.line,
|
|
|
|
name: name.lexeme,
|
2016-11-12 23:31:19 -08:00
|
|
|
doc: doc.map(|t| t.lexeme[1..].trim()),
|
2016-10-23 16:43:52 -07:00
|
|
|
dependencies: dependencies,
|
|
|
|
dependency_tokens: dependency_tokens,
|
2016-10-31 19:11:27 -07:00
|
|
|
parameters: parameters,
|
2016-10-27 18:48:55 -07:00
|
|
|
lines: lines,
|
2016-10-23 16:43:52 -07:00
|
|
|
shebang: shebang,
|
2016-11-12 16:12:00 -08:00
|
|
|
quiet: quiet,
|
2016-10-30 16:34:39 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
2016-10-16 18:59:49 -07:00
|
|
|
}
|
2016-10-22 23:18:26 -07:00
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
fn expression(&mut self, interpolation: bool) -> Result<Expression<'a>, CompileError<'a>> {
|
2016-10-25 19:11:58 -07:00
|
|
|
let first = self.tokens.next().unwrap();
|
2016-10-27 18:48:55 -07:00
|
|
|
let lhs = match first.kind {
|
2016-10-30 16:56:22 -07:00
|
|
|
Name => Expression::Variable {name: first.lexeme, token: first},
|
|
|
|
Backtick => Expression::Backtick {
|
2016-10-29 23:39:12 -07:00
|
|
|
raw: &first.lexeme[1..first.lexeme.len()-1],
|
|
|
|
token: first
|
|
|
|
},
|
2016-11-12 09:15:13 -08:00
|
|
|
RawString | StringToken => {
|
|
|
|
Expression::String{cooked_string: cook_string(&first)?}
|
2016-10-28 00:06:36 -07:00
|
|
|
}
|
|
|
|
_ => return Err(self.unexpected_token(&first, &[Name, StringToken])),
|
2016-10-25 19:11:58 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if self.accepted(Plus) {
|
2016-11-11 21:37:16 -08:00
|
|
|
let rhs = self.expression(interpolation)?;
|
2016-10-25 19:11:58 -07:00
|
|
|
Ok(Expression::Concatination{lhs: Box::new(lhs), rhs: Box::new(rhs)})
|
2016-10-26 22:04:12 -07:00
|
|
|
} else if interpolation && self.peek(InterpolationEnd) {
|
|
|
|
Ok(lhs)
|
2016-10-25 19:11:58 -07:00
|
|
|
} else if let Some(token) = self.expect_eol() {
|
2016-10-26 22:04:12 -07:00
|
|
|
if interpolation {
|
2016-10-27 00:13:10 -07:00
|
|
|
return Err(self.unexpected_token(&token, &[Plus, Eol, InterpolationEnd]))
|
2016-10-26 22:04:12 -07:00
|
|
|
} else {
|
|
|
|
Err(self.unexpected_token(&token, &[Plus, Eol]))
|
|
|
|
}
|
2016-10-25 19:11:58 -07:00
|
|
|
} else {
|
|
|
|
Ok(lhs)
|
|
|
|
}
|
2016-10-22 23:18:26 -07:00
|
|
|
}
|
2016-10-16 18:59:49 -07:00
|
|
|
|
2016-11-12 14:24:33 -08:00
|
|
|
fn assignment(&mut self, name: Token<'a>, export: bool) -> Result<(), CompileError<'a>> {
|
2016-10-30 16:34:39 -07:00
|
|
|
if self.assignments.contains_key(name.lexeme) {
|
|
|
|
return Err(name.error(ErrorKind::DuplicateVariable {variable: name.lexeme}));
|
|
|
|
}
|
|
|
|
if export {
|
|
|
|
self.exports.insert(name.lexeme);
|
|
|
|
}
|
2016-11-11 21:37:16 -08:00
|
|
|
let expression = self.expression(false)?;
|
2016-10-30 16:34:39 -07:00
|
|
|
self.assignments.insert(name.lexeme, expression);
|
|
|
|
self.assignment_tokens.insert(name.lexeme, name);
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-10-22 23:18:26 -07:00
|
|
|
|
2016-11-16 20:17:24 -08:00
|
|
|
fn justfile(mut self) -> Result<Justfile<'a>, CompileError<'a>> {
|
2016-11-12 23:31:19 -08:00
|
|
|
let mut doc = None;
|
2016-10-22 23:18:26 -07:00
|
|
|
loop {
|
|
|
|
match self.tokens.next() {
|
2016-10-27 18:48:55 -07:00
|
|
|
Some(token) => match token.kind {
|
2016-10-22 23:18:26 -07:00
|
|
|
Eof => break,
|
2016-11-12 23:31:19 -08:00
|
|
|
Eol => {
|
|
|
|
doc = None;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Comment => {
|
|
|
|
if let Some(token) = self.expect_eol() {
|
|
|
|
return Err(token.error(ErrorKind::InternalError {
|
|
|
|
message: format!("found comment followed by {}", token.kind),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
doc = Some(token);
|
|
|
|
}
|
2016-11-12 16:12:00 -08:00
|
|
|
At => if let Some(name) = self.accept(Name) {
|
2016-11-12 23:31:19 -08:00
|
|
|
self.recipe(name, doc, true)?;
|
|
|
|
doc = None;
|
2016-11-12 16:12:00 -08:00
|
|
|
} else {
|
|
|
|
let unexpected = &self.tokens.next().unwrap();
|
|
|
|
return Err(self.unexpected_token(unexpected, &[Name]));
|
|
|
|
},
|
2016-10-30 16:15:18 -07:00
|
|
|
Name => if token.lexeme == "export" {
|
|
|
|
let next = self.tokens.next().unwrap();
|
|
|
|
if next.kind == Name && self.accepted(Equals) {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.assignment(next, true)?;
|
2016-11-12 23:31:19 -08:00
|
|
|
doc = None;
|
2016-10-30 16:15:18 -07:00
|
|
|
} else {
|
|
|
|
self.tokens.put_back(next);
|
2016-11-12 23:31:19 -08:00
|
|
|
self.recipe(token, doc, false)?;
|
|
|
|
doc = None;
|
2016-10-30 16:15:18 -07:00
|
|
|
}
|
|
|
|
} else if self.accepted(Equals) {
|
2016-11-11 21:37:16 -08:00
|
|
|
self.assignment(token, false)?;
|
2016-11-12 23:31:19 -08:00
|
|
|
doc = None;
|
2016-10-23 16:43:52 -07:00
|
|
|
} else {
|
2016-11-12 23:31:19 -08:00
|
|
|
self.recipe(token, doc, false)?;
|
|
|
|
doc = None;
|
2016-10-23 16:43:52 -07:00
|
|
|
},
|
2016-12-10 16:31:38 -08:00
|
|
|
_ => return Err(self.unexpected_token(&token, &[Name, At])),
|
2016-10-22 23:18:26 -07:00
|
|
|
},
|
2016-11-12 14:24:33 -08:00
|
|
|
None => return Err(CompileError {
|
2016-10-22 23:18:26 -07:00
|
|
|
text: self.text,
|
|
|
|
index: 0,
|
|
|
|
line: 0,
|
|
|
|
column: 0,
|
2016-10-23 16:43:52 -07:00
|
|
|
width: None,
|
2016-10-22 23:18:26 -07:00
|
|
|
kind: ErrorKind::InternalError {
|
|
|
|
message: "unexpected end of token stream".to_string()
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2016-10-16 18:59:49 -07:00
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
if let Some(token) = self.tokens.next() {
|
|
|
|
return Err(token.error(ErrorKind::InternalError{
|
2016-10-27 18:48:55 -07:00
|
|
|
message: format!("unexpected token remaining after parsing completed: {:?}", token.kind)
|
2016-10-22 23:18:26 -07:00
|
|
|
}))
|
2016-10-06 17:43:30 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
resolve_recipes(&self.recipes, &self.assignments, self.text)?;
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2016-10-30 16:34:39 -07:00
|
|
|
for recipe in self.recipes.values() {
|
2016-11-12 09:15:13 -08:00
|
|
|
for parameter in &recipe.parameters {
|
|
|
|
if self.assignments.contains_key(parameter.token.lexeme) {
|
|
|
|
return Err(parameter.token.error(ErrorKind::ParameterShadowsVariable {
|
|
|
|
parameter: parameter.token.lexeme
|
2016-10-25 19:11:58 -07:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 00:13:10 -07:00
|
|
|
|
2016-10-29 00:14:41 -07:00
|
|
|
for dependency in &recipe.dependency_tokens {
|
2016-10-31 19:11:27 -07:00
|
|
|
if !self.recipes[dependency.lexeme].parameters.is_empty() {
|
|
|
|
return Err(dependency.error(ErrorKind::DependencyHasParameters {
|
2016-10-29 00:14:41 -07:00
|
|
|
recipe: recipe.name,
|
|
|
|
dependency: dependency.lexeme,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 21:37:16 -08:00
|
|
|
resolve_assignments(&self.assignments, &self.assignment_tokens)?;
|
2016-10-29 20:39:21 -07:00
|
|
|
|
2016-10-27 18:48:55 -07:00
|
|
|
Ok(Justfile {
|
2016-10-30 16:34:39 -07:00
|
|
|
recipes: self.recipes,
|
|
|
|
assignments: self.assignments,
|
|
|
|
exports: self.exports,
|
2016-10-25 19:11:58 -07:00
|
|
|
})
|
2016-10-02 22:30:28 -07:00
|
|
|
}
|
|
|
|
}
|