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
2 changes: 1 addition & 1 deletion src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'src> Alias<'src> {

impl RecipeAlias<'_> {
pub(crate) fn is_public(&self) -> bool {
!self.name.lexeme().starts_with('_') && !self.attributes.contains(AttributeKind::Private)
!self.name.lexeme().starts_with('_') && !self.attributes.private()
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ impl<'run, 'src> Analyzer<'run, 'src> {
}
Item::Module {
absolute,
attributes,
doc,
groups,
name,
optional,
private,
..
} => {
if let Some(absolute) = absolute {
Expand All @@ -93,12 +92,12 @@ impl<'run, 'src> Analyzer<'run, 'src> {
asts,
config,
doc.clone(),
groups.as_slice(),
&attributes.groups(),
loaded,
Some(*name),
overrides,
paths,
*private,
attributes.private(),
absolute,
)?);
} else if *optional {
Expand All @@ -116,7 +115,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
self.analyze_set(set)?;
self.sets.insert(set.clone());
}
Item::Unexport { name } => {
Item::Unexport { name, .. } => {
if !self.unexports.insert(name.lexeme().to_string()) {
return Err(name.error(DuplicateUnexport {
variable: name.lexeme(),
Expand Down
4 changes: 0 additions & 4 deletions src/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ pub(crate) type Assignment<'src> = Binding<'src, Expression<'src>>;

impl Display for Assignment<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if self.private {
writeln!(f, "[private]")?;
}

if self.export {
write!(f, "export ")?;
}
Expand Down
22 changes: 22 additions & 0 deletions src/attribute_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,32 @@ impl<'src> AttributeSet<'src> {
self.0.keys().find(|attribute| attribute.kind() == kind)
}

pub(crate) fn groups(&self) -> Vec<StringLiteral<'src>> {
self
.0
.keys()
.filter_map(|attribute| {
if let Attribute::Group(group) = attribute {
Some(group.clone())
} else {
None
}
})
.collect()
}

pub(crate) fn name(&self, attribute: &Attribute<'src>) -> Name<'src> {
self.0[attribute]
}

pub(crate) fn new() -> Self {
Self::default()
}

pub(crate) fn private(&self) -> bool {
self.contains(AttributeKind::Private)
}

pub(crate) fn iter(&self) -> btree_map::Keys<'_, Attribute<'src>, Name<'src>> {
self.0.keys()
}
Expand Down
2 changes: 2 additions & 0 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::*;
/// A binding of `name` to `value`
#[derive(Debug, Clone, PartialEq, Serialize)]
pub(crate) struct Binding<'src, V = Value> {
#[serde(skip)]
pub(crate) attributes: AttributeSet<'src>,
pub(crate) eager: bool,
pub(crate) export: bool,
#[serde(skip)]
Expand Down
1 change: 1 addition & 0 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl Compiler {
relative,
absolute,
optional,
..
} => {
let import = current
.path
Expand Down
3 changes: 3 additions & 0 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
};

self.scope.bind(Binding {
attributes: AttributeSet::new(),
eager: assignment.eager,
export: assignment.export
|| self
Expand Down Expand Up @@ -286,6 +287,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
for ((name, number), argument) in function.parameters.iter().copied().zip(arguments) {
let value = self.evaluate_value(argument)?;
scope.bind(Binding {
attributes: AttributeSet::new(),
eager: false,
export: false,
file_depth: 0,
Expand Down Expand Up @@ -809,6 +811,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
};

evaluator.scope.bind(Binding {
attributes: AttributeSet::new(),
eager: false,
export: parameter.export,
file_depth: 0,
Expand Down
1 change: 1 addition & 0 deletions src/function_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct FunctionDefinition<'src> {
pub(crate) attributes: AttributeSet<'src>,
pub(crate) body: Expression<'src>,
pub(crate) name: Name<'src>,
pub(crate) parameters: Vec<(Name<'src>, Number)>,
Expand Down
65 changes: 52 additions & 13 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,77 @@ pub(crate) enum Item<'src> {
Function(FunctionDefinition<'src>),
Import {
absolute: Option<PathBuf>,
attributes: AttributeSet<'src>,
optional: bool,
relative: StringLiteral<'src>,
},
Module {
absolute: Option<PathBuf>,
attributes: AttributeSet<'src>,
doc: Option<String>,
groups: Vec<StringLiteral<'src>>,
name: Name<'src>,
optional: bool,
private: bool,
relative: Option<StringLiteral<'src>>,
},
Newline,
Recipe(UnresolvedRecipe<'src>),
Set(Set<'src>),
Unexport {
attributes: AttributeSet<'src>,
name: Name<'src>,
},
}

impl<'src> Item<'src> {
fn attributes(&self) -> Option<&AttributeSet<'src>> {
match self {
Self::Alias(alias) => Some(&alias.attributes),
Self::Assignment(assignment) => Some(&assignment.attributes),
Self::Comment(_) | Self::Newline => None,
Self::Function(function) => Some(&function.attributes),
Self::Import { attributes, .. }
| Self::Module { attributes, .. }
| Self::Unexport { attributes, .. } => Some(attributes),
Self::Recipe(recipe) => Some(&recipe.attributes),
Self::Set(set) => Some(&set.attributes),
}
}

fn doc_comment(&self) -> Option<&str> {
match self {
Self::Module {
attributes, doc, ..
} => {
if attributes.contains(AttributeKind::Doc) {
None
} else {
doc.as_deref()
}
}
Self::Recipe(recipe) => {
if recipe.attributes.contains(AttributeKind::Doc) {
None
} else {
recipe.doc.as_deref()
}
}
_ => None,
}
}
}

impl ColorDisplay for Item<'_> {
fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result {
if let Some(doc) = self.doc_comment() {
writeln!(f, "# {doc}")?;
}

if let Some(attributes) = self.attributes() {
for attribute in attributes {
writeln!(f, "[{attribute}]")?;
}
}

match self {
Self::Alias(alias) => write!(f, "{alias}"),
Self::Assignment(assignment) => write!(f, "{assignment}"),
Expand All @@ -57,21 +106,11 @@ impl ColorDisplay for Item<'_> {
write!(f, " {relative}")
}
Self::Module {
doc,
groups,
name,
optional,
relative,
..
} => {
if let Some(doc) = doc {
writeln!(f, "# {doc}")?;
}

for group in groups {
writeln!(f, "[group: {group}]")?;
}

write!(f, "mod")?;

if *optional {
Expand All @@ -89,7 +128,7 @@ impl ColorDisplay for Item<'_> {
Self::Newline => Ok(()),
Self::Recipe(recipe) => write!(f, "{}", recipe.color_display(color)),
Self::Set(set) => write!(f, "{set}"),
Self::Unexport { name } => write!(f, "unexport {name}"),
Self::Unexport { name, .. } => write!(f, "unexport {name}"),
}
}
}
2 changes: 1 addition & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<'src> Node<'src> for Item<'src> {
}
Self::Recipe(recipe) => recipe.tree(),
Self::Set(set) => set.tree(),
Self::Unexport { name } => {
Self::Unexport { name, .. } => {
let mut unexport = Tree::atom(Keyword::Unexport.lexeme());
unexport.push_mut(name.lexeme().replace('-', "_"));
unexport
Expand Down
38 changes: 21 additions & 17 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,10 @@ impl<'run, 'src> Parser<'run, 'src> {
Some(Keyword::Unexport) if self.line_is(&[Identifier, Identifier]) => {
self.presume_keyword(Keyword::Unexport)?;
let name = self.parse_name()?;
Item::Unexport { name }
Item::Unexport {
attributes: AttributeSet::new(),
name,
}
}
Some(Keyword::Import)
if self.next_are(&[Identifier, Identifier, StringToken])
Expand All @@ -543,6 +546,7 @@ impl<'run, 'src> Parser<'run, 'src> {
let relative = self.parse_string_literal()?;
Item::Import {
absolute: None,
attributes: AttributeSet::new(),
optional,
relative,
}
Expand Down Expand Up @@ -579,22 +583,12 @@ impl<'run, 'src> Parser<'run, 'src> {

let doc = self.take_doc_comment(&attributes);

let private = attributes.contains(AttributeKind::Private);

let mut groups = Vec::new();
for attribute in attributes {
if let Attribute::Group(group) = attribute {
groups.push(group);
}
}

Item::Module {
absolute: None,
attributes,
doc,
groups,
name,
optional,
private,
relative,
}
}
Expand Down Expand Up @@ -663,6 +657,7 @@ impl<'run, 'src> Parser<'run, 'src> {
let body = self.parse_expression()?;

Ok(FunctionDefinition {
attributes: AttributeSet::new(),
body,
name,
parameters,
Expand All @@ -680,11 +675,12 @@ impl<'run, 'src> Parser<'run, 'src> {
self.presume(ColonEquals)?;
let value = self.parse_expression()?;

let private = attributes.contains(AttributeKind::Private);

attributes.ensure_valid_attributes("assignment", *name, &[AttributeKind::Private])?;

let private = attributes.private();

Ok(Assignment {
attributes,
eager,
export,
file_depth: self.file_depth,
Expand Down Expand Up @@ -1398,7 +1394,7 @@ impl<'run, 'src> Parser<'run, 'src> {
}));
}

let private = name.lexeme().starts_with('_') || attributes.contains(AttributeKind::Private);
let private = name.lexeme().starts_with('_') || attributes.private();

let doc = self.take_doc_comment(&attributes);

Expand Down Expand Up @@ -1578,7 +1574,11 @@ impl<'run, 'src> Parser<'run, 'src> {
};

if let Some(value) = set_bool {
return Ok(Set { name, value });
return Ok(Set {
attributes: AttributeSet::new(),
name,
value,
});
}

self.expect(ColonEquals)?;
Expand Down Expand Up @@ -1627,7 +1627,11 @@ impl<'run, 'src> Parser<'run, 'src> {
};

if let Some(value) = set_value {
return Ok(Set { name, value });
return Ok(Set {
attributes: AttributeSet::new(),
name,
value,
});
}

Err(name.error(CompileErrorKind::UnknownSetting {
Expand Down
Loading
Loading