babel-bridge
Version:
a 'runtime' parsing expression grammar parser
65 lines (50 loc) • 1.46 kB
text/coffeescript
{log, wordsArray} = Neptune.Art.StandardLib
{Parser, Nodes} = Neptune.BabelBridge
module.exports = suite: ->
test "one node with custom node class", ->
class MyParser extends Parser
root:
pattern: /boo/
myMember: -> 123
mainNode = MyParser.parse "boo"
assert.eq mainNode.myMember(), 123
test "multi-pattern type 1", ->
class MyParser extends Parser
root:
pattern: [
/boo/
/bad/
]
myMember: -> 123
mainNode = MyParser.parse "boo"
assert.eq mainNode.myMember(), 123
mainNode = MyParser.parse "bad"
assert.eq mainNode.myMember(), 123
test "multi-pattern type 2", ->
class MyParser extends Parser
root: [
/boo/
/bad/
myMember: -> 123
]
mainNode = MyParser.parse "boo"
assert.eq mainNode.myMember(), 123
mainNode = MyParser.parse "bad"
assert.eq mainNode.myMember(), 123
test "simple math", ->
class MyParser extends Parser
expression:
root: true
pattern: "n:number '+' expression"
compute: -> .compute() + .compute()
expression:
pattern: "number"
compute: -> .compute()
number:
pattern: /[0-9]+/
compute: -> | 0
mainNode = MyParser.parse "123+321+111"
assert.eq mainNode.compute(), 555