Advertisement
Guest User

index.html

a guest
Aug 30th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>ANTLR Grammar Example</title>
  6. <script>
  7. var antlr4;
  8. var CalcLexer;
  9. var CalcParser;
  10.  
  11. function print(message) {
  12.   var output = document.getElementById('output');
  13.   if (!output) return;
  14.   var p = document.createElement('p');
  15.   if (!p) return;
  16.   p.innerHTML = message;
  17.   output.appendChild(p);
  18. }
  19.  
  20. function getInputLine() {
  21.   var input = document.getElementById('input');
  22.   return (input) ? input.value : null;
  23. }
  24.  
  25. function parse() {
  26.   if (!antlr4 || !CalcLexer || !CalcParser) return;
  27.   var inputLine = getInputLine();
  28.   if (!inputLine) return;
  29.   var chars = new antlr4.InputStream(inputLine);
  30.   var lexer = new MyGrammarLexer.MyGrammarLexer(chars);
  31.   var tokens  = new antlr4.CommonTokenStream(lexer);
  32.   var parser = new MyGrammarParser.MyGrammarParser(tokens);
  33.   print('Result: ' + parser.eval().value);
  34. }
  35.  
  36. function init() {
  37.   document.body.style.backgroundColor = 'Silver';
  38.   print('Import ANTLR...');
  39.   antlr4 = require('antlr4/index');
  40.   print('Import CalcLexer...');
  41.   CalcLexer = require('./CalcLexer');
  42.   print('Import CalcParser...');
  43.   CalcParser = require('./CalcParser');
  44.   print('OK');
  45. }
  46. </script>
  47. </head>
  48. <body onload="init()">
  49. <div><input id="input" type="text" /></div>
  50. <div><button onclick="parse()">Parse</button></div>
  51. <div id="output"></div>
  52. <script src="lib/require.js"></script>
  53. </body>
  54. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement