This repository has been archived by the owner on May 1, 2024. It is now read-only.
forked from GerHobbelt/ebnf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebnf-parser.js
58 lines (55 loc) · 2.19 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.token) {
if (!grammar.extra_tokens) grammar.extra_tokens = [];
grammar.extra_tokens.push(decl.token);
} else if (decl.token_list) {
if (!grammar.extra_tokens) grammar.extra_tokens = [];
decl.token_list.forEach(function (tok) {
grammar.extra_tokens.push(tok);
});
} else if (decl.parseParam) {
if (!grammar.parseParams) grammar.parseParams = [];
grammar.parseParams = grammar.parseParams.concat(decl.parseParam);
} else if (decl.parserType) {
if (!grammar.options) grammar.options = {};
grammar.options.type = decl.parserType;
} else if (decl.include) {
if (!grammar.moduleInclude) grammar.moduleInclude = '';
grammar.moduleInclude += decl.include;
} else if (decl.options) {
if (!grammar.options) grammar.options = {};
// last occurrence of %option wins:
for (var i = 0; i < decl.options.length; i++) {
grammar.options[decl.options[i][0]] = decl.options[i][1];
}
} else if (decl.unknownDecl) {
if (!grammar.unknownDecls) grammar.unknownDecls = [];
grammar.unknownDecls.push(decl.unknownDecl);
} else if (decl.imports) {
if (!grammar.imports) grammar.imports = [];
grammar.imports.push(decl.imports);
} else if (decl.actionInclude) {
if (!grammar.actionInclude) {
grammar.actionInclude = '';
}
grammar.actionInclude += decl.actionInclude;
}
};
// parse an embedded lex section
var parseLex = function (text) {
text = text.replace(/(?:^%lex)|(?:\/lex$)/g, '');
return jisonlex.parse(text);
};