annotate parser-demo.js @ 10:8de776b8ed31

made the demo work on the web instead of the command line.
author Atul Varma <varmaa@toolness.com>
date Sat, 30 May 2009 16:08:25 -0700
parents
children 49145e1db3e5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 function print(text) {
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
46 var node = document.createTextNode(text.toString() + '\n');
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
47 $('.output').append(node);
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
48 }
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
49
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
50 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
51 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
52 text: code});
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
53
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
54 function printTokens(tokens) {
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
55 tokens.forEach(
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
56 function(token) {
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
57 var repr = token.name;
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
58 if (token.value)
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
59 repr += ":" + token.value;
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
60 repr += " @L" + token.lineNo + ":" + token.charNo;
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
61 print(repr);
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
62 });
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
63 }
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
64
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
65 printTokens(tokens);
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
66 var parser = new Parsing.Parser(tokens);
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
67 print(parser.parse());
8de776b8ed31 made the demo work on the web instead of the command line.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
68 });