Skip to content

arrays.equals and nullabe TerminalNodes #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion antlr-kotlin-examples-js/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.10'
ext.kotlin_version = '1.2.40'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import org.antlr.v4.kotlinruntime.ANTLRInputStream
import org.antlr.v4.kotlinruntime.CommonTokenStream
import org.antlr.v4.kotlinruntime.ast.Point
import org.antlr.v4.kotlinruntime.ast.pos
import org.antlr.v4.kotlinruntime.atn.EmptyPredictionContext
import org.antlr.v4.kotlinruntime.atn.PredictionContext
import org.antlr.v4.kotlinruntime.tree.TerminalNode
import kotlin.test.*
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.Test as test

class TestingParser {
Expand Down Expand Up @@ -66,12 +65,12 @@ class TestingParser {
val inputDecl = statement.findInputDeclaration()!!

val inputKw = inputDecl.INPUT()
assertEquals("input", inputKw.text)
assertEquals("input", inputKw?.text)

val type = inputDecl.findType()!!

val intKw = (type as MiniCalcParser.IntegerContext).INT()
assertEquals("Int", intKw.text)
assertEquals("Int", intKw?.text)

val id = inputDecl.ID()!!
assertEquals("width", id.text)
Expand Down
18 changes: 8 additions & 10 deletions antlr-kotlin-examples-jvm/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.10'
ext.kotlin_version = '1.2.40'

repositories {
mavenCentral()
Expand All @@ -21,28 +21,26 @@ repositories {
}

dependencies {
compile "com.strumenta.antlr-kotlin:antlr-kotlin-runtime-common:0.0.1"
compile "com.strumenta.antlr-kotlin:antlr-kotlin-runtime-jvm:0.0.1"
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
//testCompile 'junit:junit:4.12'
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
}

generateKotlinGrammarSource {
maxHeapSize = "64m"
arguments += ['-package', 'me.tomassetti.minicalc']
outputDirectory = new File("generated-src/antlr/".toString())
}
compileKotlin.dependsOn generateKotlinGrammarSource

sourceSets {
generated {
kotlin.srcDir 'generated-src/antlr/'
}
}
compileKotlin.source sourceSets.generated.kotlin, sourceSets.main.kotlin

clean{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing this?

delete "generated-src"
compileKotlin {
dependsOn generateKotlinGrammarSource
}

idea {
Expand Down
7 changes: 2 additions & 5 deletions antlr-kotlin-examples-jvm/src/main/kotlin/UsingLexer.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

import me.tomassetti.minicalc.MiniCalcLexer
import org.antlr.v4.kotlinruntime.ANTLRInputStream
import org.antlr.v4.kotlinruntime.CharStream
import org.antlr.v4.kotlinruntime.Token
import java.lang.RuntimeException
import me.tomassetti.minicalc.MiniCalcLexer

fun main(args: Array<String>) {

Expand All @@ -18,7 +15,7 @@ fun main(args: Array<String>) {

val input = ANTLRInputStream("1 + 2")
val lexer = MiniCalcLexer(input)
var token : Token? = null
var token: Token? = null
do {
token = lexer.nextToken()
println("TOKEN $token")
Expand Down
13 changes: 5 additions & 8 deletions antlr-kotlin-examples-jvm/src/main/kotlin/UsingParser.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import org.antlr.v4.kotlinruntime.ANTLRInputStream
import org.antlr.v4.kotlinruntime.CharStream
import org.antlr.v4.kotlinruntime.CommonTokenStream
import org.antlr.v4.kotlinruntime.Token
import java.lang.RuntimeException
import me.tomassetti.minicalc.MiniCalcLexer
import me.tomassetti.minicalc.MiniCalcParser
import org.antlr.v4.kotlinruntime.ANTLRInputStream
import org.antlr.v4.kotlinruntime.CommonTokenStream

fun main(args: Array<String>) {

Expand All @@ -22,10 +19,10 @@ fun main(args: Array<String>) {
var parser = MiniCalcParser(CommonTokenStream(lexer))
try {
val root = parser.miniCalcFile()
println("Parsed: ${root.javaClass}")
println("Parsed: ${root::class}")
println("Parsed: ${root.childCount}")
println("Parsed: ${root.children!![0].javaClass}")
} catch (e : Throwable) {
println("Parsed: ${root.children!![0]::class}")
} catch (e: Throwable) {
println("Error: $e")
}
}
16 changes: 8 additions & 8 deletions antlr-kotlin-examples-jvm/src/test/kotlin/MiniCalcParserTest.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import me.tomassetti.minicalc.MiniCalcLexer
import me.tomassetti.minicalc.MiniCalcParser
import org.antlr.v4.kotlinruntime.ANTLRInputStream
import org.antlr.v4.kotlinruntime.CommonTokenStream
import org.antlr.v4.kotlinruntime.ast.Point
import org.antlr.v4.kotlinruntime.ast.pos
import org.antlr.v4.kotlinruntime.atn.EmptyPredictionContext
import org.antlr.v4.kotlinruntime.atn.PredictionContext
import org.antlr.v4.kotlinruntime.tree.TerminalNode
import kotlin.test.*
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.Test as test
import me.tomassetti.minicalc.MiniCalcLexer
import me.tomassetti.minicalc.MiniCalcParser

class TestingParser {

@test fun simplestFileUsingHomogeneousAPI() {
@test
fun simplestFileUsingHomogeneousAPI() {
val input = ANTLRInputStream("input Int width\n")
val lexer = MiniCalcLexer(input)
var parser = MiniCalcParser(CommonTokenStream(lexer))
Expand Down Expand Up @@ -68,12 +68,12 @@ class TestingParser {
val inputDecl = statement.findInputDeclaration()!!

val inputKw = inputDecl.INPUT()
assertEquals("input", inputKw.text)
assertEquals("input", inputKw?.text)

val type = inputDecl.findType()!!

val intKw = (type as MiniCalcParser.IntegerContext).INT()
assertEquals("Int", intKw.text)
assertEquals("Int", intKw?.text)

val id = inputDecl.ID()!!
assertEquals("width", id.text)
Expand Down
2 changes: 1 addition & 1 deletion antlr-kotlin-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.31'
ext.kotlin_version = '1.2.40'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion antlr-kotlin-runtime-common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

buildscript {
ext.kotlin_version = '1.2.31'
ext.kotlin_version = '1.2.40'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,24 @@ expect object Arrays {

fun equals(a: Array<*>, b: Array<*>): Boolean

fun equals(a: IntArray, b: IntArray): Boolean

fun toString(a: Array<*>): String
}

expect class BitSet {
constructor()
fun Arrays.equals(a: Array<*>?, b: Array<*>?): Boolean {
if (a == null && b == null) return true
if (a == null && b != null || a != null && b == null) return false
return equals(a!!, b!!)
}

fun Arrays.equals(a: IntArray?, b: IntArray?): Boolean {
if (a == null && b == null) return true
if (a == null && b != null || a != null && b == null) return false
return equals(a!!, b!!)
}

expect class BitSet() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove the constructor from BitSet?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only moved the constructor so it is right after the classname 'BitSet()'. There is no difference semantically.

fun set(bitIndex: Int)
fun clear(bitIndex: Int)
fun get(bitIndex: Int): Boolean
Expand All @@ -41,8 +53,6 @@ expect class BitSet {
fun or(alts: BitSet)
}

//expect class ArrayList<T> : List<T>

expect object Collections {
fun unmodifiableList(asList: Collection<*>): List<*>
fun <T, U> unmodifiableMap(t: T): U
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

package org.antlr.v4.kotlinruntime.atn

import com.strumenta.kotlinmultiplatform.Arrays
import com.strumenta.kotlinmultiplatform.equals

class ArrayPredictionContext(
/** Parent can be null only if full ctx mode and we make an array
* from [.EMPTY] and non-empty. We merge [.EMPTY] by using null parent and
Expand Down Expand Up @@ -57,9 +60,8 @@ class ArrayPredictionContext(
return false // can't be same if hash is different
}

val a = o as ArrayPredictionContext?
TODO()
//return Arrays.equals(returnStates, a!!.returnStates) && Arrays.equals(parents, a.parents)
o as ArrayPredictionContext
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why casting it if we do not save it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the compiler can infer the type later. IntelliJ uses the same trick when generating equals(). Maybe it's a hack, I'm not sure.

return Arrays.equals(returnStates, o.returnStates) && Arrays.equals(parents, o.parents)
}
//
// override fun toString(): String {
Expand Down
2 changes: 1 addition & 1 deletion antlr-kotlin-runtime-js/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.31'
ext.kotlin_version = '1.2.40'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ actual object Arrays {
return true
}

actual fun equals(a: IntArray, b: IntArray): Boolean {
if (a === b) return true
if (a.size != b.size) {
return false
}
for (i in a.indices) {
if (a[i] != b[i]) {
return false
}
}
return true
}

actual fun toString(a: Array<*>): String {
return "[${a.joinToString(separator = ", ") { it?.toString() ?: "null"}}]"
}
Expand Down
2 changes: 1 addition & 1 deletion antlr-kotlin-runtime-jvm/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.31'
ext.kotlin_version = '1.2.40'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ actual object Arrays {
return java.util.Arrays.equals(a, b)
}

actual fun equals(a: IntArray, b: IntArray): Boolean {
return java.util.Arrays.equals(a, b)
}

actual fun toString(a: Array<*>): String {
return java.util.Arrays.toString(a)
}
Expand Down
2 changes: 1 addition & 1 deletion antlr-kotlin-target/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.31'
ext.kotlin_version = '1.2.40'

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,11 @@ RuleContextDecl(r) ::= "var <r.name>: <r.ctxName>? = null"
RuleContextListDecl(rdecl) ::= "var <rdecl.name> : MutableList\<<rdecl.ctxName>> = ArrayList\<<rdecl.ctxName>>()"

ContextTokenGetterDecl(t) ::=
"fun <t.name>() : TerminalNode = getToken(<parser.name>.Tokens.<t.name>.id, 0) as TerminalNode"
"fun <t.name>() : TerminalNode? = getToken(<parser.name>.Tokens.<t.name>.id, 0)"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably making these types nullable is causing the compilation issue we see on Travis

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I probably need to update some of the dependent code as well.

ContextTokenListGetterDecl(t) ::=
"fun <t.name>() : List\<TerminalNode> = getTokens(<parser.name>.Tokens.<t.name>.id) as TerminalNode"
"fun <t.name>() : List\<TerminalNode> = getTokens(<parser.name>.Tokens.<t.name>.id)"
ContextTokenListIndexedGetterDecl(t) ::= <<
fun <t.name>(int i) : TerminalNode = getToken(<parser.name>.Tokens.<t.name>.id, i) as TerminalNode
fun <t.name>(int i) : TerminalNode? = getToken(<parser.name>.Tokens.<t.name>.id, i)
>>
ContextRuleGetterDecl(r) ::= <<
fun find<r.name; format="cap">() : <r.ctxName>? = getRuleContext(solver.getType("<r.ctxName>"),0)
Expand Down