Mercurial > jsparser
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:bc6f30e0f948 | 10:8de776b8ed31 |
---|---|
1 var MyLexicon = [ | |
2 new Parsing.BinaryOrUnaryOp({name: 'plus', | |
3 match: '+', | |
4 leftBindingPower: 60}), | |
5 | |
6 new Parsing.BinaryOrUnaryOp({name: 'minus', | |
7 match: '-', | |
8 leftBindingPower: 60}), | |
9 | |
10 new Parsing.Symbol({name: 'left parenthesis', | |
11 match: '(', | |
12 nullDenotation: function(parser) { | |
13 var contents = parser.expression(0); | |
14 parser.advance('right parenthesis'); | |
15 return contents; | |
16 }}), | |
17 | |
18 new Parsing.Symbol({name: 'right parenthesis', | |
19 match: ')'}), | |
20 | |
21 new Parsing.BinaryOp({name: 'multiply', | |
22 match: '*', | |
23 leftBindingPower: 70}), | |
24 | |
25 new Parsing.BinaryOp({name: 'divide', | |
26 match: '/', | |
27 leftBindingPower: 70}), | |
28 | |
29 new Parsing.Symbol({name: 'number', | |
30 match: /^[0-9]+/, | |
31 nullDenotation: function() { | |
32 return this; | |
33 }, | |
34 toString: function() { | |
35 return this.value; | |
36 }}), | |
37 | |
38 new Parsing.Symbol({name: 'whitespace', | |
39 match: /^\s+/, | |
40 ignore: true}) | |
41 ]; | |
42 | |
43 $(window).ready( | |
44 function() { | |
45 function print(text) { | |
46 var node = document.createTextNode(text.toString() + '\n'); | |
47 $('.output').append(node); | |
48 } | |
49 | |
50 var code = $('.input').text(); | |
51 var tokens = Parsing.tokenize({lexicon: MyLexicon, | |
52 text: code}); | |
53 | |
54 function printTokens(tokens) { | |
55 tokens.forEach( | |
56 function(token) { | |
57 var repr = token.name; | |
58 if (token.value) | |
59 repr += ":" + token.value; | |
60 repr += " @L" + token.lineNo + ":" + token.charNo; | |
61 print(repr); | |
62 }); | |
63 } | |
64 | |
65 printTokens(tokens); | |
66 var parser = new Parsing.Parser(tokens); | |
67 print(parser.parse()); | |
68 }); |