Advertisement
Guest User

index.html

a guest
Aug 30th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   parser.buildParseTrees = true;
  34.   print('Result: ' + parser.eval().value);
  35. }
  36.  
  37. function init() {
  38.   document.body.style.backgroundColor = 'Silver';
  39.   print('Import ANTLR...');
  40.   antlr4 = require('antlr4/index');
  41.   print('Import CalcLexer...');
  42.   CalcLexer = require('./CalcLexer');
  43.   print('Import CalcParser...');
  44.   CalcParser = require('./CalcParser');
  45.   print('OK');
  46. }
  47. </script>
  48. </head>
  49. <body onload="init()">
  50. <div><input id="input" type="text" /></div>
  51. <div><button onclick="parse()">Parse</button></div>
  52. <div id="output"></div>
  53. <script src="lib/require.js"></script>
  54. </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement