Skip to content

Commit

Permalink
refactor: update runtime up to commit a400163f23b29eb0b164ebc933c9201…
Browse files Browse the repository at this point in the history
…f7641a499

See antlr/antlr4@a400163
  • Loading branch information
lppedd committed Dec 5, 2023
1 parent 851c7a3 commit 289ad45
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 73 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# ANTLR Kotlin

[![Build Status](https://travis-ci.org/Strumenta/antlr-kotlin.svg?branch=master)](https://travis-ci.org/Strumenta/antlr-kotlin) [![Release](https://jitpack.io/v/com.strumenta/antlr-kotlin.svg)](https://jitpack.io/#com.strumenta/antlr-kotlin)
Up-to-date with mainstream commit: a400163f23b29eb0b164ebc933c9201f7641a499

Files to re-verify and sync:

- Parser
- ATNDeserializer
- ATNSerializer
- IntegerList
- InterpreterDataReader
- tree/xpath/**

---

[![License Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)

This project contains everything is needed to support Kotlin as a target for ANTLR.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ open class BufferedTokenStream(
override val text: String
get() = getText(Interval.of(0, size() - 1))

init {
if (tokenSource == null) {
throw NullPointerException("tokenSource cannot be null")
}
}

// override fun getTokenSource(): TokenSource {
// return tokenSource
// }
Expand Down Expand Up @@ -421,7 +415,7 @@ open class BufferedTokenStream(
val start = interval.a
var stop = interval.b
if (start < 0 || stop < 0) return ""
fill()
sync(stop)
if (stop >= tokens.size) stop = tokens.size - 1

val buf = StringBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ object RuntimeMetaData {
runtimeConflictsWithCompileTimeTool = runtimeVersion != compileTimeVersion && getMajorMinorVersion(runtimeVersion) != getMajorMinorVersion(compileTimeVersion)

if (runtimeConflictsWithGeneratingTool) {
System.err.println("ANTLR Tool version $generatingToolVersion used for code generation does not match the current runtime version $runtimeVersion")
System.err.println("ANTLR Tool version $generatingToolVersion used for code generation does not match the current runtime version $runtimeVersion\n")
}
if (runtimeConflictsWithCompileTimeTool) {
System.err.println("ANTLR Runtime version $compileTimeVersion used for parser compilation does not match the current runtime version $runtimeVersion")
System.err.println("ANTLR Runtime version $compileTimeVersion used for parser compilation does not match the current runtime version $runtimeVersion\n")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,18 @@ class VocabularyImpl
* @see .getSymbolicName
* @see .getDisplayName
*/
constructor(literalNames: Array<String?>?, symbolicNames: Array<String?>?, displayNames: Array<String?>? = null) : Vocabulary {


private val literalNames: Array<String?>

private val symbolicNames: Array<String?>

private val displayNames: Array<String?>

override val maxTokenType: Int

init {
this.literalNames = literalNames ?: EMPTY_NAMES
this.symbolicNames = symbolicNames ?: EMPTY_NAMES
this.displayNames = displayNames ?: EMPTY_NAMES
// See note here on -1 part: https://github.com/antlr/antlr4/pull/1146
this.maxTokenType = Math.max(this.displayNames.size,
Math.max(this.literalNames.size, this.symbolicNames.size)) - 1
}
constructor(
literalNames: Array<String?>?,
symbolicNames: Array<String?>?,
displayNames: Array<String?>? = null,
) : Vocabulary {
val literalNames: Array<String?> = literalNames ?: EMPTY_NAMES
val symbolicNames: Array<String?> = symbolicNames ?: EMPTY_NAMES
val displayNames: Array<String?> = displayNames ?: EMPTY_NAMES

// See note here on -1 part: https://github.com/antlr/antlr4/pull/1146
override val maxTokenType: Int =
Math.max(this.displayNames.size, Math.max(this.literalNames.size, this.symbolicNames.size)) - 1

override fun getLiteralName(tokenType: Int): String? {
return if (tokenType >= 0 && tokenType < literalNames.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class ATNDeserializer constructor(deserializationOptions: ATNDeserializationOpti
} else data
}

fun deserialize(data: CharArray): ATN = deserialize(decodeIntsEncodedAs16BitWords(data))
fun deserialize(data: CharArray): ATN =
deserialize(decodeIntsEncodedAs16BitWords(data))

fun deserialize(data: IntArray): ATN {
var p = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,25 @@ import org.antlr.v4.kotlinruntime.misc.IntervalSet
* Utility class to create [AtomTransition], [RangeTransition],
* and [SetTransition] appropriately based on the range of the input.
*
* To keep the serialized ATN size small, we only inline atom and
* range transitions for Unicode code points <= U+FFFF.
*
* Whenever we encounter a Unicode code point > U+FFFF, we represent that
* as a set transition (even if it is logically an atom or a range).
* Previously, we distinguished between atom and range transitions for
* Unicode code points <= U+FFFF and those above. We used a set
* transition for a Unicode code point > U+FFFF. Now that we can serialize
* 32-bit int/chars in the ATN serialization, this is no longer necessary.
*/
object CodePointTransitions {
/**
* If `codePoint` is <= U+FFFF, returns a new [AtomTransition].
* Otherwise, returns a new [SetTransition].
* Return new [AtomTransition].
*/
fun createWithCodePoint(target: ATNState, codePoint: Int): Transition {
return if (Char.isSupplementaryCodePoint(codePoint)) {
SetTransition(target, IntervalSet.of(codePoint))
} else {
AtomTransition(target, codePoint)
}
return createWithCodePointRange(target, codePoint, codePoint)
}

/**
* If `codePointFrom` and `codePointTo` are both
* <= U+FFFF, returns a new [RangeTransition].
* Otherwise, returns a new [SetTransition].
* Return new [AtomTransition] if range represents one atom, else [SetTransition].
*/
fun createWithCodePointRange(
target: ATNState,
codePointFrom: Int,
codePointTo: Int): Transition {
return if (Char.isSupplementaryCodePoint(codePointFrom) || Char.isSupplementaryCodePoint(codePointTo)) {
SetTransition(target, IntervalSet.of(codePointFrom, codePointTo))
fun createWithCodePointRange(target: ATNState, codePointFrom: Int, codePointTo: Int): Transition {
return if (codePointFrom == codePointTo) {
AtomTransition(target, codePointFrom)
} else {
RangeTransition(target, codePointFrom, codePointTo)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ open class Array2DHashSet<T> constructor(comparator: AbstractEqualityComparator<
/** How many elements in set */
protected var n = 0

protected var threshold = Math.floor(INITAL_CAPACITY * LOAD_FACTOR).toInt() // when to expand

protected var currentPrime = 1 // jump by 4 primes each expand or whatever
protected var initialBucketCapacity = INITAL_BUCKET_CAPACITY

/** when to expand */
protected var threshold: Int = 0
protected val initialCapacity: Int
protected val initialBucketCapacity: Int

init {
var comparator = comparator
Expand All @@ -43,8 +45,10 @@ open class Array2DHashSet<T> constructor(comparator: AbstractEqualityComparator<
}

this.comparator = comparator
this.buckets = createBuckets(initialCapacity) as Array<Array<T>>
this.initialCapacity = initialCapacity
this.initialBucketCapacity = initialBucketCapacity
this.buckets = createBuckets(initialCapacity) as Array<Array<T>>
this.threshold = Math.floor(initialCapacity * LOAD_FACTOR).toInt()
}

/**
Expand Down Expand Up @@ -366,9 +370,9 @@ open class Array2DHashSet<T> constructor(comparator: AbstractEqualityComparator<


override fun clear() {
buckets = createBuckets(INITAL_CAPACITY) as Array<Array<T>>
n = 0
threshold = Math.floor(INITAL_CAPACITY * LOAD_FACTOR).toInt()
buckets = createBuckets(initialCapacity) as Array<Array<T>>
threshold = Math.floor(initialCapacity * LOAD_FACTOR).toInt()
}

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.antlr.v4.kotlinruntime.misc

import com.strumenta.kotlinmultiplatform.Math

/** A limited map (many unsupported operations) that lets me use
* varying hashCode/equals.
*/
Expand Down Expand Up @@ -36,10 +38,12 @@ open class FlexibleHashMap<K, V> constructor(

override val size: Int = n

protected var threshold = (INITAL_CAPACITY * LOAD_FACTOR).toInt() // when to expand

protected var currentPrime = 1 // jump by 4 primes each expand or whatever
protected var initialBucketCapacity = INITAL_BUCKET_CAPACITY

/** when to expand */
protected var threshold: Int = 0
protected val initialCapacity: Int
protected val initialBucketCapacity: Int

class Entry<K, V>(val key: K, var value: V) {

Expand All @@ -55,8 +59,10 @@ open class FlexibleHashMap<K, V> constructor(
}

this.comparator = comparator
this.buckets = createEntryListArray(initialBucketCapacity)
this.initialCapacity = initialCapacity
this.initialBucketCapacity = initialBucketCapacity
this.threshold = Math.floor(initialCapacity * LOAD_FACTOR).toInt()
this.buckets = createEntryListArray(initialBucketCapacity)
}

protected fun getBucket(key: K): Int {
Expand Down Expand Up @@ -185,8 +191,9 @@ open class FlexibleHashMap<K, V> constructor(
}

override fun clear() {
buckets = createEntryListArray(INITAL_CAPACITY)
buckets = createEntryListArray(initialCapacity)
n = 0
threshold = Math.floor(initialCapacity * LOAD_FACTOR).toInt()
}

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,7 @@ class Interval(var a: Int, var b: Int) {

val INVALID = Interval(-1, -2)

internal var cache = arrayOfNulls<Interval>(INTERVAL_POOL_MAX_VALUE + 1)

var creates = 0
var misses = 0
var hits = 0
var outOfRange = 0
internal val cache = arrayOfNulls<Interval>(INTERVAL_POOL_MAX_VALUE + 1)

/** Interval objects are used readonly so share all with the
* same single value a==b up to some max size. Use an array as a perfect hash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import org.antlr.v4.kotlinruntime.ParserRuleContext
import org.antlr.v4.kotlinruntime.RuleContext

open class ParseTreeWalker {
/**
* Performs a walk on the given parse tree starting at the root and going down recursively
* with depth-first search. On each node, [ParseTreeWalker.enterRule] is called before
* recursively walking down into child nodes, then [ParseTreeWalker.exitRule] is called
* after the recursive call to wind up.
*
* @param listener The listener used by the walker to process grammar rules
* @param t The parse tree to be walked on
*/
open fun walk(listener: ParseTreeListener, t: ParseTree) {
if (t is ErrorNode) {
listener.visitErrorNode(t)
Expand All @@ -28,17 +37,25 @@ open class ParseTreeWalker {
}

/**
* The discovery of a rule node, involves sending two events: the generic
* [ParseTreeListener.enterEveryRule] and a
* [RuleContext]-specific event. First we trigger the generic and then
* the rule specific. We to them in reverse order upon finishing the node.
* Enters a grammar rule by first triggering the generic event [ParseTreeListener.enterEveryRule]
* then by triggering the event specific to the given parse tree node.
*
* @param listener The listener responding to the trigger events
* @param r The grammar rule containing the rule context
*/
protected fun enterRule(listener: ParseTreeListener, r: RuleNode) {
val ctx = r.ruleContext as ParserRuleContext
listener.enterEveryRule(ctx)
ctx.enterRule(listener)
}

/**
* Exits a grammar rule by first triggering the event specific to the given parse tree node
* then by triggering the generic event [ParseTreeListener.exitEveryRule].
*
* @param listener The listener responding to the trigger events
* @param r The grammar rule containing the rule context
*/
protected fun exitRule(listener: ParseTreeListener, r: RuleNode) {
val ctx = r.ruleContext as ParserRuleContext
ctx.exitRule(listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ interface Tree {
// this.parent = value
// }

// TODO(Edoardo): rename it back to getParent
fun readParent(): Tree?

/**
* This method returns whatever object represents the data at this note. For
* This method returns whatever object represents the data at this node. For
* example, for parse trees, the payload can be a [Token] representing
* a leaf node or a [RuleContext] object representing a rule
* invocation. For abstract syntax trees (ASTs), this is a [Token]
Expand Down

0 comments on commit 289ad45

Please sign in to comment.