Skip to content

Commit

Permalink
Add alias attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaddeo committed Feb 1, 2025
1 parent da87b3d commit 58aea8a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/alias.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

/// An alias, e.g. `name := target`
#[derive(Debug, PartialEq, Clone, Serialize)]
#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Clone, Serialize)]
pub(crate) struct Alias<'src, T = Rc<Recipe<'src>>> {
pub(crate) attributes: AttributeSet<'src>,
pub(crate) name: Name<'src>,
Expand Down
12 changes: 12 additions & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ impl<'run, 'src> Analyzer<'run, 'src> {
if recipe.enabled() {
Self::analyze_recipe(recipe)?;
self.recipes.push(recipe);

for attribute in &recipe.attributes {
if let Attribute::Alias(name) = attribute {
Self::define(&mut definitions, *name, "alias", false)?;
let alias = Alias {
name: *name,
target: recipe.name,
attributes: AttributeSet::new(),
};
self.aliases.insert(alias);
}
}
}
}
Item::Set(set) => {
Expand Down
31 changes: 28 additions & 3 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::*;
#[strum_discriminants(derive(EnumString, Ord, PartialOrd))]
#[strum_discriminants(strum(serialize_all = "kebab-case"))]
pub(crate) enum Attribute<'src> {
Alias(Name<'src>),
Confirm(Option<StringLiteral<'src>>),
Doc(Option<StringLiteral<'src>>),
ExitMessage,
Expand All @@ -32,7 +33,7 @@ impl AttributeDiscriminant {
fn argument_range(self) -> RangeInclusive<usize> {
match self {
Self::Confirm | Self::Doc => 0..=1,
Self::Group | Self::Extension | Self::WorkingDirectory => 1..=1,
Self::Alias | Self::Group | Self::Extension | Self::WorkingDirectory => 1..=1,
Self::ExitMessage
| Self::Linux
| Self::Macos
Expand All @@ -52,7 +53,7 @@ impl AttributeDiscriminant {
impl<'src> Attribute<'src> {
pub(crate) fn new(
name: Name<'src>,
arguments: Vec<StringLiteral<'src>>,
arguments: Vec<(Token<'src>, StringLiteral<'src>)>,
) -> CompileResult<'src, Self> {
let discriminant = name
.lexeme()
Expand All @@ -77,7 +78,30 @@ impl<'src> Attribute<'src> {
);
}

let alias_args = arguments.clone();
let arguments = arguments.into_iter().map(|(_, arg)| arg);
Ok(match discriminant {
AttributeDiscriminant::Alias => Self::Alias({
let (token, string_literal) = alias_args.into_iter().next().unwrap();
let delim = string_literal.kind.delimiter_len();
let token = Token {
kind: TokenKind::Identifier,
column: token.column + delim,
length: token.length - (delim * 2),
offset: token.offset + delim,
..token
};

if !token
.lexeme()
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_')
{
todo!("alias is not a valid identifier")
}

Name::from_identifier(token)
}),
AttributeDiscriminant::Confirm => Self::Confirm(arguments.into_iter().next()),
AttributeDiscriminant::Doc => Self::Doc(arguments.into_iter().next()),
AttributeDiscriminant::ExitMessage => Self::ExitMessage,
Expand Down Expand Up @@ -115,7 +139,7 @@ impl<'src> Attribute<'src> {
}

pub(crate) fn repeatable(&self) -> bool {
matches!(self, Attribute::Group(_))
matches!(self, Attribute::Group(_) | Attribute::Alias(_))
}
}

Expand All @@ -124,6 +148,7 @@ impl Display for Attribute<'_> {
write!(f, "{}", self.name())?;

match self {
Self::Alias(argument) => write!(f, "({argument})")?,
Self::Confirm(Some(argument))
| Self::Doc(Some(argument))
| Self::Extension(argument)
Expand Down
6 changes: 5 additions & 1 deletion src/attribute_set.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use {super::*, std::collections};

#[derive(Default, Debug, Clone, PartialEq, Serialize)]
#[derive(Default, Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Serialize)]
pub(crate) struct AttributeSet<'src>(BTreeSet<Attribute<'src>>);

impl<'src> AttributeSet<'src> {
pub(crate) fn new() -> Self {
Self(BTreeSet::new())
}

pub(crate) fn len(&self) -> usize {
self.0.len()
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,10 @@ impl<'run, 'src> Parser<'run, 'src> {
let mut arguments = Vec::new();

if self.accepted(Colon)? {
arguments.push(self.parse_string_literal()?);
arguments.push(self.parse_string_literal_token()?);
} else if self.accepted(ParenL)? {
loop {
arguments.push(self.parse_string_literal()?);
arguments.push(self.parse_string_literal_token()?);

if !self.accepted(Comma)? {
break;
Expand Down

0 comments on commit 58aea8a

Please sign in to comment.