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

Side by Side Diff: frog/pad/frogpad.dart

Issue 9392005: initial frogpad (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: updated comment Created 8 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 | frog/pad/frogpad.py » ('j') | frog/pad/frogpad.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * This is the entrypoint for a version of the frog compiler that
7 * can run in the browser.
8 * Because this is running in the browser, frog cannot access
9 * the file system. Instead, we assume the all necessary files
10 * have been placed in inert <script> elements somewhere on the page.
11 * (See frogpad.py for more details on how the html page is constructed.)
12 */
13
14 #import("dart:html", prefix:"html");
15 #import("../lang.dart");
16 #import("../file_system.dart");
17
18 // TODO - remove once corelib has an Exception base class
Siggi Cherem (dart-lang) 2012/02/13 18:43:49 I don't think you need this becase you don't exten
Jennifer Messerly 2012/02/13 19:12:12 Agree
mattsh 2012/02/13 20:57:36 Done.
mattsh 2012/02/13 20:57:36 Done.
19 class Exception_ implements Exception {
20 final String message;
21 Exception_(String message) : message = message {}
22 String toString() { return message; }
Bob Nystrom 2012/02/13 18:10:18 Why not just: String toString() => message;
mattsh 2012/02/13 20:57:36 Done.
23 }
24
25 // id of script element containing name of the main dart file
Bob Nystrom 2012/02/13 18:10:18 Should be a complete sentence and capitalized.
mattsh 2012/02/13 20:57:36 Prefer to keep it lowercase since then it matches
Bob Nystrom 2012/02/13 21:33:40 Then do "The id"?
26 // to compile.
27 final String MAIN_ID = "main_id";
Bob Nystrom 2012/02/13 18:10:18 I think our (undocumented at this point) conventio
mattsh 2012/02/13 20:57:36 Prefer to keep double quotes here.
Bob Nystrom 2012/02/13 21:33:40 Why?
28
29 void main() {
30 String warnings = "";
31 HtmlFileSystem fs = new HtmlFileSystem();
Bob Nystrom 2012/02/13 18:10:18 These type annotations seem pretty redundant...
mattsh 2012/02/13 20:57:36 Prefer to keep type annotations.
32 String mainFile = getText(MAIN_ID);
33 setText("input", fs.readAll(mainFile));
34
35 // TODO - figure out why parseOptions ignores its first two arguments
Bob Nystrom 2012/02/13 18:10:18 TODO(mattsh)...
Siggi Cherem (dart-lang) 2012/02/13 18:43:49 it currently does because in all shell programms a
mattsh 2012/02/13 20:57:36 Done.
mattsh 2012/02/13 20:57:36 Done.
36 List<String> args = ["dummy_arg1", "dummy_arg2", mainFile];
37
38 // TODO - figure out whether this home directory argument is needed
Emily Fortuna 2012/02/13 18:27:58 TODO(mattsh) (again)
Siggi Cherem (dart-lang) 2012/02/13 18:43:49 IIRC, this is only needed to determine where to fi
mattsh 2012/02/13 20:57:36 Done.
mattsh 2012/02/13 20:57:36 Done.
39 parseOptions("dummy_home_dir", args, fs);
40
41 options.useColors = false;
42
43 initializeWorld(fs);
44 world.messageHandler = void _(
Bob Nystrom 2012/02/13 18:10:18 Why not just: world.messageHandler = (prefix, mes
mattsh 2012/02/13 20:57:36 We want static type checking of the return type, s
45 String prefix, String message, SourceSpan span) {
46 String location = "";
47 if (span !== null) {
Bob Nystrom 2012/02/13 18:10:18 You can make this a one-liner if you like.
mattsh 2012/02/13 20:57:36 Prefer to keep the curlies.
48 location = span.locationText;
49 }
50 warnings += prefix + message + location + "\n";
51 };
52 bool success = world.compile();
53 if (success) {
54 setText("output", world.getGeneratedCode());
55 }
56 setText("warnings", warnings);
57 }
58
59 void setText(String id, String text) {
60 html.Element element = html.document.query("#" + id);
Bob Nystrom 2012/02/13 18:10:18 Interpolate.
mattsh 2012/02/13 20:57:36 Done.
61 if (element === null) {
62 throw new Exception_("Can't find element " + id);
Bob Nystrom 2012/02/13 18:10:18 Here too.
mattsh 2012/02/13 20:57:36 Done.
63 }
64 element.innerHTML = htmlEscape(text);
65 }
66
67 String getText(String id) {
68 html.Element element = html.document.query("#$id");
69 if (element === null) {
70 throw new Exception_("Can't find element " + id);
Emily Fortuna 2012/02/13 18:27:58 Interpolate.
mattsh 2012/02/13 20:57:36 Done.
71 }
72 return element.text.trim();
73 }
74
75 // TODO(rnystrom): should exist in standard lib somewhere
Bob Nystrom 2012/02/13 18:10:18 No, this still doesn't have a home. Maybe file a b
mattsh 2012/02/13 20:57:36 http://code.google.com/p/dart/issues/detail?id=165
76 String htmlEscape(String text) {
77 return text.replaceAll('&', '&amp;').replaceAll(
78 '>', '&gt;').replaceAll('<', '&lt;');
79 }
80
81 class HtmlFileSystem implements FileSystem {
82
83 HtmlFileSystem() {}
84
85 String readAll(String filename) {
86 String text = getText(idOfFilename(filename));
87 print("read $filename (${text.length} bytes)");
Bob Nystrom 2012/02/13 18:10:18 Is this for debugging? If so, I'd remove it.
mattsh 2012/02/13 20:57:36 Done.
88 return text;
89 }
90
91 /**
92 * Returns the id of the <script> element that contains
Bob Nystrom 2012/02/13 18:10:18 The wrapping is strange here. Should be at 80 char
mattsh 2012/02/13 20:57:36 Keep as is.
Bob Nystrom 2012/02/13 21:33:40 Why?
93 * the contents of this file.
94 * The id is constructed by taking the last directory
95 * of the path, plus the file name, and using _ as a
96 * separator. So, for example, the file name:
97 *
98 * "/usr/local/src/dart/frog/lang.dart"
99 *
100 * becomes:
101 *
102 * "frog_lang_dart"
103 *
104 * And, so this file's contents will be found in a <script>
105 * element that looks like this:
106 *
107 * <script type=application/inert id="frog_lang_dart">
108 * ... contents of file lang.dart placed here ...
109 * </script>
110 */
111 String idOfFilename(String filename) {
112 List<String> components = filename.split("/");
113 if (components.isEmpty()) {
114 throw new Exception_("bad filename");
115 }
116 // grab the last two components (the directory name and file name):
Bob Nystrom 2012/02/13 18:10:18 "Grab"
mattsh 2012/02/13 20:57:36 Done.
117 int startIndex = Math.max(0, components.length - 2);
118 int endIndex = components.length - startIndex;
119 components = components.getRange(startIndex, endIndex);
120
121 // join components with underscore, and also replace dots with underscore
Bob Nystrom 2012/02/13 18:10:18 Should be a sentence.
mattsh 2012/02/13 20:57:36 Done.
122 return Strings.join(components, "_").replaceAll(".", "_");
123 }
124
125 void writeString(String outfile, String text) {
126 throw new UnsupportedOperationException("");
127 }
128
129 bool fileExists(String filename) {
130 return true;
Bob Nystrom 2012/02/13 18:10:18 Shouldn't you check for the <div> here?
mattsh 2012/02/13 20:57:36 Added a comment.
131 }
132
133 void createDirectory(String path, [bool recursive]) {
134 throw new UnsupportedOperationException("");
135 }
136 void removeDirectory(String path, [bool recursive]) {
Bob Nystrom 2012/02/13 18:10:18 Add a blank line above this.
mattsh 2012/02/13 20:57:36 Done.
137 throw new UnsupportedOperationException("");
138 }
139 }
OLDNEW
« no previous file with comments | « no previous file | frog/pad/frogpad.py » ('j') | frog/pad/frogpad.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698