-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes state handling and adds more tests
- Loading branch information
Brian Mock
committed
Jul 5, 2017
1 parent
97b27de
commit 32316cf
Showing
20 changed files
with
490 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
suite('all', function() { | ||
|
||
test('should pass state through', function() { | ||
function test(n) { | ||
return Parsimmon(function(input, i, state) { | ||
assert.strictEqual(state, n); | ||
return Parsimmon.makeSuccess(i, undefined, state); | ||
}); | ||
} | ||
test(10) | ||
.then(Parsimmon.all) | ||
.then(test(10)) | ||
.tryParse('memes', 10); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,69 @@ | ||
'use strict'; | ||
|
||
test('Parsimmon.alt', function(){ | ||
var toNode = function(nodeType){ | ||
return function(value) { | ||
return {type: nodeType, value: value}; | ||
suite('Parsimmon.alt', function() { | ||
|
||
test('should work in general', function() { | ||
var toNode = function(nodeType){ | ||
return function(value) { | ||
return {type: nodeType, value: value}; | ||
}; | ||
}; | ||
}; | ||
|
||
var stringParser = Parsimmon.seq( | ||
Parsimmon.string('"'), | ||
Parsimmon.regexp(/[^"]*/), | ||
Parsimmon.string('"') | ||
).map(toNode('string')); | ||
var stringParser = Parsimmon.seq( | ||
Parsimmon.string('"'), | ||
Parsimmon.regexp(/[^"]*/), | ||
Parsimmon.string('"') | ||
).map(toNode('string')); | ||
|
||
var identifierParser = | ||
Parsimmon.regexp(/[a-zA-Z]*/) | ||
.map(toNode('identifier')); | ||
var identifierParser = | ||
Parsimmon.regexp(/[a-zA-Z]*/) | ||
.map(toNode('identifier')); | ||
|
||
var parser = Parsimmon.alt(stringParser, identifierParser); | ||
var parser = Parsimmon.alt(stringParser, identifierParser); | ||
|
||
assert.deepEqual( | ||
parser.parse('"a string, to be sure"').value, | ||
{ | ||
type: 'string', | ||
value: ['"', 'a string, to be sure', '"'] | ||
} | ||
); | ||
assert.deepEqual( | ||
parser.parse('"a string, to be sure"').value, | ||
{ | ||
type: 'string', | ||
value: ['"', 'a string, to be sure', '"'] | ||
} | ||
); | ||
|
||
assert.deepEqual( | ||
parser.parse('anIdentifier').value, | ||
{ | ||
type: 'identifier', | ||
value: 'anIdentifier' | ||
} | ||
); | ||
assert.deepEqual( | ||
parser.parse('anIdentifier').value, | ||
{ | ||
type: 'identifier', | ||
value: 'anIdentifier' | ||
} | ||
); | ||
|
||
assert.throws(function() { | ||
Parsimmon.alt('not a parser'); | ||
}); | ||
|
||
assert.throws(function() { | ||
Parsimmon.alt('not a parser'); | ||
|
||
assert.strictEqual(Parsimmon.alt().parse('').status, false); | ||
}); | ||
|
||
test('should pass state through each parser', function() { | ||
function test(n) { | ||
return Parsimmon(function(input, i, state) { | ||
assert.strictEqual(state, n); | ||
return Parsimmon.makeSuccess(i, undefined, state); | ||
}); | ||
} | ||
var inc = Parsimmon(function(input, i, state) { | ||
return Parsimmon.makeSuccess(i, null, state + 1); | ||
}); | ||
var a = Parsimmon.string('a'); | ||
var b = Parsimmon.string('b'); | ||
var c = Parsimmon.string('c'); | ||
var parser = Parsimmon.alt( | ||
test(0).then(inc).then(a).skip(test(1)), | ||
test(0).then(inc).then(b).skip(test(1)), | ||
test(0).then(inc).then(c).skip(test(1)) | ||
).then(test(1)); | ||
parser.tryParse('c', 0); | ||
}); | ||
|
||
assert.strictEqual(Parsimmon.alt().parse('').status, false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
suite('any', function() { | ||
|
||
test('should pass state through', function() { | ||
function test(n) { | ||
return Parsimmon(function(input, i, state) { | ||
assert.strictEqual(state, n); | ||
return Parsimmon.makeSuccess(i, undefined, state); | ||
}); | ||
} | ||
test(10) | ||
.then(Parsimmon.any) | ||
.then(test(10)) | ||
.tryParse('x', 10); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
'use strict'; | ||
|
||
test('eof', function() { | ||
var parser = | ||
Parsimmon.optWhitespace | ||
.skip(Parsimmon.eof) | ||
.or(Parsimmon.all.result('default')); | ||
|
||
assert.equal(parser.parse(' ').value, ' '); | ||
assert.equal(parser.parse('x').value, 'default'); | ||
suite('eof', function() { | ||
|
||
test('should work in general', function() { | ||
var parser = | ||
Parsimmon.optWhitespace | ||
.skip(Parsimmon.eof) | ||
.or(Parsimmon.all.result('default')); | ||
|
||
assert.equal(parser.parse(' ').value, ' '); | ||
assert.equal(parser.parse('x').value, 'default'); | ||
}); | ||
|
||
test('should pass state through', function() { | ||
function test(n) { | ||
return Parsimmon(function(input, i, state) { | ||
assert.strictEqual(state, n); | ||
return Parsimmon.makeSuccess(i, undefined, state); | ||
}); | ||
} | ||
test(10) | ||
.then(Parsimmon.eof) | ||
.then(test(10)) | ||
.tryParse('', 10); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
'use strict'; | ||
|
||
test('index', function() { | ||
var parser = Parsimmon.regexp(/^[x\n]*/).then(Parsimmon.index); | ||
assert.deepEqual(parser.parse('').value, { | ||
offset: 0, | ||
line: 1, | ||
column: 1 | ||
}); | ||
assert.deepEqual(parser.parse('xx').value, { | ||
offset: 2, | ||
line: 1, | ||
column: 3 | ||
suite('index', function() { | ||
|
||
test('should work in general', function() { | ||
var parser = Parsimmon.regexp(/^[x\n]*/).then(Parsimmon.index); | ||
assert.deepEqual(parser.parse('').value, { | ||
offset: 0, | ||
line: 1, | ||
column: 1 | ||
}); | ||
assert.deepEqual(parser.parse('xx').value, { | ||
offset: 2, | ||
line: 1, | ||
column: 3 | ||
}); | ||
assert.deepEqual(parser.parse('xx\nxx').value, { | ||
offset: 5, | ||
line: 2, | ||
column: 3 | ||
}); | ||
}); | ||
assert.deepEqual(parser.parse('xx\nxx').value, { | ||
offset: 5, | ||
line: 2, | ||
column: 3 | ||
|
||
test('should pass state through', function() { | ||
function test(n) { | ||
return Parsimmon(function(input, i, state) { | ||
assert.strictEqual(state, n); | ||
return Parsimmon.makeSuccess(i, undefined, state); | ||
}); | ||
} | ||
test(10) | ||
.then(Parsimmon.index) | ||
.then(test(10)) | ||
.tryParse('', 10); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.