/
JS.scala
226 lines (193 loc) · 7.85 KB
/
JS.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
package scalaxy.js.ast
object JS {
sealed trait Node extends Api {
def needsParen = false
def pos: SourcePos
}
trait Api {
self: Node =>
def select(name: String)(implicit pos: SourcePos) = Select(this, Ident(name))
def select(name: Node)(implicit pos: SourcePos) = Select(this, name)
def apply(args: List[Node])(implicit pos: SourcePos) = Apply(this, args)
def apply(name: String, args: List[Node])(implicit pos: SourcePos) = Apply(select(name), args)
def apply(name: Node, args: List[Node])(implicit pos: SourcePos) = Apply(select(name), args)
def op(op: String, rhs: Node)(implicit pos: SourcePos) = BinOp(this, op, rhs)
def preOp(op: String)(implicit pos: SourcePos) = PrefixOp(op, this)
def postOp(op: String)(implicit pos: SourcePos) = PostfixOp(this, op)
def assign(rhs: Node)(implicit pos: SourcePos): Assign = Assign(this, rhs)
def assign(ident: String)(implicit pos: SourcePos): Assign = assign(Ident(ident))
def asVar(name: String)(implicit pos: SourcePos) = VarDef(name, this)
}
def new_(name: String)(implicit pos: SourcePos) = New(Ident(name))
val binaryOperators = Set(
"+", "-", "*", "/", "%",
"<", "<=", ">", ">=", "==", "!=", "===", "!==", "||", "&&", "^^",
"<<", ">>", ">>>", "^", "&", "|"
)
case object NoNode extends Node {
def pos: SourcePos = null
}
case class BinOp(lhs: Node, op: String, rhs: Node)(implicit val pos: SourcePos) extends Node {
override def needsParen = true
}
case class Commented(comment: String, node: Node)(implicit val pos: SourcePos) extends Node
case class PrefixOp(op: String, a: Node)(implicit val pos: SourcePos) extends Node
case class PostfixOp(a: Node, op: String)(implicit val pos: SourcePos) extends Node
case class Block(body: List[Node])(implicit val pos: SourcePos) extends Node
case class If(condition: Node, thenNode: Node, elseNode: Node)(implicit val pos: SourcePos) extends Node
case class Function(name: Option[String], args: List[Node], body: Block)(implicit val pos: SourcePos) extends Node {
override def needsParen = true
}
case class New(target: Node)(implicit val pos: SourcePos) extends Node
case class Ident(name: String)(implicit val pos: SourcePos) extends Node
case class Literal(value: Any)(implicit val pos: SourcePos) extends Node
case class Assign(lhs: Node, rhs: Node)(implicit val pos: SourcePos) extends Node {
override def needsParen = true
}
case class VarDef(name: String, rhs: Node)(implicit val pos: SourcePos) extends Node
case class Select(target: Node, name: Node)(implicit val pos: SourcePos) extends Node
case class Apply(target: Node, args: List[Node])(implicit val pos: SourcePos) extends Node
case class Return(returnNode: Node)(implicit val pos: SourcePos) extends Node
case class Interpolation(fragments: List[String], values: List[Node])(implicit val pos: SourcePos) extends Node {
override def needsParen = true
}
case class JSONObject(map: Map[String, Node])(implicit val pos: SourcePos) extends Node
case class JSONArray(values: List[Node])(implicit val pos: SourcePos) extends Node
def newEmptyJSON(implicit pos: SourcePos) = JSONObject(Map()) // TODO: JSONObject
def path(path: String)(implicit pos: SourcePos): Node = {
path.split("\\.").map(JS.Ident(_): JS.Node).reduce(JS.Select(_, _))
}
def prettyPrint(node: Node, depth: Int = 0): PosAnnotatedString = {
val pos = node.pos
node match {
case JSONObject(map) =>
reduceOr(
map.toList.sortBy(_._1).map({
case (name, value) =>
indent(depth + 1) ++ prettyPrint(Literal(name)(pos), depth + 1) ++
a": " ++
prettyPrint(value, depth + 1)
}),
pos("{\n"), ",\n", a"\n" ++ indent(depth) ++ a"}",
pos("{}"))
case JSONArray(values) =>
reduceOr(
values.map(prettyPrint(_, depth)),
pos("["), ", ", pos("]"),
pos("[]"))
case Block(body) =>
reduceOr(
body.map(s => indent(depth + 1) ++ prettyPrint(s, depth + 1) ++ a";\n"),
pos("{\n"), "", indent(depth) ++ a"}",
pos("{}"))
case If(condition, thenNode, elseNode) =>
pos("if (") ++ prettyPrint(condition, depth) ++ a") " ++ prettyPrint(thenNode, depth) ++
(
if (elseNode == NoNode) a""
else a"\n" ++ indent(depth) ++ a"else " ++ prettyPrint(elseNode, depth)
)
case Interpolation(fragments, values) =>
(
fragments.zip(values).map({ case (s, a) =>
pos(s) ++ prettyPrint(a, depth)
}) :+ pos(fragments.last)
).reduce(_ ++ _)
case Return(returnNode) =>
pos("return ") ++ prettyPrint(returnNode, depth)
case Function(name: Option[String], args, body: Block) =>
pos("function" ++ name.map(" " ++ _).getOrElse("") ++ "(") ++
reduceOr(
args.map(prettyPrint(_, depth)),
a"", ", ", a"",
a""
) ++
a") " ++ prettyPrint(body, depth)
case Ident(name: String) =>
pos(name.trim)
case Literal(value: Any) =>
pos(value match {
case s: String =>
"'" +
s.replaceAll("\\\\", "\\\\")
.replaceAll("'", "\\\\'")
.replaceAll("\n", "\\\\n")
.replaceAll("\r", "\\\\r")
.replaceAll("\b", "\\\\b")
.replaceAll("\f", "\\\\f")
.replaceAll("\t", "\\\\t")
.replaceAll("\u000b", "\\\\v")
.replaceAll("\0", "\\\\0") +
"'" // TODO: proper JS escapes
case c @ ((_: java.lang.Character) | (_: Char)) =>
"'" + c + "'"
case _ =>
value.toString // TODO?
})
case NoNode =>
a""
case VarDef(name, rhs) =>
pos("var " + name + (if (rhs == NoNode) "" else " = ")) ++ prettyPrint(rhs, depth)
case Assign(lhs, rhs) =>
prettyPrint(lhs, depth) ++
pos(" = ") ++
prettyPrint(rhs, depth)
case Commented(comment, node) =>
val sub = prettyPrint(node, depth)
if (comment.contains("\n"))
pos(comment.replaceAll("\n", "\n" + indent(depth).value) + "\n") ++
indent(depth) ++ sub
else
pos(comment) ++ sub
case BinOp(lhs, op, rhs) =>
prettyPrint(lhs, depth) ++
pos(" " ++ op ++ " ") ++
prettyPrint(rhs, depth)
case PrefixOp(op, operand) =>
pos(op) ++
prettyPrint(operand, depth)
case New(target) =>
pos("new ") ++ prettyPrint(target, depth)
case PostfixOp(operand, op) =>
prettyPrint(operand, depth) ++
pos(op)
case Select(target, name) =>
wrapWithParenIfNeeded(target, pos, depth) ++
(
name match {
case Ident(_) =>
pos(".") ++ prettyPrint(name, depth)
case _ =>
pos("[") ++ prettyPrint(name, depth) ++ a"]"
}
)
case Apply(target, args) =>
val ts = prettyPrint(target, depth)
wrapWithParenIfNeeded(target, pos, depth) ++
reduceOr(
args.map(prettyPrint(_, depth)),
pos("("), ", ", pos(")"),
pos("()")
)
}
}
private def wrapWithParenIfNeeded(node: Node, pos: SourcePos, depth: Int) = {
val ts = prettyPrint(node, depth)
if (node.needsParen)
pos("(") ++ ts ++ pos(")")
else
ts
}
private def reduceOr(
col: List[PosAnnotatedString],
prefix: PosAnnotatedString, sep: String, suffix: PosAnnotatedString,
or: PosAnnotatedString) =
if (col.isEmpty) or
else prefix ++ col.reduce(_ ++ a"$sep" ++ _) ++ suffix
private lazy val indent = new collection.mutable.HashMap[Int, PosAnnotatedString]() {
override def default(depth: Int) = {
val s = PosAnnotatedString((1 to depth).map(i => " ").mkString(""))
this(depth) = s
s
}
}
}