Navigation Menu

Skip to content

Commit

Permalink
Split source code into one file per class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Apr 14, 2010
1 parent 420b2a2 commit f541cb2
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 140 deletions.
8 changes: 8 additions & 0 deletions jake.yml
Expand Up @@ -15,6 +15,14 @@ packages:
stake:
files:
- stake
- stake/any_char_parser
- stake/char_class_parser
- stake/choice_parser
- stake/maybe_parser
- stake/not_parser
- stake/repeat_parser
- stake/sequence_parser
- stake/string_parser
meta:
provides:
- Stake
Expand Down
140 changes: 0 additions & 140 deletions source/stake.js
Expand Up @@ -44,143 +44,3 @@ Stake.extend({
})
});

Stake.extend({
AnyCharParser: new JS.Class(Stake.Parser, {
consume: function(input, offset) {
if (input === '') return null;
return this._syntaxNode(input.substring(0,1), offset);
}
}),

CharClassParser: new JS.Class(Stake.Parser, {
initialize: function(charClass) {
this._charClass = charClass;
this._pattern = new RegExp('^' + charClass);
},

consume: function(input, offset) {
var match = input.match(this._pattern);
return match ? this._syntaxNode(String(match), offset) : null;
}
}),

ChoiceParser: new JS.Class(Stake.Parser, {
extend: {
create: function() {
return new this(Array.prototype.slice.call(arguments));
}
},

initialize: function(choices) {
this._choices = choices;
},

consume: function(input, offset) {
var choices = this._choices,
n = choices.length, i, node;

for (i = 0; i < n; i++) {
if (node = choices[i].consume(input, offset))
return node;
}
return null;
}
}),

MaybeParser: new JS.Class(Stake.Parser, {
initialize: function(parser) {
this._parser = parser;
},

consume: function(input, offset) {
var node = this._parser.consume(input, offset);
return node || this._syntaxNode('', offset);
}
}),

NotParser: new JS.Class(Stake.Parser, {
initialize: function(parser) {
this._parser = parser;
},

consume: function(input, offset) {
var node = this._parser.consume(input, offset);
return node ? null : this._syntaxNode('', offset);
}
}),

RepeatParser: new JS.Class(Stake.Parser, {
extend: {
create: function(minimum, parser) {
return new this(parser, minimum);
}
},

initialize: function(parser, minimum) {
this._parser = parser;
this._minimum = minimum || 0;
},

consume: function(input, offset) {
var elements = [],
textValue = '',
counter = offset,
remaining = this._minimum,
node = true;

while (node = this._parser.consume(input, counter)) {
elements.push(node);
input = input.substring(node.textValue.length);
textValue += node.textValue;
counter += node.textValue.length;
remaining -= 1;
}
if (remaining > 0) return null;
return this._syntaxNode(textValue, offset, elements);
}
}),

SequenceParser: new JS.Class(Stake.Parser, {
extend: {
create: function() {
return new this(Array.prototype.slice.call(arguments));
}
},

initialize: function(parsers) {
this._parsers = parsers;
},

consume: function(input, offset) {
var elements = [],
parsers = this._parsers,
textValue = '',
counter = offset,
n = parsers.length, i, node;

for (i = 0; i < n; i++) {
node = parsers[i].consume(input, counter);
if (!node) return null;

elements.push(node);
input = input.substring(node.textValue.length);

textValue += node.textValue;
counter += node.textValue.length;
}
return this._syntaxNode(textValue, offset, elements);
}
}),

StringParser: new JS.Class(Stake.Parser, {
initialize: function(string) {
this._string = string;
},

consume: function(input, offset) {
if (!this._begins(input, this._string)) return null;
return this._syntaxNode(this._string, offset);
}
})
});

9 changes: 9 additions & 0 deletions source/stake/any_char_parser.js
@@ -0,0 +1,9 @@
Stake.extend({
AnyCharParser: new JS.Class(Stake.Parser, {
consume: function(input, offset) {
if (input === '') return null;
return this._syntaxNode(input.substring(0,1), offset);
}
})
});

14 changes: 14 additions & 0 deletions source/stake/char_class_parser.js
@@ -0,0 +1,14 @@
Stake.extend({
CharClassParser: new JS.Class(Stake.Parser, {
initialize: function(charClass) {
this._charClass = charClass;
this._pattern = new RegExp('^' + charClass);
},

consume: function(input, offset) {
var match = input.match(this._pattern);
return match ? this._syntaxNode(String(match), offset) : null;
}
})
});

25 changes: 25 additions & 0 deletions source/stake/choice_parser.js
@@ -0,0 +1,25 @@
Stake.extend({
ChoiceParser: new JS.Class(Stake.Parser, {
extend: {
create: function() {
return new this(Array.prototype.slice.call(arguments));
}
},

initialize: function(choices) {
this._choices = choices;
},

consume: function(input, offset) {
var choices = this._choices,
n = choices.length, i, node;

for (i = 0; i < n; i++) {
if (node = choices[i].consume(input, offset))
return node;
}
return null;
}
})
});

13 changes: 13 additions & 0 deletions source/stake/maybe_parser.js
@@ -0,0 +1,13 @@
Stake.extend({
MaybeParser: new JS.Class(Stake.Parser, {
initialize: function(parser) {
this._parser = parser;
},

consume: function(input, offset) {
var node = this._parser.consume(input, offset);
return node || this._syntaxNode('', offset);
}
})
});

13 changes: 13 additions & 0 deletions source/stake/not_parser.js
@@ -0,0 +1,13 @@
Stake.extend({
NotParser: new JS.Class(Stake.Parser, {
initialize: function(parser) {
this._parser = parser;
},

consume: function(input, offset) {
var node = this._parser.consume(input, offset);
return node ? null : this._syntaxNode('', offset);
}
})
});

33 changes: 33 additions & 0 deletions source/stake/repeat_parser.js
@@ -0,0 +1,33 @@
Stake.extend({
RepeatParser: new JS.Class(Stake.Parser, {
extend: {
create: function(minimum, parser) {
return new this(parser, minimum);
}
},

initialize: function(parser, minimum) {
this._parser = parser;
this._minimum = minimum || 0;
},

consume: function(input, offset) {
var elements = [],
textValue = '',
counter = offset,
remaining = this._minimum,
node = true;

while (node = this._parser.consume(input, counter)) {
elements.push(node);
input = input.substring(node.textValue.length);
textValue += node.textValue;
counter += node.textValue.length;
remaining -= 1;
}
if (remaining > 0) return null;
return this._syntaxNode(textValue, offset, elements);
}
})
});

34 changes: 34 additions & 0 deletions source/stake/sequence_parser.js
@@ -0,0 +1,34 @@
Stake.extend({
SequenceParser: new JS.Class(Stake.Parser, {
extend: {
create: function() {
return new this(Array.prototype.slice.call(arguments));
}
},

initialize: function(parsers) {
this._parsers = parsers;
},

consume: function(input, offset) {
var elements = [],
parsers = this._parsers,
textValue = '',
counter = offset,
n = parsers.length, i, node;

for (i = 0; i < n; i++) {
node = parsers[i].consume(input, counter);
if (!node) return null;

elements.push(node);
input = input.substring(node.textValue.length);

textValue += node.textValue;
counter += node.textValue.length;
}
return this._syntaxNode(textValue, offset, elements);
}
})
});

13 changes: 13 additions & 0 deletions source/stake/string_parser.js
@@ -0,0 +1,13 @@
Stake.extend({
StringParser: new JS.Class(Stake.Parser, {
initialize: function(string) {
this._string = string;
},

consume: function(input, offset) {
if (!this._begins(input, this._string)) return null;
return this._syntaxNode(this._string, offset);
}
})
});

0 comments on commit f541cb2

Please sign in to comment.