Permalink
Browse files

breaking parser

  • Loading branch information...
chrisdew committed Feb 23, 2011
1 parent 9052caa commit 5890dc110533f4b270886e2d6a33e947c97254e5
Showing with 40 additions and 9 deletions.
  1. +30 −5 lib/parser.js
  2. +10 −4 test/parser-test.js
View
@@ -1,13 +1,38 @@
var c = require('./constants');
-var direction = [c.DIRECTION | c.VERB];
+var parsers = { direction: [c.DIRECTION]
+ , verbOnly: [c.VERB]
+ , verbNoun: [c.VERB, c.NOUN]
+ , verbAdjectiveNoun: [c.VERB, c.ADJECTIVE, c.NOUN]
+ , verbArticleNoun: [c.VERB, c.ARTICLE, c.NOUN]
+ , verbArticleAdjectiveNoun: [c.VERB, c.ARTICLE, c.ADJECTIVE, c.NOUN]
+};
+/*
var verb = [verbPhrase];
var verbSubject = [verbPhrase, nounPhrase];
var verbSubjectPrepObject = [verbPhrase, nounPhrase, c.PREPOSITION, nounPhrase];
var verbPhrase = [c.VERB];
var nounPhrase = [c.ARTICLE | c.OPTIONAL];
-
-
+*/
+
+// The parse function takes the output from the lex and fails or succeeds.
+// It also provides some completions.
+function parse(lex) {
+ var matches = [];
+ var pos = [];
+ for (var p in parsers) {
+ for (var i in lex.pos) {
+ if (i >= parsers[p].length) {
+ break;
+ }
+ if (!(lex.pos[i] & parsers[p][i])) {
+ break;
+ }
+ pos.push(parsers[p]);
+ matches.push(p);
+ }
+ }
+ return { matches: matches, pos: pos };
+}
-
-exports.direction = direction;
+exports.parse = parse;
View
@@ -3,17 +3,23 @@
var vows = require('vows')
, assert = require('assert')
, lex = require('../lib/lexer').lex
- , parser = require('../lib/parser')
+ , parse = require('../lib/parser').parse
, dict = require('../lib/dict')
, c = require('../lib/constants')
;
var suite = vows.describe('parser').addBatch(
- { "empty string"
+ { "direction"
: { topic
- : parser.direction
+ : lex("north")
, "test direction parsing"
- : function(topic) { assert.deepEqual(topic(lex("north")), { direction: c.N }); }
+ : function(topic) { assert.deepEqual(parse(topic), { matches: [ 'direction' ], pos: [ [ 4096 ] ] } ); }
+ }
+ , "verb..."
+ : { topic
+ : lex("get a")
+ , "test completion"
+ : function(topic) { assert.deepEqual(parse(topic), {}); }
}
}
)

0 comments on commit 5890dc1

Please sign in to comment.