comparison jsparser.js @ 4:559378a3ec26

renamed computeSelf and computeLeft to nullDenotation and leftDenotation.
author Atul Varma <varmaa@toolness.com>
date Sat, 30 May 2009 13:28:20 -0700
parents f174153281a9
children f66ec534e75a
comparison
equal deleted inserted replaced
3:f174153281a9 4:559378a3ec26
7 this[name] = options[name]; 7 this[name] = options[name];
8 }; 8 };
9 9
10 Parsing.Symbol.prototype = { 10 Parsing.Symbol.prototype = {
11 leftBindingPower: 0, 11 leftBindingPower: 0,
12 computeSelf: function() { 12 nullDenotation: function() {
13 throw new Error("No computeSelf for " + this.name); 13 throw new Error("No nullDenotation for " + this.name);
14 }, 14 },
15 computeLeft: function() { 15 leftDenotation: function() {
16 throw new Error("No computeLeft for " + this.name); 16 throw new Error("No leftDenotation for " + this.name);
17 }, 17 },
18 extend: function(contents) { 18 extend: function(contents) {
19 function Subclass() {} 19 function Subclass() {}
20 Subclass.prototype = this; 20 Subclass.prototype = this;
21 var instance = new Subclass(); 21 var instance = new Subclass();
45 Parsing.BinaryOrUnaryOp = function BinaryOrUnaryOp(options) { 45 Parsing.BinaryOrUnaryOp = function BinaryOrUnaryOp(options) {
46 Parsing.Symbol.call(this, options); 46 Parsing.Symbol.call(this, options);
47 }; 47 };
48 48
49 Parsing.BinaryOrUnaryOp.prototype = new Parsing.Symbol( 49 Parsing.BinaryOrUnaryOp.prototype = new Parsing.Symbol(
50 {computeSelf: function(parser) { 50 {nullDenotation: function(parser) {
51 var unaryOp = this.extend(Parsing.UnaryOperator.prototype); 51 var unaryOp = this.extend(Parsing.UnaryOperator.prototype);
52 return unaryOp.computeSelf(parser); 52 return unaryOp.nullDenotation(parser);
53 }, 53 },
54 computeLeft: function(parser, left) { 54 leftDenotation: function(parser, left) {
55 var binaryOp = this.extend(Parsing.BinaryOperator.prototype); 55 var binaryOp = this.extend(Parsing.BinaryOperator.prototype);
56 return binaryOp.computeLeft(parser, left); 56 return binaryOp.leftDenotation(parser, left);
57 } 57 }
58 }); 58 });
59 59
60 Parsing.UnaryOperator = function UnaryOperator(options) { 60 Parsing.UnaryOperator = function UnaryOperator(options) {
61 Parsing.Symbol.call(this, options); 61 Parsing.Symbol.call(this, options);
62 }; 62 };
63 63
64 Parsing.UnaryOperator.prototype = new Parsing.Symbol( 64 Parsing.UnaryOperator.prototype = new Parsing.Symbol(
65 {computeSelf: function(parser) { 65 {nullDenotation: function(parser) {
66 this.arity = "unary"; 66 this.arity = "unary";
67 this.operand = parser.token; 67 this.operand = parser.token;
68 parser.advance(); 68 parser.advance();
69 return this; 69 return this;
70 }, 70 },
76 Parsing.BinaryOperator = function BinaryOperator(options) { 76 Parsing.BinaryOperator = function BinaryOperator(options) {
77 Parsing.Symbol.call(this, options); 77 Parsing.Symbol.call(this, options);
78 }; 78 };
79 79
80 Parsing.BinaryOperator.prototype = new Parsing.Symbol( 80 Parsing.BinaryOperator.prototype = new Parsing.Symbol(
81 {computeLeft: function(parser, left) { 81 {leftDenotation: function(parser, left) {
82 this.arity = "binary"; 82 this.arity = "binary";
83 this.leftOperand = left; 83 this.leftOperand = left;
84 this.rightOperand = parser.expression(this.leftBindingPower); 84 this.rightOperand = parser.expression(this.leftBindingPower);
85 return this; 85 return this;
86 }, 86 },
146 }; 146 };
147 147
148 this.expression = function expression(rightBindingPower) { 148 this.expression = function expression(rightBindingPower) {
149 var leftToken = self.token; 149 var leftToken = self.token;
150 self.advance(); 150 self.advance();
151 var leftValue = leftToken.computeSelf(self); 151 var leftValue = leftToken.nullDenotation(self);
152 while (rightBindingPower < self.token.leftBindingPower) { 152 while (rightBindingPower < self.token.leftBindingPower) {
153 leftToken = self.token; 153 leftToken = self.token;
154 self.advance(); 154 self.advance();
155 leftValue = leftToken.computeLeft(self, leftValue); 155 leftValue = leftToken.leftDenotation(self, leftValue);
156 } 156 }
157 return leftValue; 157 return leftValue;
158 }; 158 };
159 159
160 this.parse = function parse() { 160 this.parse = function parse() {
173 match: '-', 173 match: '-',
174 leftBindingPower: 60}), 174 leftBindingPower: 60}),
175 175
176 new Parsing.Symbol({name: 'number', 176 new Parsing.Symbol({name: 'number',
177 match: /^[0-9]+/, 177 match: /^[0-9]+/,
178 computeSelf: function() { 178 nullDenotation: function() {
179 return this; 179 return this;
180 }, 180 },
181 toString: function() { 181 toString: function() {
182 return this.value; 182 return this.value;
183 }}), 183 }}),