Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/scala/viper/silver/parser/FastParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ object FastParser extends PosParser[Char, String] {
*/
val standard_import_directory = "import"

var _lines: Array[Int] = null
var _line_offset: Array[Int] = null

def parse(s: String, f: Path, plugins: Option[SilverPluginManager] = None) = {
_file = f.toAbsolutePath

// Add an empty line at the end to make `computeFrom(s.length)` return `(lines.length, 1)`, as the old
// implementation of `computeFrom` used to do.
val lines = s.linesWithSeparators
_lines = lines.map(_.length).toArray
_line_offset = (lines.map(_.length) ++ Seq(0)).toArray

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As described in the comment above, the ++ Seq(0) here is used to accurately reproduce an edge case of the old behavior. However, It's not clear to me how important that edge case is. Maybe we can ignore it.

var offset = 0
for (i <- _line_offset.indices) {
val line_length = _line_offset(i)
_line_offset(i) = offset
offset += line_length
}

// Strategy to handle imports
// Idea: Import every import reference and merge imported methods, functions, imports, .. into current program
Expand Down Expand Up @@ -155,7 +164,6 @@ object FastParser extends PosParser[Char, String] {
column = pos.column
}
ParseError(msg, SourcePosition(_file, line, column))
case _:Throwable =>
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/main/scala/viper/silver/parser/ParserPositions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ case class FilePosition(file: Path, vline: Int, col: Int) extends util.parsing.i

trait PosComputer {
def computeFrom(index: Int) : (Int, Int) = {
var left = index
var i = 0
val arr = FastParser._lines
while (i < arr.length && left >= arr(i)){
left -= arr(i)
i += 1
val line_offset = FastParser._line_offset
val result = java.util.Arrays.binarySearch(line_offset, index)
if (result >= 0) {
// Exact match
val line = result
(line + 1, index - line_offset(line) + 1)
} else {
// The binary search returned `- insertionPoint - 1`
val line = - result - 2
(line + 1, index - line_offset(line) + 1)
}
val r1 = (i + 1, left + 1)
r1
}
}
/**
Expand Down