Skip to content

Commit

Permalink
feat: Added support for arbitrary symbols in UnitKey
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Aug 19, 2024
1 parent 9a1685d commit 8dc61cf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
18 changes: 9 additions & 9 deletions example/src/main/kotlin/units/parse/ParseDistance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ import units.DistanceUnitKey

object ParseDistance {
// Metric
val kilometers = UnitsParseUnitKeyFunction.ofWords(
val kilometers = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Kilometers,
"km", "kilometer", "kilometers"
"km", "kilometer", "kilometers", "\$CSACS*(_)(*"
)
val meters = UnitsParseUnitKeyFunction.ofWords(
val meters = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Meters,
"m", "meter", "meters"
)
val centimeters = UnitsParseUnitKeyFunction.ofWords(
val centimeters = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Centimeters,
"cm", "centimeter", "centimeters"
)
val millimeters = UnitsParseUnitKeyFunction.ofWords(
val millimeters = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Millimeters,
"mm", "millimeter", "millimeters"
)

// Imperial
val mile = UnitsParseUnitKeyFunction.ofWords(
val mile = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Miles,
"mi", "mile"
)
val yard = UnitsParseUnitKeyFunction.ofWords(
val yard = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Yards,
"yd", "yard"
)
val foot = UnitsParseUnitKeyFunction.ofWords(
val foot = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Feet,
"ft", "foot", "feet"
)
val inches = UnitsParseUnitKeyFunction.ofWords(
val inches = UnitsParseUnitKeyFunction.ofStrings(
DistanceUnitKey.Inches,
"in", "inch", "inches"
)
Expand Down
8 changes: 4 additions & 4 deletions example/src/main/kotlin/units/parse/ParseTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import me.y9san9.calkt.units.parse.plus
import units.TimeUnitKey

object ParseTime {
val hours = UnitsParseUnitKeyFunction.ofWords(
val hours = UnitsParseUnitKeyFunction.ofStrings(
TimeUnitKey.Hours,
"h", "hour", "hours"
)
val minutes = UnitsParseUnitKeyFunction.ofWords(
val minutes = UnitsParseUnitKeyFunction.ofStrings(
TimeUnitKey.Minutes,
"min", "mins", "minute", "minutes"
)
val seconds = UnitsParseUnitKeyFunction.ofWords(
val seconds = UnitsParseUnitKeyFunction.ofStrings(
TimeUnitKey.Seconds,
"sec", "second", "seconds"
)
val milliseconds = UnitsParseUnitKeyFunction.ofWords(
val milliseconds = UnitsParseUnitKeyFunction.ofStrings(
TimeUnitKey.Millis,
"millis", "millisecond", "milliseconds"
)
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kotlin = "2.0.0"
bignum = "0.3.10"
maven-publish = "0.29.0"

calkt = "0.0.2"
calkt = "0.0.3"

[libraries]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package me.y9san9.calkt.units.parse

import me.y9san9.calkt.parse.ParseContext
import me.y9san9.calkt.parse.base.token
import me.y9san9.calkt.parse.base.word
import me.y9san9.calkt.parse.base.*
import me.y9san9.calkt.parse.cause.ExpectedInputCause
import me.y9san9.calkt.units.UnitKey

public fun interface UnitsParseUnitKeyFunction {
public operator fun invoke(context: ParseContext): UnitKey

public companion object {
public fun ofWords(
public fun ofStrings(
key: UnitKey,
vararg words: String
vararg strings: String
): UnitsParseUnitKeyFunction = UnitsParseUnitKeyFunction { context ->
context.token {
context.word(*words) {
ExpectedInputCause.of("$key: One of ${words.joinToString()}")
for (string in strings) {
if (context.startsWith(string)) {
context.drop(string.length)
return@UnitsParseUnitKeyFunction key
}
}
}
key
context.fail(ExpectedInputCause.of("$key: One of ${strings.joinToString()}"))
}
}
}

0 comments on commit 8dc61cf

Please sign in to comment.