UNPKG

cashew-js

Version:

A JavaToJavaScript parser, outputs a Mozilla AST

114 lines (104 loc) 3.1 kB
<!DOCTYPE html> <html> <head> <title>Cashew Web Parser</title> <meta charset="utf-8"> <style> html, body { margin: 0; padding: 0; height: 100%; } textarea { font-size: 18px; } #codeArea, #jsText, #astText, #outputText { width: 45%; height: 45%; padding: 10px 15px; font-size: 16px; margin: 10px; border: 1px solid silver; display: inline-block; overflow: hidden; } .btn-parser { padding: 10px 25px; background-color: #E6DB74; font-size: 16px; border: none; position: absolute; left: 38%; top: 42%; z-index: 9; } </style> <script src="cashew.js"></script> <script src="escodegen.browser.js"></script> <script> (function() { var oldLog = console.log; console.log = function(message){ outputText.innerHTML += (message + '\n'); oldLog.apply(console, arguments); }; })(); var Parser = cashew.Parse; function submitCode(){ var code = editor.getValue(); outputText.innerHTML = ''; try { var ast = Parser(code); // Display the AST text astHigh.setValue(JSON.stringify(ast, null, 2)); astHigh.gotoLine(1); // Generate and display the JS code var js = escodegen.generate(ast); js = "(function(___JavaRuntime){" + js + "})(cashew.___JavaRuntime);"; jseditor.setValue(js); jseditor.gotoLine(1); // Execute the transpiled code eval(js); } catch(err){ console.log(err); } } </script> </head> <body> <button class="btn-parser" onclick='submitCode()'>Parse it!</button> <div id="codeArea">public class MyClass { /** main method */ public static void main(String[] args) { System.out.println("Type your code here"); } } </div> <pre id="jsText"></pre> <pre id="astText"></pre> <pre id="outputText"></pre> <script type="text/javascript" src="vendor/ace.js"></script> <script type="text/javascript"> var editor = ace.edit("codeArea"); editor.setTheme("ace/theme/github"); editor.getSession().setTabSize(2); editor.getSession().setMode("ace/mode/java"); editor.$blockScrolling = Infinity; var jseditor = ace.edit("jsText"); jseditor.setTheme("ace/theme/textmate"); jseditor.getSession().setTabSize(2); jseditor.getSession().setMode("ace/mode/javascript"); jseditor.setHighlightActiveLine(false); jseditor.setOption('showGutter', false); jseditor.setReadOnly(true); jseditor.$blockScrolling = Infinity; var astHigh = ace.edit("astText"); astHigh.setTheme("ace/theme/github"); astHigh.getSession().setTabSize(2); astHigh.getSession().setMode("ace/mode/json"); astHigh.setHighlightActiveLine(false); astHigh.setOption('showGutter', false); astHigh.setReadOnly(true); astHigh.$blockScrolling = Infinity; </script> </body> </html>