Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: pkg/compiler/lib/src/js/builder.dart

Issue 952643002: Revert "dart2js: Avoid escaping in strings if it's not necessary." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_backend/constant_emitter.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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|"|\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 "\"" : return r'\"'; 301 case "\"" : return r'\"';
326 case "\b" : return r"\b"; 302 case "\b" : return r"\b";
327 case "\t" : return r"\t"; 303 case "\t" : return r"\t";
328 case "\f" : return r"\f"; 304 case "\f" : return r"\f";
329 case "\v" : return r"\v"; 305 case "\v" : return r"\v";
330 case "\u2028" : return r"\u2028";
331 case "\u2029" : return r"\u2029";
332 } 306 }
333 }); 307 });
334 LiteralString result = string(escaped); 308 LiteralString result = string(escaped);
335 // 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
336 // into ". Verify that assumption. 310 // into ". Verify that assumption.
337 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0)); 311 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0));
338 return result; 312 return result;
339 } 313 }
340 314
341 /// 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
1355 1329
1356 Catch parseCatch() { 1330 Catch parseCatch() {
1357 expectCategory(LPAREN); 1331 expectCategory(LPAREN);
1358 Declaration errorName = parseVariableDeclaration(); 1332 Declaration errorName = parseVariableDeclaration();
1359 expectCategory(RPAREN); 1333 expectCategory(RPAREN);
1360 expectCategory(LBRACE); 1334 expectCategory(LBRACE);
1361 Block body = parseBlock(); 1335 Block body = parseBlock();
1362 return new Catch(errorName, body); 1336 return new Catch(errorName, body);
1363 } 1337 }
1364 } 1338 }
OLDNEW
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_backend/constant_emitter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698