Mercurial > jsparser
annotate parser-demo.js @ 11:49145e1db3e5
added an interactive tokenization display.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sat, 30 May 2009 16:38:20 -0700 |
parents | 8de776b8ed31 |
children | 1b7ea033b3f6 |
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]+/, |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
31 nullDenotation: function() { |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
32 return this; |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
33 }, |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
34 toString: 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.value; |
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 |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
38 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
|
39 match: /^\s+/, |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
40 ignore: true}) |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
41 ]; |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
42 |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
43 $(window).ready( |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
44 function() { |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
45 var code = $('.input').text(); |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
46 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
|
47 text: code}); |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
48 |
11
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
49 jQuery.each( |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
50 tokens, |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
51 function() { |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
52 var token = this; |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
53 var node = $('<span class="token"></span>'); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
54 node.text(token.value); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
55 node.hover( |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
56 function onIn() { |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
57 var overlay = $('<div class="overlay"></div>'); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
58 overlay.text(token.name); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
59 overlay.css({left: $(this).position().left}); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
60 $(this).append(overlay); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
61 $(this).addClass("highlight"); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
62 }, |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
63 function onOut() { |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
64 $(".overlay", this).remove(); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
65 $(this).removeClass("highlight"); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
66 }); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
67 $('.tokenization').append(node); |
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
68 }); |
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
69 |
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
70 var parser = new Parsing.Parser(tokens); |
11
49145e1db3e5
added an interactive tokenization display.
Atul Varma <varmaa@toolness.com>
parents:
10
diff
changeset
|
71 $('.parse-tree').text(parser.parse().toString()); |
10
8de776b8ed31
made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
72 }); |