Skip to content

Commit

Permalink
parse productions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhconnelly committed Dec 6, 2012
1 parent e416cd7 commit a7678d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions prettybnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,15 @@ Parser.prototype.expressions = function () {
return { type: 'expressions', expressions: expressions };
};

// <production> ::= <nonterminal> <ws> "::=" <ws> <expressions> ";";
Parser.prototype.production = function () {
var lhs = this.nonterminal();
this.ws();
this.eat(':'); this.eat(':'); this.eat('=');
this.ws();
var rhs = this.expressions();
this.eat(';');
return { type: 'production', lhs: lhs, rhs: rhs };
};

}(typeof exports === 'undefined' ? this.prettybnf = {} : exports));
14 changes: 14 additions & 0 deletions test_prettybnf.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,17 @@ exports.testParser_expressions = function (t) {
t.throws(function () { new Parser('| <a>').expressions(); }, SyntaxError);
t.done();
};

exports.testParser_production = function (t) {
var node = new Parser('<a>\t::= <b> | "c" <d> | ""\n\t;').production();
t.equal(node.type, 'production');
t.equal(node.lhs.type, 'nonterminal');
t.equal(node.rhs.type, 'expressions');
t.equal(node.rhs.expressions.length, 3);
t.throws(function () { new Parser(' <a> ::= "b";').production(); }, SyntaxError);
t.throws(function () { new Parser('::= "b";').production(); }, SyntaxError);
t.throws(function () { new Parser('<a> ::= ;').production(); }, SyntaxError);
t.throws(function () { new Parser('<a> ').production(); }, SyntaxError);
t.throws(function () { new Parser('<a> ::= "b"').production(); }, SyntaxError);
t.done();
};

0 comments on commit a7678d7

Please sign in to comment.