-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from /issues/177
Fixes #177 adds seqObj
- Loading branch information
Showing
7 changed files
with
223 additions
and
9 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
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,44 @@ | ||
'use strict'; | ||
|
||
// Run me with Node to see my output! | ||
|
||
let util = require('util'); | ||
let P = require('../'); | ||
|
||
/////////////////////////////////////////////////////////////////////// | ||
|
||
let Lang = P.createLanguage({ | ||
|
||
_: () => P.optWhitespace, | ||
|
||
LParen: () => P.string('('), | ||
RParen: () => P.string(')'), | ||
Comma: () => P.string(','), | ||
Dot: () => P.string('.'), | ||
|
||
Identifier: () => P.letters.node('Identifier'), | ||
|
||
MethodCall: r => | ||
P.seqObj( | ||
['receiver', r.Identifier], | ||
r.Dot.trim(r._), | ||
['method', r.Identifier], | ||
r.LParen, | ||
['arguments', r.Identifier.trim(r._).sepBy(r.Comma)], | ||
r.RParen | ||
).node('MethodCall'), | ||
|
||
}); | ||
|
||
/////////////////////////////////////////////////////////////////////// | ||
|
||
let text = 'console.log(bar, baz, quux)'; | ||
|
||
function prettyPrint(x) { | ||
let opts = {depth: null, colors: 'auto'}; | ||
let s = util.inspect(x, opts); | ||
console.log(s); | ||
} | ||
|
||
let ast = Lang.MethodCall.tryParse(text); | ||
prettyPrint(ast); |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
suite('Parsimmon.seqObj', function() { | ||
|
||
test('does not accept duplicate keys', function() { | ||
assert.throws(function() { | ||
Parsimmon.seqObj( | ||
['a', Parsimmon.of(1)], | ||
['b', Parsimmon.of(2)], | ||
['a', Parsimmon.of(3)] | ||
); | ||
}); | ||
}); | ||
|
||
test('requires at least one key', function() { | ||
assert.throws(function() { | ||
Parsimmon.seqObj(); | ||
}); | ||
}); | ||
|
||
test('every key is present in the result object', function() { | ||
var parser = Parsimmon.seqObj( | ||
['a', Parsimmon.of(1)], | ||
['b', Parsimmon.of(2)], | ||
['c', Parsimmon.of(3)] | ||
); | ||
var result = parser.tryParse(''); | ||
assert.deepStrictEqual(result, { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}); | ||
}); | ||
|
||
test('every parser is used', function() { | ||
var parser = Parsimmon.seqObj( | ||
Parsimmon.string('a'), | ||
['b', Parsimmon.string('b')], | ||
Parsimmon.string('c'), | ||
['d', Parsimmon.string('d')], | ||
Parsimmon.string('e') | ||
); | ||
var result = parser.tryParse('abcde'); | ||
assert.deepStrictEqual(result, { | ||
b: 'b', | ||
d: 'd', | ||
}); | ||
}); | ||
|
||
test('named parser arrays are formed properly', function() { | ||
assert.throws(function() { | ||
Parsimmon.seqObj([]); | ||
}); | ||
assert.throws(function() { | ||
Parsimmon.seqObj(['a', Parsimmon.of(1), 'oops extra']); | ||
}); | ||
assert.throws(function() { | ||
Parsimmon.seqObj([123, Parsimmon.of(1)]); | ||
}); | ||
assert.throws(function() { | ||
Parsimmon.seqObj(['cool', 'potato']); | ||
}); | ||
}); | ||
|
||
}); |