Skip to content
This repository was archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
Make Lixy a multiplatform project (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
utybo committed Apr 21, 2020
1 parent 81d015d commit a5f9ed3
Show file tree
Hide file tree
Showing 39 changed files with 204 additions and 129 deletions.
63 changes: 63 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
id "org.jetbrains.kotlin.multiplatform" version "1.3.72"
//id "org.jetbrains.dokka" version "0.10.0"
}

repositories {
mavenCentral()
}

kotlin {
jvm()
js {
browser {
}
nodejs {
}
}

sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
}
}

commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}

jvmMain {
dependencies {
implementation kotlin('stdlib-jdk8')
implementation "org.jetbrains:annotations:18.0.0"
}
}

jvmTest {
dependencies {
implementation kotlin('test-junit')
}
}

nonJvmMain {
dependsOn commonMain
}

jsMain {
dependencies {
implementation kotlin('stdlib-js')
}
dependsOn nonJvmMain
}

jsTest {
dependencies {
implementation kotlin('test-js')
}
}
}
}
58 changes: 0 additions & 58 deletions build.gradle.kts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package guru.zoroark.lixy

import guru.zoroark.lixy.matchers.*
import guru.zoroark.lixy.matchers.RegexPatternRecognizer
import org.intellij.lang.annotations.Language
import java.util.regex.Pattern

/**
* This classed is used to build a lexer state ([LixyState]) using a DSL, and is
Expand Down Expand Up @@ -50,19 +47,6 @@ class LixyDslStateEnvironment : Buildable<LixyState> {
tokenMatchers += this.selfBuildable()
}

/**
* Create a recognizer that recognizes the given regular expression. Use
* this before [isToken] to create a matcher that matches against a regular
* expression.
*
* @param regex The regular expression to use in the recognizer
* @see isToken
*/
fun matches(@Language("RegExp") regex: String): LixyTokenRecognizer =
RegexPatternRecognizer(
Pattern.compile(regex)
)

/**
* Anything that matches the given recognizer (or pseudo-recognizer) exactly
* will be ignored when encountered. This would be equivalent to a `isToken`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package guru.zoroark.lixy

internal class OffsetCharSequence(val charSequence: CharSequence, val offsetBy: Int) : CharSequence {
internal class OffsetCharSequence(
val charSequence: CharSequence,
val offsetBy: Int
) : CharSequence {
init {
if (offsetBy > charSequence.length)
error("Invalid offset, exceeds original sequence's length")
}

override val length: Int
get() = charSequence.length - offsetBy

override fun get(index: Int): Char = charSequence[index + offsetBy]
override fun get(index: Int): Char =
if (index + offsetBy >= charSequence.length)
throw IndexOutOfBoundsException("Index ${index + offsetBy} is out of bounds (max. ${charSequence.length}")
else
charSequence[index + offsetBy]

override fun subSequence(startIndex: Int, endIndex: Int): CharSequence =
charSequence.subSequence(startIndex + offsetBy, endIndex + offsetBy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ fun toRecognizer(x: Any): LixyTokenRecognizer =
is String -> LixyStringTokenRecognizer(x)
is CharRange -> LixyCharRangeTokenRecognizer(x)
else -> throw LixyException(
"Unable to convert ${x::class.java.simpleName} to a recognizer."
"Unable to convert ${x::class.simpleName} to a recognizer."
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package guru.zoroark.lixy.matchers

import guru.zoroark.lixy.LixyDslStateEnvironment

/**
* Create a recognizer that recognizes the given regular expression. Use
* this before isToken to create a matcher that matches against a regular
* expression.
*
* @param pattern The regular expression to use in the recognizer
*/
expect fun LixyDslStateEnvironment.matches(pattern: String): LixyTokenRecognizer
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package guru.zoroark.lixy

import guru.zoroark.lixy.matchers.anyOf
import guru.zoroark.lixy.matchers.matches
import kotlin.test.*

class IgnoreMatcherTest {
@Test
fun `Ignore matcher on string in single state`() {
fun ignore_matcher_on_string_in_single_state() {
val tone = tokenType()
val ttwo = tokenType()
val lexer = lixy {
Expand All @@ -31,7 +32,7 @@ class IgnoreMatcherTest {
}

@Test
fun `Ignore matcher on string in multiple states`() {
fun ignore_matcher_on_string_in_multiple_states() {
val tkey = tokenType()
val tvalue = tokenType()
val svalue = stateLabel()
Expand Down Expand Up @@ -63,7 +64,7 @@ class IgnoreMatcherTest {
}

@Test
fun `Ignore matcher on any matcher in single state`() {
fun ignore_matcher_on_any_matcher_in_single_state() {
val tspace = tokenType()
val tword = tokenType()
val lexer = lixy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.*

class LixyStateTest {
@Test
fun `Lixy supports constructing one default state`() {
fun lixy_supports_constructing_one_default_state() {
val dottoken = tokenType()
val lixy = lixy {
default state {
Expand All @@ -26,7 +26,7 @@ class LixyStateTest {
}

@Test
fun `Lixy fails to construct unlabeled then labeled default`() {
fun lixy_fails_to_construct_unlabeled_then_labeled_default() {
val ttype = tokenType()
assertFailsWith<LixyException> {
lixy {
Expand All @@ -41,7 +41,7 @@ class LixyStateTest {
}

@Test
fun `Lixy fails to construct labeled default then unlabeled`() {
fun lixy_fails_to_construct_labeled_default_then_unlabeled() {
val ttype = tokenType()
assertFailsWith<LixyException> {
lixy {
Expand All @@ -56,7 +56,7 @@ class LixyStateTest {
}

@Test
fun `Lixy fails to construct multiple unlabeled states`() {
fun lixy_fails_to_construct_multiple_unlabeled_states() {
val ttype = tokenType()
assertFailsWith<LixyException> {
lixy {
Expand All @@ -71,7 +71,7 @@ class LixyStateTest {
}

@Test
fun `Lixy fails to construct multiple default states`() {
fun lixy_fails_to_construct_multiple_default_states() {
val ttype = tokenType()
assertFailsWith<LixyException> {
lixy {
Expand All @@ -86,7 +86,7 @@ class LixyStateTest {
}

@Test
fun `Lixy cannot create two states with the same label`() {
fun lixy_cannot_create_two_states_with_the_same_label() {
val a = stateLabel()
val ta = tokenType()
val tb = tokenType()
Expand All @@ -103,11 +103,11 @@ class LixyStateTest {
}
}
}
assert(exc.message!!.contains("two states with the same label"))
assertTrue(exc.message!!.contains("two states with the same label"))
}

@Test
fun `Lixy successfully constructs multiple states and starts on default`() {
fun lixy_successfully_constructs_multiple_states_and_starts_on_default() {
val one = tokenType()
val two = tokenType()
val other = stateLabel()
Expand Down Expand Up @@ -149,7 +149,7 @@ class LixyStateTest {
}

@Test
fun `Lixy supports switching from state to state`() {
fun lixy_supports_switching_from_state_to_state() {
val one = tokenType()
val two = tokenType()
val other = stateLabel()
Expand All @@ -175,7 +175,7 @@ class LixyStateTest {
}

@Test
fun `Lixy supports redirecting the default state`() {
fun lixy_supports_redirecting_the_default_state() {
val a = stateLabel()
val b = stateLabel()
val ta = tokenType()
Expand Down Expand Up @@ -205,7 +205,7 @@ class LixyStateTest {
}

@Test
fun `Lixy cannot redefine default after redirecting default`() {
fun lixy_cannot_redefine_default_after_redirecting_default() {
val a = stateLabel()
val ta = tokenType()
val b = stateLabel()
Expand All @@ -221,11 +221,11 @@ class LixyStateTest {
}
}
}
assert(exc.message!!.contains("already defined"))
assertTrue(exc.message!!.contains("already defined"))
}

@Test
fun `Lixy cannot redefine default state in single state lexer kind`() {
fun lixy_cannot_redefine_default_state_in_single_state_lexer_kind() {
val a = stateLabel()
val ta = tokenType()
val exc = assertFailsWith<LixyException> {
Expand All @@ -236,7 +236,7 @@ class LixyStateTest {
default state a
}
}
assert(exc.message!!.contains("Cannot redefine"))
assert(exc.message!!.contains("single-state"))
assertTrue(exc.message!!.contains("Cannot redefine"))
assertTrue(exc.message!!.contains("single-state"))
}
}
Loading

0 comments on commit a5f9ed3

Please sign in to comment.