forked from codebox/regex_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
executable file
·183 lines (171 loc) · 5.92 KB
/
grammar.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
var grammar = (function () {
var PRODUCTION_RULES_TXT = [
"REGEX : TERM",
"REGEX : ALTERNATIVES",
"ALTERNATIVES : TERM | REGEX",
"TERM : FACTOR TERM",
"TERM :",
"FACTOR : BASE MULTIPLIERS",
"BASE : CHAR",
"BASE : DOT",
"BASE : PARENS",
"BASE : BACKREF",
"PARENS : ( REGEX )",
"MULTIPLIERS : MULTIPLIER MULTIPLIERS",
"MULTIPLIERS :",
"MULTIPLIER : ZERO_OR_MORE",
"MULTIPLIER : ZERO_OR_ONE",
"MULTIPLIER : ONE_OR_MORE",
"MULTIPLIER : NUMBER_OF",
"MULTIPLIER : NUMBER_OR_MORE_OF",
"MULTIPLIER : NUMBER_RANGE_OF",
"ZERO_OR_MORE : *",
"ZERO_OR_ONE : ?",
"ONE_OR_MORE : +",
"NUMBER_OF : { NUMBER }",
"NUMBER_OR_MORE_OF : { NUMBER , }",
"NUMBER_RANGE_OF : { NUMBER , NUMBER }",
"DOT : .",
"CHAR : NON_META_CHAR",
"CHAR : DIGIT",
"CHAR : WHITESPACE",
"CHAR : NON_WHITESPACE",
"CHAR : DIGIT_ALIAS",
"CHAR : NON_DIGIT_ALIAS",
"CHAR : WORD_CHAR",
"CHAR : NON_WORD_CHAR",
"CHAR : INCLUSIVE_CHAR_LIST",
"CHAR : EXCLUSIVE_CHAR_LIST",
"INCLUSIVE_CHAR_LIST : [ CHAR_SPEC CHAR_SPECS ]",
"EXCLUSIVE_CHAR_LIST : [ ^ CHAR_SPEC CHAR_SPECS ]",
"CHAR_SPEC : NON_META_CHAR",
"CHAR_SPEC : RANGE",
"RANGE : NON_META_CHAR - NON_META_CHAR",
"CHAR_SPECS : CHAR_SPEC CHAR_SPECS",
"CHAR_SPECS : ",
"NON_META_CHAR : ~^{}[]().|*+?\\",
"DIGIT : 1234567890",
"DIGITS : DIGIT DIGITS",
"DIGITS :",
"NUMBER : DIGIT DIGITS",
"BACKREF : \\ DIGIT",
"WHITESPACE : \\ s",
"NON_WHITESPACE : \\ S",
"DIGIT_ALIAS : \\ d",
"NON_DIGIT_ALIAS : \\ D",
"WORD_CHAR : \\ w",
"NON_WORD_CHAR : \\ W"
];
var productionRuleMap = {};
PRODUCTION_RULES_TXT.forEach(function (productionRuleText) {
var parts = productionRuleText.split(':', 2).map(function (s) {
return s.trim();
});
var nonTerminalName = parts[0];
var production = parts[1];
if (!(nonTerminalName in productionRuleMap)) {
productionRuleMap[nonTerminalName] = [];
}
productionRuleMap[nonTerminalName].push(production);
});
function makeNonTerminal(name) {
return {
isTerminal: false,
name: name,
toString: function () {
return name;
}
};
}
function makeTerminal(text) {
var notPrefix = (text[0] == '~');
if (notPrefix) {
text = text.substring(1);
}
return {
isTerminal: true,
matches: function (c) {
if (!c){
return false;
}
var charIsInList = text.indexOf(c) > -1;
return charIsInList ^ notPrefix;
},
toString: function () {
return (notPrefix ? 'not one of: ' : '') + '"' + text + '"';
}
};
}
function makeEpsilon() {
return {
isTerminal: true,
isEpsilon: true,
matches: function () {
return true;
},
toString: function () {
return '<epsilon>';
}
};
}
var productionRules = (function () {
var nameMap = {};
return {
nameMap : nameMap,
'get': function (name) {
return nameMap[name];
},
'put': function (name) {
if (!nameMap[name]) {
nameMap[name] = makeNonTerminal(name);
}
},
'forEach': function (fn) {
Object.keys(nameMap).forEach(function (name) {
fn(nameMap[name], name);
});
},
'toString': function () {
var result = [];
this.forEach(function (nt) {
result.push(nt.name + ': ' + nt.mappings.map(function (nt) {
return nt.toString();
}).join(', '));
});
return result.join('\n');
}
};
}());
// Create empty non-terminals for each key productionRuleMap
Object.keys(productionRuleMap).forEach(function (name) {
productionRules.put(name);
});
function identity(x) {
return x;
}
// Add mappings to each of the productionRules by reading the values in productionRuleMap
productionRules.forEach(function (nonTerminal) {
nonTerminal.mappings = productionRuleMap[nonTerminal.name].map(function (productionTxt) {
return productionTxt.split(' ').map(function (txt) {
if (txt) {
var nonTerminal = productionRules.get(txt);
if (nonTerminal) {
return nonTerminal;
} else {
return makeTerminal(txt);
}
} else {
return makeEpsilon();
}
});
});
});
return {
'startSymbolName': 'REGEX',
'productionRules': productionRules
};
})();
module.exports = {
grammar: grammar
};