Skip to content

Commit

Permalink
runutils: print token location on antlr error.
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinafyi committed Mar 13, 2024
1 parent b6a2409 commit 5afc399
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/main/scala/util/RunUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Parsers.*
import Parsers.SemanticsParser.*
import org.antlr.v4.runtime.tree.ParseTreeWalker
import org.antlr.v4.runtime.BailErrorStrategy
import org.antlr.v4.runtime.{CharStreams, CommonTokenStream}
import org.antlr.v4.runtime.{CharStreams, CommonTokenStream, Token}
import translating.*
import util.Logger
import java.util.Base64
Expand Down Expand Up @@ -114,18 +114,31 @@ object IRLoading {

val semantics = mods.map(_.auxData("ast").data.toStringUtf8.parseJson.convertTo[Map[String, Array[Array[String]]]])

def parse_insn(f: String): StmtContext = {
def parse_insn(line: String): StmtContext = {
val semanticsLexer = SemanticsLexer(CharStreams.fromString(line))
val tokens = CommonTokenStream(semanticsLexer)
val parser = SemanticsParser(tokens)
parser.setErrorHandler(BailErrorStrategy())
parser.setBuildParseTree(true)

try {
val semanticsLexer = SemanticsLexer(CharStreams.fromString(f))
val tokens = CommonTokenStream(semanticsLexer)
val parser = SemanticsParser(tokens)
parser.setErrorHandler(BailErrorStrategy())
parser.setBuildParseTree(true)
parser.stmt()
} catch {
case e: org.antlr.v4.runtime.misc.ParseCancellationException =>
Logger.error(f)
throw RuntimeException(e)
val extra = e.getCause match {
case mismatch: org.antlr.v4.runtime.InputMismatchException =>
val token = mismatch.getOffendingToken
s"""
exn: ${mismatch}
offending token: ${token}

${line.replace('\n', ' ')}
${" " * token.getStartIndex}^ here!
""".stripIndent
case _ => ""
}
Logger.error(s"""Semantics parse error:\n line: ${line}\n${extra}""")
throw e
}
}

Expand Down

0 comments on commit 5afc399

Please sign in to comment.