Remove dependency on dart:json.

Review URL: https://codereview.chromium.org//11364025

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@14790 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
ahe@google.com 2012-11-12 13:59:24 +00:00
parent df55ddbbb6
commit e44285367c
4 changed files with 80 additions and 50 deletions

View file

@ -92,51 +92,12 @@ class ConstantEmitter implements ConstantVisitor {
void writeEscapedString(DartString string,
CodeBuffer buffer,
Node diagnosticNode) {
Iterator<int> iterator = string.iterator();
while (iterator.hasNext) {
int code = iterator.next();
if (identical(code, $SQ)) {
buffer.add(r"\'");
} else if (identical(code, $LF)) {
buffer.add(r'\n');
} else if (identical(code, $CR)) {
buffer.add(r'\r');
} else if (identical(code, $LS)) {
// This Unicode line terminator and $PS are invalid in JS string
// literals.
buffer.add(r'\u2028');
} else if (identical(code, $PS)) {
buffer.add(r'\u2029');
} else if (identical(code, $BACKSLASH)) {
buffer.add(r'\\');
} else {
if (code > 0xffff) {
compiler.reportError(
diagnosticNode,
'Unhandled non-BMP character: U+${code.toRadixString(16)}');
}
// TODO(lrn): Consider whether all codes above 0x7f really need to
// be escaped. We build a Dart string here, so it should be a literal
// stage that converts it to, e.g., UTF-8 for a JS interpreter.
if (code < 0x20) {
buffer.add(r'\x');
if (code < 0x10) buffer.add('0');
buffer.add(code.toRadixString(16));
} else if (code >= 0x80) {
if (code < 0x100) {
buffer.add(r'\x');
} else {
buffer.add(r'\u');
if (code < 0x1000) {
buffer.add('0');
}
}
buffer.add(code.toRadixString(16));
} else {
buffer.addCharCode(code);
}
}
void onError(code) {
compiler.reportError(
diagnosticNode,
'Unhandled non-BMP character: U+${code.toRadixString(16)}');
}
writeJsonEscapedCharsOn(string.iterator(), buffer, onError);
}
void visitString(StringConstant constant) {

View file

@ -10,7 +10,13 @@ class ScannerTask extends CompilerTask {
void scanLibrary(LibraryElement library) {
var compilationUnit = library.entryCompilationUnit;
compiler.log("scanning library ${compilationUnit.script.name}");
var canonicalUri = library.uri.toString();
var resolvedUri = compilationUnit.script.uri.toString();
if (canonicalUri == resolvedUri) {
compiler.log("scanning library $canonicalUri");
} else {
compiler.log("scanning library $canonicalUri ($resolvedUri)");
}
scan(compilationUnit);
}

View file

@ -4,9 +4,8 @@
library source_map_builder;
import 'dart:json';
import 'scanner/scannerlib.dart';
import 'util/util.dart';
import 'scanner/scannerlib.dart' show Token;
import 'source_file.dart';
class SourceMapBuilder {
@ -53,6 +52,19 @@ class SourceMapBuilder {
entries.add(new SourceMapEntry(sourceLocation, targetOffset));
}
void printStringListOn(List<String> strings, StringBuffer buffer) {
bool first = true;
buffer.add('[');
for (String string in strings) {
if (!first) buffer.add(',');
buffer.add("'");
writeJsonEscapedCharsOn(string.charCodes.iterator(), buffer, null);
buffer.add("'");
first = false;
}
buffer.add(']');
}
String build(SourceFile targetFile) {
StringBuffer buffer = new StringBuffer();
buffer.add('{\n');
@ -61,10 +73,10 @@ class SourceMapBuilder {
entries.forEach((SourceMapEntry entry) => writeEntry(entry, targetFile, buffer));
buffer.add('",\n');
buffer.add(' "sources": ');
JSON.printOn(sourceUrlList, buffer);
printStringListOn(sourceUrlList, buffer);
buffer.add(',\n');
buffer.add(' "names": ');
JSON.printOn(sourceNameList, buffer);
printStringListOn(sourceNameList, buffer);
buffer.add('\n}\n');
return buffer.toString();
}

View file

@ -5,6 +5,7 @@
library org_dartlang_compiler_util;
import 'util_implementation.dart';
import 'characters.dart';
part 'link.dart';
@ -22,3 +23,53 @@ class SpannableAssertionFailure {
String toString() => 'compiler crashed.';
}
/// Writes the characters of [iterator] on [buffer]. The characters
/// are escaped as suitable for JavaScript and JSON. [buffer] is
/// anything which supports [:add:] and [:addCharCode:], for example,
/// [StringBuffer].
void writeJsonEscapedCharsOn(Iterator<int> iterator, buffer, onError(code)) {
while (iterator.hasNext) {
int code = iterator.next();
if (identical(code, $SQ)) {
buffer.add(r"\'");
} else if (identical(code, $LF)) {
buffer.add(r'\n');
} else if (identical(code, $CR)) {
buffer.add(r'\r');
} else if (identical(code, $LS)) {
// This Unicode line terminator and $PS are invalid in JS string
// literals.
buffer.add(r'\u2028');
} else if (identical(code, $PS)) {
buffer.add(r'\u2029');
} else if (identical(code, $BACKSLASH)) {
buffer.add(r'\\');
} else {
if (code > 0xffff) {
if (onError != null) onError(code);
throw 'Unhandled non-BMP character: ${code.toRadixString(16)}';
}
// TODO(lrn): Consider whether all codes above 0x7f really need to
// be escaped. We build a Dart string here, so it should be a literal
// stage that converts it to, e.g., UTF-8 for a JS interpreter.
if (code < 0x20) {
buffer.add(r'\x');
if (code < 0x10) buffer.add('0');
buffer.add(code.toRadixString(16));
} else if (code >= 0x80) {
if (code < 0x100) {
buffer.add(r'\x');
} else {
buffer.add(r'\u');
if (code < 0x1000) {
buffer.add('0');
}
}
buffer.add(code.toRadixString(16));
} else {
buffer.addCharCode(code);
}
}
}
}