Skip to content

Commit

Permalink
Better error messages during parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Feb 20, 2024
1 parent f8e6205 commit d30d25c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PRE_TOKEN, TOKEN, TOKEN_TYPES } from '../typings/syntaxTypes'
*/
export default function parser (preTokens: PRE_TOKEN[]): TOKEN[] {
let mainLoopIndex: number
const startOfSearch: PRE_TOKEN[] = []

function getNextRawToken () {
mainLoopIndex++
Expand Down Expand Up @@ -35,12 +36,19 @@ export default function parser (preTokens: PRE_TOKEN[]): TOKEN[] {
case ']':
case ')':
case '}':
if (startOfSearch.length > 0) {
const startingToken = startOfSearch.pop()
throw new Error(`At line: ${CurrentPreToken.line}. Expecting to close the '${startingToken?.value}' ` +
`started at line ${startingToken?.line}, but found '${CurrentPreToken.value}'.`)
}
throw new Error(`At line: ${CurrentPreToken.line}. Unexpected closing '${CurrentPreToken.value}'.`)
case '[':
startOfSearch.push(CurrentPreToken)
RetToken = { type: 'Arr', value: '', precedence: 0, line: CurrentPreToken.line }
RetToken.params = getTokensUntil(']', RetToken.type, RetToken.line)
return RetToken
case '(':
startOfSearch.push(CurrentPreToken)
if (mainLoopIndex > 1 && preTokens[mainLoopIndex - 2].type === 'Variable') {
RetToken = { type: 'Function', value: '', precedence: 0, line: CurrentPreToken.line }
} else {
Expand All @@ -49,6 +57,7 @@ export default function parser (preTokens: PRE_TOKEN[]): TOKEN[] {
RetToken.params = getTokensUntil(')', RetToken.type, RetToken.line)
return RetToken
case '{':
startOfSearch.push(CurrentPreToken)
RetToken = { type: 'CodeDomain', value: '', precedence: 0, line: CurrentPreToken.line }
RetToken.params = getTokensUntil('}', RetToken.type, RetToken.line)
return RetToken
Expand All @@ -74,6 +83,7 @@ export default function parser (preTokens: PRE_TOKEN[]): TOKEN[] {
`Missing closing '${endChar}' for for '${parentType}' started at line: ${line}.`)
}
}
startOfSearch.pop()
// discard closing char
mainLoopIndex++
return returnedTokens
Expand Down

0 comments on commit d30d25c

Please sign in to comment.