Skip to content

Commit

Permalink
feat: support underline error in UnexpectedTokenError
Browse files Browse the repository at this point in the history
  • Loading branch information
rycont authored Jan 12, 2025
1 parent 2a10b92 commit 5004412
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
6 changes: 3 additions & 3 deletions core/error/function.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Token } from '../prepare/tokenize/token.ts'
import type { Position } from '../type/position.ts'

import { YaksokError } from './common.ts'

export class CannotReturnOutsideFunctionError extends YaksokError {
constructor(props: { tokens: Token[]; resource?: unknown }) {
constructor(props: { tokens: Token[] }) {
super(props)
this.message = `"약속 그만"은 약속 안에서만 사용할 수 있어요.`
}
}

export class FunctionMustHaveOneOrMoreStringPartError extends YaksokError {
constructor(props: { position?: Position }) {
constructor(props: { tokens: Token[] }) {
super(props)
this.message = `약속(번역)을 선언할 때엔 적어도 하나의 고정되는 부분이 있어야 해요.`
}
Expand Down
4 changes: 2 additions & 2 deletions core/error/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export class UnexpectedEndOfCodeError extends YaksokError {
export class UnexpectedTokenError extends YaksokError {
constructor(props: {
resource: {
token: Token
parts: string
}
tokens: Token[]
position?: Position
}) {
super(props)

this.message = `${tokenToText(props.resource.token)}${bold(
this.message = `${tokenToText(props.tokens[0])}${bold(
props.resource.parts,
)}에 사용할 수 없어요.`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ function assertValidFunctionHeader(
pieces: FunctionTemplatePiece[],
tokens: Token[],
) {
const position = tokens[0].position

const hasStaticPiece = pieces.some((piece) => piece.type === 'static')
if (!hasStaticPiece) {
throw new FunctionMustHaveOneOrMoreStringPartError({
position,
tokens,
})
}

Expand All @@ -104,11 +102,10 @@ function assertValidFunctionHeader(

if (!isNextTokenValid) {
throw new UnexpectedTokenError({
position: nextToken?.position,
resource: {
token: nextToken,
parts: '약속 인자',
},
tokens: [nextToken],
})
}

Expand All @@ -118,11 +115,10 @@ function assertValidFunctionHeader(

if (!isNextNextTokenValid) {
throw new UnexpectedTokenError({
position: nextNextToken?.position,
resource: {
token: nextNextToken,
parts: '약속 이름',
parts: '약속 인자를 닫는 괄호',
},
tokens: [nextNextToken],
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions runtest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { yaksok } from '@dalbit-yaksok/core'

await yaksok(`
약속, 말하기
약속, (10) 말하기
반복
결과: "안녕하세요"
결과: "안녕하세요"
말하기 보여주기
말하기 보여주기
`)

0 comments on commit 5004412

Please sign in to comment.