2019-11-07 10:55:15 -08:00
|
|
|
use crate::common::*;
|
|
|
|
|
2020-02-14 04:49:25 -08:00
|
|
|
/// Methods commmon to all AST nodes. Currently only used in parser unit tests.
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) trait Node<'src> {
|
2020-02-14 04:49:25 -08:00
|
|
|
/// Construct an untyped tree of atoms representing this Node. This function,
|
|
|
|
/// and `Tree` type, are only used in parser unit tests.
|
2019-11-07 10:55:15 -08:00
|
|
|
fn tree(&self) -> Tree<'src>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Module<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
Tree::atom("justfile")
|
|
|
|
.extend(self.items.iter().map(|item| item.tree()))
|
|
|
|
.extend(self.warnings.iter().map(|warning| warning.tree()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Item<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
match self {
|
|
|
|
Item::Alias(alias) => alias.tree(),
|
|
|
|
Item::Assignment(assignment) => assignment.tree(),
|
|
|
|
Item::Recipe(recipe) => recipe.tree(),
|
2019-11-10 23:17:47 -08:00
|
|
|
Item::Set(set) => set.tree(),
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:39:32 -08:00
|
|
|
impl<'src> Node<'src> for Alias<'src, Name<'src>> {
|
2019-11-07 10:55:15 -08:00
|
|
|
fn tree(&self) -> Tree<'src> {
|
2020-10-26 18:16:42 -07:00
|
|
|
Tree::atom(Keyword::Alias.lexeme())
|
2019-11-07 10:55:15 -08:00
|
|
|
.push(self.name.lexeme())
|
|
|
|
.push(self.target.lexeme())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Assignment<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
if self.export {
|
2020-10-26 18:16:42 -07:00
|
|
|
Tree::atom("assignment")
|
|
|
|
.push("#")
|
|
|
|
.push(Keyword::Export.lexeme())
|
2019-11-07 10:55:15 -08:00
|
|
|
} else {
|
|
|
|
Tree::atom("assignment")
|
|
|
|
}
|
|
|
|
.push(self.name.lexeme())
|
2019-12-07 03:09:21 -08:00
|
|
|
.push(self.value.tree())
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Expression<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
match self {
|
|
|
|
Expression::Concatination { lhs, rhs } => Tree::atom("+").push(lhs.tree()).push(rhs.tree()),
|
2020-10-26 18:16:42 -07:00
|
|
|
Expression::Conditional {
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
then,
|
|
|
|
otherwise,
|
|
|
|
inverted,
|
|
|
|
} => {
|
|
|
|
let mut tree = Tree::atom(Keyword::If.lexeme());
|
|
|
|
tree.push_mut(lhs.tree());
|
|
|
|
if *inverted {
|
|
|
|
tree.push_mut("!=")
|
|
|
|
} else {
|
|
|
|
tree.push_mut("==")
|
|
|
|
}
|
|
|
|
tree.push_mut(rhs.tree());
|
|
|
|
tree.push_mut(then.tree());
|
|
|
|
tree.push_mut(otherwise.tree());
|
|
|
|
tree
|
|
|
|
},
|
2019-11-21 10:14:10 -08:00
|
|
|
Expression::Call { thunk } => {
|
|
|
|
let mut tree = Tree::atom("call");
|
|
|
|
|
|
|
|
use Thunk::*;
|
|
|
|
match thunk {
|
|
|
|
Nullary { name, .. } => tree.push_mut(name.lexeme()),
|
|
|
|
Unary { name, arg, .. } => {
|
|
|
|
tree.push_mut(name.lexeme());
|
|
|
|
tree.push_mut(arg.tree());
|
2020-02-10 20:07:06 -08:00
|
|
|
},
|
2019-11-21 10:14:10 -08:00
|
|
|
Binary {
|
|
|
|
name, args: [a, b], ..
|
|
|
|
} => {
|
|
|
|
tree.push_mut(name.lexeme());
|
|
|
|
tree.push_mut(a.tree());
|
|
|
|
tree.push_mut(b.tree());
|
2020-02-10 20:07:06 -08:00
|
|
|
},
|
2019-11-21 10:14:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
tree
|
2020-02-10 20:07:06 -08:00
|
|
|
},
|
2019-11-07 10:55:15 -08:00
|
|
|
Expression::Variable { name } => Tree::atom(name.lexeme()),
|
|
|
|
Expression::StringLiteral {
|
|
|
|
string_literal: StringLiteral { cooked, .. },
|
|
|
|
} => Tree::string(cooked),
|
|
|
|
Expression::Backtick { contents, .. } => Tree::atom("backtick").push(Tree::string(contents)),
|
|
|
|
Expression::Group { contents } => Tree::List(vec![contents.tree()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 04:03:03 -08:00
|
|
|
impl<'src> Node<'src> for UnresolvedRecipe<'src> {
|
2019-11-07 10:55:15 -08:00
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
let mut t = Tree::atom("recipe");
|
|
|
|
|
|
|
|
if self.quiet {
|
|
|
|
t.push_mut("#");
|
|
|
|
t.push_mut("quiet");
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(doc) = self.doc {
|
|
|
|
t.push_mut(Tree::string(doc));
|
|
|
|
}
|
|
|
|
|
|
|
|
t.push_mut(self.name.lexeme());
|
|
|
|
|
|
|
|
if !self.parameters.is_empty() {
|
|
|
|
let mut params = Tree::atom("params");
|
|
|
|
|
|
|
|
for parameter in &self.parameters {
|
2020-06-13 01:49:13 -07:00
|
|
|
if let Some(prefix) = parameter.kind.prefix() {
|
|
|
|
params.push_mut(prefix);
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
params.push_mut(parameter.tree());
|
|
|
|
}
|
|
|
|
|
|
|
|
t.push_mut(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.dependencies.is_empty() {
|
2019-12-07 04:03:03 -08:00
|
|
|
let mut dependencies = Tree::atom("deps");
|
|
|
|
|
|
|
|
for dependency in &self.dependencies {
|
|
|
|
let mut d = Tree::atom(dependency.recipe.lexeme());
|
|
|
|
|
|
|
|
for argument in &dependency.arguments {
|
|
|
|
d.push_mut(argument.tree());
|
|
|
|
}
|
|
|
|
|
|
|
|
dependencies.push_mut(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
t.push_mut(dependencies);
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.body.is_empty() {
|
|
|
|
t.push_mut(Tree::atom("body").extend(self.body.iter().map(|line| line.tree())));
|
|
|
|
}
|
|
|
|
|
|
|
|
t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Parameter<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
let mut children = Vec::new();
|
|
|
|
children.push(Tree::atom(self.name.lexeme()));
|
|
|
|
|
|
|
|
if let Some(default) = &self.default {
|
|
|
|
children.push(default.tree());
|
|
|
|
}
|
|
|
|
|
|
|
|
Tree::List(children)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Line<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
Tree::list(self.fragments.iter().map(|fragment| fragment.tree()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Fragment<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
match self {
|
|
|
|
Fragment::Text { token } => Tree::string(token.lexeme()),
|
|
|
|
Fragment::Interpolation { expression } => Tree::List(vec![expression.tree()]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 23:17:47 -08:00
|
|
|
impl<'src> Node<'src> for Set<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
2020-10-26 18:16:42 -07:00
|
|
|
let mut set = Tree::atom(Keyword::Set.lexeme());
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2021-03-28 22:38:07 -07:00
|
|
|
set.push_mut(self.name.lexeme().replace('-', "_"));
|
2019-11-10 23:17:47 -08:00
|
|
|
|
|
|
|
use Setting::*;
|
|
|
|
match &self.value {
|
2021-04-24 18:29:58 -07:00
|
|
|
DotenvLoad(value) | Export(value) | PositionalArguments(value) =>
|
|
|
|
set.push_mut(value.to_string()),
|
2019-11-10 23:17:47 -08:00
|
|
|
Shell(setting::Shell { command, arguments }) => {
|
|
|
|
set.push_mut(Tree::string(&command.cooked));
|
|
|
|
for argument in arguments {
|
|
|
|
set.push_mut(Tree::string(&argument.cooked));
|
|
|
|
}
|
2020-02-10 20:07:06 -08:00
|
|
|
},
|
2019-11-10 23:17:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 23:39:23 -07:00
|
|
|
impl<'src> Node<'src> for Warning {
|
2019-11-07 10:55:15 -08:00
|
|
|
fn tree(&self) -> Tree<'src> {
|
2021-03-28 23:39:23 -07:00
|
|
|
unreachable!()
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|