Skip to content

Commit 800855b

Browse files
committed
fix: キーがIdentifierなときにのみ使用可能にする
1 parent 7825713 commit 800855b

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/parser/syntaxes/expressions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ function parseReference(s: ITokenStream): Ast.Identifier {
577577

578578
/**
579579
* ```abnf
580-
* Object = "{" [ObjectKey [":" Expr] *(SEP ObjectKey [":" Expr]) [SEP]] "}"
580+
* Object = "{" [(ObjectKey ":" Expr / IDENT) *(SEP (ObjectKey ":" Expr / IDENT)) [SEP]] "}"
581581
* ```
582582
*/
583583
function parseObject(s: ITokenStream, isStatic: boolean): Ast.Obj {
@@ -594,6 +594,7 @@ function parseObject(s: ITokenStream, isStatic: boolean): Ast.Obj {
594594
while (!s.is(TokenKind.CloseBrace)) {
595595
const startPos = s.getPos();
596596

597+
const isIdentifierKey = s.is(TokenKind.Identifier);
597598
const k = parseObjectKey(s);
598599
if (map.has(k)) {
599600
throw new AiScriptSyntaxError(`Key ${k} is duplicated.`, s.getPos());
@@ -602,7 +603,7 @@ function parseObject(s: ITokenStream, isStatic: boolean): Ast.Obj {
602603

603604
let v: Ast.Expression;
604605

605-
if (isStatic) s.expect(TokenKind.Colon);
606+
if (!isIdentifierKey || isStatic) s.expect(TokenKind.Colon);
606607
if (s.is(TokenKind.Colon)){
607608
s.next();
608609

test/literals.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ describe('literal', () => {
251251
eq(res, OBJ(new Map([['a', NUM(1)]])));
252252
});
253253

254+
test.concurrent('obj (reserved word as shorthand)', async () => {
255+
await expect(() => exe(`
256+
<: { exists }
257+
`)).rejects.toThrow(AiScriptSyntaxError);
258+
});
259+
260+
test.concurrent('obj (string as shorthand)', async () => {
261+
await expect(() => exe(`
262+
<: { "hoge" }
263+
`)).rejects.toThrow(AiScriptSyntaxError);
264+
});
265+
254266
test.concurrent('obj (escaped reserved word as key)', async () => {
255267
await expect(async () => await exe(`
256268
<: {

0 commit comments

Comments
 (0)