| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Utilities for building JS ASTs at runtime. Contains a builder class | 5 // Utilities for building JS ASTs at runtime. Contains a builder class |
| 6 // and a parser that parses part of the language. | 6 // and a parser that parses part of the language. |
| 7 | 7 |
| 8 part of js_ast; | 8 part of js_ast; |
| 9 | 9 |
| 10 | 10 |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 * context that expects a template. | 282 * context that expects a template. |
| 283 */ | 283 */ |
| 284 Template expressionTemplateYielding(Node ast) { | 284 Template expressionTemplateYielding(Node ast) { |
| 285 return new Template.withExpressionResult(ast); | 285 return new Template.withExpressionResult(ast); |
| 286 } | 286 } |
| 287 | 287 |
| 288 Template statementTemplateYielding(Node ast) { | 288 Template statementTemplateYielding(Node ast) { |
| 289 return new Template.withStatementResult(ast); | 289 return new Template.withStatementResult(ast); |
| 290 } | 290 } |
| 291 | 291 |
| 292 static RegExp _stringEscapeRegExp = | |
| 293 new RegExp('\\\\|\n|\r|"|\b|\t|\v|\u2028|\u2029'); | |
| 294 | |
| 295 /// Creates a literal js string from [value]. | 292 /// Creates a literal js string from [value]. |
| 296 LiteralString escapedString(String value) { | 293 LiteralString escapedString(String value) { |
| 297 // Relevant sections of ECMA-262: | 294 // Start by escaping the backslashes. |
| 298 // | 295 String escaped = value.replaceAll('\\', '\\\\'); |
| 299 // 7.3 Line Terminators | |
| 300 // LineTerminator :: | |
| 301 // <LF> | |
| 302 // <CR> | |
| 303 // <LS> | |
| 304 // <PS> | |
| 305 // | |
| 306 // 7.8.4 String Literals | |
| 307 // StringLiteral :: | |
| 308 // " DoubleStringCharacters? " | |
| 309 // ' SingleStringCharacters? ' | |
| 310 // | |
| 311 // DoubleStringCharacters :: | |
| 312 // DoubleStringCharacter DoubleStringCharacters? | |
| 313 // | |
| 314 // DoubleStringCharacter :: | |
| 315 // SourceCharacter but not one of " or \ or LineTerminator | |
| 316 // \ EscapeSequence | |
| 317 // LineContinuation | |
| 318 // | |
| 319 // Do not escape unicode characters and ' because they are allowed in the | 296 // Do not escape unicode characters and ' because they are allowed in the |
| 320 // string literal anyway. | 297 // string literal anyway. |
| 321 String escaped = value.replaceAllMapped(_stringEscapeRegExp, (Match match) { | 298 escaped = escaped.replaceAllMapped(new RegExp('\n|"|\b|\t|\v'), (match) { |
| 322 switch (match.group(0)) { | 299 switch (match.group(0)) { |
| 323 case "\\" : return r"\\"; | |
| 324 case "\n" : return r"\n"; | 300 case "\n" : return r"\n"; |
| 325 case "\r" : return r"\r"; | |
| 326 case "\"" : return r'\"'; | 301 case "\"" : return r'\"'; |
| 327 case "\b" : return r"\b"; | 302 case "\b" : return r"\b"; |
| 328 case "\t" : return r"\t"; | 303 case "\t" : return r"\t"; |
| 329 case "\f" : return r"\f"; | 304 case "\f" : return r"\f"; |
| 330 case "\v" : return r"\v"; | 305 case "\v" : return r"\v"; |
| 331 case "\u2028" : return r"\u2028"; | |
| 332 case "\u2029" : return r"\u2029"; | |
| 333 } | 306 } |
| 334 }); | 307 }); |
| 335 LiteralString result = string(escaped); | 308 LiteralString result = string(escaped); |
| 336 // We don't escape ' under the assumption that the string is wrapped | 309 // We don't escape ' under the assumption that the string is wrapped |
| 337 // into ". Verify that assumption. | 310 // into ". Verify that assumption. |
| 338 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0)); | 311 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0)); |
| 339 return result; | 312 return result; |
| 340 } | 313 } |
| 341 | 314 |
| 342 /// Creates a literal js string from [value]. | 315 /// Creates a literal js string from [value]. |
| (...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1356 | 1329 |
| 1357 Catch parseCatch() { | 1330 Catch parseCatch() { |
| 1358 expectCategory(LPAREN); | 1331 expectCategory(LPAREN); |
| 1359 Declaration errorName = parseVariableDeclaration(); | 1332 Declaration errorName = parseVariableDeclaration(); |
| 1360 expectCategory(RPAREN); | 1333 expectCategory(RPAREN); |
| 1361 expectCategory(LBRACE); | 1334 expectCategory(LBRACE); |
| 1362 Block body = parseBlock(); | 1335 Block body = parseBlock(); |
| 1363 return new Catch(errorName, body); | 1336 return new Catch(errorName, body); |
| 1364 } | 1337 } |
| 1365 } | 1338 } |
| OLD | NEW |