-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
194 lines (155 loc) · 3.95 KB
/
mod.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
type AnyObject = Record<string, unknown>;
enum TokenType {
BlockStart,
BlockEnd,
String,
}
interface Token {
type: TokenType;
data?: string;
}
class Tokenizer {
private _code: string;
private _position: number;
public constructor(code: string) {
this._code = code;
this._position = 0;
}
public nextToken(): Token {
while (true) {
this._ignoreWhitespace();
if (!this._ignoreComment()) {
break;
}
}
const current = this._current();
if (current === '{') {
this._move();
return { type: TokenType.BlockStart };
} else if (current === '}') {
this._move();
return { type: TokenType.BlockEnd };
} else {
const data = this._getString();
return { type: TokenType.String, data };
}
}
private _getString(): string {
let result = '';
let quoted = false;
if (this._current() === '"') {
quoted = true;
this._move();
}
while (true) {
if (!quoted && ['{', '}'].includes(this._current())) {
break;
}
if (quoted && this._current() === '"') {
break;
}
result += this._current();
this._move();
}
if (quoted) {
this._move();
}
return result;
}
private _ignoreWhitespace(): void {
// console.log('----\n', this._code.slice(this._position));
while (['\t', '\n', ' '].includes(this._current())) {
this._move();
}
}
private _ignoreComment(): boolean {
if (this._current() === '/' && this._next() === '/') {
while (this._current() !== '\n') {
this._move();
}
return true;
}
return false;
}
private _current(): string {
if (this._position >= this._code.length) {
throw new Error('position > code.length');
}
return this._code.at(this._position)!;
}
private _next(): string {
if (this._position + 1 >= this._code.length) {
throw new Error('position + 1 > code.length');
}
return this._code.at(this._position)!;
}
private _move() {
if (this._position + 1 >= this._code.length) {
throw new Error('position + 1 > code.length');
}
this._position += 1;
}
}
const _parse = (tokenizer: Tokenizer): AnyObject => {
const result: AnyObject = {};
let key: string | undefined = undefined;
while (true) {
const token = tokenizer.nextToken();
if (typeof key !== 'undefined') {
if (token.type === TokenType.BlockStart) {
const value = _parse(tokenizer);
if (typeof result[key] === 'object') {
result[key] = { ...result[key] as AnyObject, ...value };
} else {
result[key] = value;
}
} else if (token.type === TokenType.String) {
result[key] = token.data;
}
key = undefined;
} else {
if (token.type === TokenType.BlockEnd) {
break;
}
key = token.data;
}
}
return result;
};
const _stringify = (
obj: AnyObject,
indent: boolean,
tabSize: number,
): string => {
let result = '';
const tab = indent ? ' '.repeat(tabSize) : '';
const eof = indent ? '\n' : '';
const space = indent ? ' ' : '';
for (const [key, value] of Object.entries(obj)) {
result += `${tab}"${key}"${space}`;
if (typeof value === 'object') {
const next = _stringify(value as AnyObject, indent, tabSize + 1)
.trimEnd();
result += `{${eof}${next}${eof}${tab}}${eof}`;
} else {
result += `"${value.toString()}"${eof}`;
}
}
return result;
};
export class ValveKV {
public static parse(code: string): AnyObject {
if (typeof code !== 'string') {
throw new Error('code is not a string');
}
const result: AnyObject = {};
const tokenizer = new Tokenizer(code + '\n');
const key = tokenizer.nextToken();
tokenizer.nextToken();
result[key.data!] = _parse(tokenizer);
return result;
}
public static stringify(obj: AnyObject, indent = false): string {
return _stringify(obj, indent, 0);
}
}