2017-11-17 10:30:08 +03:00
|
|
|
use common::*;
|
|
|
|
|
2017-12-03 01:59:07 +03:00
|
|
|
use misc::{Or, write_error_context, show_whitespace, maybe_s};
|
2017-11-17 10:30:08 +03:00
|
|
|
|
2017-11-18 04:28:06 +03:00
|
|
|
pub type CompilationResult<'a, T> = Result<T, CompilationError<'a>>;
|
|
|
|
|
2017-11-17 10:30:08 +03:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct CompilationError<'a> {
|
|
|
|
pub text: &'a str,
|
|
|
|
pub index: usize,
|
|
|
|
pub line: usize,
|
|
|
|
pub column: usize,
|
|
|
|
pub width: Option<usize>,
|
|
|
|
pub kind: CompilationErrorKind<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum CompilationErrorKind<'a> {
|
|
|
|
CircularRecipeDependency{recipe: &'a str, circle: Vec<&'a str>},
|
|
|
|
CircularVariableDependency{variable: &'a str, circle: Vec<&'a str>},
|
|
|
|
DependencyHasParameters{recipe: &'a str, dependency: &'a str},
|
|
|
|
DuplicateDependency{recipe: &'a str, dependency: &'a str},
|
|
|
|
DuplicateParameter{recipe: &'a str, parameter: &'a str},
|
|
|
|
DuplicateRecipe{recipe: &'a str, first: usize},
|
|
|
|
DuplicateVariable{variable: &'a str},
|
|
|
|
ExtraLeadingWhitespace,
|
2017-12-03 01:59:07 +03:00
|
|
|
FunctionArgumentCountMismatch{function: &'a str, found: usize, expected: usize},
|
2017-11-17 10:30:08 +03:00
|
|
|
InconsistentLeadingWhitespace{expected: &'a str, found: &'a str},
|
|
|
|
Internal{message: String},
|
|
|
|
InvalidEscapeSequence{character: char},
|
|
|
|
MixedLeadingWhitespace{whitespace: &'a str},
|
|
|
|
OuterShebang,
|
2017-12-02 16:37:10 +03:00
|
|
|
ParameterFollowsVariadicParameter{parameter: &'a str},
|
2017-11-17 10:30:08 +03:00
|
|
|
ParameterShadowsVariable{parameter: &'a str},
|
|
|
|
RequiredParameterFollowsDefaultParameter{parameter: &'a str},
|
|
|
|
UndefinedVariable{variable: &'a str},
|
|
|
|
UnexpectedToken{expected: Vec<TokenKind>, found: TokenKind},
|
|
|
|
UnknownDependency{recipe: &'a str, unknown: &'a str},
|
2017-12-02 16:37:10 +03:00
|
|
|
UnknownFunction{function: &'a str},
|
2017-11-17 10:30:08 +03:00
|
|
|
UnknownStartOfToken,
|
2017-12-02 23:49:31 +03:00
|
|
|
UnterminatedInterpolation,
|
2017-11-17 10:30:08 +03:00
|
|
|
UnterminatedString,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display for CompilationError<'a> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
use CompilationErrorKind::*;
|
|
|
|
let error = Color::fmt(f).error();
|
|
|
|
let message = Color::fmt(f).message();
|
|
|
|
|
|
|
|
write!(f, "{} {}", error.paint("error:"), message.prefix())?;
|
|
|
|
|
|
|
|
match self.kind {
|
|
|
|
CircularRecipeDependency{recipe, ref circle} => {
|
|
|
|
if circle.len() == 2 {
|
|
|
|
writeln!(f, "Recipe `{}` depends on itself", recipe)?;
|
|
|
|
} else {
|
|
|
|
writeln!(f, "Recipe `{}` has circular dependency `{}`",
|
|
|
|
recipe, circle.join(" -> "))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CircularVariableDependency{variable, ref circle} => {
|
|
|
|
if circle.len() == 2 {
|
|
|
|
writeln!(f, "Variable `{}` is defined in terms of itself", variable)?;
|
|
|
|
} else {
|
|
|
|
writeln!(f, "Variable `{}` depends on its own value: `{}`",
|
|
|
|
variable, circle.join(" -> "))?;
|
|
|
|
}
|
|
|
|
}
|
2018-07-01 05:19:13 +03:00
|
|
|
|
2017-11-17 10:30:08 +03:00
|
|
|
InvalidEscapeSequence{character} => {
|
2018-07-01 05:19:13 +03:00
|
|
|
let representation = match character {
|
|
|
|
'`' => r"\`".to_string(),
|
|
|
|
'\\' => r"\".to_string(),
|
|
|
|
'\'' => r"'".to_string(),
|
|
|
|
'"' => r#"""#.to_string(),
|
|
|
|
_ => character.escape_default().collect(),
|
|
|
|
};
|
|
|
|
writeln!(f, "`\\{}` is not a valid escape sequence", representation)?;
|
2017-11-17 10:30:08 +03:00
|
|
|
}
|
|
|
|
DuplicateParameter{recipe, parameter} => {
|
|
|
|
writeln!(f, "Recipe `{}` has duplicate parameter `{}`", recipe, parameter)?;
|
|
|
|
}
|
|
|
|
DuplicateVariable{variable} => {
|
|
|
|
writeln!(f, "Variable `{}` has multiple definitions", variable)?;
|
|
|
|
}
|
|
|
|
UnexpectedToken{ref expected, found} => {
|
|
|
|
writeln!(f, "Expected {}, but found {}", Or(expected), found)?;
|
|
|
|
}
|
|
|
|
DuplicateDependency{recipe, dependency} => {
|
|
|
|
writeln!(f, "Recipe `{}` has duplicate dependency `{}`", recipe, dependency)?;
|
|
|
|
}
|
|
|
|
DuplicateRecipe{recipe, first} => {
|
|
|
|
writeln!(f, "Recipe `{}` first defined on line {} is redefined on line {}",
|
|
|
|
recipe, first + 1, self.line + 1)?;
|
|
|
|
}
|
|
|
|
DependencyHasParameters{recipe, dependency} => {
|
|
|
|
writeln!(f, "Recipe `{}` depends on `{}` which requires arguments. \
|
|
|
|
Dependencies may not require arguments", recipe, dependency)?;
|
|
|
|
}
|
|
|
|
ParameterShadowsVariable{parameter} => {
|
|
|
|
writeln!(f, "Parameter `{}` shadows variable of the same name", parameter)?;
|
|
|
|
}
|
|
|
|
RequiredParameterFollowsDefaultParameter{parameter} => {
|
|
|
|
writeln!(f, "Non-default parameter `{}` follows default parameter", parameter)?;
|
|
|
|
}
|
|
|
|
ParameterFollowsVariadicParameter{parameter} => {
|
|
|
|
writeln!(f, "Parameter `{}` follows variadic parameter", parameter)?;
|
|
|
|
}
|
|
|
|
MixedLeadingWhitespace{whitespace} => {
|
|
|
|
writeln!(f,
|
|
|
|
"Found a mix of tabs and spaces in leading whitespace: `{}`\n\
|
|
|
|
Leading whitespace may consist of tabs or spaces, but not both",
|
|
|
|
show_whitespace(whitespace)
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
ExtraLeadingWhitespace => {
|
|
|
|
writeln!(f, "Recipe line has extra leading whitespace")?;
|
|
|
|
}
|
2017-12-03 01:59:07 +03:00
|
|
|
FunctionArgumentCountMismatch{function, found, expected} => {
|
|
|
|
writeln!(
|
|
|
|
f,
|
|
|
|
"Function `{}` called with {} argument{} but takes {}",
|
|
|
|
function, found, maybe_s(found), expected
|
|
|
|
)?;
|
|
|
|
}
|
2017-11-17 10:30:08 +03:00
|
|
|
InconsistentLeadingWhitespace{expected, found} => {
|
|
|
|
writeln!(f,
|
|
|
|
"Recipe line has inconsistent leading whitespace. \
|
|
|
|
Recipe started with `{}` but found line with `{}`",
|
|
|
|
show_whitespace(expected), show_whitespace(found)
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
OuterShebang => {
|
|
|
|
writeln!(f, "`#!` is reserved syntax outside of recipes")?;
|
|
|
|
}
|
|
|
|
UnknownDependency{recipe, unknown} => {
|
|
|
|
writeln!(f, "Recipe `{}` has unknown dependency `{}`", recipe, unknown)?;
|
|
|
|
}
|
|
|
|
UndefinedVariable{variable} => {
|
|
|
|
writeln!(f, "Variable `{}` not defined", variable)?;
|
|
|
|
}
|
2017-12-02 16:37:10 +03:00
|
|
|
UnknownFunction{function} => {
|
|
|
|
writeln!(f, "Call to unknown function `{}`", function)?;
|
|
|
|
}
|
2017-11-17 10:30:08 +03:00
|
|
|
UnknownStartOfToken => {
|
|
|
|
writeln!(f, "Unknown start of token:")?;
|
|
|
|
}
|
2017-12-02 23:49:31 +03:00
|
|
|
UnterminatedInterpolation => {
|
|
|
|
writeln!(f, "Unterminated interpolation")?;
|
|
|
|
}
|
2017-11-17 10:30:08 +03:00
|
|
|
UnterminatedString => {
|
|
|
|
writeln!(f, "Unterminated string")?;
|
|
|
|
}
|
|
|
|
Internal{ref message} => {
|
|
|
|
writeln!(f, "Internal error, this may indicate a bug in just: {}\n\
|
|
|
|
consider filing an issue: https://github.com/casey/just/issues/new",
|
|
|
|
message)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", message.suffix())?;
|
|
|
|
|
|
|
|
write_error_context(f, self.text, self.index, self.line, self.column, self.width)
|
|
|
|
}
|
|
|
|
}
|