From 297c558f4988a0500ee3a95778ce3eac1549eee8 Mon Sep 17 00:00:00 2001 From: kujirahand Date: Fri, 20 Dec 2024 18:11:20 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AA=E3=83=96=E3=82=B8=E3=82=A7=E3=82=AF?= =?UTF-8?q?=E3=83=88=E8=A8=98=E6=B3=95=E3=81=AE=E5=BE=8C=E3=82=8D=E3=81=AE?= =?UTF-8?q?=E9=85=8D=E5=88=97=E3=82=A2=E3=82=AF=E3=82=BB=E3=82=B9=E3=81=8C?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=82=92=E4=BF=AE=E6=AD=A3=20#1858?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/nako_parser3.mts | 21 ++++++++++++++++++++- core/test/array_test.mjs | 9 +++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/core/src/nako_parser3.mts b/core/src/nako_parser3.mts index dbd5c472..cdcee368 100644 --- a/core/src/nako_parser3.mts +++ b/core/src/nako_parser3.mts @@ -2448,8 +2448,27 @@ export class NakoParser extends NakoParserBase { return a } + yJSONObject(): AstBlocks | Ast | null { + const a = this.yJSONObjectRaw() + if (!a) { return null } + // 配列の直後に@や[]があるか?助詞がある場合には、別の引数の可能性があるので無視。 (例) [0,1,2]を[3,4,5]に配列*** + if (a.josi === '' && this.checkTypes(['@', '['])) { + const ast: Ast = { + type: 'ref_array', + name: '__ARRAY__', + index: [a], + josi: '', + line: a.line, + end: this.peekSourceMap() + } + this.yValueWordGetIndex(ast) + return ast + } + return a + } + /** @returns {Ast | null} */ - yJSONObject (): AstBlocks | null { + yJSONObjectRaw (): AstBlocks | null { const map = this.peekSourceMap() if (this.accept(['{', '}'])) { return { diff --git a/core/test/array_test.mjs b/core/test/array_test.mjs index 32bcc5bd..e32cc59e 100644 --- a/core/test/array_test.mjs +++ b/core/test/array_test.mjs @@ -66,14 +66,19 @@ describe('array_test', async () => { await cmp('A=[[0,1],[2,3]]。A[1]と[2]を連続表示', '2,32') await cmp('A=[[0,1],[2,3]]。0にA[1]の[9,9]を配列一括挿入してJSONエンコードして表示', '[9,9,2,3]') }) - it('『["a","b","c","d"]@2を表示』がエラー #1858', async () => { + it('配列記法の後ろの配列アクセスがエラー #1858 @', async () => { await cmp('[0,1,2,3,4,5]@2を表示', '2') await cmp('["a","b","c"]@2を表示', 'c') await cmp('[[0,1,2],[3,4,5]]@1,0を表示', '3') }) - it('『["a","b","c","d"]@2を表示』がエラー #1858', async () => { + it('配列記法の後ろの配列アクセスがエラー #1858 []', async () => { await cmp('[0,1,2,3,4,5][2]を表示', '2') await cmp('["a","b","c"][2]を表示', 'c') await cmp('[[0,1,2],[3,4,5]][1,0]を表示', '3') }) + it('オブジェクト記法の後ろの配列アクセスがエラー #1858 @', async () => { + await cmp('{"a":1, "b":2}["b"]を表示', '2') + await cmp('{"a":1, "b":2}@"b"を表示', '2') + await cmp('[{"a":1, "b":2}]@0,"a"を表示', '1') + }) })