-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added support for arbitrary symbols in UnitKey
- Loading branch information
Showing
4 changed files
with
23 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 9 additions & 7 deletions
16
units/src/commonMain/kotlin/me/y9san9/calkt/units/parse/UnitsParseUnitKeyFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}")) | ||
} | ||
} | ||
} |