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>;
|
|
|
|
}
|
|
|
|
|
2021-07-23 20:26:27 -07:00
|
|
|
impl<'src> Node<'src> for Ast<'src> {
|
2019-11-07 10:55:15 -08:00
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
Tree::atom("justfile")
|
2022-01-30 12:16:10 -08:00
|
|
|
.extend(self.items.iter().map(Node::tree))
|
|
|
|
.extend(self.warnings.iter().map(Node::tree))
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Item<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
match self {
|
|
|
|
Item::Alias(alias) => alias.tree(),
|
|
|
|
Item::Assignment(assignment) => assignment.tree(),
|
2021-06-08 01:01:27 -07:00
|
|
|
Item::Comment(comment) => comment.tree(),
|
2019-11-07 10:55:15 -08:00
|
|
|
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,
|
2021-09-16 16:45:56 -07:00
|
|
|
operator,
|
2020-10-26 18:16:42 -07:00
|
|
|
} => {
|
|
|
|
let mut tree = Tree::atom(Keyword::If.lexeme());
|
|
|
|
tree.push_mut(lhs.tree());
|
2021-09-16 16:45:56 -07:00
|
|
|
tree.push_mut(operator.to_string());
|
2020-10-26 18:16:42 -07:00
|
|
|
tree.push_mut(rhs.tree());
|
|
|
|
tree.push_mut(then.tree());
|
|
|
|
tree.push_mut(otherwise.tree());
|
|
|
|
tree
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-21 10:14:10 -08:00
|
|
|
Expression::Call { thunk } => {
|
2021-05-07 00:14:38 -07:00
|
|
|
use Thunk::*;
|
|
|
|
|
2019-11-21 10:14:10 -08:00
|
|
|
let mut tree = Tree::atom("call");
|
|
|
|
|
|
|
|
match thunk {
|
|
|
|
Nullary { name, .. } => tree.push_mut(name.lexeme()),
|
|
|
|
Unary { name, arg, .. } => {
|
|
|
|
tree.push_mut(name.lexeme());
|
|
|
|
tree.push_mut(arg.tree());
|
2021-09-16 06:44:40 -07: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());
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-10-14 17:00:58 -07:00
|
|
|
BinaryPlus {
|
|
|
|
name,
|
|
|
|
args: ([a, b], rest),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
tree.push_mut(name.lexeme());
|
|
|
|
tree.push_mut(a.tree());
|
|
|
|
tree.push_mut(b.tree());
|
|
|
|
for arg in rest {
|
|
|
|
tree.push_mut(arg.tree());
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 12:39:45 -07:00
|
|
|
Ternary {
|
|
|
|
name,
|
|
|
|
args: [a, b, c],
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
tree.push_mut(name.lexeme());
|
|
|
|
tree.push_mut(a.tree());
|
|
|
|
tree.push_mut(b.tree());
|
|
|
|
tree.push_mut(c.tree());
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-21 10:14:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
tree
|
2021-09-16 06:44:40 -07: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");
|
2021-07-22 00:20:25 -07:00
|
|
|
let mut subsequents = Tree::atom("sups");
|
2019-12-07 04:03:03 -08:00
|
|
|
|
2021-07-22 00:20:25 -07:00
|
|
|
for (i, dependency) in self.dependencies.iter().enumerate() {
|
2019-12-07 04:03:03 -08:00
|
|
|
let mut d = Tree::atom(dependency.recipe.lexeme());
|
|
|
|
|
|
|
|
for argument in &dependency.arguments {
|
|
|
|
d.push_mut(argument.tree());
|
|
|
|
}
|
|
|
|
|
2021-07-22 00:20:25 -07:00
|
|
|
if i < self.priors {
|
|
|
|
dependencies.push_mut(d);
|
|
|
|
} else {
|
|
|
|
subsequents.push_mut(d);
|
|
|
|
}
|
2019-12-07 04:03:03 -08:00
|
|
|
}
|
|
|
|
|
2021-07-22 00:20:25 -07:00
|
|
|
if let Tree::List(_) = dependencies {
|
|
|
|
t.push_mut(dependencies);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Tree::List(_) = subsequents {
|
|
|
|
t.push_mut(subsequents);
|
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !self.body.is_empty() {
|
2022-01-30 12:16:10 -08:00
|
|
|
t.push_mut(Tree::atom("body").extend(self.body.iter().map(Node::tree)));
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Node<'src> for Parameter<'src> {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
2021-05-07 00:14:38 -07:00
|
|
|
let mut children = vec![Tree::atom(self.name.lexeme())];
|
2019-11-07 10:55:15 -08:00
|
|
|
|
|
|
|
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> {
|
2022-01-30 12:16:10 -08:00
|
|
|
Tree::list(self.fragments.iter().map(Node::tree))
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2021-05-07 00:14:38 -07:00
|
|
|
use Setting::*;
|
2019-11-10 23:17:47 -08:00
|
|
|
|
2021-05-07 00:14:38 -07:00
|
|
|
let mut set = Tree::atom(Keyword::Set.lexeme());
|
2021-03-28 22:38:07 -07:00
|
|
|
set.push_mut(self.name.lexeme().replace('-', "_"));
|
2019-11-10 23:17:47 -08:00
|
|
|
|
|
|
|
match &self.value {
|
2022-02-14 18:37:06 -08:00
|
|
|
AllowDuplicateRecipes(value)
|
|
|
|
| DotenvLoad(value)
|
|
|
|
| Export(value)
|
|
|
|
| PositionalArguments(value)
|
|
|
|
| WindowsPowerShell(value) => {
|
2021-09-16 06:44:40 -07:00
|
|
|
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));
|
|
|
|
}
|
2021-09-16 06:44:40 -07: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
|
|
|
}
|
|
|
|
}
|
2021-06-08 01:01:27 -07:00
|
|
|
|
|
|
|
impl<'src> Node<'src> for str {
|
|
|
|
fn tree(&self) -> Tree<'src> {
|
|
|
|
Tree::atom("comment").push(["\"", self, "\""].concat())
|
|
|
|
}
|
|
|
|
}
|