Skip to content

Commit

Permalink
Merge pull request #124 from lppedd/refactor/runtime
Browse files Browse the repository at this point in the history
Port additional runtime fixes and apply minor cleanups
  • Loading branch information
ftomassetti authored Dec 19, 2023
2 parents b144223 + 80002a2 commit 6f9a85d
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
* that can follow the current rule.
*/
override fun recover(recognizer: Parser, e: RecognitionException) {
var lastErrorStatesTemp = lastErrorStates

if (
lastErrorIndex == recognizer.tokenStream.index() &&
lastErrorStates != null &&
lastErrorStates!!.contains(recognizer.state)
lastErrorStatesTemp != null &&
lastErrorStatesTemp.contains(recognizer.state)
) {
// Uh oh, another error at same token index and previously-visited
// state in ATN; must be a case where LT(1) is in the recovery
Expand All @@ -147,11 +149,12 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {

lastErrorIndex = recognizer.tokenStream.index()

if (lastErrorStates == null) {
lastErrorStates = IntervalSet()
if (lastErrorStatesTemp == null) {
lastErrorStatesTemp = IntervalSet()
lastErrorStates = lastErrorStatesTemp
}

lastErrorStates!!.add(recognizer.state)
lastErrorStatesTemp.add(recognizer.state)

val followSet = getErrorRecoverySet(recognizer)
consumeUntil(recognizer, followSet)
Expand Down Expand Up @@ -269,7 +272,7 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
*
* @see reportError
*/
protected fun reportNoViableAlternative(recognizer: Parser, e: NoViableAltException) {
protected open fun reportNoViableAlternative(recognizer: Parser, e: NoViableAltException) {
val tokens = recognizer.tokenStream
val input = if (e.startToken!!.type == Token.EOF) {
"<EOF>"
Expand All @@ -289,7 +292,7 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
*
* @see reportError
*/
protected fun reportInputMismatch(recognizer: Parser, e: InputMismatchException) {
protected open fun reportInputMismatch(recognizer: Parser, e: InputMismatchException) {
val tokenErrorDisplay = getTokenErrorDisplay(e.offendingToken)
val expectedToken = e.expectedTokens!!.toString(recognizer.vocabulary)
val msg = "mismatched input $tokenErrorDisplay expecting $expectedToken"
Expand All @@ -304,7 +307,7 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
*
* @see reportError
*/
protected fun reportFailedPredicate(recognizer: Parser, e: FailedPredicateException) {
protected open fun reportFailedPredicate(recognizer: Parser, e: FailedPredicateException) {
val ruleName = recognizer.ruleNames[recognizer.context!!.ruleIndex]
val msg = "rule $ruleName ${e.message}"
recognizer.notifyErrorListeners(e.offendingToken!!, msg, e)
Expand All @@ -327,7 +330,7 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
*
* @param recognizer The parser instance
*/
protected fun reportUnwantedToken(recognizer: Parser) {
protected open fun reportUnwantedToken(recognizer: Parser) {
if (inErrorRecoveryMode(recognizer)) {
return
}
Expand Down Expand Up @@ -357,7 +360,7 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
*
* @param recognizer The parser instance
*/
protected fun reportMissingToken(recognizer: Parser) {
protected open fun reportMissingToken(recognizer: Parser) {
if (inErrorRecoveryMode(recognizer)) {
return
}
Expand Down Expand Up @@ -462,8 +465,9 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
* @return `true` if single-token insertion is a viable recovery
* strategy for the current mismatched input, otherwise `false`
*/
protected fun singleTokenInsertion(recognizer: Parser): Boolean {
protected open fun singleTokenInsertion(recognizer: Parser): Boolean {
val currentSymbolType = recognizer.tokenStream.LA(1)

// If current token is consistent with what could come after current
// ATN state, then we know we're missing a token; error recovery
// is free to conjure up and insert the missing token
Expand Down Expand Up @@ -499,14 +503,14 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
* @return The successfully matched [Token] instance if single-token
* deletion successfully recovers from the mismatched input, otherwise `null`
*/
protected fun singleTokenDeletion(recognizer: Parser): Token? {
protected open fun singleTokenDeletion(recognizer: Parser): Token? {
val nextTokenType = recognizer.tokenStream.LA(2)
val expecting = getExpectedTokens(recognizer)

if (expecting.contains(nextTokenType)) {
reportUnwantedToken(recognizer)

// simply delete extra token
// Simply delete extra token
recognizer.consume()

// We want to return the token we're actually matching
Expand Down Expand Up @@ -564,7 +568,7 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
}

return recognizer.tokenFactory.create(
Pair(current.tokenSource, current.tokenSource!!.inputStream),
Pair(current.tokenSource, current.inputStream),
expectedTokenType,
tokenText,
Token.DEFAULT_CHANNEL,
Expand Down Expand Up @@ -596,7 +600,7 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
return "<no token>"
}

var s: String? = getSymbolText(t)
var s = getSymbolText(t)

if (s == null) {
s = if (getSymbolType(t) == Token.EOF) {
Expand All @@ -609,8 +613,8 @@ public open class DefaultErrorStrategy : ANTLRErrorStrategy {
return escapeWSAndQuote(s)
}

protected open fun getSymbolText(symbol: Token): String =
symbol.text!!
protected open fun getSymbolText(symbol: Token): String? =
symbol.text

protected open fun getSymbolType(symbol: Token): Int =
symbol.type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public open class ParserInterpreter(
}

val errToken = tokenFactory.create(
Pair(tok!!.tokenSource!!, tok.tokenSource!!.inputStream!!),
Pair(tok!!.tokenSource, tok.inputStream),
expectedTokenType,
tok.text,
Token.DEFAULT_CHANNEL,
Expand All @@ -415,7 +415,7 @@ public open class ParserInterpreter(
// NoViableAlt
val tok = e.offendingToken
val errToken = tokenFactory.create(
Pair(tok!!.tokenSource!!, tok.tokenSource!!.inputStream!!),
Pair(tok!!.tokenSource, tok.inputStream),
Token.INVALID_TYPE,
tok.text,
Token.DEFAULT_CHANNEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.antlr.v4.kotlinruntime.tree.*
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass


/**
* A rule invocation record for parsing.
*
Expand Down Expand Up @@ -160,11 +159,14 @@ public open class ParserRuleContext : RuleContext {
* @since 4.7
*/
public fun <T : ParseTree> addAnyChild(t: T): T {
if (children == null) {
children = ArrayList()
var childrenTemp = children

if (childrenTemp == null) {
childrenTemp = ArrayList()
children = childrenTemp
}

children!!.add(t)
childrenTemp.add(t)
return t
}

Expand Down Expand Up @@ -291,44 +293,36 @@ public open class ParserRuleContext : RuleContext {

public fun getTokens(ttype: Int): List<TerminalNode> {
val tempChildren = children ?: return emptyList()
var tokens: MutableList<TerminalNode>? = null
val tokens = ArrayList<TerminalNode>()

for (o in tempChildren) {
if (o is TerminalNode) {
val symbol = o.symbol

if (symbol!!.type == ttype) {
if (tokens == null) {
tokens = ArrayList()
}

tokens.add(o)
}
}
}

return tokens ?: emptyList()
return tokens
}

public fun <T : ParserRuleContext> getRuleContext(ctxType: KClass<T>, i: Int): T? =
getChild(ctxType, i)

public fun <T : ParserRuleContext> getRuleContexts(ctxType: KClass<T>): List<T> {
val tempChildren = children ?: return emptyList()
var contexts: MutableList<T>? = null
val contexts = ArrayList<T>()

for (o in tempChildren) {
if (ctxType.isInstance(o)) {
if (contexts == null) {
contexts = ArrayList()
}

@Suppress("UNCHECKED_CAST")
contexts.add(o as T)
}
}

return contexts ?: emptyList()
return contexts
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,6 @@ public class DecisionInfo(public val decision: Int) {
append(LL_TotalLook)
append(", LL_ATNTransitions=")
append(LL_ATNTransitions)
append('}')
append("}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.antlr.v4.kotlinruntime.atn.SemanticContext.*
import org.antlr.v4.kotlinruntime.misc.MurmurHash
import kotlin.jvm.JvmStatic


/**
* A tree structure used to record the semantic context in which
* an ATN configuration is valid.
Expand Down Expand Up @@ -70,23 +69,19 @@ public abstract class SemanticContext {
}

private fun filterPrecedencePredicates(collection: MutableCollection<out SemanticContext>): List<PrecedencePredicate> {
var result: ArrayList<PrecedencePredicate>? = null
val result = ArrayList<PrecedencePredicate>()
val iterator = collection.iterator()

while (iterator.hasNext()) {
val context = iterator.next()

if (context is PrecedencePredicate) {
if (result == null) {
result = ArrayList()
}

result.add(context)
iterator.remove()
}
}

return result ?: emptyList()
return result
}
}

Expand Down Expand Up @@ -223,7 +218,7 @@ public abstract class SemanticContext {
return true
}

return this.precedence == other.precedence
return precedence == other.precedence
}

override fun toString(): String =
Expand Down Expand Up @@ -258,7 +253,7 @@ public abstract class SemanticContext {
get() = opnds.asList()

init {
val operands = HashSet<SemanticContext>()
val operands = LinkedHashSet<SemanticContext>()

if (a is AND) {
operands.addAll(a.opnds.asList())
Expand Down Expand Up @@ -351,7 +346,7 @@ public abstract class SemanticContext {
}

override fun toString(): String =
opnds.asList().joinToString("&&")
opnds.joinToString("&&")
}

/**
Expand All @@ -365,7 +360,7 @@ public abstract class SemanticContext {
get() = opnds.asList()

init {
val operands = HashSet<SemanticContext>()
val operands = LinkedHashSet<SemanticContext>()

if (a is OR) {
operands.addAll(a.opnds.asList())
Expand Down Expand Up @@ -458,6 +453,6 @@ public abstract class SemanticContext {
}

override fun toString(): String =
opnds.asList().joinToString("||")
opnds.joinToString("||")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public open class DFASerializer(
buf.append(getEdgeLabel(i))
buf.append("->")
buf.append(getStateString(t))
buf.append('\n')
buf.append("\n")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public open class Array2DHashSet<T>(
}

val buf = StringBuilder()
buf.append('{')
buf.append("{")

var first = true

Expand All @@ -440,10 +440,11 @@ public open class Array2DHashSet<T>(
}
}

buf.append('}')
buf.append("}")
return buf.toString()
}

@Suppress("DuplicatedCode")
public fun toTableString(): String {
val buf = StringBuilder()

Expand All @@ -453,7 +454,7 @@ public open class Array2DHashSet<T>(
continue
}

buf.append('[')
buf.append("[")
var first = true

for (o in bucket) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public open class FlexibleHashMap<K, V>(

var first = true
val buf = StringBuilder()
buf.append('{')
buf.append("{")

for (bucket in buckets) {
if (bucket == null) {
Expand All @@ -239,10 +239,11 @@ public open class FlexibleHashMap<K, V>(
}
}

buf.append('}')
buf.append("}")
return buf.toString()
}

@Suppress("DuplicatedCode")
public fun toTableString(): String {
val buf = StringBuilder()

Expand All @@ -252,7 +253,7 @@ public open class FlexibleHashMap<K, V>(
continue
}

buf.append('[')
buf.append("[")
var first = true

for (e in bucket) {
Expand Down
Loading

0 comments on commit 6f9a85d

Please sign in to comment.