/
parseGrammar.ts
99 lines (77 loc) · 1.97 KB
/
parseGrammar.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
export enum State {
Comment,
Consuming,
Parsing,
String
}
export type RawGrammar = Array<[string, string]>;
export default function parseGrammar(grammar: string): RawGrammar {
const _grammar: RawGrammar = [];
let index = -1;
let currentNode: string | undefined;
let state: State = State.Parsing;
let prevState: State | undefined;
let buffer: string[] = [];
while (index++ < grammar.length) {
const char = grammar.charAt(index);
buffer.push(char);
// if we're dealing with a comment, continue consuming input until
// a newline is reached. multi-line comments are not supported.
if (state === State.Comment) {
if (char !== "\n") {
continue;
}
}
if (state === State.String && char !== '"') {
continue;
}
// if we consume a " that isn't preceded by a \, we're either
// starting a string or terminating a string.
if (char === '"') {
if (grammar.charAt(index - 1) === "\\") {
// ...
} else {
if (state === State.String) {
state = prevState || State.Parsing;
} else {
state = State.String;
}
}
}
if (char === "#") {
state = State.Comment;
}
if (char === "\n") {
buffer.pop();
if (state === State.Comment) {
state = State.Parsing;
buffer = [];
}
}
if (char === ":") {
if (state === State.String) {
continue;
}
const part = buffer
.slice(0, buffer.length - 1)
.join("")
.trim();
currentNode = part;
buffer = [];
}
if (char === ";") {
if (state === State.String || grammar.charAt(index - 1) === "\\") {
continue;
}
buffer.pop();
const part = buffer.join("").trim();
if (!currentNode) {
throw new Error("invalid token grammar");
}
_grammar.push([currentNode, part]);
index += 1; // consume the ";"
buffer = [];
}
}
return _grammar;
}