Skip to content

Commit

Permalink
FormatTokens: method to report token lookup error
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jul 6, 2024
1 parent c662742 commit 6e70080
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(val arr: Array[FormatToken]
override def length: Int = arr.length
override def apply(idx: Int): FormatToken = arr(idx)

private def get(tok: Token, isBefore: Boolean): FormatToken = {
val idx = leftTok2tok.getOrElse(
FormatTokens.thash(tok),
throw new NoSuchElementException(
s"Missing token index [${tok.start}:${tok.end}]: `$tok`",
),
)
private def getAt(tok: Token, isBefore: Boolean)(idx: Int): FormatToken =
if (idx >= arr.length) arr.last
else {
val ft = arr(idx)
Expand All @@ -44,7 +38,12 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(val arr: Array[FormatToken]
else if (ft.left.start >= tok.start) ft
else at(idx + 1)
}
}

private def get(tok: Token, isBefore: Boolean): FormatToken =
getAt(tok, isBefore)(leftTok2tok.getOrElse(
FormatTokens.thash(tok),
FormatTokens.throwNoToken(tok, "Missing token index"),
))

def at(off: Int): FormatToken =
if (off < 0) arr.head else if (off < arr.length) arr(off) else arr.last
Expand Down Expand Up @@ -80,9 +79,7 @@ class FormatTokens(leftTok2tok: Map[TokenHash, Int])(val arr: Array[FormatToken]
@inline
def matching(token: Token): Token = matchingParentheses.getOrElse(
FormatTokens.thash(token),
throw new NoSuchElementException(
s"Missing matching token index [${token.start}:${token.end}]: `$token`",
),
FormatTokens.throwNoToken(token, "Missing matching token index"),
)
@inline
def matchingOpt(token: Token): Option[Token] = matchingParentheses
Expand Down Expand Up @@ -360,6 +357,9 @@ object FormatTokens {
FormatTokensRewrite(ftoks, styleMap) -> styleMap
}

private def throwNoToken(t: Token, msg: String): Nothing =
throw new NoSuchElementException(s"$msg ${t.structure}: `$t`")

@inline
def thash(token: Token): TokenHash = hash(token)

Expand Down

0 comments on commit 6e70080

Please sign in to comment.