Skip to content

Commit

Permalink
feature(grammar): allow key-value options
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Jun 7, 2015
1 parent d212acc commit efec681
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions bnf.l
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ id [a-zA-Z][a-zA-Z0-9_-]*
":" return ':';
";" return ';';
"|" return '|';
"=" return '=';
"%%" this.pushState(ebnf ? 'ebnf' : 'bnf'); return '%%';
"%ebnf" if (!yy.options) yy.options = {}; ebnf = yy.options.ebnf = true;
"%prec" return 'PREC';
Expand Down
16 changes: 15 additions & 1 deletion bnf.y
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,24 @@ declaration
;

options
: OPTIONS token_list
: OPTIONS options_list
{$$ = $2;}
;

options_list
: options_list option
{ $$ = $1; $$[$2[0]] = $2[1]; }
| option
{$$ = {}; $$[$1[0]] = $1[1];}
;

option
: symbol
{$$ = [$1, true];}
| symbol '=' symbol
{$$ = [$1, $3];}
;

parse_param
: PARSE_PARAM token_list
{$$ = $2;}
Expand Down
6 changes: 3 additions & 3 deletions ebnf-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ bnf.yy.addDeclaration = function (grammar, decl) {

} else if (decl.options) {
if (!grammar.options) grammar.options = {};
for (var i=0; i < decl.options.length; i++) {
grammar.options[decl.options[i]] = true;
}
Object.keys(decl.options).forEach(function(option) {
grammar.options[option] = decl.options[option];
});
}

};
Expand Down
7 changes: 7 additions & 0 deletions tests/bnf_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,10 @@ exports["test options"] = function () {

assert.deepEqual(bnf.parse(grammar), expected, "grammar should be parsed correctly");
};

exports["test key-value options"] = function () {
var grammar = "%options foo=bar baz\n%%hello: world;%%";
var expected = {bnf: {hello: ["world"]}, options: {foo: 'bar', baz: true}};

assert.deepEqual(bnf.parse(grammar), expected, "grammar should be parsed correctly");
};

0 comments on commit efec681

Please sign in to comment.