-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.ts
208 lines (158 loc) · 4.64 KB
/
Parser.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import util from "util";
import { RefRule, Rule, canCompileRule } from "./compileGrammar";
import Token from "./Token";
import Stack from "./Stack";
import TokenStream from "./TokenStream";
interface Node {
name: string;
children: Array<Token | Node>;
}
type ParserStack = Stack<Token | Node>;
const REGEXP_SPECIAL_CHARS = "$?|*.[]+()/".split("");
const REGEXP_SPECIAL_CHARS_REGEXP = new RegExp(
`${REGEXP_SPECIAL_CHARS.map(c => "\\" + c).join("|")}`,
"g"
);
function printStack(stack: ParserStack) {
console.log(
stack.stack.map(t => (t instanceof Token ? t.value : t.name)).join(" ")
);
}
class Parser {
private stack: ParserStack = new Stack();
private lookahead: number = 1;
constructor(private grammar: Rule[]) {
// console.log(grammar);
// console.log(util.inspect(this.grammar, { colors: true, depth: 10 }));
}
public compileToRegexString(rule: Rule, parent?: Rule): string {
switch (rule!.__type) {
case "complex": {
let ruleRegexes = [];
for (let subRule of rule.rules) {
ruleRegexes.push(this.compileToRegexString(subRule));
}
return "^" + ruleRegexes.join("") + "$";
}
case "literal": {
return rule.value.replace(
REGEXP_SPECIAL_CHARS_REGEXP,
char => "\\" + char
);
}
case "or": {
let ruleRegexes = [];
for (let subRule of rule.rules) {
ruleRegexes.push(this.compileToRegexString(subRule));
}
return `(?:${ruleRegexes.join("|")})`;
}
case "ref": {
const target = this.grammar.find(r => r.name === rule.target)!;
let targetRegexStr = this.compileToRegexString(target);
// optional - ?
// repeats - +
// optional + repeats - *
if (rule.optional && rule.repeats) {
targetRegexStr = `(?:${targetRegexStr})*`;
} else if (rule.optional) {
targetRegexStr = `(?:${targetRegexStr})?`;
} else if (rule.repeats) {
targetRegexStr = `(?:${targetRegexStr})+`;
}
return targetRegexStr;
}
}
}
public match(rule: Rule, stack: ParserStack): boolean {
if (rule.__type === "complex") {
if (!canCompileRule(this.grammar, rule)) {
if (rule.rules.length > stack.size()) {
return false;
}
const tempStack = stack.clone();
for (let ruleIdx = rule.rules.length - 1; ruleIdx >= 0; ruleIdx--) {
if (!this.match(rule.rules[ruleIdx], tempStack)) {
return false;
}
tempStack.pop();
}
return true;
}
}
const t = stack.peek()!;
if (!(t instanceof Token)) {
if (rule.__type === "ref") {
return rule.target === t.name;
}
if (rule.__type === "literal") {
const target = t.children[0];
if (target instanceof Token) {
return rule.value === target.value;
}
return false;
}
if (rule.__type === "or") {
for (let r of rule.rules) {
if (this.match(r, this.stack)) {
return true;
}
}
return false;
}
return false;
}
if (!canCompileRule(this.grammar, rule)) {
return false;
}
const ruleRegexStr = this.compileToRegexString(rule);
const ruleRegex = new RegExp(`^${ruleRegexStr}$`);
if (ruleRegex.test(t.value)) {
return true;
}
return false;
}
public parse(tokenStream: TokenStream) {
let currenToken = tokenStream.currentToken();
while (currenToken) {
this.stack.push(currenToken);
console.log(util.inspect(this.stack, { colors: true }));
this.reduce();
currenToken = tokenStream.nextToken();
}
this.reduce();
return this.stack;
}
public reduce() {
printStack(this.stack);
outer: while (true) {
for (let rule of this.grammar) {
// terminal symbols are not applied in the grammar
if (rule.name[0].toUpperCase() === rule.name[0]) {
continue;
}
if (this.match(rule, this.stack)) {
console.log("reducing with", rule.name);
const children = [];
if (
rule.__type === "complex" &&
!canCompileRule(this.grammar, rule)
) {
for (let i = 0; i < rule.rules.length; i++) {
children.push(this.stack.pop());
}
} else {
children.push(this.stack.pop());
}
this.stack.push({
name: rule.name,
children: children.reverse()
} as Node);
this.reduce();
}
}
return;
}
}
}
export default Parser;