changeset 13:1b7ea033b3f6

input is interactive now, and tokenization/parsing errors are displayed.
author Atul Varma <varmaa@toolness.com>
date Sat, 30 May 2009 16:55:51 -0700
parents 485783e3b54c
children 95b27aa47788
files jsparser.html parser-demo.js
diffstat 2 files changed, 48 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/jsparser.html	Sat May 30 16:44:43 2009 -0700
+++ b/jsparser.html	Sat May 30 16:55:51 2009 -0700
@@ -6,10 +6,10 @@
 </head>
 <body>
 <p>Input</p>
-<pre class="input">
+<textarea class="input">
 5+(1-3) * 4+       
      -4
-</pre>
+</textarea>
 <p>Tokenization</p>
 <pre class="tokenization"></pre>
 <p>Parse Tree</p>
--- a/parser-demo.js	Sat May 30 16:44:43 2009 -0700
+++ b/parser-demo.js	Sat May 30 16:55:51 2009 -0700
@@ -40,33 +40,54 @@
                       ignore: true})
 ];
 
-$(window).ready(
-  function() {
-    var code = $('.input').text();
+function onInputChange() {
+  var code = $('.input').val();
+  $('.tokenization').empty();
+  $('.parse-tree').empty();
+
+  try {
     var tokens = Parsing.tokenize({lexicon: MyLexicon,
                                    text: code});
+  } catch (e) {
+    $('.tokenization').text(e.message);
+    throw e;
+  }
 
-    jQuery.each(
-      tokens,
-      function() {
-        var token = this;
-        var node = $('<span class="token"></span>');
-        node.text(token.value);
-        node.hover(
-          function onIn() {
-            var overlay = $('<div class="overlay"></div>');
-            overlay.text(token.name);
-            overlay.css({left: $(this).position().left});
-            $(this).append(overlay);
-            $(this).addClass("highlight");
-          },
-          function onOut() {
-            $(".overlay", this).remove();
-            $(this).removeClass("highlight");
-          });
-        $('.tokenization').append(node);
-      });
+  jQuery.each(
+    tokens,
+    function() {
+      var token = this;
+      var node = $('<span class="token"></span>');
+      node.text(token.value);
+      node.hover(
+        function onIn() {
+          var overlay = $('<div class="overlay"></div>');
+          overlay.text(token.name);
+          overlay.css({left: $(this).position().left});
+          $(this).append(overlay);
+          $(this).addClass("highlight");
+        },
+        function onOut() {
+          $(".overlay", this).remove();
+          $(this).removeClass("highlight");
+        });
+      $('.tokenization').append(node);
+    });
 
-    var parser = new Parsing.Parser(tokens);
-    $('.parse-tree').text(parser.parse().toString());
+  var parser = new Parsing.Parser(tokens);
+  var parseTree;
+  try {
+    parseTree = parser.parse();
+  } catch (e) {
+    $('.parse-tree').text(e.message);
+    throw e;
+  }
+
+  $('.parse-tree').text(parseTree.toString());
+}
+
+$(window).ready(
+  function() {
+    $('.input').keyup(onInputChange);
+    onInputChange();
   });