Permalink
Browse files

Added support for parser lexeme transformations.

  • Loading branch information...
1 parent 775eb59 commit b83bf3326d7cc0dfc851426149bb08f11a5b4f78 @mcantelon committed Feb 26, 2011
Showing with 39 additions and 0 deletions.
  1. +11 −0 lib/parser.js
  2. +28 −0 test/mingy.test.js
View
@@ -13,6 +13,7 @@ exports.Parser = function Parser(commands) {
this.loadCommands(commands)
this.validators = {}
this.env = {}
+ this.lexemeTransforms = []
}
exports.Parser.prototype = {
@@ -62,6 +63,10 @@ exports.Parser.prototype = {
this.validators[name] = logic
},
+ addLexemeTransform: function(logic) {
+ this.lexemeTransforms.push(logic)
+ },
+
parse: function(input, callback, system) {
input = this.cleanInput(input)
@@ -111,6 +116,12 @@ exports.Parser.prototype = {
var output = ''
+ // cycle through plugins
+ for (var index in this.lexemeTransforms) {
+
+ lexemes = this.lexemeTransforms[index](lexemes, this.env)
+ }
+
// cycle through commands looking for syntax match
var validCommands = this.validCommands(lexemes)
for (var index in validCommands) {
View
@@ -181,5 +181,33 @@ module.exports = {
first_test.should.equal('got test one')
var second_test = parser.parse('test two')
second_test.should.equal('got test two')
+ },
+
+ // test out parser lexeme transformation functionality
+ 'parser lexeme transform': function() {
+
+ var parser = new Parser()
+ parser.setEnv('fullNames', {"monkey": "golden monkey"})
+
+ parser.addCommand('touch')
+ .set('syntax', ['touch <thing>'])
+ .set('logic', function(args) {
+
+ return "You touch the " + args.thing + "."
+ })
+
+ parser.addLexemeTransform(function(lexemes, env) {
+
+ for (var lexemeIndex in lexemes) {
+ if (env.fullNames[(lexemes[lexemeIndex])]) {
+ lexemes[lexemeIndex] = env.fullNames[(lexemes[lexemeIndex])]
+ }
+ }
+
+ return lexemes
+ })
+
+ var output = parser.parse('touch monkey')
+ output.should.equal('You touch the golden monkey.')
}
}

0 comments on commit b83bf33

Please sign in to comment.