Skip to content

Commit

Permalink
refactor: use objects instead of managing singletons manually
Browse files Browse the repository at this point in the history
  • Loading branch information
lppedd authored and ftomassetti committed Aug 22, 2024
1 parent d2c17df commit 7951a3f
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public abstract class Parser(input: TokenStream) : Recognizer<Token, ParserATNSi
}
}

public class TrimToSizeListener : ParseTreeListener {
public companion object {
public val INSTANCE: TrimToSizeListener = TrimToSizeListener()
}

public object TrimToSizeListener : ParseTreeListener {
override fun enterEveryRule(ctx: ParserRuleContext) {
// Noop
}
Expand Down Expand Up @@ -143,16 +139,16 @@ public abstract class Parser(input: TokenStream) : Recognizer<Token, ParserATNSi
* This property is set to `false` by default for a newly constructed parser.
*/
public var trimParseTree: Boolean
get() = parseListeners.contains(TrimToSizeListener.INSTANCE)
get() = parseListeners.contains(TrimToSizeListener)
set(value) {
if (value) {
if (trimParseTree) {
return
}

addParseListener(TrimToSizeListener.INSTANCE)
addParseListener(TrimToSizeListener)
} else {
removeParseListener(TrimToSizeListener.INSTANCE)
removeParseListener(TrimToSizeListener)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public open class ATNConfig {
state: ATNState,
alt: Int,
context: PredictionContext?,
semanticContext: SemanticContext = SemanticContext.Empty.Instance,
semanticContext: SemanticContext = SemanticContext.Empty,
) {
this.state = state
this.alt = alt
Expand Down Expand Up @@ -181,7 +181,7 @@ public open class ATNConfig {
buf.append("]")
}

if (semanticContext !== SemanticContext.Empty.Instance) {
if (semanticContext !== SemanticContext.Empty) {
buf.append(",")
buf.append(semanticContext)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ public open class ATNConfigSet(public val fullCtx: Boolean = true) : MutableSet<
* the number of objects associated with ATNConfigs. The other solution is to
* use a hash table that lets us specify the equals/hashcode operation.
*/
public class ConfigHashSet : AbstractConfigHashSet(ConfigEqualityComparator.INSTANCE)

public class ConfigEqualityComparator private constructor() : AbstractEqualityComparator<ATNConfig>() {
public companion object {
public val INSTANCE: ConfigEqualityComparator = ConfigEqualityComparator()
}
public class ConfigHashSet : AbstractConfigHashSet(ConfigEqualityComparator)

public object ConfigEqualityComparator : AbstractEqualityComparator<ATNConfig>() {
override fun hashCode(obj: ATNConfig): Int {
var hashCode = 7
hashCode = 31 * hashCode + obj.state.stateNumber
Expand All @@ -50,9 +46,9 @@ public open class ATNConfigSet(public val fullCtx: Boolean = true) : MutableSet<
return false
}

return a.state.stateNumber == b.state.stateNumber &&
a.alt == b.alt &&
a.semanticContext == b.semanticContext
return a.state.stateNumber == b.state.stateNumber
&& a.alt == b.alt
&& a.semanticContext == b.semanticContext
}
}

Expand Down Expand Up @@ -119,7 +115,7 @@ public open class ATNConfigSet(public val fullCtx: Boolean = true) : MutableSet<
val preds = ArrayList<SemanticContext>()

for (c in configs) {
if (c.semanticContext !== SemanticContext.Empty.Instance) {
if (c.semanticContext !== SemanticContext.Empty) {
preds.add(c.semanticContext)
}
}
Expand Down Expand Up @@ -171,7 +167,7 @@ public open class ATNConfigSet(public val fullCtx: Boolean = true) : MutableSet<
throw IllegalStateException("This set is readonly")
}

if (config.semanticContext !== SemanticContext.Empty.Instance) {
if (config.semanticContext !== SemanticContext.Empty) {
hasSemanticContext = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ public open class ATNDeserializer(private val deserializationOptions: ATNDeseria
LexerActionType.CHANNEL -> LexerChannelAction(data1)
LexerActionType.CUSTOM -> LexerCustomAction(data1, data2)
LexerActionType.MODE -> LexerModeAction(data1)
LexerActionType.MORE -> LexerMoreAction.INSTANCE
LexerActionType.POP_MODE -> LexerPopModeAction.INSTANCE
LexerActionType.MORE -> LexerMoreAction
LexerActionType.POP_MODE -> LexerPopModeAction
LexerActionType.PUSH_MODE -> LexerPushModeAction(data1)
LexerActionType.SKIP -> LexerSkipAction.INSTANCE
LexerActionType.SKIP -> LexerSkipAction
LexerActionType.TYPE -> LexerTypeAction(data1)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
// Copyright 2017-present Strumenta and contributors, licensed under Apache 2.0.
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.

package org.antlr.v4.kotlinruntime.atn

import kotlin.jvm.JvmStatic

@Suppress("EqualsOrHashCode")
public class EmptyPredictionContext : SingletonPredictionContext(null, EMPTY_RETURN_STATE) {
public companion object {
/**
* Represents `$` in local context prediction, which means wildcard.
* `*+x = *`.
*/
@JvmStatic
public val Instance: EmptyPredictionContext = EmptyPredictionContext()
}

public object EmptyPredictionContext : SingletonPredictionContext(null, EMPTY_RETURN_STATE) {
override val isEmpty: Boolean =
true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public open class LL1Analyzer(public val atn: ATN) {
_LOOK(
s = s.transition(alt).target,
stopState = null,
ctx = EmptyPredictionContext.Instance,
ctx = EmptyPredictionContext,
look = look[alt]!!,
lookBusy = lookBusy,
calledRuleStack = BitSet(),
Expand Down Expand Up @@ -192,7 +192,7 @@ public open class LL1Analyzer(public val atn: ATN) {
return
}

if (ctx !== EmptyPredictionContext.Instance) {
if (ctx !== EmptyPredictionContext) {
// Run through all possible stack tops in ctx
val removed = calledRuleStack.get(s.ruleIndex)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class LexerATNConfig : ATNConfig {
state: ATNState,
alt: Int,
context: PredictionContext,
) : super(state, alt, context, SemanticContext.Empty.Instance) {
) : super(state, alt, context, SemanticContext.Empty) {
passedThroughNonGreedyDecision = false
lexerActionExecutor = null
}
Expand All @@ -30,7 +30,7 @@ public class LexerATNConfig : ATNConfig {
alt: Int,
context: PredictionContext,
lexerActionExecutor: LexerActionExecutor,
) : super(state, alt, context, SemanticContext.Empty.Instance) {
) : super(state, alt, context, SemanticContext.Empty) {
this.lexerActionExecutor = lexerActionExecutor
passedThroughNonGreedyDecision = false
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public class LexerATNConfig : ATNConfig {
return false
}

if (!ObjectEqualityComparator.INSTANCE.equals(lexerActionExecutor, other.lexerActionExecutor)) {
if (!ObjectEqualityComparator.equals(lexerActionExecutor, other.lexerActionExecutor)) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public open class LexerATNSimulator(
}

protected open fun computeStartState(input: CharStream, p: ATNState): ATNConfigSet {
val initialContext = EmptyPredictionContext.Instance
val initialContext = EmptyPredictionContext
val configs = OrderedATNConfigSet()

for (i in 0..<p.numberOfTransitions) {
Expand Down Expand Up @@ -460,7 +460,7 @@ public open class LexerATNSimulator(
return true
}

configs.add(LexerATNConfig(config, config.state, EmptyPredictionContext.Instance))
configs.add(LexerATNConfig(config, config.state, EmptyPredictionContext))
tempCurrentAltReachedAcceptState = true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
// Copyright 2017-present Strumenta and contributors, licensed under Apache 2.0.
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.

package org.antlr.v4.kotlinruntime.atn

import org.antlr.v4.kotlinruntime.Lexer
import org.antlr.v4.kotlinruntime.atn.LexerMoreAction.Companion.INSTANCE
import org.antlr.v4.kotlinruntime.misc.MurmurHash

/**
* Implements the `more` lexer action by calling [Lexer.more].
*
* The `more` command does not have any parameters, so this action is
* implemented as a singleton instance exposed by [INSTANCE].
* The `more` command does not have any parameters,
* so this action is implemented as an object.
*
* @author Sam Harwell
* @since 4.2
*/
public class LexerMoreAction private constructor() : LexerAction {
public companion object {
/**
* Provides a singleton instance of this parameterless lexer action.
*/
public val INSTANCE: LexerMoreAction = LexerMoreAction()
}

public object LexerMoreAction : LexerAction {
/**
* Returns [LexerActionType.MORE].
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
// Copyright 2017-present Strumenta and contributors, licensed under Apache 2.0.
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.

package org.antlr.v4.kotlinruntime.atn

import org.antlr.v4.kotlinruntime.Lexer
import org.antlr.v4.kotlinruntime.atn.LexerPopModeAction.Companion.INSTANCE
import org.antlr.v4.kotlinruntime.misc.MurmurHash

/**
* Implements the `popMode` lexer action by calling [Lexer.popMode].
*
* The `popMode` command does not have any parameters, so this action is
* implemented as a singleton instance exposed by [INSTANCE].
* The `popMode` command does not have any parameters,
* so this action is implemented as an object.
*
* @author Sam Harwell
* @since 4.2
*/
public class LexerPopModeAction private constructor() : LexerAction {
public companion object {
/**
* Provides a singleton instance of this parameterless lexer action.
*/
public val INSTANCE: LexerPopModeAction = LexerPopModeAction()
}

public object LexerPopModeAction : LexerAction {
/**
* Returns [LexerActionType.POP_MODE].
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
// Copyright 2017-present Strumenta and contributors, licensed under Apache 2.0.
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.

package org.antlr.v4.kotlinruntime.atn

import org.antlr.v4.kotlinruntime.Lexer
import org.antlr.v4.kotlinruntime.atn.LexerSkipAction.Companion.INSTANCE
import org.antlr.v4.kotlinruntime.misc.MurmurHash

/**
* Implements the `skip` lexer action by calling [Lexer.skip].
*
* The `skip` command does not have any parameters, so this action is
* implemented as a singleton instance exposed by [INSTANCE].
* The `skip` command does not have any parameters,
* so this action is implemented as an object.
*
* @author Sam Harwell
* @since 4.2
*/
public class LexerSkipAction private constructor() : LexerAction {
public companion object {
/**
* Provides a singleton instance of this parameterless lexer action.
*/
public val INSTANCE: LexerSkipAction = LexerSkipAction()
}

public object LexerSkipAction : LexerAction {
/**
* Returns [LexerActionType.SKIP].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.antlr.v4.kotlinruntime.misc.ObjectEqualityComparator
* @author Sam Harwell
*/
public class OrderedATNConfigSet : ATNConfigSet() {
public class LexerConfigHashSet : AbstractConfigHashSet(ObjectEqualityComparator.INSTANCE)
public class LexerConfigHashSet : AbstractConfigHashSet(ObjectEqualityComparator)

init {
configLookup = LexerConfigHashSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,8 +1210,8 @@ public open class ParserATNSimulator(

for (i in 1..nAlts) {
if (altToPred[i] == null) {
altToPred[i] = SemanticContext.Empty.Instance
} else if (altToPred[i] !== SemanticContext.Empty.Instance) {
altToPred[i] = SemanticContext.Empty
} else if (altToPred[i] !== SemanticContext.Empty) {
nPredAlts++
}
}
Expand Down Expand Up @@ -1249,7 +1249,7 @@ public open class ParserATNSimulator(
pairs.add(DFAState.PredPrediction(pred, i))
}

if (pred !== SemanticContext.Empty.Instance) {
if (pred !== SemanticContext.Empty) {
containsPredicate = true
}
}
Expand Down Expand Up @@ -1362,7 +1362,7 @@ public open class ParserATNSimulator(
val failed = ATNConfigSet(configs.fullCtx)

for (c in configs) {
if (c.semanticContext !== SemanticContext.Empty.Instance) {
if (c.semanticContext !== SemanticContext.Empty) {
val predicateEvaluationResult = evalSemanticContext(c.semanticContext, outerContext, c.alt, configs.fullCtx)

if (predicateEvaluationResult) {
Expand Down Expand Up @@ -1396,7 +1396,7 @@ public open class ParserATNSimulator(
val predictions = BitSet()

for (pair in predPredictions) {
if (pair.pred === SemanticContext.Empty.Instance) {
if (pair.pred === SemanticContext.Empty) {
predictions.set(pair.alt)

if (!complete) {
Expand Down Expand Up @@ -1511,7 +1511,7 @@ public open class ParserATNSimulator(
for (i in 0..<config.context!!.size()) {
if (config.context!!.getReturnState(i) == PredictionContext.EMPTY_RETURN_STATE) {
if (fullCtx) {
configs.add(ATNConfig(config, config.state, EmptyPredictionContext.Instance), mergeCache)
configs.add(ATNConfig(config, config.state, EmptyPredictionContext), mergeCache)
continue
}

Expand Down
Loading

0 comments on commit 7951a3f

Please sign in to comment.