-
Notifications
You must be signed in to change notification settings - Fork 39
feat: AiScript Object Notation #897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cbb1162
feat: AiScript Object Notation
kakkokari-gtyih 1c6825d
Update Changelog
kakkokari-gtyih 797ad7d
Update Changelog
kakkokari-gtyih 656db10
update test
kakkokari-gtyih 67bd115
Update unreleased/aison.md
kakkokari-gtyih bae8bdb
add Aison.parse type
FineArchs 32e1add
Update aiscript.api.md
FineArchs 586cd63
indent
FineArchs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /** | ||
| * AiSON: AiScript Object Notation | ||
| */ | ||
| import { nodeToJs } from '../utils/node-to-js.js'; | ||
| import { Scanner } from './scanner.js'; | ||
| import { parseAiSonTopLevel } from './syntaxes/aison.js'; | ||
|
|
||
| export class AiSON { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| public static parse(input: string): any { | ||
| const scanner = new Scanner(input); | ||
| const ast = parseAiSonTopLevel(scanner); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| return nodeToJs(ast) as any; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { TokenKind } from '../token.js'; | ||
| import { AiScriptSyntaxError } from '../../error.js'; | ||
| import { parseExpr } from './expressions.js'; | ||
| import type * as Ast from '../../node.js'; | ||
| import type { ITokenStream } from '../streams/token-stream.js'; | ||
|
|
||
| export function parseAiSonTopLevel(s: ITokenStream): Ast.Node { | ||
| let node: Ast.Node | null = null; | ||
|
|
||
| while (s.is(TokenKind.NewLine)) { | ||
| s.next(); | ||
| } | ||
|
|
||
| while (!s.is(TokenKind.EOF)) { | ||
| if (node == null) { | ||
| node = parseExpr(s, true); | ||
| } else { | ||
| throw new AiScriptSyntaxError('AiSON only supports one top-level expression.', s.getPos()); | ||
| } | ||
|
|
||
| // terminator | ||
| switch (s.getTokenKind()) { | ||
| case TokenKind.NewLine: | ||
| case TokenKind.SemiColon: { | ||
| while (s.is(TokenKind.NewLine) || s.is(TokenKind.SemiColon)) { | ||
| s.next(); | ||
| } | ||
| break; | ||
| } | ||
| case TokenKind.EOF: { | ||
| break; | ||
| } | ||
| default: { | ||
| throw new AiScriptSyntaxError('Multiple statements cannot be placed on a single line.', s.getPos()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (node == null) { | ||
| throw new AiScriptSyntaxError('AiSON requires at least one top-level expression.', s.getPos()); | ||
| } | ||
|
|
||
| return node; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { JsValue } from '../interpreter/util.js'; | ||
| import type * as Ast from '../node.js'; | ||
|
|
||
| export function nodeToJs(node: Ast.Node): JsValue { | ||
| switch (node.type) { | ||
| case 'arr': return node.value.map(item => nodeToJs(item)); | ||
| case 'bool': return node.value; | ||
| case 'null': return null; | ||
| case 'num': return node.value; | ||
| case 'obj': { | ||
| const obj: { [keys: string]: JsValue } = {}; | ||
| for (const [k, v] of node.value.entries()) { | ||
| // TODO: keyが__proto__とかじゃないかチェック | ||
| obj[k] = nodeToJs(v); | ||
| } | ||
| return obj; | ||
| } | ||
| case 'str': return node.value; | ||
| default: return undefined; | ||
| } | ||
| } |
FineArchs marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, expect, test } from 'vitest'; | ||
| import { AiSON } from '../src/parser/aison'; | ||
|
|
||
| describe('parse', () => { | ||
| test.concurrent('str', () => { | ||
| expect(AiSON.parse('"Ai-chan kawaii"')).toEqual('Ai-chan kawaii'); | ||
| }); | ||
|
|
||
| test.concurrent('number', () => { | ||
| expect(AiSON.parse('42')).toEqual(42); | ||
| }); | ||
|
|
||
| test.concurrent('bool', () => { | ||
| expect(AiSON.parse('true')).toEqual(true); | ||
| }); | ||
|
|
||
| test.concurrent('null', () => { | ||
| expect(AiSON.parse('null')).toEqual(null); | ||
| }); | ||
|
|
||
| test.concurrent('array', () => { | ||
| expect(AiSON.parse('[1, 2, 3]')).toEqual([1, 2, 3]); | ||
| }); | ||
|
|
||
| test.concurrent('object', () => { | ||
| expect(AiSON.parse('{key: "value"}')).toEqual({ key: 'value' }); | ||
| }); | ||
|
|
||
| test.concurrent('nested', () => { | ||
| expect(AiSON.parse('[{key: "value"}]')).toEqual([{ key: 'value' }]); | ||
| }); | ||
|
|
||
| test.concurrent('invalid: unclosed string', () => { | ||
| expect(() => AiSON.parse('"hello')).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('invalid: unclosed array', () => { | ||
| expect(() => AiSON.parse('[1, 2, 3')).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: empty', () => { | ||
| expect(() => AiSON.parse('')).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: function', () => { | ||
| expect(() => AiSON.parse(`@greet() { return "hello" } | ||
|
|
||
| greet()`)).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: variable assignment', () => { | ||
| expect(() => AiSON.parse('let x = 42')).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: namespace', () => { | ||
| expect(() => AiSON.parse(`:: Ai { | ||
| let x = 42 | ||
| }`)).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: expression', () => { | ||
| expect(() => AiSON.parse('{key: (3 + 5)}')).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: multiple statements (string)', () => { | ||
| expect(() => AiSON.parse(`"hello" | ||
|
|
||
| "hi"`)).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: multiple statements in the same line', () => { | ||
| expect(() => AiSON.parse('"hello" "hi"')).toThrow(); | ||
| }); | ||
|
|
||
| test.concurrent('not allowed: multiple statements (object)', () => { | ||
| expect(() => AiSON.parse(`{key: "value"} | ||
|
|
||
| {foo: "bar"}`)).toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| - AiScriptのオブジェクトの表記法を利用したデータ交換用フォーマット「AiScript Object Notation (AiSON)」およびそのパーサーを追加しました。 | ||
kakkokari-gtyih marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - 現在、`AiSON.parse()`(パースしてJavaScriptオブジェクトに変換する)が使用できます。 | ||
| - 通常のAiScriptと異なるのは以下の点です: | ||
| - リテラルはトップレベルにひとつだけしか許可されません。 | ||
| - 動的な式(関数・オブジェクトのvalueに対する動的なバインディングなど)は許可されません。 | ||
| - 名前空間・メタデータなど、リテラルとコメント以外をトップレベルに書くことは許可されていません。 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.