| OLD | NEW |
| 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 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * This is the entrypoint for a version of the frog compiler that | 7 * This is the entrypoint for a version of the frog compiler that |
| 8 * can run in the browser. | 8 * can run in the browser. |
| 9 * Because this is running in the browser, frog cannot access | 9 * Because this is running in the browser, frog cannot access |
| 10 * the file system. Instead, we assume the all necessary files | 10 * the file system. Instead, we assume the all necessary files |
| 11 * have been placed in inert <script> elements somewhere on the page. | 11 * have been placed in inert <script> elements somewhere on the page. |
| 12 * (See frogpad.py for more details on how the html page is constructed.) | 12 * (See frogpad.py for more details on how the html page is constructed.) |
| 13 */ | 13 */ |
| 14 | 14 |
| 15 #import("dart:html", prefix:"html"); | 15 #import("dart:html", prefix:"html"); |
| 16 #import("../../../frog/lang.dart"); | 16 #import("../../../frog/lang.dart"); |
| 17 #import("../../../frog/file_system.dart"); | 17 #import("../../../frog/file_system.dart"); |
| 18 | 18 |
| 19 // id of script element containing name of the main dart file | 19 // id of script element containing name of the main dart file |
| 20 // to compile. | 20 // to compile. |
| 21 final String MAIN_ID = "main_id"; | 21 final String MAIN_ID = "main_id"; |
| 22 | 22 |
| 23 // id of script element containing name of the frog directory | 23 // id of script element containing name of the frog directory |
| 24 final String FROGDIR_ID = "frogdir_id"; | 24 final String FROGDIR_ID = "frogdir_id"; |
| 25 | 25 |
| 26 void main() { | 26 void main() { |
| 27 String warnings = ""; | 27 StringBuffer warnings = new StringBuffer(); |
| 28 HtmlFileSystem fs = new HtmlFileSystem(); | 28 HtmlFileSystem fs = new HtmlFileSystem(); |
| 29 String frogDir = getText(FROGDIR_ID); | 29 String frogDir = getText(FROGDIR_ID); |
| 30 String mainFile = getText(MAIN_ID); | 30 String mainFile = getText(MAIN_ID); |
| 31 setText("input", fs.readAll(mainFile)); | 31 setText("input", fs.readAll(mainFile)); |
| 32 | 32 |
| 33 int time1 = new Date.now().value; | 33 int time1 = new Date.now().value; |
| 34 List<String> args = [ | 34 List<String> args = [ |
| 35 "dummy_arg1", | 35 "dummy_arg1", |
| 36 "dummy_arg2", | 36 "dummy_arg2", |
| 37 "--enable_type_checks", | 37 "--enable_type_checks", |
| 38 "--enable_asserts", | 38 "--enable_asserts", |
| 39 mainFile]; | 39 mainFile]; |
| 40 parseOptions(frogDir, args, fs); | 40 parseOptions(frogDir, args, fs); |
| 41 | 41 |
| 42 options.useColors = false; | 42 options.useColors = false; |
| 43 | 43 |
| 44 initializeWorld(fs); | 44 initializeWorld(fs); |
| 45 world.messageHandler = void _( | 45 world.messageHandler = void _( |
| 46 String prefix, String message, SourceSpan span) { | 46 String prefix, String message, SourceSpan span) { |
| 47 String location = ""; | 47 String location = ""; |
| 48 if (span !== null) { | 48 if (span !== null) { |
| 49 location = span.locationText; | 49 location = span.locationText; |
| 50 } | 50 } |
| 51 warnings += prefix + message + location + "\n"; | 51 warnings.add('$prefix$message$location\n'); |
| 52 }; | 52 }; |
| 53 bool success = world.compile(); | 53 bool success = world.compile(); |
| 54 int time2 = new Date.now().value; | 54 int time2 = new Date.now().value; |
| 55 String output = "throw 'frogpad compilation error';\n"; | 55 String output = "throw 'frogpad compilation error';\n"; |
| 56 if (success) { | 56 if (success) { |
| 57 output = world.getGeneratedCode(); | 57 output = world.getGeneratedCode(); |
| 58 } | 58 } |
| 59 setText("output", output); | 59 setText("output", output); |
| 60 setText("warnings", warnings); | 60 setText("warnings", warnings.toString()); |
| 61 | 61 |
| 62 String timing = "generated ${output.length} characters in " + | 62 String timing = "generated ${output.length} characters in " |
| 63 "${((time2 - time1) / 1000).toStringAsPrecision(3)} seconds"; | 63 "${((time2 - time1) / 1000).toStringAsPrecision(3)} seconds"; |
| 64 setText("timing", timing); | 64 setText("timing", timing); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void setText(String id, String text) { | 67 void setText(String id, String text) { |
| 68 html.Element element = html.document.query("#$id"); | 68 html.Element element = html.document.query("#$id"); |
| 69 if (element === null) { | 69 if (element === null) { |
| 70 throw new Exception("Can't find element $id"); | 70 throw new Exception("Can't find element $id"); |
| 71 } | 71 } |
| 72 element.innerHTML = htmlEscape(text); | 72 element.innerHTML = htmlEscape(text); |
| 73 } | 73 } |
| 74 | 74 |
| 75 String getText(String id) { | 75 String getText(String id) { |
| 76 html.Element element = html.document.query("#$id"); | 76 html.Element element = html.document.query("#$id"); |
| 77 if (element === null) { | 77 if (element === null) { |
| 78 throw new Exception("Can't find element $id"); | 78 throw new Exception("Can't find element $id"); |
| 79 } | 79 } |
| 80 return element.text.trim(); | 80 return element.text.trim(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // TODO(rnystrom): should exist in standard lib somewhere | 83 // TODO(rnystrom): should exist in standard lib somewhere |
| 84 String htmlEscape(String text) { | 84 String htmlEscape(String text) { |
| 85 return text.replaceAll('&', '&').replaceAll( | 85 return text.replaceAll('&', '&') |
| 86 '>', '>').replaceAll('<', '<'); | 86 .replaceAll('>', '>') |
| 87 .replaceAll('<', '<'); |
| 87 } | 88 } |
| 88 | 89 |
| 89 class HtmlFileSystem implements FileSystem { | 90 class HtmlFileSystem implements FileSystem { |
| 90 | 91 |
| 91 HtmlFileSystem() {} | 92 HtmlFileSystem() {} |
| 92 | 93 |
| 93 String readAll(String filename) { | 94 String readAll(String filename) { |
| 94 String text = getText(idOfFilename(filename)); | 95 String text = getText(idOfFilename(filename)); |
| 95 print("read $filename (${text.length} bytes)"); | 96 print("read $filename (${text.length} bytes)"); |
| 96 return text; | 97 return text; |
| 97 } | 98 } |
| 98 | 99 |
| 99 /** | 100 /** |
| 100 * Returns the id of the <script> element that contains | 101 * Returns the id of the <script> element that contains |
| 101 * the contents of this file. | 102 * the contents of this file. |
| 102 * The id is constructed by taking the filename and replacing | 103 * The id is constructed by taking the filename and replacing |
| 103 * all slashes and dots with underscores. For example, the file name: | 104 * all slashes and dots with underscores. For example, the file name: |
| 104 * | 105 * |
| 105 * "/usr/local/src/dart/frog/lang.dart" | 106 * "/usr/local/src/dart/frog/lang.dart" |
| 106 * | 107 * |
| 107 * becomes: | 108 * becomes: |
| 108 * | 109 * |
| 109 * "_usr_local_src_dart_frog_lang_dart" | 110 * "_usr_local_src_dart_frog_lang_dart" |
| 110 * | 111 * |
| 111 * And, so this file's contents will be found in a <script> | 112 * And, so this file's contents will be found in a <script> |
| 112 * element that looks like this: | 113 * element that looks like this: |
| 113 * | 114 * |
| 114 * <script type=application/inert id="_usr_local_src_dart_frog_lang_dart"> | 115 * <script type=application/inert id="_usr_local_src_dart_frog_lang_dart"> |
| 115 * ... contents of file lang.dart placed here ... | 116 * ... contents of file lang.dart placed here ... |
| 116 * etc. | 117 * etc. |
| 117 */ | 118 */ |
| 118 String idOfFilename(String filename) { | 119 String idOfFilename(String filename) { |
| 119 return filename.replaceAll("/", "_").replaceAll(".", "_"); | 120 return filename.replaceAll("/", "_").replaceAll(".", "_"); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void writeString(String outfile, String text) { | 123 void writeString(String outfile, String text) { |
| 123 throw new UnsupportedOperationException(""); | 124 throw new UnsupportedOperationException(""); |
| 124 } | 125 } |
| 125 | 126 |
| 126 bool fileExists(String filename) { | 127 bool fileExists(String filename) { |
| 127 // frog calls this to check if files exist before reading them. We return | 128 // frog calls this to check if files exist before reading them. We return |
| 128 // true here for all files, and let it fail later if frog attempts to read | 129 // true here for all files, and let it fail later if frog attempts to read |
| 129 // the contents of a non-existent file. | 130 // the contents of a non-existent file. |
| 130 return true; | 131 return true; |
| 131 } | 132 } |
| 132 | 133 |
| 133 void createDirectory(String path, [bool recursive]) { | 134 void createDirectory(String path, [bool recursive]) { |
| 134 throw new UnsupportedOperationException(""); | 135 throw new UnsupportedOperationException(""); |
| 135 } | 136 } |
| 136 | 137 |
| 137 void removeDirectory(String path, [bool recursive]) { | 138 void removeDirectory(String path, [bool recursive]) { |
| 138 throw new UnsupportedOperationException(""); | 139 throw new UnsupportedOperationException(""); |
| 139 } | 140 } |
| 140 } | 141 } |
| OLD | NEW |