forked from scalameta/scalafmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrees.scala
242 lines (171 loc) · 7.79 KB
/
Trees.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
/* __ *\
** ________ ___ / / ___ __ ____ Scala.js tools **
** / __/ __// _ | / / / _ | __ / // __/ (c) 2014, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ **
** /____/\___/_/ |_/____/_/ | |__/ /____/ **
** |/____/ **
\* */
package org.scalajs.core.tools.javascript
import scala.annotation.switch
import org.scalajs.core.ir
import ir.Position
import ir.Position.NoPosition
object Trees {
import ir.Trees.requireValidIdent
/** AST node of JavaScript. */
abstract sealed class Tree {
val pos: Position
def show: String = {
val writer = new java.io.StringWriter
val printer = new Printers.JSTreePrinter(writer)
printer.printTree(this, isStat = true)
writer.toString()
}
}
case object EmptyTree extends Tree {
val pos = NoPosition
}
// Comments
case class DocComment(text: String)(implicit val pos: Position) extends Tree
// Identifiers and properties
sealed trait PropertyName {
def name: String
def pos: Position
}
case class Ident(name: String, originalName: Option[String])(
implicit val pos: Position) extends PropertyName {
requireValidIdent(name)
}
object Ident {
def apply(name: String)(implicit pos: Position): Ident =
new Ident(name, Some(name))
}
// Definitions
sealed trait LocalDef extends Tree {
def name: Ident
def mutable: Boolean
def ref(implicit pos: Position): Tree = VarRef(name)
}
case class VarDef(name: Ident, rhs: Tree)(implicit val pos: Position) extends LocalDef {
def mutable: Boolean = true
}
/** ES6 let or const (depending on the mutable flag). */
case class Let(name: Ident, mutable: Boolean, rhs: Tree)(implicit val pos: Position) extends LocalDef
case class ParamDef(name: Ident, rest: Boolean)(implicit val pos: Position) extends LocalDef {
def mutable: Boolean = true
}
// Control flow constructs
case class Skip()(implicit val pos: Position) extends Tree
class Block private (val stats: List[Tree])(implicit val pos: Position) extends Tree {
override def toString(): String =
stats.mkString("Block(", ",", ")")
}
object Block {
def apply(stats: List[Tree])(implicit pos: Position): Tree = {
val flattenedStats = stats flatMap {
case Skip() => Nil
case Block(subStats) => subStats
case other => other :: Nil
}
flattenedStats match {
case Nil => Skip()
case only :: Nil => only
case _ => new Block(flattenedStats)
}
}
def apply(stats: Tree*)(implicit pos: Position): Tree =
apply(stats.toList)
def unapply(block: Block): Some[List[Tree]] = Some(block.stats)
}
case class Labeled(label: Ident, body: Tree)(implicit val pos: Position) extends Tree
case class Assign(lhs: Tree, rhs: Tree)(implicit val pos: Position) extends Tree {
require(lhs match {
case _:VarRef | _:DotSelect | _:BracketSelect => true
case _ => false
}, s"Invalid lhs for Assign: $lhs")
}
case class Return(expr: Tree)(implicit val pos: Position) extends Tree
case class If(cond: Tree, thenp: Tree, elsep: Tree)(implicit val pos: Position) extends Tree
case class While(cond: Tree, body: Tree, label: Option[Ident] = None)(implicit val pos: Position) extends Tree
case class DoWhile(body: Tree, cond: Tree, label: Option[Ident] = None)(implicit val pos: Position) extends Tree
case class Try(block: Tree, errVar: Ident, handler: Tree, finalizer: Tree)(implicit val pos: Position) extends Tree
case class Throw(expr: Tree)(implicit val pos: Position) extends Tree
case class Break(label: Option[Ident] = None)(implicit val pos: Position) extends Tree
case class Continue(label: Option[Ident] = None)(implicit val pos: Position) extends Tree
case class Switch(selector: Tree, cases: List[(Tree, Tree)], default: Tree)(implicit val pos: Position) extends Tree
case class Debugger()(implicit val pos: Position) extends Tree
// Expressions
case class New(ctor: Tree, args: List[Tree])(implicit val pos: Position) extends Tree
case class DotSelect(qualifier: Tree, item: Ident)(implicit val pos: Position) extends Tree
case class BracketSelect(qualifier: Tree, item: Tree)(implicit val pos: Position) extends Tree
/** Syntactic apply.
* It is a method call if fun is a dot-select or bracket-select. It is a
* function call otherwise.
*/
case class Apply(fun: Tree, args: List[Tree])(implicit val pos: Position) extends Tree
/** `...items`, the "spread" operator of ECMAScript 6.
*
* It is only valid in ECMAScript 6, in the `args`/`items` of a [[New]],
* [[Apply]], or [[ArrayConstr]].
*
* @param items An iterable whose items will be spread
*/
case class Spread(items: Tree)(implicit val pos: Position) extends Tree
case class Delete(prop: Tree)(implicit val pos: Position) extends Tree {
require(prop match {
case _:DotSelect | _:BracketSelect => true
case _ => false
}, s"Invalid prop for Delete: $prop")
}
/** Unary operation (always preserves pureness).
*
* Operations which do not preserve pureness are not allowed in this tree.
* These are notably ++ and --
*/
case class UnaryOp(op: UnaryOp.Code, lhs: Tree)(implicit val pos: Position) extends Tree
object UnaryOp {
/** Codes are the same as in the IR. */
type Code = ir.Trees.JSUnaryOp.Code
}
/** Binary operation (always preserves pureness).
*
* Operations which do not preserve pureness are not allowed in this tree.
* These are notably +=, -=, *=, /= and %=
*/
case class BinaryOp(op: BinaryOp.Code, lhs: Tree, rhs: Tree)(implicit val pos: Position) extends Tree
object BinaryOp {
/** Codes are the same as in the IR. */
type Code = ir.Trees.JSBinaryOp.Code
}
case class ArrayConstr(items: List[Tree])(implicit val pos: Position) extends Tree
case class ObjectConstr(fields: List[(PropertyName, Tree)])(implicit val pos: Position) extends Tree
// Literals
/** Marker for literals. Literals are always pure. */
sealed trait Literal extends Tree
case class Undefined()(implicit val pos: Position) extends Literal
case class Null()(implicit val pos: Position) extends Literal
case class BooleanLiteral(value: Boolean)(implicit val pos: Position) extends Literal
case class IntLiteral(value: Int)(implicit val pos: Position) extends Literal
case class DoubleLiteral(value: Double)(implicit val pos: Position) extends Literal
case class StringLiteral(value: String)(
implicit val pos: Position) extends Literal with PropertyName {
override def name = value
}
// Atomic expressions
case class VarRef(ident: Ident)(implicit val pos: Position) extends Tree
case class This()(implicit val pos: Position) extends Tree
case class Function(args: List[ParamDef], body: Tree)(implicit val pos: Position) extends Tree
// Named function definition
case class FunctionDef(name: Ident, args: List[ParamDef], body: Tree)(
implicit val pos: Position) extends Tree
// ECMAScript 6 classes
case class ClassDef(className: Option[Ident], parentClass: Option[Tree],
members: List[Tree])(implicit val pos: Position) extends Tree
case class MethodDef(static: Boolean, name: PropertyName, args: List[ParamDef],
body: Tree)(implicit val pos: Position) extends Tree
case class GetterDef(static: Boolean, name: PropertyName,
body: Tree)(implicit val pos: Position) extends Tree
case class SetterDef(static: Boolean, name: PropertyName, param: ParamDef,
body: Tree)(implicit val pos: Position) extends Tree
case class Super()(implicit val pos: Position) extends Tree
}