Skip to content

Commit eb13d3d

Browse files
committed
Require commas between moshscript arguments
1 parent 028b3ff commit eb13d3d

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/interpreter.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,20 @@ export function parse(tokens) {
6666
const name = expect("id").v;
6767
expect("punc", "(");
6868
const args = [];
69+
let expectArg = true;
6970
while (peek() && !(peek().t === "punc" && peek().v === ")")) {
7071
const a = next();
71-
if (a.t === "punc" && a.v === ",") continue;
72+
if (a.t === "punc" && a.v === ",") {
73+
if (expectArg) throw new Error("moshscript: expected argument before comma");
74+
expectArg = true;
75+
continue;
76+
}
77+
if (!expectArg) throw new Error("moshscript: expected comma between arguments");
78+
if (a.t === "punc") throw new Error(`moshscript: unexpected ${JSON.stringify(a.v)}`);
7279
args.push(a.v);
80+
expectArg = false;
7381
}
82+
if (expectArg && args.length) throw new Error("moshscript: expected argument after comma");
7483
expect("punc", ")");
7584
if (peek() && peek().t === "punc" && peek().v === ";") next(); // optional ;
7685
return { type: "call", name, args };

test/interpreter.test.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ test("compile preserves valid moshscript behavior", () => {
1414
args: ["hi"],
1515
});
1616
});
17+
18+
test("compile requires commas between call arguments", () => {
19+
assert.throws(() => compile("say(\"one\" \"two\");"), /expected comma/);
20+
assert.throws(() => compile("say(\"one\",);"), /expected argument after comma/);
21+
assert.throws(() => compile("say(,\"one\");"), /expected argument before comma/);
22+
23+
assert.deepEqual(compile("say(\"one\", \"two\");").body[0], {
24+
type: "call",
25+
name: "say",
26+
args: ["one", "two"],
27+
});
28+
});

0 commit comments

Comments
 (0)