-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
35 lines (28 loc) · 1005 Bytes
/
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
import { tokenTypes } from "./tokens.js";
export function parser(tokens) {
let current = 0;
function parseExpression() {
let expr = [];
while (current < tokens.length) {
let token = tokens[current];
if (token.type === tokenTypes.WORD.name || token.type === tokenTypes.STRING.name) {
expr.push(token);
current++;
} else if (token.type === tokenTypes.PUNCTUATION.name) {
if(token.value === tokenTypes.PUNCTUATION.openTag) {
current++;
let nestedExpr = parseExpression();
expr[expr.length - 1].children = nestedExpr;
}
if(token.value === tokenTypes.PUNCTUATION.closeTag) {
current++;
return expr;
}
} else {
current++;
}
}
return expr;
}
return parseExpression();
}