Skip to content

Commit

Permalink
refactor: Reuse safeTokenize and safeParse
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Jul 24, 2024
1 parent 6b0a271 commit 874dd9a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 44 deletions.
36 changes: 5 additions & 31 deletions metals/src/main/scala/scala/meta/internal/parsing/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import scala.meta.internal.metals.Report
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.ScalaVersionSelector
import scala.meta.io.AbsolutePath
import scala.meta.parsers.Parse
import scala.meta.parsers.ParseException
import scala.meta.parsers.Parsed
import scala.meta.tokens.Tokens

import org.eclipse.lsp4j.Diagnostic
import org.eclipse.lsp4j.DiagnosticSeverity
import org.eclipse.{lsp4j => l}
import org.scalameta.invariants.InvariantFailedException

/**
* Manages parsing of Scala source files into Scalameta syntax trees.
Expand Down Expand Up @@ -130,17 +128,9 @@ final class Trees(
} yield tokens
}

def tokenized(input: inputs.Input.VirtualFile): Tokenized = try {
scalaVersionSelector
.getDialect(input.path.toAbsolutePath)(input)
.tokenize
} catch {
case t: InvariantFailedException =>
Tokenized.Error(
Position.None,
s"Could not tokenize ${input.path}",
t,
)
def tokenized(input: inputs.Input.VirtualFile): Tokenized = {
val dialect = scalaVersionSelector.getDialect(input.path.toAbsolutePath)
input.value.safeTokenize(dialect)
}

private def parse(
Expand All @@ -155,11 +145,9 @@ final class Trees(
val input = Input.VirtualFile(path.toURI.toString(), skipFistShebang)
if (path.isAmmoniteScript || path.isMill) {
val ammoniteInput = Input.Ammonite(input)
Trees.safeParse { () =>
dialect(ammoniteInput).parse(Parse.parseAmmonite)
}
ammoniteInput.safeParse[MultiSource](dialect)
} else {
Trees.safeParse(dialect, input)
input.safeParse[Source](dialect)
}
} catch {
// if the parsers breaks we should not throw the exception further
Expand All @@ -186,20 +174,6 @@ final class Trees(

object Trees {

def safeParse[T](f: () => Parsed[T]): Parsed[T] = {
try {
f()
} catch {
case t: InvariantFailedException =>
Parsed.Error(Position.None, t.toString(), t)
}
}

def safeParse(dialect: Dialect, input: Input): Parsed[Source] =
safeParse { () =>
dialect.apply(input).parse[Source]
}

/* Tokenizing works perfectly fine with 212 dialect as long as we are only
* interested in having stable results. This is not the case for parsing.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ object InverseLegacyToken {
VIEWBOUND -> "VIEWBOUND",
WHITESPACE_SPC -> "WHITESPACE_SPC",
WHITESPACE_TAB -> "WHITESPACE_TAB",
WHITESPACE_LF -> "WHITESPACE_TAB",
WHITESPACE_FF -> "WHITESPACE_TAB",
WHITESPACE_CR -> "WHITESPACE_TAB",
WHITESPACE_CRLF -> "WHITESPACE_TAB",
WHITESPACE_END -> "WHITESPACE_TAB",
WHITESPACE_LF -> "WHITESPACE_LF",
WHITESPACE_FF -> "WHITESPACE_FF",
WHITESPACE_CR -> "WHITESPACE_CR",
WHITESPACE_CRLF -> "WHITESPACE_CRLF",
WHITESPACE_END -> "WHITESPACE_END",
COMMENT -> "COMMENT",
UNQUOTE -> "UNQUOTE",
ELLIPSIS -> "ELLIPSIS"
Expand Down
11 changes: 3 additions & 8 deletions mtags/src/main/scala/scala/meta/internal/mtags/ScalaMtags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import scala.meta.internal.trees._
import scala.meta.parsers.Parsed
import scala.meta.transversers.SimpleTraverser

import org.scalameta.invariants.InvariantFailedException

object ScalaMtags {
def index(input: Input.VirtualFile, dialect: Dialect): MtagsIndexer = {
new ScalaMtags(input, dialect)
Expand All @@ -27,12 +25,9 @@ class ScalaMtags(
with MtagsIndexer {

private val root: Parsed[Source] =
try {
parsedTree.map(Parsed.Success(_)).getOrElse(dialect(input).parse[Source])
} catch {
case t: InvariantFailedException =>
Parsed.Error(Position.None, t.toString(), t)
}
parsedTree
.map(Parsed.Success(_))
.getOrElse(input.safeParse[Source](dialect))

def source: Source = root.get
override def language: Language = Language.SCALA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,16 @@ trait ScalametaCommonEnrichments extends CommonMtagsEnrichments {
toOffset(startLine, startColumn),
toOffset(endLine, endColumn)
)

def safeParse[T <: m.Tree](dialect: m.Dialect)(implicit
parse: m.parsers.Parse[T]
): m.parsers.Parsed[T] =
try {
parse(input, dialect)
} catch {
case t: InvariantFailedException =>
m.parsers.Parsed.Error(Position.None, t.toString(), t)
}
}

implicit class XtensionSymbolInformation(info: s.SymbolInformation) {
Expand Down

0 comments on commit 874dd9a

Please sign in to comment.