simplifying RangeNode grammar a bit.

This commit is contained in:
Jeremy Ashkenas
2010-09-04 06:48:20 -04:00
parent 493780efab
commit 3b60aad487
5 changed files with 129 additions and 133 deletions

View File

@@ -299,31 +299,30 @@
return new ValueNode(new LiteralNode('this')); return new ValueNode(new LiteralNode('this'));
}) })
], ],
RangeDots: [
o(". .", function() {
return 'inclusive';
}), o(". . .", function() {
return 'exclusive';
})
],
ThisProperty: [ ThisProperty: [
o("@ Identifier", function() { o("@ Identifier", function() {
return new ValueNode(new LiteralNode('this'), [new AccessorNode($2)]); return new ValueNode(new LiteralNode('this'), [new AccessorNode($2)]);
}) })
], ],
Range: [ Range: [
o("[ Expression . . Expression ]", function() { o("[ Expression RangeDots Expression ]", function() {
return new RangeNode($2, $5); return new RangeNode($2, $4, $3);
}), o("[ Expression . . . Expression ]", function() {
return new RangeNode($2, $6, true);
}) })
], ],
Slice: [ Slice: [
o("INDEX_START Expression . . Expression INDEX_END", function() { o("INDEX_START Expression RangeDots Expression INDEX_END", function() {
return new RangeNode($2, $5); return new RangeNode($2, $4, $3);
}), o("INDEX_START Expression . . . Expression INDEX_END", function() { }), o("INDEX_START Expression RangeDots INDEX_END", function() {
return new RangeNode($2, $6, true); return new RangeNode($2, null, $3);
}), o("INDEX_START Expression . . INDEX_END", function() { }), o("INDEX_START RangeDots Expression INDEX_END", function() {
return new RangeNode($2, null); return new RangeNode(null, $3, $2);
}), o("INDEX_START Expression . . . INDEX_END", function() {
return new RangeNode($2, null, true);
}), o("INDEX_START . . Expression INDEX_END", function() {
return new RangeNode(null, $4);
}), o("INDEX_START . . . Expression INDEX_END", function() {
return new RangeNode(null, $5, true);
}) })
], ],
Array: [ Array: [

View File

@@ -596,11 +596,11 @@
return IndexNode; return IndexNode;
})(); })();
exports.RangeNode = (function() { exports.RangeNode = (function() {
RangeNode = function(_b, _c, exclusive) { RangeNode = function(_b, _c, tag) {
this.to = _c; this.to = _c;
this.from = _b; this.from = _b;
RangeNode.__super__.constructor.call(this); RangeNode.__super__.constructor.call(this);
this.exclusive = !!exclusive; this.exclusive = tag === 'exclusive';
this.equals = this.exclusive ? '' : '='; this.equals = this.exclusive ? '' : '=';
return this; return this;
}; };

File diff suppressed because one or more lines are too long

View File

@@ -340,6 +340,11 @@ grammar =
o "@", -> new ValueNode new LiteralNode 'this' o "@", -> new ValueNode new LiteralNode 'this'
] ]
RangeDots: [
o ". .", -> 'inclusive'
o ". . .", -> 'exclusive'
]
# A reference to a property on *this*. # A reference to a property on *this*.
ThisProperty: [ ThisProperty: [
o "@ Identifier", -> new ValueNode new LiteralNode('this'), [new AccessorNode($2)] o "@ Identifier", -> new ValueNode new LiteralNode('this'), [new AccessorNode($2)]
@@ -347,18 +352,14 @@ grammar =
# The CoffeeScript range literal. # The CoffeeScript range literal.
Range: [ Range: [
o "[ Expression . . Expression ]", -> new RangeNode $2, $5 o "[ Expression RangeDots Expression ]", -> new RangeNode $2, $4, $3
o "[ Expression . . . Expression ]", -> new RangeNode $2, $6, true
] ]
# The slice literal. # The slice literal.
Slice: [ Slice: [
o "INDEX_START Expression . . Expression INDEX_END", -> new RangeNode $2, $5 o "INDEX_START Expression RangeDots Expression INDEX_END", -> new RangeNode $2, $4, $3
o "INDEX_START Expression . . . Expression INDEX_END", -> new RangeNode $2, $6, true o "INDEX_START Expression RangeDots INDEX_END", -> new RangeNode $2, null, $3
o "INDEX_START Expression . . INDEX_END", -> new RangeNode $2, null o "INDEX_START RangeDots Expression INDEX_END", -> new RangeNode null, $3, $2
o "INDEX_START Expression . . . INDEX_END", -> new RangeNode $2, null, true
o "INDEX_START . . Expression INDEX_END", -> new RangeNode null, $4
o "INDEX_START . . . Expression INDEX_END", -> new RangeNode null, $5, true
] ]
# The array literal. # The array literal.

View File

@@ -550,9 +550,9 @@ exports.RangeNode = class RangeNode extends BaseNode
class: 'RangeNode' class: 'RangeNode'
children: ['from', 'to'] children: ['from', 'to']
constructor: (@from, @to, exclusive) -> constructor: (@from, @to, tag) ->
super() super()
@exclusive = !!exclusive @exclusive = tag is 'exclusive'
@equals = if @exclusive then '' else '=' @equals = if @exclusive then '' else '='
# Compiles the range's source variables -- where it starts and where it ends. # Compiles the range's source variables -- where it starts and where it ends.