| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of js_ast; | 5 part of js_ast; |
| 6 | 6 |
| 7 class TemplateManager { | 7 class TemplateManager { |
| 8 Map<String, Template> expressionTemplates = new Map<String, Template>(); | 8 Map<String, Template> expressionTemplates = new Map<String, Template>(); |
| 9 Map<String, Template> statementTemplates = new Map<String, Template>(); | 9 Map<String, Template> statementTemplates = new Map<String, Template>(); |
| 10 | 10 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 if (node is InterpolatedExpression) { | 193 if (node is InterpolatedExpression) { |
| 194 var nameOrPosition = node.nameOrPosition; | 194 var nameOrPosition = node.nameOrPosition; |
| 195 return (arguments) { | 195 return (arguments) { |
| 196 var value = arguments[nameOrPosition]; | 196 var value = arguments[nameOrPosition]; |
| 197 Expression toExpression(item) { | 197 Expression toExpression(item) { |
| 198 if (item is Expression) return item; | 198 if (item is Expression) return item; |
| 199 if (item is String) return new Identifier(item); | 199 if (item is String) return new Identifier(item); |
| 200 return error('Interpolated value #$nameOrPosition is not ' | 200 return error('Interpolated value #$nameOrPosition is not ' |
| 201 'an Expression or List of Expressions: $value'); | 201 'an Expression or List of Expressions: $value'); |
| 202 } | 202 } |
| 203 |
| 203 if (value is Iterable) return value.map(toExpression); | 204 if (value is Iterable) return value.map(toExpression); |
| 204 return toExpression(value); | 205 return toExpression(value); |
| 205 }; | 206 }; |
| 206 } | 207 } |
| 207 return visit(node); | 208 return visit(node); |
| 208 } | 209 } |
| 209 | 210 |
| 210 Instantiator visitInterpolatedLiteral(InterpolatedLiteral node) { | 211 Instantiator visitInterpolatedLiteral(InterpolatedLiteral node) { |
| 211 var nameOrPosition = node.nameOrPosition; | 212 var nameOrPosition = node.nameOrPosition; |
| 212 return (arguments) { | 213 return (arguments) { |
| 213 var value = arguments[nameOrPosition]; | 214 var value = arguments[nameOrPosition]; |
| 214 if (value is Literal) return value; | 215 if (value is Literal) return value; |
| 215 error('Interpolated value #$nameOrPosition is not a Literal: $value'); | 216 error('Interpolated value #$nameOrPosition is not a Literal: $value'); |
| 216 }; | 217 }; |
| 217 } | 218 } |
| 218 | 219 |
| 219 Instantiator visitInterpolatedParameter(InterpolatedParameter node) { | 220 Instantiator visitInterpolatedParameter(InterpolatedParameter node) { |
| 220 var nameOrPosition = node.nameOrPosition; | 221 var nameOrPosition = node.nameOrPosition; |
| 221 return (arguments) { | 222 return (arguments) { |
| 222 var value = arguments[nameOrPosition]; | 223 var value = arguments[nameOrPosition]; |
| 223 | 224 |
| 224 Parameter toIdentifier(item) { | 225 Parameter toIdentifier(item) { |
| 225 if (item is Parameter) return item; | 226 if (item is Parameter) return item; |
| 226 if (item is String) return new Identifier(item); | 227 if (item is String) return new Identifier(item); |
| 227 return error('Interpolated value #$nameOrPosition is not an Identifier' | 228 return error('Interpolated value #$nameOrPosition is not an Identifier' |
| 228 ' or List of Identifiers: $value'); | 229 ' or List of Identifiers: $value'); |
| 229 } | 230 } |
| 231 |
| 230 if (value is Iterable) return value.map(toIdentifier); | 232 if (value is Iterable) return value.map(toIdentifier); |
| 231 return toIdentifier(value); | 233 return toIdentifier(value); |
| 232 }; | 234 }; |
| 233 } | 235 } |
| 234 | 236 |
| 235 Instantiator visitInterpolatedSelector(InterpolatedSelector node) { | 237 Instantiator visitInterpolatedSelector(InterpolatedSelector node) { |
| 236 // A selector is an expression, as in `a[selector]`. | 238 // A selector is an expression, as in `a[selector]`. |
| 237 // A String argument converted into a LiteralString, so `a.#` with argument | 239 // A String argument converted into a LiteralString, so `a.#` with argument |
| 238 // 'foo' generates `a["foo"]` which prints as `a.foo`. | 240 // 'foo' generates `a["foo"]` which prints as `a.foo`. |
| 239 var nameOrPosition = node.nameOrPosition; | 241 var nameOrPosition = node.nameOrPosition; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 256 | 258 |
| 257 Instantiator visitInterpolatedMethod(InterpolatedMethod node) { | 259 Instantiator visitInterpolatedMethod(InterpolatedMethod node) { |
| 258 var nameOrPosition = node.nameOrPosition; | 260 var nameOrPosition = node.nameOrPosition; |
| 259 return (arguments) { | 261 return (arguments) { |
| 260 var value = arguments[nameOrPosition]; | 262 var value = arguments[nameOrPosition]; |
| 261 Method toMethod(item) { | 263 Method toMethod(item) { |
| 262 if (item is Method) return item; | 264 if (item is Method) return item; |
| 263 return error('Interpolated value #$nameOrPosition is not a Method ' | 265 return error('Interpolated value #$nameOrPosition is not a Method ' |
| 264 'or List of Methods: $value'); | 266 'or List of Methods: $value'); |
| 265 } | 267 } |
| 268 |
| 266 if (value is Iterable) return value.map(toMethod); | 269 if (value is Iterable) return value.map(toMethod); |
| 267 return toMethod(value); | 270 return toMethod(value); |
| 268 }; | 271 }; |
| 269 } | 272 } |
| 270 | 273 |
| 271 Instantiator visitInterpolatedIdentifier(InterpolatedIdentifier node) { | 274 Instantiator visitInterpolatedIdentifier(InterpolatedIdentifier node) { |
| 272 var nameOrPosition = node.nameOrPosition; | 275 var nameOrPosition = node.nameOrPosition; |
| 273 return (arguments) { | 276 return (arguments) { |
| 274 var item = arguments[nameOrPosition]; | 277 var item = arguments[nameOrPosition]; |
| 275 if (item is Identifier) return item; | 278 if (item is Identifier) return item; |
| 276 if (item is String) return new Identifier(item); | 279 if (item is String) return new Identifier(item); |
| 277 return error('Interpolated value #$nameOrPosition is not a ' | 280 return error('Interpolated value #$nameOrPosition is not a ' |
| 278 'Identifier or String: $item'); | 281 'Identifier or String: $item'); |
| 279 }; | 282 }; |
| 280 } | 283 } |
| 281 | 284 |
| 282 Instantiator visitSplayableStatement(Node node) { | 285 Instantiator visitSplayableStatement(Node node) { |
| 283 if (node is InterpolatedStatement) { | 286 if (node is InterpolatedStatement) { |
| 284 var nameOrPosition = node.nameOrPosition; | 287 var nameOrPosition = node.nameOrPosition; |
| 285 return (arguments) { | 288 return (arguments) { |
| 286 var value = arguments[nameOrPosition]; | 289 var value = arguments[nameOrPosition]; |
| 287 Statement toStatement(item) { | 290 Statement toStatement(item) { |
| 288 if (item is Statement) return item; | 291 if (item is Statement) return item; |
| 289 if (item is Expression) return item.toStatement(); | 292 if (item is Expression) return item.toStatement(); |
| 290 ; | 293 ; |
| 291 return error('Interpolated value #$nameOrPosition is not ' | 294 return error('Interpolated value #$nameOrPosition is not ' |
| 292 'a Statement or List of Statements: $value'); | 295 'a Statement or List of Statements: $value'); |
| 293 } | 296 } |
| 297 |
| 294 if (value is Iterable) return value.map(toStatement); | 298 if (value is Iterable) return value.map(toStatement); |
| 295 return toStatement(value); | 299 return toStatement(value); |
| 296 }; | 300 }; |
| 297 } | 301 } |
| 298 return visit(node); | 302 return visit(node); |
| 299 } | 303 } |
| 300 | 304 |
| 301 Instantiator visitProgram(Program node) { | 305 Instantiator visitProgram(Program node) { |
| 302 List instantiators = node.body.map(visitSplayableStatement).toList(); | 306 List instantiators = node.body.map(visitSplayableStatement).toList(); |
| 303 return (arguments) { | 307 return (arguments) { |
| 304 List<Statement> statements = <Statement>[]; | 308 List<Statement> statements = <Statement>[]; |
| 305 void add(node) { | 309 void add(node) { |
| 306 if (node is EmptyStatement) return; | 310 if (node is EmptyStatement) return; |
| 307 if (node is Iterable) { | 311 if (node is Iterable) { |
| 308 for (var n in node) statements.add(n); | 312 for (var n in node) statements.add(n); |
| 309 } else { | 313 } else { |
| 310 statements.add(node.toStatement()); | 314 statements.add(node.toStatement()); |
| 311 } | 315 } |
| 312 } | 316 } |
| 317 |
| 313 for (Instantiator instantiator in instantiators) { | 318 for (Instantiator instantiator in instantiators) { |
| 314 add(instantiator(arguments)); | 319 add(instantiator(arguments)); |
| 315 } | 320 } |
| 316 return new Program(statements); | 321 return new Program(statements); |
| 317 }; | 322 }; |
| 318 } | 323 } |
| 319 | 324 |
| 320 Instantiator visitBlock(Block node) { | 325 Instantiator visitBlock(Block node) { |
| 321 List instantiators = node.statements.map(visitSplayableStatement).toList(); | 326 List instantiators = node.statements.map(visitSplayableStatement).toList(); |
| 322 return (arguments) { | 327 return (arguments) { |
| 323 List<Statement> statements = <Statement>[]; | 328 List<Statement> statements = <Statement>[]; |
| 324 void add(node) { | 329 void add(node) { |
| 325 if (node is EmptyStatement) return; | 330 if (node is EmptyStatement) return; |
| 326 if (node is Iterable) { | 331 if (node is Iterable) { |
| 327 for (var n in node) statements.add(n); | 332 for (var n in node) statements.add(n); |
| 328 } else if (node is Block) { | 333 } else if (node is Block) { |
| 329 statements.addAll(node.statements); | 334 statements.addAll(node.statements); |
| 330 } else { | 335 } else { |
| 331 statements.add(node.toStatement()); | 336 statements.add(node.toStatement()); |
| 332 } | 337 } |
| 333 } | 338 } |
| 339 |
| 334 for (Instantiator instantiator in instantiators) { | 340 for (Instantiator instantiator in instantiators) { |
| 335 add(instantiator(arguments)); | 341 add(instantiator(arguments)); |
| 336 } | 342 } |
| 337 return new Block(statements); | 343 return new Block(statements); |
| 338 }; | 344 }; |
| 339 } | 345 } |
| 340 | 346 |
| 341 Instantiator visitExpressionStatement(ExpressionStatement node) { | 347 Instantiator visitExpressionStatement(ExpressionStatement node) { |
| 342 Instantiator buildExpression = visit(node.expression); | 348 Instantiator buildExpression = visit(node.expression); |
| 343 return (arguments) { | 349 return (arguments) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 362 var nameOrPosition = node.nameOrPosition; | 368 var nameOrPosition = node.nameOrPosition; |
| 363 return (arguments) { | 369 return (arguments) { |
| 364 var value = arguments[nameOrPosition]; | 370 var value = arguments[nameOrPosition]; |
| 365 if (value is bool) return value; | 371 if (value is bool) return value; |
| 366 if (value is Expression) return value; | 372 if (value is Expression) return value; |
| 367 if (value is String) return new Identifier(value); | 373 if (value is String) return new Identifier(value); |
| 368 error('Interpolated value #$nameOrPosition ' | 374 error('Interpolated value #$nameOrPosition ' |
| 369 'is not an Expression: $value'); | 375 'is not an Expression: $value'); |
| 370 }; | 376 }; |
| 371 } | 377 } |
| 378 |
| 372 var makeCondition = compileCondition(node.condition); | 379 var makeCondition = compileCondition(node.condition); |
| 373 Instantiator makeThen = visit(node.then); | 380 Instantiator makeThen = visit(node.then); |
| 374 Instantiator makeOtherwise = visit(node.otherwise); | 381 Instantiator makeOtherwise = visit(node.otherwise); |
| 375 return (arguments) { | 382 return (arguments) { |
| 376 var condition = makeCondition(arguments); | 383 var condition = makeCondition(arguments); |
| 377 if (condition is bool) { | 384 if (condition is bool) { |
| 378 if (condition == true) { | 385 if (condition == true) { |
| 379 return makeThen(arguments); | 386 return makeThen(arguments); |
| 380 } else { | 387 } else { |
| 381 return makeOtherwise(arguments); | 388 return makeOtherwise(arguments); |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 } | 567 } |
| 561 | 568 |
| 562 Instantiator visitConditional(Conditional cond) { | 569 Instantiator visitConditional(Conditional cond) { |
| 563 Instantiator makeCondition = visit(cond.condition); | 570 Instantiator makeCondition = visit(cond.condition); |
| 564 Instantiator makeThen = visit(cond.then); | 571 Instantiator makeThen = visit(cond.then); |
| 565 Instantiator makeOtherwise = visit(cond.otherwise); | 572 Instantiator makeOtherwise = visit(cond.otherwise); |
| 566 return (arguments) => new Conditional(makeCondition(arguments), | 573 return (arguments) => new Conditional(makeCondition(arguments), |
| 567 makeThen(arguments), makeOtherwise(arguments)); | 574 makeThen(arguments), makeOtherwise(arguments)); |
| 568 } | 575 } |
| 569 | 576 |
| 570 Instantiator visitNew(New node) => | 577 Instantiator visitNew(New node) => handleCallOrNew(node, |
| 571 handleCallOrNew(node, (target, arguments) => | 578 (target, arguments) => new New(target, arguments as List<Expression>)); |
| 572 new New(target, arguments as List<Expression>)); | |
| 573 | 579 |
| 574 Instantiator visitCall(Call node) => | 580 Instantiator visitCall(Call node) => handleCallOrNew(node, |
| 575 handleCallOrNew(node, (target, arguments) => | 581 (target, arguments) => new Call(target, arguments as List<Expression>)); |
| 576 new Call(target, arguments as List<Expression>)); | |
| 577 | 582 |
| 578 Instantiator handleCallOrNew(Call node, finish(target, arguments)) { | 583 Instantiator handleCallOrNew(Call node, finish(target, arguments)) { |
| 579 Instantiator makeTarget = visit(node.target); | 584 Instantiator makeTarget = visit(node.target); |
| 580 Iterable<Instantiator> argumentMakers = | 585 Iterable<Instantiator> argumentMakers = |
| 581 node.arguments.map(visitSplayableExpression).toList(); | 586 node.arguments.map(visitSplayableExpression).toList(); |
| 582 | 587 |
| 583 // TODO(sra): Avoid copying call arguments if no interpolation or forced | 588 // TODO(sra): Avoid copying call arguments if no interpolation or forced |
| 584 // copying. | 589 // copying. |
| 585 return (arguments) { | 590 return (arguments) { |
| 586 Node target = makeTarget(arguments); | 591 Node target = makeTarget(arguments); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 907 if (count != before) containsInterpolatedNode.add(node); | 912 if (count != before) containsInterpolatedNode.add(node); |
| 908 return null; | 913 return null; |
| 909 } | 914 } |
| 910 | 915 |
| 911 visitInterpolatedNode(InterpolatedNode node) { | 916 visitInterpolatedNode(InterpolatedNode node) { |
| 912 containsInterpolatedNode.add(node); | 917 containsInterpolatedNode.add(node); |
| 913 if (node.isNamed) holeNames.add(node.nameOrPosition); | 918 if (node.isNamed) holeNames.add(node.nameOrPosition); |
| 914 ++count; | 919 ++count; |
| 915 } | 920 } |
| 916 } | 921 } |
| OLD | NEW |