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
| function parseExpression(program) {
program = skipSpace(program);
var match, expr;
if ((match = /^"([^"]*)"/.exec(program)))
expr = { type: "value", value: match[1] };
else if ((match = /^\d+\b/.exec(program)))
expr = { type: "value", value: Number(match[0]) };
else if ((match = /^[^\s(),"]+/.exec(program)))
expr = { type: "word", name: match[0] };
else throw new SyntaxError("Unexpected syntax: " + program);
return parseApply(expr, program.slice(match[0].length));
}
function skipSpace(string) {
var first = string.search(/\S/);
if (first == -1) return "";
return string.slice(first);
}
function parseApply(expr, program) {
program = skipSpace(program);
if (program[0] != "(") return { expr: expr, rest: program };
program = skipSpace(program.slice(1));
expr = { type: "apply", operator: expr, args: [] };
while (program[0] != ")") {
var arg = parseExpression(program);
expr.args.push(arg.expr);
program = skipSpace(arg.rest);
if (program[0] == ",") program = skipSpace(program.slice(1));
else if (program[0] != ")") throw new SyntaxError("Expected ',' or ')'");
}
return parseApply(expr, program.slice(1));
}
function parse(program) {
var result = parseExpression(program);
if (skipSpace(result.rest).length > 0)
throw new SyntaxError("Unexpected text after program");
return result.expr;
}
console.log(parse("+(a, 10)"));
// → {type: "apply",
// operator: {type: "word", name: "+"},
// args: [{type: "word", name: "a"},
// {type: "value", value: 10}]}
|