Skip to content

Commit

Permalink
Add a parser for regexp-style character classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Apr 13, 2010
1 parent cf96701 commit 8d52d46
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
12 changes: 12 additions & 0 deletions source/stake.js
Expand Up @@ -45,6 +45,18 @@ Stake.extend({
});

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;
}
}),

ChoiceParser: new JS.Class(Stake.Parser, {
extend: {
create: function() {
Expand Down
3 changes: 2 additions & 1 deletion spec/runner.js
Expand Up @@ -7,7 +7,8 @@ JS.Packages(function() { with(this) {
}})

require('JS.Test', 'Stake', function() {
require('Stake.ChoiceParserSpec',
require('Stake.CharClassParserSpec',
'Stake.ChoiceParserSpec',
'Stake.MaybeParserSpec',
'Stake.RepeatParserSpec',
'Stake.SequenceParserSpec',
Expand Down
78 changes: 78 additions & 0 deletions spec/stake/char_class_parser_spec.js
@@ -0,0 +1,78 @@
Stake.CharClassParserSpec = JS.Test.describe(Stake.CharClassParser,
function() { with(this) {
describe('positive', function() { with(this) {
before(function() { with(this) {
this.parser = Stake.Parser.fromSexp(
['char-class', '[a-z]'])
}})

it('parses characters within the class', function() { with(this) {
assertEqual( {textValue: 'a', offset: 0, elements: []}, parser.parse('a') )
}})

it('does not parse characters outside the class', function() { with(this) {
assertNull( parser.parse('7') )
}})

it('does not parse characters within the class appearing too late', function() { with(this) {
assertNull( parser.parse('7a') )
}})
}})

describe('negative', function() { with(this) {
before(function() { with(this) {
this.parser = Stake.Parser.fromSexp(
['char-class', '[^a-z]'])
}})

it('parses characters within the class', function() { with(this) {
assertEqual( {textValue: '7', offset: 0, elements: []}, parser.parse('7') )
}})

it('does not parse characters outside the class', function() { with(this) {
assertNull( parser.parse('a') )
}})

it('does not parse characters within the class appearing too late', function() { with(this) {
assertNull( parser.parse('a7') )
}})
}})

describe('with sequencing and repetion', function() { with(this) {
before(function() { with(this) {
this.parser = Stake.Parser.fromSexp(
['sequence',
['char-class', '[1-9]'],
['repeat', 0, ['char-class', '[0-9]']]])
}})

it('parses integers', function() { with(this) {
assertEqual( {
textValue: '3718',
offset: 0,
elements: [
{textValue: '3', offset: 0, elements: []},
{
textValue: '718',
offset: 1,
elements: [
{textValue: '7', offset: 1, elements: []},
{textValue: '1', offset: 2, elements: []},
{textValue: '8', offset: 3, elements: []}
]
}
]
},
parser.parse('3718') )
}})

it('does not parse floats', function() { with(this) {
assertNull( parser.parse('7.4') )
}})

it('does not parse octal', function() { with(this) {
assertNull( parser.parse('0644') )
}})
}})
}})

0 comments on commit 8d52d46

Please sign in to comment.