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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/dart_backend/dart_printer.dart

Issue 336413002: Allow whitespace and \ before the first newline of multiline string. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use more intuitive test. Created 6 years, 6 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
OLDNEW
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 // TODO(asgerf): Include metadata. 5 // TODO(asgerf): Include metadata.
6 // TODO(asgerf): Include cascade operator. 6 // TODO(asgerf): Include cascade operator.
7 library dart_printer; 7 library dart_printer;
8 8
9 import '../dart2jslib.dart' as dart2js; 9 import '../dart2jslib.dart' as dart2js;
10 import '../tree/tree.dart' as tree; 10 import '../tree/tree.dart' as tree;
(...skipping 1221 matching lines...) Expand 10 before | Expand all | Expand 10 after
1232 1232
1233 void writeType(TypeAnnotation type) { 1233 void writeType(TypeAnnotation type) {
1234 write(type.name); 1234 write(type.name);
1235 if (type.typeArguments != null && type.typeArguments.length > 0) { 1235 if (type.typeArguments != null && type.typeArguments.length > 0) {
1236 write('<'); 1236 write('<');
1237 writeEach(',', type.typeArguments, writeType); 1237 writeEach(',', type.typeArguments, writeType);
1238 write('>'); 1238 write('>');
1239 } 1239 }
1240 } 1240 }
1241 1241
1242 /// A list of string quotings that the printer may use to quote strings.
1243 // Ignore multiline quotings for now. Would need to make sure that no
1244 // newline (potentially prefixed by whitespace) follows the quoting.
1245 // TODO(asgerf): Include multiline quotation schemes.
1246 static const _QUOTINGS = const <tree.StringQuoting>[
1247 const tree.StringQuoting(characters.$DQ, raw: false, leftQuoteLength: 1),
1248 const tree.StringQuoting(characters.$DQ, raw: true, leftQuoteLength: 1),
1249 const tree.StringQuoting(characters.$SQ, raw: false, leftQuoteLength: 1),
1250 const tree.StringQuoting(characters.$SQ, raw: true, leftQuoteLength: 1),
1251 ];
1252
1242 static StringLiteralOutput analyzeStringLiteral(Expression node) { 1253 static StringLiteralOutput analyzeStringLiteral(Expression node) {
1243 // TODO(asgerf): This might be a bit too expensive. Benchmark. 1254 // TODO(asgerf): This might be a bit too expensive. Benchmark.
1244 // Flatten the StringConcat tree. 1255 // Flatten the StringConcat tree.
1245 List parts = []; // Expression or int (char node) 1256 List parts = []; // Expression or int (char node)
1246 void collectParts(Expression e) { 1257 void collectParts(Expression e) {
1247 if (e is StringConcat) { 1258 if (e is StringConcat) {
1248 e.expressions.forEach(collectParts); 1259 e.expressions.forEach(collectParts);
1249 } else if (e is Literal && e.value is dart2js.StringConstant) { 1260 } else if (e is Literal && e.value is dart2js.StringConstant) {
1250 for (int char in e.value.value) { 1261 for (int char in e.value.value) {
1251 parts.add(char); 1262 parts.add(char);
(...skipping 22 matching lines...) Expand all
1274 return quot.leftQuoteLength + quot.rightQuoteLength; 1285 return quot.leftQuoteLength + quot.rightQuoteLength;
1275 } 1286 }
1276 1287
1277 // Create initial scores for each StringQuoting and index them 1288 // Create initial scores for each StringQuoting and index them
1278 // into raw/non-raw and single-quote/double-quote. 1289 // into raw/non-raw and single-quote/double-quote.
1279 List<OpenStringChunk> best = <OpenStringChunk>[]; 1290 List<OpenStringChunk> best = <OpenStringChunk>[];
1280 List<int> raws = <int>[]; 1291 List<int> raws = <int>[];
1281 List<int> nonRaws = <int>[]; 1292 List<int> nonRaws = <int>[];
1282 List<int> sqs = <int>[]; 1293 List<int> sqs = <int>[];
1283 List<int> dqs = <int>[]; 1294 List<int> dqs = <int>[];
1284 for (tree.StringQuoting q in tree.StringQuoting.mapping) { 1295 for (tree.StringQuoting q in _QUOTINGS) {
1285 // Ignore multiline quotings for now. Encoding of line breaks is unclear.
1286 // TODO(asgerf): Include multiline quotation schemes.
1287 if (q.leftQuoteCharCount >= 3)
1288 continue;
1289 OpenStringChunk chunk = new OpenStringChunk(null, q, getQuoteCost(q)); 1296 OpenStringChunk chunk = new OpenStringChunk(null, q, getQuoteCost(q));
1290 int index = best.length; 1297 int index = best.length;
1291 best.add(chunk); 1298 best.add(chunk);
1292 1299
1293 if (q.raw) { 1300 if (q.raw) {
1294 raws.add(index); 1301 raws.add(index);
1295 } else { 1302 } else {
1296 nonRaws.add(index); 1303 nonRaws.add(index);
1297 } 1304 }
1298 if (q.quote == characters.$SQ) { 1305 if (q.quote == characters.$SQ) {
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1494 final StringChunk previous; 1501 final StringChunk previous;
1495 final tree.StringQuoting quoting; 1502 final tree.StringQuoting quoting;
1496 num cost; 1503 num cost;
1497 1504
1498 OpenStringChunk(this.previous, this.quoting, this.cost); 1505 OpenStringChunk(this.previous, this.quoting, this.cost);
1499 1506
1500 StringChunk end(int endIndex) { 1507 StringChunk end(int endIndex) {
1501 return new StringChunk(previous, quoting, endIndex); 1508 return new StringChunk(previous, quoting, endIndex);
1502 } 1509 }
1503 } 1510 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698