Mercurial > jsparser
annotate parser-demo.js @ 14:95b27aa47788 default tip
parse tree now displays as html.
| author | Atul Varma <varmaa@toolness.com> |
|---|---|
| date | Sun, 31 May 2009 03:57:37 -0700 |
| parents | 1b7ea033b3f6 |
| children |
| rev | line source |
|---|---|
|
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
1 var MyLexicon = [ |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
2 new Parsing.BinaryOrUnaryOp({name: 'plus', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
3 match: '+', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
4 leftBindingPower: 60}), |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
5 |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
6 new Parsing.BinaryOrUnaryOp({name: 'minus', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
7 match: '-', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
8 leftBindingPower: 60}), |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
9 |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
10 new Parsing.Symbol({name: 'left parenthesis', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
11 match: '(', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
12 nullDenotation: function(parser) { |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
13 var contents = parser.expression(0); |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
14 parser.advance('right parenthesis'); |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
15 return contents; |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
16 }}), |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
17 |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
18 new Parsing.Symbol({name: 'right parenthesis', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
19 match: ')'}), |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
20 |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
21 new Parsing.BinaryOp({name: 'multiply', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
22 match: '*', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
23 leftBindingPower: 70}), |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
24 |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
25 new Parsing.BinaryOp({name: 'divide', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
26 match: '/', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
27 leftBindingPower: 70}), |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
28 |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
29 new Parsing.Symbol({name: 'number', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
30 match: /^[0-9]+/, |
|
14
95b27aa47788
parse tree now displays as html.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
31 toHtml: function() { |
|
95b27aa47788
parse tree now displays as html.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
32 return document.createTextNode(this.value); |
|
95b27aa47788
parse tree now displays as html.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
33 }, |
|
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
34 nullDenotation: function() { |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
35 return this; |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
36 }, |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
37 toString: function() { |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
38 return this.value; |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
39 }}), |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
40 |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
41 new Parsing.Symbol({name: 'whitespace', |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
42 match: /^\s+/, |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
43 ignore: true}) |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
44 ]; |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
45 |
|
13
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
46 function onInputChange() { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
47 var code = $('.input').val(); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
48 $('.tokenization').empty(); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
49 $('.parse-tree').empty(); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
50 |
|
14
95b27aa47788
parse tree now displays as html.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
51 if (!code) |
|
95b27aa47788
parse tree now displays as html.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
52 return; |
|
95b27aa47788
parse tree now displays as html.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
53 |
|
13
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
54 try { |
|
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
55 var tokens = Parsing.tokenize({lexicon: MyLexicon, |
|
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
56 text: code}); |
|
13
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
57 } catch (e) { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
58 $('.tokenization').text(e.message); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
59 throw e; |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
60 } |
|
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
61 |
|
13
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
62 jQuery.each( |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
63 tokens, |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
64 function() { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
65 var token = this; |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
66 var node = $('<span class="token"></span>'); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
67 node.text(token.value); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
68 node.hover( |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
69 function onIn() { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
70 var overlay = $('<div class="overlay"></div>'); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
71 overlay.text(token.name); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
72 overlay.css({left: $(this).position().left}); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
73 $(this).append(overlay); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
74 $(this).addClass("highlight"); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
75 }, |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
76 function onOut() { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
77 $(".overlay", this).remove(); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
78 $(this).removeClass("highlight"); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
79 }); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
80 $('.tokenization').append(node); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
81 }); |
|
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
82 |
|
13
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
83 var parser = new Parsing.Parser(tokens); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
84 var parseTree; |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
85 try { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
86 parseTree = parser.parse(); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
87 } catch (e) { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
88 $('.parse-tree').text(e.message); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
89 throw e; |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
90 } |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
91 |
|
14
95b27aa47788
parse tree now displays as html.
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
92 $('.parse-tree').append(parseTree.toHtml()); |
|
13
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
93 } |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
94 |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
95 $(window).ready( |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
96 function() { |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
97 $('.input').keyup(onInputChange); |
|
1b7ea033b3f6
input is interactive now, and tokenization/parsing errors are displayed.
Atul Varma <varmaa@toolness.com>
parents:
11
diff
changeset
|
98 onInputChange(); |
|
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
99 }); |
