Skip to content

Commit

Permalink
Parser: better syntax error prediction
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Jun 5, 2023
1 parent 4628c76 commit 3a83943
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
5 changes: 0 additions & 5 deletions data/test.no

This file was deleted.

1 change: 1 addition & 0 deletions src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const prettyIndex = (index: number, source: Source): string => {
const highlight = ' '.repeat(start.column) + '^'
const lineNum = `${start.line + 1} | `
return `\
${lineNum}${line}
${' '.repeat(lineNum.length)}${highlight}`
}
Expand Down
7 changes: 4 additions & 3 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,19 @@ export const rules: Map<ParserTokenName, Rule> = new Map(rawRules.map((r: Rule)
export const parse = (tokens: LexerToken[], node: TokenName = 'program', index: number = 0): Token | SyntaxErrorInfo | true => {
const rule = rules.get(<ParserTokenName>node)!
if (rule) {
const errorInfos: SyntaxErrorInfo[] = []
let syntaxError: SyntaxErrorInfo | undefined
for (const branch of rule.branches) {
if (isEmptyBranch(branch)) return true
const transform = { name: <ParserTokenName>node, branch }
const branchToken = parseTransform(transform, tokens, index)
if ('name' in branchToken) {
return branchToken
} else {
errorInfos.push(branchToken)
if (branchToken.location.start > (syntaxError?.location.start ?? -1))
syntaxError = branchToken
}
}
return errorInfos.at(-1)!
return syntaxError!
} else {
const error = { expect: [node], got: tokens[index].name, location: tokens[index].location }
return node === tokens[index].name ? tokens[index] : error
Expand Down

0 comments on commit 3a83943

Please sign in to comment.