diff --git a/src/parser/syntaxes/expressions.ts b/src/parser/syntaxes/expressions.ts index 48b76b36..f0a96cb5 100644 --- a/src/parser/syntaxes/expressions.ts +++ b/src/parser/syntaxes/expressions.ts @@ -288,6 +288,7 @@ function parseAtom(s: ITokenStream, isStatic: boolean): Ast.Expression { return expr; } case TokenKind.Sharp: { + if (isStatic) break; return parseExprWithLabel(s); } } diff --git a/test/aison.ts b/test/aison.ts index f4d71aff..d6fd38df 100644 --- a/test/aison.ts +++ b/test/aison.ts @@ -62,6 +62,10 @@ greet()`)).toThrow(); expect(() => AiSON.parse('{key: (3 + 5)}')).toThrow(); }); + test.concurrent('not allowed: labeled expression', () => { + expect(() => AiSON.parse('#label: eval { 1 }')).toThrow(); + }); + test.concurrent('not allowed: multiple statements (string)', () => { expect(() => AiSON.parse(`"hello" diff --git a/test/index.ts b/test/index.ts index b94fd9ac..ed7a819d 100644 --- a/test/index.ts +++ b/test/index.ts @@ -4,7 +4,7 @@ */ import * as assert from 'assert'; -import { describe, test } from 'vitest'; +import { describe, expect, test } from 'vitest'; import { Parser, Interpreter, Ast } from '../src'; import { NUM, STR, NULL, ARR, OBJ, BOOL, TRUE, FALSE, ERROR ,FN_NATIVE } from '../src/interpreter/value'; import { AiScriptSyntaxError, AiScriptRuntimeError, AiScriptIndexOutOfRangeError } from '../src/error'; @@ -948,6 +948,16 @@ describe('Attribute', () => { const attr = member.attr[0]; assert.equal(attr.name, 'test'); }); + + test.concurrent('non-static expression is not allowed', async () => { + const parser = new Parser(); + expect(() => { + parser.parse(` + #[x #label: eval { 1 }] + @f() {} + `); + }).toThrow(); + }); }); describe('Location', () => { diff --git a/test/syntax.ts b/test/syntax.ts index 628dd6cf..72dcf190 100644 --- a/test/syntax.ts +++ b/test/syntax.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe, test } from 'vitest'; +import { describe, expect, test } from 'vitest'; import { utils } from '../src'; import { NUM, STR, NULL, ARR, OBJ, BOOL, TRUE, FALSE, ERROR ,FN_NATIVE } from '../src/interpreter/value'; import { AiScriptRuntimeError, AiScriptUnexpectedEOFError } from '../src/error'; @@ -1115,6 +1115,14 @@ describe('meta', () => { assert.fail(); }); }); + + describe('Labeled expression', () => { + test.concurrent('invalid', async () => { + expect(() => getMeta(` + ### x #label: eval { 1 } + `)).toThrow(); + }); + }); }); describe('namespace', () => { diff --git a/unreleased/fix-labeled-exprs-are-allowed-in-static-expr.md b/unreleased/fix-labeled-exprs-are-allowed-in-static-expr.md new file mode 100644 index 00000000..cf24942a --- /dev/null +++ b/unreleased/fix-labeled-exprs-are-allowed-in-static-expr.md @@ -0,0 +1 @@ +- Fix: メタデータ構文や属性、AiSONにラベル付きの式を記述してもエラーが発生しない問題を修正