Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
asts: &'run HashMap<PathBuf, Ast<'src>>,
config: &Config,
doc: Option<String>,
groups: &[StringLiteral],
groups: &[StringLiteral<'src>],
loaded: &[PathBuf],
name: Option<Name<'src>>,
paths: &HashMap<PathBuf, PathBuf>,
Expand All @@ -30,7 +30,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
asts: &'run HashMap<PathBuf, Ast<'src>>,
config: &Config,
doc: Option<String>,
groups: &[StringLiteral],
groups: &[StringLiteral<'src>],
loaded: &[PathBuf],
name: Option<Name<'src>>,
paths: &HashMap<PathBuf, PathBuf>,
Expand Down
2 changes: 1 addition & 1 deletion src/arg_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(crate) struct ArgAttribute<'src> {
pub(crate) help: Option<String>,
pub(crate) long: Option<String>,
pub(crate) name: Token<'src>,
pub(crate) pattern: Option<Pattern>,
pub(crate) pattern: Option<Pattern<'src>>,
pub(crate) short: Option<char>,
pub(crate) value: Option<String>,
}
174 changes: 76 additions & 98 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,33 @@ use super::*;
#[strum_discriminants(strum(serialize_all = "kebab-case"))]
pub(crate) enum Attribute<'src> {
Arg {
help: Option<StringLiteral>,
long: Option<StringLiteral>,
#[serde(skip)]
long_token: Option<Token<'src>>,
name: StringLiteral,
#[serde(skip)]
name_token: Token<'src>,
#[serde(skip)]
pattern: Option<Pattern>,
#[serde(rename = "pattern")]
pattern_literal: Option<StringLiteral>,
short: Option<StringLiteral>,
#[serde(skip)]
short_token: Option<Token<'src>>,
value: Option<StringLiteral>,
help: Option<StringLiteral<'src>>,
long: Option<StringLiteral<'src>>,
name: StringLiteral<'src>,
pattern: Option<Pattern<'src>>,
short: Option<StringLiteral<'src>>,
value: Option<StringLiteral<'src>>,
},
Confirm(Option<StringLiteral>),
Confirm(Option<StringLiteral<'src>>),
Default,
Doc(Option<StringLiteral>),
Doc(Option<StringLiteral<'src>>),
ExitMessage,
Extension(StringLiteral),
Group(StringLiteral),
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Macos,
Metadata(Vec<StringLiteral>),
Metadata(Vec<StringLiteral<'src>>),
NoCd,
NoExitMessage,
NoQuiet,
Openbsd,
Parallel,
PositionalArguments,
Private,
Script(Option<Interpreter<StringLiteral>>),
Script(Option<Interpreter<StringLiteral<'src>>>),
Unix,
Windows,
WorkingDirectory(StringLiteral),
WorkingDirectory(StringLiteral<'src>),
}

impl AttributeDiscriminant {
Expand Down Expand Up @@ -74,16 +65,38 @@ impl AttributeDiscriminant {
}

impl<'src> Attribute<'src> {
fn check_option_name(
parameter: &StringLiteral<'src>,
literal: &StringLiteral<'src>,
) -> CompileResult<'src> {
if literal.cooked.contains('=') {
return Err(
literal
.token
.error(CompileErrorKind::OptionNameContainsEqualSign {
parameter: parameter.cooked.clone(),
}),
);
}

if literal.cooked.is_empty() {
return Err(literal.token.error(CompileErrorKind::OptionNameEmpty {
parameter: parameter.cooked.clone(),
}));
}

Ok(())
}

pub(crate) fn new(
name: Name<'src>,
arguments: Vec<(Token<'src>, StringLiteral)>,
mut keyword_arguments: BTreeMap<&'src str, (Name<'src>, Token<'src>, StringLiteral)>,
arguments: Vec<StringLiteral<'src>>,
mut keyword_arguments: BTreeMap<&'src str, (Name<'src>, StringLiteral<'src>)>,
) -> CompileResult<'src, Self> {
let discriminant = name
.lexeme()
.parse::<AttributeDiscriminant>()
.ok()
.ok_or_else(|| {
.map_err(|_| {
name.error(CompileErrorKind::UnknownAttribute {
attribute: name.lexeme(),
})
Expand All @@ -94,99 +107,68 @@ impl<'src> Attribute<'src> {
if !range.contains(&found) {
return Err(
name.error(CompileErrorKind::AttributeArgumentCountMismatch {
attribute: name.lexeme(),
attribute: name,
found,
min: *range.start(),
max: *range.end(),
}),
);
}

let (tokens, arguments): (Vec<Token>, Vec<StringLiteral>) = arguments.into_iter().unzip();

let attribute = match discriminant {
AttributeDiscriminant::Arg => {
let name = arguments.into_iter().next().unwrap();
let name_token = tokens.into_iter().next().unwrap();

let (long_token, long) =
if let Some((_name, token, literal)) = keyword_arguments.remove("long") {
if literal.cooked.contains('=') {
return Err(token.error(CompileErrorKind::OptionNameContainsEqualSign {
parameter: name.cooked,
}));
}

if literal.cooked.is_empty() {
return Err(token.error(CompileErrorKind::OptionNameEmpty {
parameter: name.cooked,
}));
}

(Some(token), Some(literal))
} else {
(None, None)
};

let (short_token, short) =
if let Some((_name, token, literal)) = keyword_arguments.remove("short") {
if literal.cooked.contains('=') {
return Err(token.error(CompileErrorKind::OptionNameContainsEqualSign {
parameter: name.cooked,
}));
}
let long = keyword_arguments
.remove("long")
.map(|(_name, literal)| {
Self::check_option_name(&name, &literal)?;
Ok(literal)
})
.transpose()?;

if literal.cooked.is_empty() {
return Err(token.error(CompileErrorKind::OptionNameEmpty {
parameter: name.cooked,
}));
}
let short = keyword_arguments
.remove("short")
.map(|(_name, literal)| {
Self::check_option_name(&name, &literal)?;

if literal.cooked.chars().count() != 1 {
return Err(
token.error(CompileErrorKind::ShortOptionWithMultipleCharacters {
parameter: name.cooked,
}),
);
return Err(literal.token.error(
CompileErrorKind::ShortOptionWithMultipleCharacters {
parameter: name.cooked.clone(),
},
));
}

(Some(token), Some(literal))
} else {
(None, None)
};
Ok(literal)
})
.transpose()?;

let (pattern_literal, pattern) = keyword_arguments
let pattern = keyword_arguments
.remove("pattern")
.map(|(_name, token, literal)| {
let pattern = Pattern::new(token, &literal)?;
Ok((Some(literal), Some(pattern)))
.map(|(_name, literal)| Pattern::new(&literal))
.transpose()?;

let value = keyword_arguments
.remove("value")
.map(|(name, literal)| {
if long.is_none() && short.is_none() {
return Err(name.error(CompileErrorKind::ArgAttributeValueRequiresOption));
}
Ok(literal)
})
.transpose()?
.unwrap_or((None, None));

let value = if let Some((name, _token, literal)) = keyword_arguments.remove("value") {
if long.is_none() && short.is_none() {
return Err(name.error(CompileErrorKind::ArgAttributeValueRequiresOption));
}
Some(literal)
} else {
None
};
.transpose()?;

let help = keyword_arguments
.remove("help")
.map(|(_name, _token, literal)| literal);
.map(|(_name, literal)| literal);

Self::Arg {
help,
long,
long_token,
name,
name_token,
pattern,
pattern_literal,
short,
short_token,
value,
}
}
Expand Down Expand Up @@ -220,7 +202,7 @@ impl<'src> Attribute<'src> {
}
};

if let Some((_name, (keyword_name, _token, _literal))) = keyword_arguments.into_iter().next() {
if let Some((_name, (keyword_name, _literal))) = keyword_arguments.into_iter().next() {
return Err(
keyword_name.error(CompileErrorKind::UnknownAttributeKeyword {
attribute: name.lexeme(),
Expand Down Expand Up @@ -256,13 +238,9 @@ impl Display for Attribute<'_> {
Self::Arg {
help,
long,
long_token: _,
name,
name_token: _,
pattern: _,
pattern_literal,
pattern,
short,
short_token: _,
value,
} => {
write!(f, "({name}")?;
Expand All @@ -275,8 +253,8 @@ impl Display for Attribute<'_> {
write!(f, ", short={short}")?;
}

if let Some(pattern) = pattern_literal {
write!(f, ", pattern={pattern}")?;
if let Some(pattern) = pattern {
write!(f, ", pattern={}", pattern.token.lexeme())?;
}

if let Some(value) = value {
Expand Down
2 changes: 1 addition & 1 deletion src/compile_error_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) enum CompileErrorKind<'src> {
source: regex::Error,
},
AttributeArgumentCountMismatch {
attribute: &'src str,
attribute: Name<'src>,
found: usize,
min: usize,
max: usize,
Expand Down
7 changes: 4 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ impl Compiler {
relative,
absolute,
optional,
path,
} => {
let import = current
.path
Expand All @@ -88,9 +87,11 @@ impl Compiler {
});
}
*absolute = Some(import.clone());
stack.push(current.import(import, path.offset));
stack.push(current.import(import, relative.token.offset));
} else if !*optional {
return Err(Error::MissingImportFile { path: *path });
return Err(Error::MissingImportFile {
path: relative.token,
});
}
}
_ => {}
Expand Down
5 changes: 3 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) enum Error<'src> {
ArgumentPatternMismatch {
argument: String,
parameter: &'src str,
pattern: Pattern,
pattern: Box<Pattern<'src>>,
recipe: &'src str,
},
Assert {
Expand Down Expand Up @@ -363,7 +363,8 @@ impl ColorDisplay for Error<'_> {
} => {
write!(
f,
"Argument `{argument}` passed to recipe `{recipe}` parameter `{parameter}` does not match pattern '{pattern}'",
"Argument `{argument}` passed to recipe `{recipe}` parameter `{parameter}` does not match pattern '{}'",
pattern.original(),
)?;
}
Assert { message, .. } => {
Expand Down
6 changes: 3 additions & 3 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub(crate) enum Expression<'src> {
},
// `f"format string"`
FormatString {
start: StringLiteral,
expressions: Vec<(Expression<'src>, StringLiteral)>,
start: StringLiteral<'src>,
expressions: Vec<(Expression<'src>, StringLiteral<'src>)>,
},
/// `(contents)`
Group { contents: Box<Expression<'src>> },
Expand All @@ -55,7 +55,7 @@ pub(crate) enum Expression<'src> {
rhs: Box<Expression<'src>>,
},
/// `"string_literal"` or `'string_literal'`
StringLiteral { string_literal: StringLiteral },
StringLiteral { string_literal: StringLiteral<'src> },
/// `variable`
Variable { name: Name<'src> },
}
Expand Down
7 changes: 3 additions & 4 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ pub(crate) enum Item<'src> {
Import {
absolute: Option<PathBuf>,
optional: bool,
path: Token<'src>,
relative: StringLiteral,
relative: StringLiteral<'src>,
},
Module {
absolute: Option<PathBuf>,
doc: Option<String>,
groups: Vec<StringLiteral>,
groups: Vec<StringLiteral<'src>>,
name: Name<'src>,
optional: bool,
relative: Option<StringLiteral>,
relative: Option<StringLiteral<'src>>,
},
Recipe(UnresolvedRecipe<'src>),
Set(Set<'src>),
Expand Down
2 changes: 1 addition & 1 deletion src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) struct Justfile<'src> {
#[serde(rename = "first", serialize_with = "keyed::serialize_option")]
pub(crate) default: Option<Arc<Recipe<'src>>>,
pub(crate) doc: Option<String>,
pub(crate) groups: Vec<StringLiteral>,
pub(crate) groups: Vec<StringLiteral<'src>>,
#[serde(skip)]
pub(crate) loaded: Vec<PathBuf>,
#[serde(skip)]
Expand Down
Loading