Commit 28426491 authored by Kunshan Wang's avatar Kunshan Wang

WIP: HAIL parser.

parent 9551ff05
...@@ -13,6 +13,7 @@ import scala.collection.mutable.ArrayBuffer ...@@ -13,6 +13,7 @@ import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.Stream import scala.collection.immutable.Stream
import java.io.StringWriter import java.io.StringWriter
import java.nio.CharBuffer import java.nio.CharBuffer
import uvm.utils.AntlrHelpers._
class UIRTextReader(val idFactory: IDFactory) { class UIRTextReader(val idFactory: IDFactory) {
import UIRTextReader._ import UIRTextReader._
...@@ -40,23 +41,6 @@ class UIRTextReader(val idFactory: IDFactory) { ...@@ -40,23 +41,6 @@ class UIRTextReader(val idFactory: IDFactory) {
read(sb.toString(), globalBundle) read(sb.toString(), globalBundle)
} }
class AccumulativeAntlrErrorListener(source: String) extends BaseErrorListener {
val buf = new ArrayBuffer[String]()
var hasError = false
lazy val sourceLines = ArrayBuffer(source.lines.toSeq: _*)
override def syntaxError(recognizer: Recognizer[_, _], offendingSymbol: Object,
line: Int, charPositionInLine: Int, msg: String, e: RecognitionException): Unit = {
val theLine = sourceLines(line - 1)
val marker = " " * charPositionInLine + "^"
buf.add("line %d:%d %s\n%s\n%s".format(line, charPositionInLine, msg, theLine, marker))
hasError = true
}
def getMessages(): String = buf.mkString("\n")
}
def read(source: String, ais: ANTLRInputStream, globalBundle: GlobalBundle): TrantientBundle = { def read(source: String, ais: ANTLRInputStream, globalBundle: GlobalBundle): TrantientBundle = {
val ea = new AccumulativeAntlrErrorListener(source) val ea = new AccumulativeAntlrErrorListener(source)
...@@ -128,15 +112,6 @@ class UIRTextReader(val idFactory: IDFactory) { ...@@ -128,15 +112,6 @@ class UIRTextReader(val idFactory: IDFactory) {
// Printing context information (line, column, near some token) // Printing context information (line, column, near some token)
def inCtx(ctx: ParserRuleContext, s: String): String = nearTok(ctx.getStart, s)
def inCtx(ctx: TerminalNode, s: String): String = nearTok(ctx.getSymbol, s)
def nearTok(tok: Token, s: String): String = {
val line = tok.getLine()
val column = tok.getCharPositionInLine()
val near = tok.getText()
return "At %d:%d near '%s': %s".format(line, column, near, s)
}
def catchIn[T](ctx: ParserRuleContext, s: String)(func: => T): T = try { def catchIn[T](ctx: ParserRuleContext, s: String)(func: => T): T = try {
func func
......
...@@ -23,3 +23,6 @@ class UvmDivisionByZeroException(message: String = null, cause: Throwable = null ...@@ -23,3 +23,6 @@ class UvmDivisionByZeroException(message: String = null, cause: Throwable = null
/** Thrown when accessing Mu memory but the address is outside the allocated region. */ /** Thrown when accessing Mu memory but the address is outside the allocated region. */
class UvmIllegalMemoryAccessException(message: String = null, cause: Throwable = null) extends UvmRuntimeException(message, cause) class UvmIllegalMemoryAccessException(message: String = null, cause: Throwable = null) extends UvmRuntimeException(message, cause)
/** Thrown on syntax errors in HAIL scripts. */
class UvmHailParsingException(message: String = null, cause: Throwable = null) extends UvmRefImplException(message, cause)
package uvm.utils
import org.antlr.v4.runtime.RecognitionException
import scala.collection.mutable.ArrayBuffer
import org.antlr.v4.runtime.Recognizer
import org.antlr.v4.runtime.BaseErrorListener
import org.antlr.v4.runtime.tree.TerminalNode
import org.antlr.v4.runtime.ParserRuleContext
import org.antlr.v4.runtime.Token
object AntlrHelpers {
class AccumulativeAntlrErrorListener(source: String) extends BaseErrorListener {
val buf = new ArrayBuffer[String]()
var hasError = false
lazy val sourceLines = ArrayBuffer(source.lines.toSeq: _*)
override def syntaxError(recognizer: Recognizer[_, _], offendingSymbol: Object,
line: Int, charPositionInLine: Int, msg: String, e: RecognitionException): Unit = {
val theLine = sourceLines(line - 1)
val marker = " " * charPositionInLine + "^"
buf += "line %d:%d %s\n%s\n%s".format(line, charPositionInLine, msg, theLine, marker)
hasError = true
}
def getMessages(): String = buf.mkString("\n")
}
def inCtx(ctx: ParserRuleContext, s: String): String = nearTok(ctx.getStart, s)
def inCtx(ctx: TerminalNode, s: String): String = nearTok(ctx.getSymbol, s)
def nearTok(tok: Token, s: String): String = {
val line = tok.getLine()
val column = tok.getCharPositionInLine()
val near = tok.getText()
return "At %d:%d near '%s': %s".format(line, column, near, s)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment