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

Side by Side Diff: dart/frog/leg/frog_leg.dart

Issue 9639012: Implement the compiler API and use it in frog_leg.dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 9 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
« dart/frog/leg/api.dart ('K') | « dart/frog/leg/compiler.dart ('k') | no next file » | 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 #library('frog_leg'); 5 #library('frog_leg');
6 6
7 #import('../../lib/uri/uri.dart'); 7 #import('../../lib/uri/uri.dart');
8 #import('../lang.dart', prefix: 'frog'); 8 #import('../lang.dart', prefix: 'frog');
9 #import('elements/elements.dart'); 9 #import('api.dart', prefix: 'api');
10 #import('io/io.dart', prefix: 'io'); 10 #import('io/io.dart', prefix: 'io');
11 #import('leg.dart');
12 #import('tree/tree.dart');
13 #import('ssa/tracer.dart');
14 11
15 bool compile(frog.World world) { 12 bool compile(frog.World world) {
16 final throwOnError = frog.options.throwOnErrors; 13 final throwOnError = frog.options.throwOnErrors;
17 final compiler = new WorldCompiler(world, throwOnError); 14 // final compiler = new WorldCompiler(world, throwOnError);
18 Uri cwd = new Uri(scheme: 'file', path: compiler.currentDirectory); 15 Uri cwd = new Uri(scheme: 'file', path: io.getCurrentDirectory());
19 Uri uri = cwd.resolve(frog.options.dartScript); 16 Uri uri = cwd.resolve(frog.options.dartScript);
20 return compiler.run(uri); 17 String frogLibDir = frog.options.libDir;
21 } 18 if (!frogLibDir.endsWith("/")) frogLibDir = "$frogLibDir/";
19 Uri frogLib = new Uri(scheme: 'file', path: frogLibDir);
20 Uri libraryRoot = frogLib.resolve('../leg/lib/');
22 21
kasperl 2012/03/08 15:13:02 Maybe only one newline here?
ahe 2012/03/08 16:29:42 Done.
23 class WorldCompiler extends Compiler {
24 final frog.World world;
25 final bool throwOnError;
26 22
27 WorldCompiler(this.world, this.throwOnError) 23 Future<String> provider(Uri uri) {
28 : super.withCurrentDirectory(io.getCurrentDirectory(), 24 if (uri.scheme != 'file') {
29 tracer: new HTracer()); 25 throw new IllegalArgumentException(uri);
30
31 void log(message) {
32 if (frog.options.showInfo) {
33 // Avoid calling message.toString() unless showInfo is on. It
34 // could be slow.
35 world.info('[leg] $message');
36 } 26 }
27 String source = world.files.readAll(uri.path);
28 world.dartBytesRead += source.length;
29 Completer<String> completer = new Completer<String>();
30 completer.complete(source);
31 return completer.future;
37 } 32 }
38 33
39 bool run(Uri uri) { 34 void handler(Uri uri, int begin, int end, String message, bool fatal) {
40 bool success = super.run(uri); 35 if (uri === null && !fatal) {
41 if (success) { 36 world.info('[leg] $message');
42 var code = assembledCode; 37 return;
43 world.legCode = code;
44 world.jsBytesWritten = code.length;
45 for (final task in tasks) {
46 log('${task.name} took ${task.timing}msec');
47 }
48 } 38 }
49 return success; 39 print(message);
40 if (fatal && throwOnError) new AbortLeg(message);
50 } 41 }
51 42
52 spanFromNode(Node node, Script current) { 43 String code = api.compile(uri, libraryRoot, provider, handler).value;
kasperl 2012/03/08 15:13:02 Maybe add a comment here too?
ahe 2012/03/08 16:29:42 Done.
53 final begin = node.getBeginToken(); 44 if (code === null) return false;
54 final end = node.getEndToken(); 45 world.legCode = code;
55 if (begin === null || end === null) { 46 world.jsBytesWritten = code.length;
56 cancel('cannot find tokens to produce error message for $node.'); 47 return true;
57 }
58 final startOffset = begin.charOffset;
59 // TODO(ahe): Compute proper end offset.
60 final endOffset = end.charOffset + 1;
61 return new frog.SourceSpan(current.file, startOffset, endOffset);
62 }
63
64 currentScript() {
65 if (currentElement === null) return null;
66 CompilationUnitElement compilationUnit =
67 currentElement.getCompilationUnit();
68 if (compilationUnit === null) return null;
69 return compilationUnit.script;
70 }
71
72 reportWarning(Node node, var message) {
73 frog.SourceSpan span;
74 Script current = currentScript();
75 if (current === null) {
76 world.fatal('No current script');
77 } else if (node === null) {
78 span = new frog.SourceSpan(current.file, 0, 0);
79 } else {
80 span = spanFromNode(node, current);
81 }
82 world.warning('$message.', span);
83 }
84
85 reportError(Node node, var message) {
86 cancel(message.toString(), node);
87 }
88
89 Uri getUriFor(String fileName) {
90 Uri cwd = new Uri(scheme: 'file', path: currentDirectory);
91 return cwd.resolve(fileName);
92 }
93
94 Script readScript(Uri uri, [ScriptTag node]) {
95 String uriName = uri.toString();
96 // TODO(ahe): Clean this up.
97 if (uriName == 'dart:dom') {
98 uri = getUriFor(io.join([legDirectory, '..', '..',
99 'client', 'dom', 'frog', 'dom_frog.dart']));
100 } else if (uriName == 'dart:html') {
101 uri = getUriFor(io.join([legDirectory, '..', '..',
102 'client', 'html', 'frog', 'html_frog.dart']));
103 } else if (uriName == 'dart:json') {
104 uri = getUriFor(io.join([legDirectory, '..', '..',
105 'lib', 'json', 'json.dart']));
106 }
107 if (uri.scheme != 'file') cancel('cannot read $uri', node: node);
108 String text = "";
109 try {
110 text = frog.world.files.readAll(uri.path);
111 } catch (var exception) {
112 cancel("${uri.path}: $exception", node: node);
113 }
114 world.dartBytesRead += text.length;
115 frog.SourceFile sourceFile = new frog.SourceFile(uri.toString(), text);
116 return new Script(uri, sourceFile);
117 }
118
119 String get legDirectory() => io.join([frog.options.libDir, '..', 'leg']);
120
121 void cancel([String reason, Node node, token, instruction, element]) {
122 Script script = currentScript();
123 if (node !== null) {
124 print(spanFromNode(node, script).toMessageString("cancel leg: $reason"));
125 } else if (token !== null) {
126 int begin = token.charOffset;
127 int end = begin + 1; // TODO(ahe): Compute proper length.
128 print(script.file.getLocationMessage("cancel leg: $reason",
129 begin, end, true));
130 } else if (element !== null) {
131 if (element.position() === null) {
132 // Sometimes, the backend fakes up elements that have no
133 // position. So we use the enclosing element instead. It is
134 // not a good error location, but cancel really is "internal
135 // error" or "not implemented yet", so the vicinity is good
136 // enough for now.
137 element = element.enclosingElement;
138 // TODO(ahe): I plan to overhaul this infrastructure anyways.
139 }
140 if (element !== null) {
141 withCurrentElement(element,
142 () => cancel(reason: reason, token: element.position()));
143 }
144 } else if (currentElement !== null) {
145 cancel(reason, element: currentElement);
146 return;
147 }
148 if (throwOnError) {
149 throw new AbortLeg(reason);
150 }
151 super.cancel(reason, node, token, instruction);
152 }
153
154 LibraryElement scanBuiltinLibrary(String filename) {
155 String fileName = io.join([legDirectory, 'lib', filename]);
156 Uri cwd = new Uri(scheme: 'file', path: currentDirectory);
157 Uri uri = cwd.resolve(fileName);
158 LibraryElement library = scanner.loadLibrary(uri, null);
159 return library;
160 }
161 } 48 }
162 49
163 class AbortLeg { 50 class AbortLeg {
164 final message; 51 final message;
165 AbortLeg(this.message); 52 AbortLeg(this.message);
166 toString() => 'Aborted due to --throw-on-error: $message'; 53 toString() => 'Aborted due to --throw-on-error: $message';
167 } 54 }
OLDNEW
« dart/frog/leg/api.dart ('K') | « dart/frog/leg/compiler.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698