Chromium Code Reviews| Index: frog/pad/frogpad.dart |
| diff --git a/frog/pad/frogpad.dart b/frog/pad/frogpad.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9b584d0aa57a2663d1cd52617d0a98521f672ca2 |
| --- /dev/null |
| +++ b/frog/pad/frogpad.dart |
| @@ -0,0 +1,139 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * This is the entrypoint for a version of the frog compiler that |
| + * can run in the browser. |
| + * Because this is running in the browser, frog cannot access |
| + * the file system. Instead, we assume the all necessary files |
| + * have been placed in inert <script> elements somewhere on the page. |
| + * (See frogpad.py for more details on how the html page is constructed.) |
| + */ |
| + |
| +#import("dart:html", prefix:"html"); |
| +#import("../lang.dart"); |
| +#import("../file_system.dart"); |
| + |
| +// 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.
|
| +class Exception_ implements Exception { |
| + final String message; |
| + Exception_(String message) : message = message {} |
| + 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.
|
| +} |
| + |
| +// 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"?
|
| +// to compile. |
| +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?
|
| + |
| +void main() { |
| + String warnings = ""; |
| + 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.
|
| + String mainFile = getText(MAIN_ID); |
| + setText("input", fs.readAll(mainFile)); |
| + |
| + // 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.
|
| + List<String> args = ["dummy_arg1", "dummy_arg2", mainFile]; |
| + |
| + // 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.
|
| + parseOptions("dummy_home_dir", args, fs); |
| + |
| + options.useColors = false; |
| + |
| + initializeWorld(fs); |
| + 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
|
| + String prefix, String message, SourceSpan span) { |
| + String location = ""; |
| + 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.
|
| + location = span.locationText; |
| + } |
| + warnings += prefix + message + location + "\n"; |
| + }; |
| + bool success = world.compile(); |
| + if (success) { |
| + setText("output", world.getGeneratedCode()); |
| + } |
| + setText("warnings", warnings); |
| +} |
| + |
| +void setText(String id, String text) { |
| + html.Element element = html.document.query("#" + id); |
|
Bob Nystrom
2012/02/13 18:10:18
Interpolate.
mattsh
2012/02/13 20:57:36
Done.
|
| + if (element === null) { |
| + 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.
|
| + } |
| + element.innerHTML = htmlEscape(text); |
| +} |
| + |
| +String getText(String id) { |
| + html.Element element = html.document.query("#$id"); |
| + if (element === null) { |
| + 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.
|
| + } |
| + return element.text.trim(); |
| +} |
| + |
| +// 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
|
| +String htmlEscape(String text) { |
| + return text.replaceAll('&', '&').replaceAll( |
| + '>', '>').replaceAll('<', '<'); |
| +} |
| + |
| +class HtmlFileSystem implements FileSystem { |
| + |
| + HtmlFileSystem() {} |
| + |
| + String readAll(String filename) { |
| + String text = getText(idOfFilename(filename)); |
| + 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.
|
| + return text; |
| + } |
| + |
| + /** |
| + * 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?
|
| + * the contents of this file. |
| + * The id is constructed by taking the last directory |
| + * of the path, plus the file name, and using _ as a |
| + * separator. So, for example, the file name: |
| + * |
| + * "/usr/local/src/dart/frog/lang.dart" |
| + * |
| + * becomes: |
| + * |
| + * "frog_lang_dart" |
| + * |
| + * And, so this file's contents will be found in a <script> |
| + * element that looks like this: |
| + * |
| + * <script type=application/inert id="frog_lang_dart"> |
| + * ... contents of file lang.dart placed here ... |
| + * </script> |
| + */ |
| + String idOfFilename(String filename) { |
| + List<String> components = filename.split("/"); |
| + if (components.isEmpty()) { |
| + throw new Exception_("bad filename"); |
| + } |
| + // 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.
|
| + int startIndex = Math.max(0, components.length - 2); |
| + int endIndex = components.length - startIndex; |
| + components = components.getRange(startIndex, endIndex); |
| + |
| + // 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.
|
| + return Strings.join(components, "_").replaceAll(".", "_"); |
| + } |
| + |
| + void writeString(String outfile, String text) { |
| + throw new UnsupportedOperationException(""); |
| + } |
| + |
| + bool fileExists(String filename) { |
| + 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.
|
| + } |
| + |
| + void createDirectory(String path, [bool recursive]) { |
| + throw new UnsupportedOperationException(""); |
| + } |
| + 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.
|
| + throw new UnsupportedOperationException(""); |
| + } |
| +} |