comparison jsparser.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 bc6f30e0f948
children 95b27aa47788
comparison
equal deleted inserted replaced
9:bc6f30e0f948 10:8de776b8ed31
166 var value = this.expression(0); 166 var value = this.expression(0);
167 this.advance("end of input"); 167 this.advance("end of input");
168 return value; 168 return value;
169 }; 169 };
170 }; 170 };
171
172 function testParsing(print) {
173 var MyLexicon = [
174 new Parsing.BinaryOrUnaryOp({name: 'plus',
175 match: '+',
176 leftBindingPower: 60}),
177
178 new Parsing.BinaryOrUnaryOp({name: 'minus',
179 match: '-',
180 leftBindingPower: 60}),
181
182 new Parsing.Symbol({name: 'left parenthesis',
183 match: '(',
184 nullDenotation: function(parser) {
185 var contents = parser.expression(0);
186 parser.advance('right parenthesis');
187 return contents;
188 }}),
189
190 new Parsing.Symbol({name: 'right parenthesis',
191 match: ')'}),
192
193 new Parsing.BinaryOp({name: 'multiply',
194 match: '*',
195 leftBindingPower: 70}),
196
197 new Parsing.BinaryOp({name: 'divide',
198 match: '/',
199 leftBindingPower: 70}),
200
201 new Parsing.Symbol({name: 'number',
202 match: /^[0-9]+/,
203 nullDenotation: function() {
204 return this;
205 },
206 toString: function() {
207 return this.value;
208 }}),
209
210 new Parsing.Symbol({name: 'whitespace',
211 match: /^\s+/,
212 ignore: true})
213 ];
214
215 var code = '5+(1-3) * 4+ \n -4';
216 var tokens = Parsing.tokenize({lexicon: MyLexicon,
217 text: code});
218
219 function printTokens(tokens) {
220 tokens.forEach(
221 function(token) {
222 var repr = token.name;
223 if (token.value)
224 repr += ":" + token.value;
225 repr += " @L" + token.lineNo + ":" + token.charNo;
226 print(repr);
227 });
228 }
229
230 printTokens(tokens);
231 var parser = new Parsing.Parser(tokens);
232 print(parser.parse());
233 }
234
235 testParsing(print);