Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/parser/syntaxes/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ function parseObject(s: ITokenStream, isStatic: boolean): Ast.Obj {

const map = new Map<string, Ast.Expression>();
while (!s.is(TokenKind.CloseBrace)) {
s.expect(TokenKind.Identifier);
const keyTokenKind = s.getTokenKind();
if (keyTokenKind !== TokenKind.Identifier && keyTokenKind !== TokenKind.StringLiteral) {
throw unexpectedTokenError(keyTokenKind, s.getPos());
}
const k = s.getTokenValue();
s.next();

Expand Down
2 changes: 2 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ describe('Object', () => {
});

/* 未実装
* see also: test/literals.ts > literal > obj (string key)
* issue: https://github.com/aiscript-dev/aiscript/issues/62
test.concurrent('string key', async () => {
const res = await exe(`
let obj = {
Expand Down
9 changes: 9 additions & 0 deletions test/literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ describe('literal', () => {
eq(res, OBJ(new Map([['a', NUM(1)], ['b', NUM(2)], ['c', NUM(3)]])));
});

test.concurrent('obj (string key)', async () => {
const res = await exe(`
<: {
"藍": 42,
}
`);
eq(res, OBJ(new Map([['藍', NUM(42)]])));
});

test.concurrent('obj and arr (separated by line break)', async () => {
const res = await exe(`
<: {
Expand Down
1 change: 1 addition & 0 deletions unreleased/object-key-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- オブジェクトリテラルのキーに文字列リテラルを記述できるようになりました。
Loading