mirror of
https://github.com/KarolS/millfork.git
synced 2024-12-25 21:29:25 +00:00
Show filename and context for compile errors
This commit is contained in:
parent
8c1492b211
commit
a7e1a24be6
@ -198,11 +198,15 @@ object MosStatementCompiler extends AbstractStatementCompiler[AssemblyLine] {
|
||||
stackPointerFixBeforeReturn(ctx) ++
|
||||
List(AssemblyLine.discardAF(), AssemblyLine.discardXF(), AssemblyLine.discardYF()) ++ returnInstructions
|
||||
case 1 =>
|
||||
ErrorReporting.warn("Returning without a value", ctx.options, statement.position)
|
||||
if (statement.position.isDefined){
|
||||
ErrorReporting.warn("Returning without a value", ctx.options, statement.position)
|
||||
}
|
||||
stackPointerFixBeforeReturn(ctx) ++
|
||||
List(AssemblyLine.discardXF(), AssemblyLine.discardYF()) ++ returnInstructions
|
||||
case 2 =>
|
||||
ErrorReporting.warn("Returning without a value", ctx.options, statement.position)
|
||||
if (statement.position.isDefined){
|
||||
ErrorReporting.warn("Returning without a value", ctx.options, statement.position)
|
||||
}
|
||||
stackPointerFixBeforeReturn(ctx) ++
|
||||
List(AssemblyLine.discardYF()) ++ returnInstructions
|
||||
}
|
||||
|
@ -3,28 +3,32 @@ package millfork.error
|
||||
import millfork.{CompilationFlag, CompilationOptions}
|
||||
import millfork.node.Position
|
||||
|
||||
import scala.collection.mutable
|
||||
|
||||
object ErrorReporting {
|
||||
|
||||
var verbosity = 0
|
||||
|
||||
var hasErrors = false
|
||||
|
||||
private var sourceLines: Option[IndexedSeq[String]] = None
|
||||
private var sourceLines: mutable.Map[String, IndexedSeq[String]] = mutable.Map()
|
||||
|
||||
private def printErrorContext(pos: Option[Position]): Unit = {
|
||||
if (sourceLines.isDefined && pos.isDefined) {
|
||||
val line = sourceLines.get.apply(pos.get.line - 1)
|
||||
val column = pos.get.column - 1
|
||||
val margin = " "
|
||||
print(margin)
|
||||
println(line)
|
||||
print(margin)
|
||||
print(" " * column)
|
||||
println("^")
|
||||
private def printErrorContext(pos: Option[Position]): Unit = synchronized {
|
||||
pos.foreach { position =>
|
||||
sourceLines.get(position.moduleName).foreach { lines =>
|
||||
val line = lines.apply(pos.get.line - 1)
|
||||
val column = pos.get.column - 1
|
||||
val margin = " "
|
||||
print(margin)
|
||||
println(line)
|
||||
print(margin)
|
||||
print(" " * column)
|
||||
println("^")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def f(position: Option[Position]): String = position.fold("")(p => s"(${p.line}:${p.column}) ")
|
||||
def f(position: Option[Position]): String = position.fold("")(p => s"(${p.moduleName}:${p.line}:${p.column}) ")
|
||||
|
||||
def info(msg: String, position: Option[Position] = None): Unit = {
|
||||
if (verbosity < 0) return
|
||||
@ -94,13 +98,18 @@ object ErrorReporting {
|
||||
}
|
||||
}
|
||||
|
||||
def clearErrors(): Unit = {
|
||||
def clearErrors(): Unit = synchronized {
|
||||
hasErrors = false
|
||||
sourceLines = None
|
||||
sourceLines.clear()
|
||||
}
|
||||
|
||||
def setSource(source: Option[IndexedSeq[String]]): Unit = {
|
||||
sourceLines = source
|
||||
def setSource(source: Option[IndexedSeq[String]]): Unit = synchronized {
|
||||
sourceLines.clear()
|
||||
source.foreach(sourceLines("") = _)
|
||||
}
|
||||
|
||||
def addSource(filename: String, lines: IndexedSeq[String]): Unit = synchronized {
|
||||
sourceLines(filename) = lines
|
||||
}
|
||||
|
||||
}
|
@ -4,7 +4,7 @@ import millfork.assembly.mos.{AddrMode, Opcode}
|
||||
import millfork.assembly.z80.{ZOpcode, ZRegisters}
|
||||
import millfork.env.{Constant, ParamPassingConvention, Type}
|
||||
|
||||
case class Position(filename: String, line: Int, column: Int, cursor: Int)
|
||||
case class Position(moduleName: String, line: Int, column: Int, cursor: Int)
|
||||
|
||||
sealed trait Node {
|
||||
var position: Option[Position] = None
|
||||
|
@ -59,7 +59,9 @@ abstract class AbstractSourceLoadingQueue[T](val initialFilenames: List[String],
|
||||
val path = Paths.get(filename)
|
||||
val parentDir = path.toFile.getAbsoluteFile.getParent
|
||||
val (src, featureConstants) = Preprocessor(options, Files.readAllLines(path, StandardCharsets.UTF_8).toIndexedSeq)
|
||||
val parser = createParser(filename, src, parentDir, featureConstants)
|
||||
val shortFileName = path.getFileName.toString
|
||||
val parser = createParser(shortFileName, src, parentDir, featureConstants)
|
||||
ErrorReporting.addSource(shortFileName, src.lines.toIndexedSeq)
|
||||
parser.toAst match {
|
||||
case Success(prog, _) =>
|
||||
parsedModules.synchronized {
|
||||
|
@ -13,11 +13,11 @@ import millfork.{CompilationFlag, CompilationOptions, SeparatedList}
|
||||
/**
|
||||
* @author Karol Stasiak
|
||||
*/
|
||||
abstract class MfParser[T](filename: String, input: String, currentDirectory: String, options: CompilationOptions, featureConstants: Map[String, Long]) {
|
||||
abstract class MfParser[T](fileId: String, input: String, currentDirectory: String, options: CompilationOptions, featureConstants: Map[String, Long]) {
|
||||
|
||||
import MfParser._
|
||||
|
||||
var lastPosition = Position(filename, 1, 1, 0)
|
||||
var lastPosition = Position(fileId, 1, 1, 0)
|
||||
var lastLabel = ""
|
||||
|
||||
def toAst: Parsed[Program] = program.parse(input + "\n\n\n")
|
||||
@ -33,7 +33,7 @@ abstract class MfParser[T](filename: String, input: String, currentDirectory: St
|
||||
}
|
||||
val columnNumber = i - lineStarts(lineNumber)
|
||||
lineNumber += 1
|
||||
val newPosition = Position(filename, lineNumber, columnNumber, i)
|
||||
val newPosition = Position(fileId, lineNumber, columnNumber, i)
|
||||
if (newPosition.cursor > lastPosition.cursor) {
|
||||
lastPosition = newPosition
|
||||
lastLabel = label
|
||||
|
Loading…
Reference in New Issue
Block a user