Skip to content

Commit

Permalink
not() attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Dec 12, 2024
1 parent a5d884e commit c3fda90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
39 changes: 23 additions & 16 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ pub(crate) enum Attribute<'src> {
Doc(Option<StringLiteral<'src>>),
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Macos,
Linux { inverted: bool },
Macos { inverted: bool },
NoCd,
NoExitMessage,
NoQuiet,
Openbsd,
PositionalArguments,
Private,
Script(Option<Interpreter<'src>>),
Unix,
Windows,
Unix { inverted: bool },
Windows { inverted: bool },
WorkingDirectory(StringLiteral<'src>),
}

Expand Down Expand Up @@ -51,6 +51,7 @@ impl<'src> Attribute<'src> {
pub(crate) fn new(
name: Name<'src>,
arguments: Vec<StringLiteral<'src>>,
inverted: bool,
) -> CompileResult<'src, Self> {
let discriminant = name
.lexeme()
Expand Down Expand Up @@ -80,8 +81,8 @@ impl<'src> Attribute<'src> {
AttributeDiscriminant::Doc => Self::Doc(arguments.into_iter().next()),
AttributeDiscriminant::Extension => Self::Extension(arguments.into_iter().next().unwrap()),
AttributeDiscriminant::Group => Self::Group(arguments.into_iter().next().unwrap()),
AttributeDiscriminant::Linux => Self::Linux,
AttributeDiscriminant::Macos => Self::Macos,
AttributeDiscriminant::Linux => Self::Linux { inverted },
AttributeDiscriminant::Macos => Self::Macos { inverted },
AttributeDiscriminant::NoCd => Self::NoCd,
AttributeDiscriminant::NoExitMessage => Self::NoExitMessage,
AttributeDiscriminant::NoQuiet => Self::NoQuiet,
Expand All @@ -95,8 +96,8 @@ impl<'src> Attribute<'src> {
arguments: arguments.collect(),
})
}),
AttributeDiscriminant::Unix => Self::Unix,
AttributeDiscriminant::Windows => Self::Windows,
AttributeDiscriminant::Unix => Self::Unix { inverted },
AttributeDiscriminant::Windows => Self::Windows { inverted },
AttributeDiscriminant::WorkingDirectory => {
Self::WorkingDirectory(arguments.into_iter().next().unwrap())
}
Expand All @@ -118,28 +119,34 @@ impl<'src> Attribute<'src> {

impl Display for Attribute<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.name())?;
let name = self.name();

match self {
Self::Confirm(Some(argument))
| Self::Doc(Some(argument))
| Self::Extension(argument)
| Self::Group(argument)
| Self::WorkingDirectory(argument) => write!(f, "({argument})")?,
Self::Script(Some(shell)) => write!(f, "({shell})")?,
| Self::WorkingDirectory(argument) => write!(f, "{name}({argument})")?,
Self::Script(Some(shell)) => write!(f, "{name}({shell})")?,
Self::Linux { inverted }
| Self::Macos { inverted }
| Self::Unix { inverted }
| Self::Windows { inverted } => {
if *inverted {
write!(f, "not({name})")?;
} else {
write!(f, "{name}")?;
}
}
Self::Confirm(None)
| Self::Doc(None)
| Self::Linux
| Self::Macos
| Self::NoCd
| Self::NoExitMessage
| Self::NoQuiet
| Self::Openbsd
| Self::PositionalArguments
| Self::Private
| Self::Script(None)
| Self::Unix
| Self::Windows => {}
| Self::Script(None) => write!(f, "{name}")?,
}

Ok(())
Expand Down
15 changes: 13 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,18 @@ impl<'run, 'src> Parser<'run, 'src> {
token.get_or_insert(bracket);

loop {
let name = self.parse_name()?;
let (name, inverted) = {
let mut i = false;
let mut n = self.parse_name()?;
if n.lexeme() == "not" {
i = true;
self.expect(ParenL)?;
n = self.parse_name()?;
self.expect(ParenR)?;
}

(n, i)
};

let mut arguments = Vec::new();

Expand All @@ -1152,7 +1163,7 @@ impl<'run, 'src> Parser<'run, 'src> {
self.expect(ParenR)?;
}

let attribute = Attribute::new(name, arguments)?;
let attribute = Attribute::new(name, arguments, inverted)?;

let first = attributes.get(&attribute).or_else(|| {
if attribute.repeatable() {
Expand Down

0 comments on commit c3fda90

Please sign in to comment.