Skip to content

Commit 1f7df4a

Browse files
authored
Check attributes in parser instead of analyzer (#2560)
1 parent 74dabba commit 1f7df4a

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

src/analyzer.rs

+3-33
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ impl<'run, 'src> Analyzer<'run, 'src> {
4949
match item {
5050
Item::Alias(alias) => {
5151
Self::define(&mut definitions, alias.name, "alias", false)?;
52-
Self::analyze_alias(alias)?;
5352
self.aliases.insert(alias.clone());
5453
}
5554
Item::Assignment(assignment) => {
@@ -65,36 +64,16 @@ impl<'run, 'src> Analyzer<'run, 'src> {
6564
}
6665
Item::Module {
6766
absolute,
68-
name,
6967
doc,
70-
attributes,
68+
groups,
69+
name,
7170
..
7271
} => {
73-
let mut doc_attr: Option<&str> = None;
74-
let mut groups = Vec::new();
75-
attributes.ensure_valid_attributes(
76-
"Module",
77-
**name,
78-
&[AttributeDiscriminant::Doc, AttributeDiscriminant::Group],
79-
)?;
80-
81-
for attribute in attributes {
82-
match attribute {
83-
Attribute::Doc(ref doc) => {
84-
doc_attr = Some(doc.as_ref().map(|s| s.cooked.as_ref()).unwrap_or_default());
85-
}
86-
Attribute::Group(ref group) => {
87-
groups.push(group.cooked.clone());
88-
}
89-
_ => unreachable!(),
90-
}
91-
}
92-
9372
if let Some(absolute) = absolute {
9473
Self::define(&mut definitions, *name, "module", false)?;
9574
self.modules.insert(Self::analyze(
9675
asts,
97-
doc_attr.or(*doc).map(ToOwned::to_owned),
76+
doc.clone(),
9877
groups.as_slice(),
9978
loaded,
10079
Some(*name),
@@ -299,15 +278,6 @@ impl<'run, 'src> Analyzer<'run, 'src> {
299278
Ok(())
300279
}
301280

302-
fn analyze_alias(alias: &Alias<'src, Name<'src>>) -> CompileResult<'src> {
303-
alias.attributes.ensure_valid_attributes(
304-
"Alias",
305-
*alias.name,
306-
&[AttributeDiscriminant::Private],
307-
)?;
308-
Ok(())
309-
}
310-
311281
fn analyze_set(&self, set: &Set<'src>) -> CompileResult<'src> {
312282
if let Some(original) = self.sets.get(set.name.lexeme()) {
313283
return Err(set.name.error(DuplicateSet {

src/attribute_set.rs

+10
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,13 @@ impl<'src, 'a> IntoIterator for &'a AttributeSet<'src> {
5858
self.0.iter()
5959
}
6060
}
61+
62+
impl<'src> IntoIterator for AttributeSet<'src> {
63+
type Item = Attribute<'src>;
64+
65+
type IntoIter = collections::btree_set::IntoIter<Attribute<'src>>;
66+
67+
fn into_iter(self) -> Self::IntoIter {
68+
self.0.into_iter()
69+
}
70+
}

src/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub(crate) enum Item<'src> {
1313
relative: StringLiteral<'src>,
1414
},
1515
Module {
16-
attributes: AttributeSet<'src>,
1716
absolute: Option<PathBuf>,
18-
doc: Option<&'src str>,
17+
doc: Option<String>,
18+
groups: Vec<String>,
1919
name: Name<'src>,
2020
optional: bool,
2121
relative: Option<StringLiteral<'src>>,

src/parser.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,30 @@ impl<'run, 'src> Parser<'run, 'src> {
393393
None
394394
};
395395

396+
let attributes = take_attributes();
397+
398+
attributes.ensure_valid_attributes(
399+
"Module",
400+
*name,
401+
&[AttributeDiscriminant::Doc, AttributeDiscriminant::Group],
402+
)?;
403+
404+
let doc = match attributes.get(AttributeDiscriminant::Doc) {
405+
Some(Attribute::Doc(Some(doc))) => Some(doc.cooked.clone()),
406+
Some(Attribute::Doc(None)) => None,
407+
None => doc.map(ToOwned::to_owned),
408+
_ => unreachable!(),
409+
};
410+
411+
let mut groups = Vec::new();
412+
for attribute in attributes {
413+
if let Attribute::Group(group) = attribute {
414+
groups.push(group.cooked);
415+
}
416+
}
417+
396418
items.push(Item::Module {
397-
attributes: take_attributes(),
419+
groups,
398420
absolute: None,
399421
doc,
400422
name,
@@ -469,6 +491,9 @@ impl<'run, 'src> Parser<'run, 'src> {
469491
self.presume_any(&[Equals, ColonEquals])?;
470492
let target = self.parse_name()?;
471493
self.expect_eol()?;
494+
495+
attributes.ensure_valid_attributes("Alias", *name, &[AttributeDiscriminant::Private])?;
496+
472497
Ok(Alias {
473498
attributes,
474499
name,
@@ -1311,14 +1336,14 @@ mod tests {
13111336

13121337
test! {
13131338
name: single_argument_attribute_shorthand,
1314-
text: "[group: 'some-group']\nalias t := test",
1315-
tree: (justfile (alias t test)),
1339+
text: "[group: 'foo']\nbar:",
1340+
tree: (justfile (recipe bar)),
13161341
}
13171342

13181343
test! {
13191344
name: single_argument_attribute_shorthand_multiple_same_line,
1320-
text: "[group: 'some-group', group: 'some-other-group']\nalias t := test",
1321-
tree: (justfile (alias t test)),
1345+
text: "[group: 'foo', group: 'bar']\nbaz:",
1346+
tree: (justfile (recipe baz)),
13221347
}
13231348

13241349
test! {

0 commit comments

Comments
 (0)