# Códigos para la Construcción de los ASTs de Egg con un algoritmo PDR
# Código de parse
function parse(p) { setProgram(p); lex(); let result = parseExpression(); if (lookahead !== null) throw new SyntaxError(`Unexpected input after reached the end of parsing ${lineno}: ${program.slice(0,10)}`); return result; }
Copied!
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# Código de parseExpression
function parseExpression() { let expr; if (lookahead.type == "STRING") { expr = {type: "value", value: lookahead.value}; lex(); return expr; } else if (lookahead.type == "NUMBER") { expr = {type: "value", value: lookahead.value}; lex(); return expr; } else if (lookahead.type == "WORD") { expr = {type: "word", name: lookahead.value}; lex(); return parseApply(expr); } else { throw new SyntaxError(`Unexpected syntax line ${lineno}: ${program.slice(0,10)}`); } }
Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Código de parseApply
function parseApply(tree) { if (!lookahead) return tree; // apply: /* vacio */ if (lookahead.type !== "LP") return tree; // apply: /* vacio */ lex(); tree = {type: 'apply', operator: tree, args: []}; while (lookahead && lookahead.type !== "RP") { let arg = parseExpression(); tree.args.push(arg); if (lookahead && lookahead.type == "COMMA") { lex(); } else if (!lookahead || lookahead.type !== "RP") { throw new SyntaxError(`Expected ',' or ')' at line ${lineno}: ... ${program.slice(0,20)}`); } } if (!lookahead) throw new SyntaxError(`Expected ')' at line ${lineno}: ... ${program.slice(0,20)}`); lex(); return parseApply(tree); }
Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22