/
AbstractSyntaxTree.scala
271 lines (229 loc) · 10.4 KB
/
AbstractSyntaxTree.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package ducttape.syntax
import scala.util.parsing.input.Positional
import java.io.File
object AbstractSyntaxTree {
/** Parent class of all types representing elements in an abstract syntax tree. */
trait ASTType extends Positional {
private var _file = new File("unknown_file")
def declaringFile_=(f: File) { _file = f }
def declaringFile: File = _file
// used for traversing AST... abstractly
def children: Seq[ASTType]
}
class Comments(val value: Option[String]) extends ASTType {
override def children = Nil
override def toString() = value match {
case Some(s:String) => s
case None => ""
}
}
/** Type of the right hand side in an assignment. */
trait RValue extends ASTType;
/** Unbound is the right hand side type in an underspecified variable declaration.
*
* Conceptually, Unbound can be thought of as the None case if one were to define Option[+RValue].
*/
case class Unbound() extends RValue {
override def children = Nil
override def toString() = ""
}
/** Type of a literal string value, in the right-hand side context of an assignment. */
case class Literal(val value: String) extends RValue {
override def children = Nil
override def toString() = "Literal='%s'".format(value)
}
/** Type of a variable reference, in the right-hand side context of an assignment. */
case class ConfigVariable(val value: String) extends RValue {
override def children = Nil
override def toString() = "$%s".format(value)
}
/** Type of a variable reference attached to a specific task,
* in the right-hand side context of an assignment. */
case class TaskVariable(val taskName: String, val value: String) extends RValue {
override def children = Nil
override def toString() = "$%s@%s".format(value,taskName)
}
/**
* Type of a branch point that defines a sequence,
* in the right-hand side context of an assignment.
*/
case class SequentialBranchPoint(val branchPointName: Option[String],
val start: BigDecimal,
val end: BigDecimal,
val increment: BigDecimal) extends RValue {
override def children = Nil
override def toString() = {
branchPointName match {
case Some(s) => "(%s: %s..%s..%s)".format(s,start,end,increment)
case None => "(%s..%s..%s)".format(start,end,increment)
}
}
}
/** Pair containing a branch point name and a branch name. */
class BranchGraftElement(val branchPointName: String,
val branchName: String) extends ASTType {
override def children = Nil
override def toString() = "%s:%s".format(branchPointName,branchName)
}
/** Type of a branch graft, in the right-hand side context of an assignment. */
case class BranchGraft(val variableName: String,
val taskName: String,
val branchGraftElements: Seq[BranchGraftElement]) extends RValue {
override def children = branchGraftElements
override def toString() =
"$%s@%s%s".format(variableName,taskName,branchGraftElements.toString())
}
/**
* Abstract specification of a variable name and its right hand side.
*/
class AbstractSpec[+A <: RValue](val name: String, val rval: A, val dotVariable: Boolean) extends ASTType {
override def children = Seq(rval)
override def hashCode() = name.hashCode()
override def equals(that: Any) = that match { case other: AbstractSpec[_] => (other.name == this.name) }
override def toString() = "%s=%s".format(name, rval)
}
type Spec = AbstractSpec[RValue]
type LiteralSpec = AbstractSpec[Literal]
/**
* A key=value assignment defined in a "config" definition or config file.
*/
class ConfigAssignment(val spec: Spec, val comments: Comments) extends ASTType {
override def children = Seq(spec, comments)
override def toString() = spec.toString()
}
trait Specs extends ASTType {
val specs: Seq[Spec]
val comments: Comments
}
case class TaskInputs(val specs: Seq[Spec], val comments: Comments) extends Specs {
override def children = specs ++ Seq(comments)
}
case class TaskOutputs(val specs: Seq[Spec], val comments: Comments) extends Specs {
override def children = specs ++ Seq(comments)
}
case class TaskParams(val specs: Seq[Spec], val comments: Comments) extends Specs {
override def children = specs ++ Seq(comments)
}
case class TaskPackageNames(val specs: Seq[Spec], val comments: Comments) extends Specs {
override def children = specs ++ Seq(comments)
}
/** Branch in a hyperworkflow, defined in the right hand side of a variable declaration. */
case class BranchPointDef(val name: Option[String], val specs: Seq[Spec]) extends RValue {
override def children = specs
override def toString() = {
name match {
case Some(s) => "(%s: %s)".format(s, specs.mkString(" "))
case None => "(%s)".format(specs.mkString(" "))
}
}
}
/** Reference, in a plan, to a branchpoint and one or more of its branches. */
case class BranchPointRef(val name: String, val branchNames: Seq[String]) extends ASTType {
override def children = Nil
override def toString() = {
"(%s: %s)".format(name, branchNames.mkString(" "))
}
}
// NOTE: This has been replaced by the bash parser and BashCode
class ShellCommands(val value: String) extends ASTType {
override def children = Nil
override def toString() = value
}
// class PackageNames(val comments:Comments, val packageNames: List[String]) extends Specs {
// override def toString = comments.toString() + "\n" + List(packageNames).mkString(" ")
// }
class TaskHeader(val specsList: List[Specs]) extends ASTType {
override def children = specsList
override def toString() = specsList.mkString(" ")
}
/** Defines a block of ducttape code, such as a task definition. */
trait Block extends ASTType;
/**
* Short for "TaskDefinition". Abbreviated due to its pervasiveness in the code.
*
* @param keyword The keyword used to declare this taskdef. Usually (always?) "task".
*/
class TaskDef(val comments: Comments,
val keyword: String,
val name: String,
val header: TaskHeader,
val commands: BashCode) extends Block {
override def children = Seq(comments, header, commands)
lazy val packageSpecList: Seq[TaskPackageNames] = header.specsList.collect{ case x: TaskPackageNames => x }
lazy val inputSpecList: Seq[TaskInputs] = header.specsList.collect{ case x: TaskInputs => x }
lazy val outputSpecList: Seq[TaskOutputs] = header.specsList.collect{ case x: TaskOutputs => x }
lazy val paramSpecList: Seq[TaskParams] = header.specsList.collect{ case x: TaskParams => x }
// a few convenience methods:
lazy val packages: Seq[Spec] = packageSpecList.flatMap{_.specs}
lazy val inputs: Seq[Spec] = inputSpecList.flatMap{_.specs}
lazy val outputs: Seq[Spec] = outputSpecList.flatMap{_.specs}
lazy val params: Seq[Spec] = paramSpecList.flatMap{_.specs}
lazy val lastHeaderLine: Int = {
val n = header.specsList.flatMap{specs: Specs => specs.specs.map{spec: Spec => spec.pos.line}}.max
System.err.println("lazy header line... " + n)
n
}
override def hashCode() = name.hashCode()
override def equals(that: Any) = that match { case other: TaskDef => (other.name == this.name) }
override def toString() = name
}
type PackageDef = TaskDef;
type ActionDef = TaskDef
class CallDefinition(val comments: Comments,
val name: String,
val header: TaskHeader,
val functionName: String) extends Block {
override def children = Seq(comments, header)
override def toString() = name
}
class GroupDefinition(val comments: Comments,
val keyword: String,
val name: String,
val header: TaskHeader,
val blocks: Seq[Block]) extends Block {
override def children = Seq(comments, header) ++ blocks
override def toString() = name
}
type VersionerDef = GroupDefinition;
class ConfigDefinition(val keyword: String,
val comments: Comments,
val name: Option[String],
val lines: Seq[ConfigAssignment]) extends Block {
override def children = Seq(comments) ++ lines
override def toString() = {
name match {
case None => "GLOBAL"
case Some(s: String) => s
}
}
}
class CrossProduct(val goals: Seq[String], val value: Seq[BranchPointRef]) extends ASTType {
override def children = value
override def toString() = {
"reach %s via %s".format(goals.mkString(","),value.mkString(" * "))
}
}
class PlanDefinition(val comments: Comments,
val name: Option[String],
val crossProducts: Seq[CrossProduct]) extends Block {
override def children = Seq(comments) ++ crossProducts
override def toString() = name match {
case None => "GLOBAL"
case Some(s: String) => s
}
}
/** Ducttape hyperworkflow file. */
class WorkflowDefinition(val file: File, val blocks: Seq[Block]) extends ASTType {
override def children = blocks
lazy val plans: Seq[PlanDefinition] = blocks.collect{case x: PlanDefinition => x}
private lazy val groupLikes: Seq[GroupDefinition] = blocks.collect{case x: GroupDefinition => x}
lazy val versioners: Seq[VersionerDef] = groupLikes.filter{x: GroupDefinition => x.keyword == "versioner"}
private lazy val configLikes: Seq[ConfigDefinition] = blocks.collect{case x: ConfigDefinition => x}
lazy val configs: Seq[ConfigDefinition] = configLikes.filter{t: ConfigDefinition => t.keyword == "config"}
lazy val globals: Seq[ConfigAssignment] = configLikes.filter{t: ConfigDefinition => t.keyword == "global"}.flatMap{ _.lines}
private lazy val taskDefs: Seq[TaskDef] = blocks.collect{case x: TaskDef => x}
lazy val tasks: Seq[TaskDef] = taskDefs.filter{t: TaskDef => t.keyword == "task"}
lazy val packages: Seq[TaskDef] = taskDefs.filter{t: TaskDef => t.keyword == "package"}
override def toString() = blocks.mkString("\n\n")
}
}