Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 interface NodeVisitor<T> { | |
| 6 T visitProgram(Program node); | |
| 7 | |
| 8 T visitBlock(Block node); | |
| 9 T visitExpressionStatement(ExpressionStatement node); | |
| 10 T visitEmptyStatement(EmptyStatement node); | |
| 11 T visitIf(If node); | |
| 12 T visitFor(For node); | |
| 13 T visitForIn(ForIn node); | |
| 14 T visitWhile(While node); | |
| 15 T visitDo(Do node); | |
| 16 T visitContinue(Continue node); | |
| 17 T visitBreak(Break node); | |
| 18 T visitReturn(Return node); | |
| 19 T visitThrow(Throw node); | |
| 20 T visitTry(Try node); | |
| 21 T visitCatch(Catch node); | |
| 22 T visitSwitch(Switch node); | |
| 23 T visitCase(Case node); | |
| 24 T visitDefault(Default node); | |
| 25 T visitFunctionDeclaration(FunctionDeclaration node); | |
| 26 T visitLabeledStatement(LabeledStatement node); | |
| 27 T visitLiteralStatement(LiteralStatement node); | |
| 28 | |
| 29 T visitLiteralExpression(LiteralExpression node); | |
| 30 T visitVariableDeclarationList(VariableDeclarationList node); | |
| 31 T visitSequence(Sequence node); | |
| 32 T visitAssignment(Assignment node); | |
| 33 T visitVariableInitialization(VariableInitialization node); | |
| 34 T visitConditional(Conditional cond); | |
| 35 T visitNew(New node); | |
| 36 T visitCall(Call node); | |
| 37 T visitBinary(Binary node); | |
| 38 T visitPrefix(Prefix node); | |
| 39 T visitPostfix(Postfix node); | |
| 40 | |
| 41 T visitVariableUse(VariableUse node); | |
| 42 T visitThis(This node); | |
| 43 T visitVariableDeclaration(VariableDeclaration node); | |
| 44 T visitParameter(Parameter node); | |
| 45 T visitAccess(PropertyAccess node); | |
| 46 | |
| 47 T visitNamedFunction(NamedFunction node); | |
| 48 T visitFun(Fun node); | |
| 49 | |
| 50 T visitLiteralBool(LiteralBool node); | |
| 51 T visitLiteralString(LiteralString node); | |
| 52 T visitLiteralNumber(LiteralNumber node); | |
| 53 T visitLiteralNull(LiteralNull node); | |
| 54 | |
| 55 T visitArrayInitialization(ArrayInitialization node); | |
| 56 T visitArrayElement(ArrayElement node); | |
| 57 T visitObjectLiteral(ObjectInitialization node); | |
| 58 T visitProperty(Property node); | |
| 59 T visitRegExpLiteral(RegExpLiteral node); | |
| 60 } | |
| 61 | |
| 62 class BaseVisitor<T> implements NodeVisitor { | |
| 63 T visitNode(Node node) { | |
| 64 node.visitChildren(this); | |
| 65 return null; | |
| 66 } | |
| 67 | |
| 68 T visitProgram(Program node) => visitNode(node); | |
| 69 | |
| 70 T visitStatement(Statement node) => visitNode(node); | |
| 71 T visitLoop(Loop node) => visitStatement(node); | |
| 72 T visitJump(Statement node) => visitStatement(node); | |
| 73 | |
| 74 T visitBlock(Block node) => visitStatement(node); | |
| 75 T visitExpressionStatement(ExpressionStatement node) | |
| 76 => visitStatement(node); | |
| 77 T visitEmptyStatement(EmptyStatement node) => visitStatement(node); | |
| 78 T visitIf(If node) => visitStatement(node); | |
| 79 T visitFor(For node) => visitLoop(node); | |
| 80 T visitForIn(ForIn node) => visitLoop(node); | |
| 81 T visitWhile(While node) => visitLoop(node); | |
| 82 T visitDo(Do node) => visitLoop(node); | |
| 83 T visitContinue(Continue node) => visitJump(node); | |
| 84 T visitBreak(Break node) => visitJump(node); | |
| 85 T visitReturn(Return node) => visitJump(node); | |
| 86 T visitThrow(Throw node) => visitJump(node); | |
| 87 T visitTry(Try node) => visitStatement(node); | |
| 88 T visitSwitch(Switch node) => visitStatement(node); | |
| 89 T visitFunctionDeclaration(FunctionDeclaration node) | |
| 90 => visitStatement(node); | |
| 91 T visitLabeledStatement(LabeledStatement node) => visitStatement(node); | |
| 92 T visitLiteralStatement(LiteralStatement node) => visitStatement(node); | |
| 93 | |
| 94 T visitCatch(Catch node) => visitNode(node); | |
| 95 T visitCase(Case node) => visitNode(node); | |
| 96 T visitDefault(Default node) => visitNode(node); | |
| 97 | |
| 98 T visitExpression(Expression node) => visitNode(node); | |
| 99 T visitVariableReference(VariableReference node) => visitExpression(node); | |
| 100 | |
| 101 T visitLiteralExpression(LiteralExpression node) => visitExpression(node); | |
| 102 T visitVariableDeclarationList(VariableDeclarationList node) | |
| 103 => visitExpression(node); | |
| 104 T visitSequence(Sequence node) => visitExpression(node); | |
| 105 T visitAssignment(Assignment node) => visitExpression(node); | |
| 106 T visitVariableInitialization(VariableInitialization node) { | |
| 107 if (node.value != null) { | |
| 108 visitAssignment(node); | |
| 109 } else { | |
| 110 visitExpression(node); | |
| 111 } | |
| 112 } | |
| 113 T visitConditional(Conditional node) => visitExpression(node); | |
| 114 T visitNew(New node) => visitExpression(node); | |
| 115 T visitCall(Call node) => visitExpression(node); | |
| 116 T visitBinary(Binary node) => visitCall(node); | |
| 117 T visitPrefix(Prefix node) => visitCall(node); | |
| 118 T visitPostfix(Postfix node) => visitCall(node); | |
| 119 T visitAccess(PropertyAccess node) => visitExpression(node); | |
| 120 | |
| 121 T visitVariableUse(VariableUse node) => visitVariableReference(node); | |
| 122 T visitVariableDeclaration(VariableDeclaration node) | |
| 123 => visitVariableReference(node); | |
| 124 T visitParameter(Parameter node) => visitVariableDeclaration(node); | |
| 125 T visitThis(This node) => visitParameter(node); | |
| 126 | |
| 127 T visitNamedFunction(NamedFunction node) => visitExpression(node); | |
| 128 T visitFun(Fun node) => visitExpression(node); | |
| 129 | |
| 130 T visitLiteral(Literal node) => visitExpression(node); | |
| 131 | |
| 132 T visitLiteralBool(LiteralBool node) => visitLiteral(node); | |
| 133 T visitLiteralString(LiteralString node) => visitLiteral(node); | |
| 134 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node); | |
| 135 T visitLiteralNull(LiteralNull node) => visitLiteral(node); | |
| 136 | |
| 137 T visitArrayInitialization(ArrayInitialization node) => visitExpression(node); | |
| 138 T visitArrayElement(ArrayElement node) => visitNode(node); | |
| 139 T visitObjectLiteral(ObjectInitialization node) => visitExpression(node); | |
|
Lasse Reichstein Nielsen
2012/08/10 11:54:51
Inconsistent naming.
The ES spec uses "ObjectIniti
floitsch
2012/08/10 13:59:01
Done.
| |
| 140 T visitProperty(Property node) => visitNode(node); | |
| 141 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node); | |
| 142 } | |
| 143 | |
| 144 class Node { | |
| 145 var sourcePosition; | |
| 146 var endSourcePosition; | |
| 147 | |
| 148 abstract accept(NodeVisitor visitor); | |
| 149 abstract void visitChildren(NodeVisitor visitor); | |
| 150 } | |
| 151 | |
| 152 class Program extends Node { | |
| 153 final List<Statement> body; | |
| 154 Program(this.body); | |
| 155 | |
| 156 accept(NodeVisitor visitor) => visitor.visitProgram(this); | |
| 157 void visitChildren(NodeVisitor visitor) { | |
|
Lasse Reichstein Nielsen
2012/08/10 11:54:51
Consider having an empty line between implemented
floitsch
2012/08/10 13:59:01
Done.
| |
| 158 for (Statement statement in body) statement.accept(visitor); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 abstract class Statement extends Node { | |
| 163 } | |
| 164 | |
| 165 class Block extends Statement { | |
| 166 final List<Statement> statements; | |
| 167 Block(this.statements); | |
| 168 Block.empty() : this.statements = <Statement>[]; | |
| 169 | |
| 170 accept(NodeVisitor visitor) => visitor.visitBlock(this); | |
| 171 void visitChildren(NodeVisitor visitor) { | |
| 172 for (Statement statement in statements) statement.accept(visitor); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 class ExpressionStatement extends Statement { | |
| 177 final Expression expression; | |
| 178 ExpressionStatement(this.expression); | |
| 179 | |
| 180 accept(NodeVisitor visitor) => visitor.visitExpressionStatement(this); | |
| 181 void visitChildren(NodeVisitor visitor) { expression.accept(visitor); } | |
| 182 } | |
| 183 | |
| 184 class EmptyStatement extends Statement { | |
| 185 EmptyStatement(); | |
| 186 | |
| 187 accept(NodeVisitor visitor) => visitor.visitEmptyStatement(this); | |
| 188 void visitChildren(NodeVisitor visitor) {} | |
| 189 } | |
| 190 | |
| 191 class If extends Statement { | |
| 192 final Expression condition; | |
| 193 final Node then; | |
| 194 final Node otherwise; | |
| 195 If(this.condition, this.then, this.otherwise); | |
| 196 If.then(this.condition, this.then) : this.otherwise = new EmptyStatement(); | |
| 197 bool get hasElse() => otherwise is !EmptyStatement; | |
| 198 | |
| 199 accept(NodeVisitor visitor) => visitor.visitIf(this); | |
| 200 void visitChildren(NodeVisitor visitor) { | |
| 201 condition.accept(visitor); | |
| 202 then.accept(visitor); | |
| 203 otherwise.accept(visitor); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 abstract class Loop extends Statement { | |
| 208 final Statement body; | |
| 209 Loop(this.body); | |
| 210 } | |
| 211 | |
| 212 class For extends Loop { | |
| 213 final Expression init; | |
| 214 final Expression condition; | |
| 215 final Expression update; | |
| 216 For(this.init, this.condition, this.update, Statement body) : super(body); | |
| 217 | |
| 218 accept(NodeVisitor visitor) => visitor.visitFor(this); | |
| 219 void visitChildren(NodeVisitor visitor) { | |
| 220 if (init !== null) init.accept(visitor); | |
| 221 if (condition !== null) condition.accept(visitor); | |
| 222 if (update !== null) update.accept(visitor); | |
| 223 body.accept(visitor); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 class ForIn extends Loop { | |
| 228 // Note that [VariableDeclarationList] is a subclass of [Expression]. | |
| 229 // Therefore we can type the leftHandSide as [Expression]. | |
| 230 final Expression leftHandSide; | |
| 231 final Expression object; | |
| 232 ForIn(this.leftHandSide, this.object, Statement body) : super(body); | |
| 233 | |
| 234 accept(NodeVisitor visitor) => visitor.visitForIn(this); | |
| 235 void visitChildren(NodeVisitor visitor) { | |
| 236 leftHandSide.accept(visitor); | |
| 237 object.accept(visitor); | |
| 238 body.accept(visitor); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 class While extends Loop { | |
| 243 final Node condition; | |
| 244 While(this.condition, Statement body) : super(body); | |
| 245 | |
| 246 accept(NodeVisitor visitor) => visitor.visitWhile(this); | |
| 247 void visitChildren(NodeVisitor visitor) { | |
| 248 condition.accept(visitor); | |
| 249 body.accept(visitor); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 class Do extends Loop { | |
| 254 final Expression condition; | |
| 255 Do(Statement body, this.condition) : super(body); | |
| 256 | |
| 257 accept(NodeVisitor visitor) => visitor.visitDo(this); | |
| 258 void visitChildren(NodeVisitor visitor) { | |
| 259 body.accept(visitor); | |
| 260 condition.accept(visitor); | |
| 261 } | |
| 262 } | |
| 263 | |
| 264 class Continue extends Statement { | |
| 265 final String targetLabel; // Can be null. | |
| 266 Continue(this.targetLabel); | |
| 267 | |
| 268 accept(NodeVisitor visitor) => visitor.visitContinue(this); | |
| 269 void visitChildren(NodeVisitor visitor) {} | |
| 270 } | |
| 271 | |
| 272 class Break extends Statement { | |
| 273 final String targetLabel; // Can be null. | |
| 274 Break(this.targetLabel); | |
| 275 | |
| 276 accept(NodeVisitor visitor) => visitor.visitBreak(this); | |
| 277 void visitChildren(NodeVisitor visitor) {} | |
| 278 } | |
| 279 | |
| 280 class Return extends Statement { | |
| 281 final Expression value; // Can be null. | |
| 282 Return([this.value = null]); | |
| 283 | |
| 284 accept(NodeVisitor visitor) => visitor.visitReturn(this); | |
| 285 void visitChildren(NodeVisitor visitor) { | |
| 286 if (value != null) value.accept(visitor); | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 class Throw extends Statement { | |
| 291 final Expression expression; | |
| 292 Throw(this.expression); | |
| 293 | |
| 294 accept(NodeVisitor visitor) => visitor.visitThrow(this); | |
| 295 void visitChildren(NodeVisitor visitor) { | |
| 296 expression.accept(visitor); | |
| 297 } | |
| 298 } | |
| 299 | |
| 300 class Try extends Statement { | |
| 301 final Block body; | |
| 302 final Catch catchPart; // Can be null if [finallyPart] is non-null. | |
| 303 final Block finallyPart; // Can be null if [catchPart] is non-null. | |
| 304 Try(this.body, this.catchPart, this.finallyPart) { | |
| 305 assert(catchPart != null || finallyPart != null); | |
| 306 } | |
| 307 | |
| 308 accept(NodeVisitor visitor) => visitor.visitTry(this); | |
| 309 void visitChildren(NodeVisitor visitor) { | |
| 310 body.accept(visitor); | |
| 311 if (catchPart !== null) catchPart.accept(visitor); | |
| 312 if (finallyPart !== null) finallyPart.accept(visitor); | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 class Catch extends Node { | |
| 317 final VariableDeclaration declaration; | |
| 318 final Block body; | |
| 319 Catch(this.declaration, this.body); | |
| 320 | |
| 321 accept(NodeVisitor visitor) => visitor.visitCatch(this); | |
| 322 void visitChildren(NodeVisitor visitor) { | |
| 323 declaration.accept(visitor); | |
| 324 body.accept(visitor); | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 class Switch extends Statement { | |
| 329 final Expression key; | |
| 330 final List<SwitchClause> cases; | |
| 331 Switch(this.key, this.cases); | |
| 332 | |
| 333 accept(NodeVisitor visitor) => visitor.visitSwitch(this); | |
| 334 void visitChildren(NodeVisitor visitor) { | |
| 335 key.accept(visitor); | |
| 336 for (SwitchClause clause in cases) clause.accept(visitor); | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 abstract class SwitchClause extends Node { | |
| 341 final Block body; | |
| 342 SwitchClause(this.body); | |
| 343 } | |
| 344 | |
| 345 class Case extends SwitchClause { | |
| 346 final Expression expression; | |
| 347 Case(this.expression, Block body) : super(body); | |
| 348 | |
| 349 accept(NodeVisitor visitor) => visitor.visitCase(this); | |
| 350 void visitChildren(NodeVisitor visitor) { | |
| 351 expression.accept(visitor); | |
| 352 body.accept(visitor); | |
| 353 } | |
| 354 } | |
| 355 | |
| 356 class Default extends SwitchClause { | |
| 357 Default(Block body) : super(body); | |
| 358 | |
| 359 accept(NodeVisitor visitor) => visitor.visitDefault(this); | |
| 360 void visitChildren(NodeVisitor visitor) { | |
| 361 body.accept(visitor); | |
| 362 } | |
| 363 } | |
| 364 | |
| 365 class FunctionDeclaration extends Statement { | |
| 366 final VariableDeclaration name; | |
| 367 final Fun function; | |
| 368 FunctionDeclaration(this.name, this.function); | |
| 369 | |
| 370 accept(NodeVisitor visitor) => visitor.visitFunctionDeclaration(this); | |
| 371 void visitChildren(NodeVisitor visitor) { | |
| 372 name.accept(visitor); | |
| 373 function.accept(visitor); | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 class LabeledStatement extends Statement { | |
| 378 final String label; | |
| 379 final Statement body; | |
| 380 LabeledStatement(this.label, this.body); | |
| 381 | |
| 382 accept(NodeVisitor visitor) => visitor.visitLabeledStatement(this); | |
| 383 void visitChildren(NodeVisitor visitor) { | |
| 384 body.accept(visitor); | |
| 385 } | |
| 386 } | |
| 387 | |
| 388 class LiteralStatement extends Statement { | |
| 389 final String code; | |
| 390 LiteralStatement(this.code); | |
| 391 | |
| 392 accept(NodeVisitor visitor) => visitor.visitLiteralStatement(this); | |
| 393 void visitChildren(NodeVisitor visitor) { } | |
| 394 } | |
| 395 | |
| 396 abstract class Expression extends Node { | |
| 397 abstract int get precedenceLevel(); | |
| 398 } | |
| 399 | |
| 400 class LiteralExpression extends Expression { | |
| 401 final String template; | |
| 402 final List<Expression> inputs; | |
| 403 LiteralExpression(this.template) : inputs = const []; | |
| 404 LiteralExpression.withData(this.template, this.inputs); | |
| 405 | |
| 406 accept(NodeVisitor visitor) => visitor.visitLiteralExpression(this); | |
| 407 void visitChildren(NodeVisitor visitor) { | |
| 408 for (Expression expr in inputs) expr.accept(visitor); | |
| 409 } | |
| 410 | |
| 411 int get precedenceLevel() => EXPRESSION; | |
| 412 } | |
| 413 | |
| 414 /** | |
| 415 * [VariableDeclarationList] is a subclass of [Expression] to simplify the | |
| 416 * AST. | |
| 417 */ | |
| 418 class VariableDeclarationList extends Expression { | |
| 419 final List<VariableInitialization> declarations; | |
| 420 VariableDeclarationList(this.declarations); | |
| 421 | |
| 422 accept(NodeVisitor visitor) => visitor.visitVariableDeclarationList(this); | |
| 423 void visitChildren(NodeVisitor visitor) { | |
| 424 for (VariableInitialization declaration in declarations) { | |
| 425 declaration.accept(visitor); | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 int get precedenceLevel() => EXPRESSION; | |
| 430 } | |
| 431 | |
| 432 class Sequence extends Expression { | |
| 433 final List<Expression> expressions; | |
| 434 Sequence(this.expressions); | |
| 435 | |
| 436 accept(NodeVisitor visitor) => visitor.visitSequence(this); | |
| 437 void visitChildren(NodeVisitor visitor) { | |
| 438 for (Expression expr in expressions) expr.accept(visitor); | |
| 439 } | |
| 440 | |
| 441 int get precedenceLevel() => EXPRESSION; | |
| 442 } | |
| 443 | |
| 444 class Assignment extends Expression { | |
| 445 final Expression leftHandSide; | |
| 446 // Null, if the assignment is not compound. | |
| 447 final VariableReference compoundTarget; | |
| 448 final Expression value; // May be null, for [VariableInitialization]s. | |
| 449 | |
| 450 Assignment(this.leftHandSide, this.value) : compoundTarget = null; | |
| 451 Assignment.compound(this.leftHandSide, String op, this.value) | |
| 452 : compoundTarget = new VariableUse(op); | |
| 453 | |
| 454 int get precedenceLevel() => ASSIGNMENT; | |
| 455 | |
| 456 bool get isCompound() => compoundTarget != null; | |
| 457 String get op() => compoundTarget == null ? null : compoundTarget.name; | |
| 458 | |
| 459 accept(NodeVisitor visitor) => visitor.visitAssignment(this); | |
| 460 void visitChildren(NodeVisitor visitor) { | |
| 461 leftHandSide.accept(visitor); | |
| 462 if (compoundTarget != null) compoundTarget.accept(visitor); | |
| 463 if (value != null) value.accept(visitor); | |
| 464 } | |
| 465 } | |
| 466 | |
| 467 class VariableInitialization extends Assignment { | |
| 468 /** [value] may be null. */ | |
| 469 VariableInitialization(VariableDeclaration declaration, Expression value) | |
| 470 : super(declaration, value); | |
| 471 | |
| 472 VariableDeclaration get declaration() => leftHandSide; | |
| 473 | |
| 474 accept(NodeVisitor visitor) => visitor.visitVariableInitialization(this); | |
| 475 } | |
| 476 | |
| 477 class Conditional extends Expression { | |
| 478 final Expression condition; | |
| 479 final Expression then; | |
| 480 final Expression otherwise; | |
| 481 Conditional(this.condition, this.then, this.otherwise); | |
| 482 | |
| 483 accept(NodeVisitor visitor) => visitor.visitConditional(this); | |
| 484 void visitChildren(NodeVisitor visitor) { | |
| 485 condition.accept(visitor); | |
| 486 then.accept(visitor); | |
| 487 otherwise.accept(visitor); | |
| 488 } | |
| 489 | |
| 490 int get precedenceLevel() => ASSIGNMENT; | |
| 491 } | |
| 492 | |
| 493 class Call extends Expression { | |
| 494 Expression target; | |
| 495 List<Expression> arguments; | |
| 496 Call(this.target, this.arguments); | |
| 497 | |
| 498 accept(NodeVisitor visitor) => visitor.visitCall(this); | |
| 499 void visitChildren(NodeVisitor visitor) { | |
| 500 target.accept(visitor); | |
| 501 for (Expression arg in arguments) arg.accept(visitor); | |
| 502 } | |
| 503 | |
| 504 int get precedenceLevel() => CALL; | |
| 505 } | |
| 506 | |
| 507 class New extends Call { | |
| 508 New(Expression cls, List<Expression> arguments) : super(cls, arguments); | |
| 509 | |
| 510 accept(NodeVisitor visitor) => visitor.visitNew(this); | |
| 511 } | |
| 512 | |
| 513 class Binary extends Call { | |
| 514 Binary(String op, Expression left, Expression right) | |
| 515 : super(new VariableUse(op), <Expression>[left, right]); | |
| 516 | |
| 517 String get op() => (target as VariableUse).name; | |
| 518 Expression get left() => arguments[0]; | |
| 519 Expression get right() => arguments[1]; | |
| 520 | |
| 521 accept(NodeVisitor visitor) => visitor.visitBinary(this); | |
| 522 | |
| 523 int get precedenceLevel() { | |
| 524 switch (op) { | |
|
Lasse Reichstein Nielsen
2012/08/10 11:54:51
For a later version, you could consider doing look
floitsch
2012/08/10 13:59:01
added TODO.
| |
| 525 case "*": | |
| 526 case "/": | |
| 527 case "%": | |
| 528 return MULTIPLICATIVE; | |
| 529 case "+": | |
| 530 case "-": | |
| 531 return ADDITIVE; | |
| 532 case "<<": | |
| 533 case ">>": | |
| 534 case ">>>": | |
| 535 return SHIFT; | |
| 536 case "<": | |
| 537 case ">": | |
| 538 case "<=": | |
| 539 case ">=": | |
| 540 case "instanceof": | |
| 541 case "in": | |
| 542 return RELATIONAL; | |
| 543 case "==": | |
| 544 case "===": | |
| 545 case "!=": | |
| 546 case "!==": | |
| 547 return EQUALITY; | |
| 548 case "&": | |
| 549 return BIT_AND; | |
| 550 case "^": | |
| 551 return BIT_XOR; | |
| 552 case "|": | |
| 553 return BIT_OR; | |
| 554 case "&&": | |
| 555 return LOGICAL_AND; | |
| 556 case "||": | |
| 557 return LOGICAL_OR; | |
| 558 default: | |
| 559 throw new leg.CompilerCancelledException( | |
| 560 "Internal Error: Unhandled binary operator: $op"); | |
| 561 } | |
| 562 } | |
| 563 } | |
| 564 | |
| 565 class Prefix extends Call { | |
| 566 Prefix(String op, Expression arg) | |
| 567 : super(new VariableUse(op), <Expression>[arg]); | |
| 568 | |
| 569 String get op() => (target as VariableUse).name; | |
| 570 Expression get argument() => arguments[0]; | |
| 571 | |
| 572 accept(NodeVisitor visitor) => visitor.visitPrefix(this); | |
| 573 | |
| 574 int get precedenceLevel() => UNARY; | |
| 575 } | |
| 576 | |
| 577 class Postfix extends Call { | |
| 578 Postfix(String op, Expression arg) | |
| 579 : super(new VariableUse(op), <Expression>[arg]); | |
| 580 | |
| 581 String get op() => (target as VariableUse).name; | |
| 582 Expression get argument() => arguments[0]; | |
| 583 | |
| 584 accept(NodeVisitor visitor) => visitor.visitPostfix(this); | |
| 585 | |
| 586 int get precedenceLevel() => UNARY; | |
| 587 } | |
| 588 | |
| 589 abstract class VariableReference extends Expression { | |
| 590 final String name; | |
| 591 | |
| 592 // We treat operators as if they were special functions. They can thus be | |
| 593 // referenced like other variables. | |
| 594 VariableReference(this.name); | |
| 595 | |
| 596 abstract accept(NodeVisitor visitor); | |
| 597 int get precedenceLevel() => PRIMARY; | |
| 598 void visitChildren(NodeVisitor visitor) {} | |
| 599 } | |
| 600 | |
| 601 class VariableUse extends VariableReference { | |
| 602 VariableUse(String name) : super(name); | |
| 603 | |
| 604 accept(NodeVisitor visitor) => visitor.visitVariableUse(this); | |
| 605 } | |
| 606 | |
| 607 class VariableDeclaration extends VariableReference { | |
| 608 VariableDeclaration(String name) : super(name); | |
| 609 | |
| 610 accept(NodeVisitor visitor) => visitor.visitVariableDeclaration(this); | |
| 611 } | |
| 612 | |
| 613 class Parameter extends VariableDeclaration { | |
| 614 Parameter(String id) : super(id); | |
| 615 | |
| 616 accept(NodeVisitor visitor) => visitor.visitParameter(this); | |
| 617 } | |
| 618 | |
| 619 class This extends Parameter { | |
| 620 This() : super("this"); | |
| 621 | |
| 622 accept(NodeVisitor visitor) => visitor.visitThis(this); | |
| 623 } | |
| 624 | |
| 625 class NamedFunction extends Expression { | |
| 626 final VariableDeclaration name; | |
| 627 final Fun function; | |
| 628 NamedFunction(this.name, this.function); | |
| 629 | |
| 630 accept(NodeVisitor visitor) => visitor.visitNamedFunction(this); | |
| 631 void visitChildren(NodeVisitor visitor) { | |
| 632 name.accept(visitor); | |
| 633 function.accept(visitor); | |
| 634 } | |
| 635 | |
| 636 int get precedenceLevel() => CALL; | |
| 637 } | |
| 638 | |
| 639 class Fun extends Expression { | |
| 640 final List<Parameter> params; | |
| 641 final Block body; | |
| 642 Fun(this.params, this.body); | |
| 643 | |
| 644 accept(NodeVisitor visitor) => visitor.visitFun(this); | |
| 645 void visitChildren(NodeVisitor visitor) { | |
| 646 for (Parameter param in params) param.accept(visitor); | |
| 647 body.accept(visitor); | |
| 648 } | |
| 649 | |
| 650 int get precedenceLevel() => CALL; | |
| 651 } | |
| 652 | |
| 653 class PropertyAccess extends Expression { | |
| 654 final Expression receiver; | |
| 655 final Expression selector; | |
| 656 PropertyAccess(this.receiver, this.selector); | |
| 657 PropertyAccess.field(this.receiver, String fieldName) | |
| 658 : selector = new LiteralString("'$fieldName'"); | |
| 659 | |
| 660 accept(NodeVisitor visitor) => visitor.visitAccess(this); | |
| 661 void visitChildren(NodeVisitor visitor) { | |
| 662 receiver.accept(visitor); | |
| 663 selector.accept(visitor); | |
| 664 } | |
| 665 | |
| 666 int get precedenceLevel() => CALL; | |
| 667 } | |
| 668 | |
| 669 abstract class Literal extends Expression { | |
| 670 void visitChildren(NodeVisitor visitor) {} | |
| 671 | |
| 672 int get precedenceLevel() => PRIMARY; | |
| 673 } | |
| 674 | |
| 675 class LiteralBool extends Literal { | |
| 676 final bool value; | |
| 677 LiteralBool(this.value); | |
| 678 | |
| 679 accept(NodeVisitor visitor) => visitor.visitLiteralBool(this); | |
| 680 // [visitChildren] inherited from [Literal]. | |
| 681 } | |
| 682 | |
| 683 class LiteralNull extends Literal { | |
| 684 LiteralNull(); | |
| 685 | |
| 686 accept(NodeVisitor visitor) => visitor.visitLiteralNull(this); | |
| 687 } | |
| 688 | |
| 689 class LiteralString extends Literal { | |
| 690 final String value; | |
| 691 LiteralString(this.value); | |
| 692 | |
| 693 accept(NodeVisitor visitor) => visitor.visitLiteralString(this); | |
| 694 } | |
| 695 | |
| 696 class LiteralNumber extends Literal { | |
| 697 final String value; | |
| 698 LiteralNumber(this.value); | |
| 699 | |
| 700 accept(NodeVisitor visitor) => visitor.visitLiteralNumber(this); | |
| 701 } | |
| 702 | |
| 703 class ArrayInitialization extends Expression { | |
| 704 final int length; | |
| 705 // We represent the array as sparse list of elements. Each element knows its | |
| 706 // position in the array. | |
| 707 final List<ArrayElement> elements; | |
| 708 ArrayInitialization(this.length, this.elements); | |
| 709 | |
| 710 accept(NodeVisitor visitor) => visitor.visitArrayInitialization(this); | |
| 711 void visitChildren(NodeVisitor visitor) { | |
| 712 for (ArrayElement element in elements) element.accept(visitor); | |
| 713 } | |
| 714 | |
| 715 int get precedenceLevel() => PRIMARY; | |
| 716 } | |
| 717 | |
| 718 /** | |
| 719 * An expression inside an [ArrayInitialization]. An [ArrayElement] knows | |
| 720 * its position in the containing [ArrayInitialization]. | |
| 721 */ | |
| 722 class ArrayElement extends Node { | |
| 723 int index; | |
| 724 Expression value; | |
| 725 ArrayElement(this.index, this.value); | |
| 726 | |
| 727 accept(NodeVisitor visitor) => visitor.visitArrayElement(this); | |
| 728 void visitChildren(NodeVisitor visitor) { | |
| 729 value.accept(visitor); | |
| 730 } | |
| 731 } | |
| 732 | |
| 733 class ObjectInitialization extends Expression { | |
| 734 List<Property> properties; | |
| 735 ObjectInitialization(this.properties); | |
| 736 | |
| 737 accept(NodeVisitor visitor) => visitor.visitObjectLiteral(this); | |
| 738 void visitChildren(NodeVisitor visitor) { | |
| 739 for (Property init in properties) init.accept(visitor); | |
| 740 } | |
| 741 | |
| 742 int get precedenceLevel() => PRIMARY; | |
| 743 } | |
| 744 | |
| 745 class Property extends Node { | |
| 746 Literal name; | |
| 747 Expression value; | |
| 748 Property(this.name, this.value); | |
| 749 | |
| 750 accept(NodeVisitor visitor) => visitor.visitProperty(this); | |
| 751 void visitChildren(NodeVisitor visitor) { | |
| 752 name.accept(visitor); | |
| 753 value.accept(visitor); | |
| 754 } | |
| 755 } | |
| 756 | |
| 757 /** | |
| 758 * [RegExpLiteral]s, despite being called "Literal", are not inheriting from | |
| 759 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and | |
| 760 * are thus not in the same category as numbers or strings. | |
| 761 */ | |
| 762 class RegExpLiteral extends Expression { | |
| 763 /** Contains the pattern and the flags.*/ | |
| 764 String pattern; | |
| 765 RegExpLiteral(this.pattern); | |
| 766 | |
| 767 accept(NodeVisitor visitor) => visitor.visitRegExpLiteral(this); | |
| 768 void visitChildren(NodeVisitor visitor) {} | |
| 769 | |
| 770 int get precedenceLevel() => PRIMARY; | |
| 771 } | |
| OLD | NEW |