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

Commit

Permalink
Properly add the @language annotation (thanks Him188!)
Browse files Browse the repository at this point in the history
  • Loading branch information
utybo committed May 3, 2020
1 parent 5883e6e commit 8c2dd9d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Usually, the matching process is separated in two steps:
information on what its associated token type is
* Match the result of the recognizer with something, usually with a token type.

That is a fairly oversimplified view of the process of making a lexer in Lixy however.

### My first lexer

```kotlin
Expand Down
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ kotlin {
}

sourceSets {
all {
languageSettings {
useExperimentalAnnotation("kotlin.RequiresOptIn")
}
}
commonMain {
dependencies {
implementation kotlin('stdlib-common')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guru.zoroark.lixy.matchers

import guru.zoroark.lixy.LixyDslStateEnvironment
import guru.zoroark.lixy.utils.Language

/**
* Create a recognizer that recognizes the given regular expression. Use
Expand All @@ -9,4 +10,4 @@ import guru.zoroark.lixy.LixyDslStateEnvironment
*
* @param pattern The regular expression to use in the recognizer
*/
expect fun LixyDslStateEnvironment.matches(pattern: String): LixyTokenRecognizer
expect fun LixyDslStateEnvironment.matches(@Language("RegExp", "", "") pattern: String): LixyTokenRecognizer
20 changes: 20 additions & 0 deletions src/commonMain/kotlin/guru/zoroark/lixy/utils/Language.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package guru.zoroark.lixy.utils

import kotlin.annotation.AnnotationTarget.*


/**
* Mapping to `org.intellij.lang.annotations.Language` on JVM, but no actual
* implementations on other platforms.
*
* Credits to @Him188 on GitHub, thanks!
*/
@Retention(AnnotationRetention.BINARY)
@Target(FUNCTION, FIELD, VALUE_PARAMETER, LOCAL_VARIABLE, ANNOTATION_CLASS)
@OptIn(ExperimentalMultiplatform::class)
@OptionalExpectation
expect annotation class Language(
val value: String,
val prefix: String, // default values aren't allowed yet
val suffix: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ internal class RegexPatternRecognizer(private val pattern: Pattern) :
}
}

actual fun LixyDslStateEnvironment.matches(
@Language("RegExp") pattern: String
): LixyTokenRecognizer = RegexPatternRecognizer(Pattern.compile(pattern))
actual fun LixyDslStateEnvironment.matches(pattern: String): LixyTokenRecognizer =
RegexPatternRecognizer(Pattern.compile(pattern))
5 changes: 5 additions & 0 deletions src/jvmMain/kotlin/guru/zoroark/lixy/utils/Language.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package guru.zoroark.lixy.utils

import org.intellij.lang.annotations.Language

actual typealias Language = Language
2 changes: 2 additions & 0 deletions src/nonJvmMain/kotlin/NonJvmRegexRecognizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import guru.zoroark.lixy.LixyDslStateEnvironment
* This version is less optimized than the JVM version due to limitations on the
* Regex object, which does not have all of the features of the JVM's Matcher and
* Pattern objects.
*
* Thanks to @Him188!
*/
internal class NonJvmRegexPatternRecognizer(private val regex: Regex) :
LixyTokenRecognizer {
Expand Down

0 comments on commit 8c2dd9d

Please sign in to comment.