|
| 1 | +import { |
| 2 | + AiAlreadyDeclaredVariableError, |
| 3 | + AiCanNotAssignToImmutableVariableError, |
| 4 | + AiCanNotCallError, |
| 5 | + AiInvalidArgumentError, |
| 6 | + AiMissingArgumentError, |
| 7 | + AiNotAssignableTypeError, |
| 8 | + AiTypeErrorKind, |
| 9 | +} from "../../errors/AiTypeError.js"; |
| 10 | +import { TypeError } from "../../index.js"; |
| 11 | +import { I18nLocalizerResult, I18nMessage, I18nMessages } from "../core.js"; |
| 12 | +import { AiScriptI18nMessages } from "./index.js"; |
| 13 | + |
| 14 | +export const typeErrorMessage = { |
| 15 | + ja: { |
| 16 | + NotAssignableType(dest: string, value: string) { |
| 17 | + return `型\`${dest}\`に、\`${value}\`は代入できません。`; |
| 18 | + }, |
| 19 | + AlreadyDeclaredVariable(name: string) { |
| 20 | + return `変数\`${name}\`はすでに定義されています。`; |
| 21 | + }, |
| 22 | + CanNotCall(type: string) { |
| 23 | + return `型\`${type}\`は関数型ではありません。`; |
| 24 | + }, |
| 25 | + InvalidArgumentError(pos: number, dest: string, but: string) { |
| 26 | + return `${pos}番目の引数には型\`${dest}\`が必要ですが、\`${but}\`が与えられました。`; |
| 27 | + }, |
| 28 | + MissingArgumentError(pos: number, dest: string) { |
| 29 | + return `${pos}番目の引数には型\`${dest}\`が必要ですが、何も与えられていません。`; |
| 30 | + }, |
| 31 | + CanNotAssignToImmutableVariable(name: string) { |
| 32 | + return `変数\`${name}\`は不変なので、新たに代入することはできません。`; |
| 33 | + }, |
| 34 | + } satisfies I18nMessage<keyof typeof AiTypeErrorKind>, |
| 35 | +} satisfies I18nMessages; |
| 36 | + |
| 37 | +export const typeErrorLocalizer = ( |
| 38 | + error: TypeError |
| 39 | +): I18nLocalizerResult<AiScriptI18nMessages> => { |
| 40 | + if (error instanceof AiNotAssignableTypeError) { |
| 41 | + return ["typing.NotAssignableType", [error.left, error.right]]; |
| 42 | + } else if (error instanceof AiAlreadyDeclaredVariableError) { |
| 43 | + return ["typing.AlreadyDeclaredVariable", [error.name]]; |
| 44 | + } else if (error instanceof AiCanNotCallError) { |
| 45 | + return ["typing.CanNotCall", [error.type]]; |
| 46 | + } else if (error instanceof AiInvalidArgumentError) { |
| 47 | + return [ |
| 48 | + "typing.InvalidArgumentError", |
| 49 | + [error.pos, error.expectType, error.butType], |
| 50 | + ]; |
| 51 | + } else if (error instanceof AiMissingArgumentError) { |
| 52 | + return ["typing.MissingArgumentError", [error.pos, error.expectType]]; |
| 53 | + } else if (error instanceof AiCanNotAssignToImmutableVariableError) { |
| 54 | + return ["typing.CanNotAssignToImmutableVariable", [error.name]]; |
| 55 | + } |
| 56 | + |
| 57 | + switch (error.kind) { |
| 58 | + case AiTypeErrorKind.AlreadyDeclaredVariable: |
| 59 | + return ["typing.AlreadyDeclaredVariable", ["<:unknown:>"]]; |
| 60 | + |
| 61 | + case AiTypeErrorKind.NotAssignableType: |
| 62 | + return ["typing.NotAssignableType", ["<:unknown:>", "<:unknown:>"]]; |
| 63 | + |
| 64 | + case AiTypeErrorKind.CanNotCall: |
| 65 | + return ["typing.CanNotCall", ["<:unknown:>"]]; |
| 66 | + |
| 67 | + case AiTypeErrorKind.InvalidArgumentError: |
| 68 | + return [ |
| 69 | + "typing.InvalidArgumentError", |
| 70 | + [-1, "<:unknown:>", "<:unknown:>"], |
| 71 | + ]; |
| 72 | + |
| 73 | + case AiTypeErrorKind.MissingArgumentError: |
| 74 | + return ["typing.MissingArgumentError", [-1, "<:unknown:>"]]; |
| 75 | + |
| 76 | + case AiTypeErrorKind.CanNotAssignToImmutableVariable: |
| 77 | + return ["typing.CanNotAssignToImmutableVariable", ["<:unknown:>"]]; |
| 78 | + } |
| 79 | +}; |
0 commit comments