Skip to content

Commit

Permalink
名前空間下の変数定義に属性を付与できるように (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
takejohn authored Jan 12, 2025
1 parent c3d71b5 commit 35c63c5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parser/syntaxes/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function parseReturn(s: ITokenStream): Ast.Return {
* StatementWithAttr = *Attr Statement
* ```
*/
function parseStatementWithAttr(s: ITokenStream): Ast.Definition {
export function parseStatementWithAttr(s: ITokenStream): Ast.Definition {
const attrs: Ast.Attribute[] = [];
while (s.is(TokenKind.OpenSharpBracket)) {
attrs.push(parseAttr(s) as Ast.Attribute);
Expand Down
6 changes: 5 additions & 1 deletion src/parser/syntaxes/toplevel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NODE } from '../utils.js';
import { TokenKind } from '../token.js';
import { AiScriptSyntaxError, AiScriptUnexpectedEOFError } from '../../error.js';
import { parseDefStatement, parseStatement } from './statements.js';
import { parseDefStatement, parseStatement, parseStatementWithAttr } from './statements.js';
import { parseExpr } from './expressions.js';

import type * as Ast from '../../node.js';
Expand Down Expand Up @@ -91,6 +91,10 @@ export function parseNamespace(s: ITokenStream): Ast.Namespace {
members.push(parseNamespace(s));
break;
}
case TokenKind.OpenSharpBracket: {
members.push(parseStatementWithAttr(s));
break;
}
}

// terminator
Expand Down
19 changes: 19 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,25 @@ describe('Attribute', () => {
if (attr.value.type !== 'bool') assert.fail();
assert.equal(attr.value.value, true);
});

test.concurrent('attribute with statement under namespace', async () => {
const parser = new Parser();
const nodes = parser.parse(`
:: Tests {
#[test]
@assert_success() {
<: "Hello, world!"
}
}
`);
assert.equal(nodes.length, 1);
const ns = nodes[0];
assert.ok(ns.type === 'ns');
const member = ns.members[0];
assert.ok(member.type === 'def');
const attr = member.attr[0];
assert.equal(attr.name, 'test');
});
});

describe('Location', () => {
Expand Down
1 change: 1 addition & 0 deletions unreleased/attr-under-ns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- 名前空間下の変数定義に属性を付与できるようになりました。

0 comments on commit 35c63c5

Please sign in to comment.