Skip to content

Commit b1ed76d

Browse files
committed
Parse module
1 parent 6747c79 commit b1ed76d

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

src/analyzer.rs

+12
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,20 @@ impl<'src> Analyzer<'src> {
9292
absolute,
9393
name,
9494
doc,
95+
attributes,
9596
..
9697
} => {
98+
99+
for attribute in attributes {
100+
if !matches!(attribute, Attribute::Doc(..)) {
101+
let alias = name.lexeme();
102+
return Err(name.token.error(AliasInvalidAttribute {
103+
alias,
104+
attribute: attribute.clone(),
105+
}));
106+
}
107+
}
108+
97109
unstable.insert(Unstable::Modules);
98110

99111
if let Some(absolute) = absolute {

src/item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub(crate) enum Item<'src> {
1313
relative: StringLiteral<'src>,
1414
},
1515
Module {
16+
attributes: BTreeSet<Attribute<'src>>,
1617
absolute: Option<PathBuf>,
1718
doc: Option<&'src str>,
1819
name: Name<'src>,

src/parser.rs

+39-21
Original file line numberDiff line numberDiff line change
@@ -373,27 +373,7 @@ impl<'run, 'src> Parser<'run, 'src> {
373373
|| self.next_are(&[Identifier, QuestionMark]) =>
374374
{
375375
let doc = pop_doc_comment(&mut items, eol_since_last_comment);
376-
377-
self.presume_keyword(Keyword::Mod)?;
378-
379-
let optional = self.accepted(QuestionMark)?;
380-
381-
let name = self.parse_name()?;
382-
383-
let relative = if self.next_is(StringToken) || self.next_are(&[Identifier, StringToken])
384-
{
385-
Some(self.parse_string_literal()?)
386-
} else {
387-
None
388-
};
389-
390-
items.push(Item::Module {
391-
absolute: None,
392-
doc,
393-
name,
394-
optional,
395-
relative,
396-
});
376+
items.push(self.parse_module(BTreeSet::new(), doc)?);
397377
}
398378
Some(Keyword::Set)
399379
if self.next_are(&[Identifier, Identifier, ColonEquals])
@@ -430,6 +410,17 @@ impl<'run, 'src> Parser<'run, 'src> {
430410
Some(Keyword::Alias) if self.next_are(&[Identifier, Identifier, ColonEquals]) => {
431411
items.push(Item::Alias(self.parse_alias(attributes)?));
432412
}
413+
Some(Keyword::Mod)
414+
if self.next_are(&[Identifier, Identifier, Comment])
415+
|| self.next_are(&[Identifier, Identifier, Eof])
416+
|| self.next_are(&[Identifier, Identifier, Eol])
417+
|| self.next_are(&[Identifier, Identifier, Identifier, StringToken])
418+
|| self.next_are(&[Identifier, Identifier, StringToken])
419+
|| self.next_are(&[Identifier, QuestionMark]) =>
420+
{
421+
let doc = pop_doc_comment(&mut items, eol_since_last_comment);
422+
items.push(self.parse_module(attributes, doc)?);
423+
}
433424
_ => {
434425
let quiet = self.accepted(At)?;
435426
let doc = pop_doc_comment(&mut items, eol_since_last_comment);
@@ -454,6 +445,33 @@ impl<'run, 'src> Parser<'run, 'src> {
454445
}
455446
}
456447

448+
fn parse_module(
449+
&mut self,
450+
attributes: BTreeSet<Attribute<'src>>,
451+
doc: Option<&'src str>,
452+
) -> CompileResult<'src, Item<'src>> {
453+
self.presume_keyword(Keyword::Mod)?;
454+
455+
let optional = self.accepted(QuestionMark)?;
456+
457+
let name = self.parse_name()?;
458+
459+
let relative = if self.next_is(StringToken) || self.next_are(&[Identifier, StringToken]) {
460+
Some(self.parse_string_literal()?)
461+
} else {
462+
None
463+
};
464+
465+
Ok(Item::Module {
466+
attributes,
467+
absolute: None,
468+
doc,
469+
name,
470+
optional,
471+
relative,
472+
})
473+
}
474+
457475
/// Parse an alias, e.g `alias name := target`
458476
fn parse_alias(
459477
&mut self,

0 commit comments

Comments
 (0)