sigma.ast.SourceContext.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sigma-state_2.12 Show documentation
Show all versions of sigma-state_2.12 Show documentation
Interpreter of a Sigma-State language
The newest version!
package sigma.ast
import fastparse.Parsed.Failure
import scala.io.Source
case class SourceContext(line: Int, column: Int, sourceLine: String) {
}
object SourceContext {
def fromParserIndex(index: Int, input: String): SourceContext = {
val lines = Source.fromString(input).getLines.toSeq
if (lines.isEmpty)
SourceContext(0, 0, "")
else
lines.tail
.scanLeft((0, lines.head.length)) { case ((_, end), line) => (end + 1, end + 1 + line.length) }
.zip(lines)
.zipWithIndex
.find { case (((start, end), _), _) => index >= start && index <= end }
.map {
case (((start, _), line), lineIndex) =>
SourceContext(lineIndex + 1, index - start + 1, line)
}.getOrElse {
// at least one line in the input
// point to the last character of the last line
val lastLine = lines.last
val iLine = lines.length - 1
val iCol = lastLine.length - 1
SourceContext(iLine, iCol, lastLine)
}
}
def fromParserFailure(e: Failure): SourceContext =
fromParserIndex(e.index , e.extra.input.slice(0, e.extra.input.length))
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy