forked from zaach/ebnf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebnf-parser.js
41 lines (31 loc) · 1.21 KB
/
ebnf-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var bnf = require("./parser").parser,
ebnf = require("./ebnf-transform"),
jisonlex = require("lex-parser");
exports.parse = function parse (grammar) { return bnf.parse(grammar); };
exports.transform = ebnf.transform;
// adds a declaration to the grammar
bnf.yy.addDeclaration = function (grammar, decl) {
if (decl.start) {
grammar.start = decl.start;
} else if (decl.lex) {
grammar.lex = parseLex(decl.lex);
} else if (decl.operator) {
if (!grammar.operators) grammar.operators = [];
grammar.operators.push(decl.operator);
} else if (decl.parseParam) {
if (!grammar.parseParams) grammar.parseParams = [];
grammar.parseParams = grammar.parseParams.concat(decl.parseParam);
} else if (decl.include) {
if (!grammar.moduleInclude) grammar.moduleInclude = '';
grammar.moduleInclude += decl.include;
} else if (decl.options) {
if (!grammar.options) grammar.options = {};
for (var i=0; i < decl.options.length; i++) {
grammar.options[decl.options[i]] = true;
}
}
};
// parse an embedded lex section
var parseLex = function (text) {
return jisonlex.parse(text.replace(/(?:^%lex)|(?:\/lex$)/g, ''));
};