Skip to content

Commit

Permalink
Merge pull request #5 from demndevel/main
Browse files Browse the repository at this point in the history
feat: add ability to parse units without number
  • Loading branch information
y9san9 authored Aug 22, 2024
2 parents 8dc61cf + fdaafce commit 374319e
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 12 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ env:

jobs:

test-jvm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Gradle Cache Setup
uses: gradle/gradle-build-action@v2.4.2
- name: Gradle Check
run: ./gradlew jvmTest

deploy-multiplatform:
runs-on: ubuntu-latest
needs:
- test-jvm
outputs:
release_version: ${{ steps.output_version.outputs.release_version }}
steps:
Expand All @@ -38,6 +49,8 @@ jobs:

deploy-jvm:
runs-on: ubuntu-latest
needs:
- test-jvm
steps:
- uses: actions/checkout@v3
- name: Gradle Cache Setup
Expand All @@ -53,6 +66,8 @@ jobs:

deploy-js:
runs-on: ubuntu-latest
needs:
- test-jvm
steps:
- uses: actions/checkout@v3
- name: Gradle Cache Setup
Expand All @@ -68,6 +83,8 @@ jobs:

deploy-ios-x64:
runs-on: macos-latest
needs:
- test-jvm
steps:
- uses: actions/checkout@v3
- name: Gradle Cache Setup
Expand All @@ -88,6 +105,8 @@ jobs:

deploy-ios-arm64:
runs-on: macos-latest
needs:
- test-jvm
steps:
- uses: actions/checkout@v3
- name: Gradle Cache Setup
Expand All @@ -108,6 +127,8 @@ jobs:

deploy-ios-simulator-arm64:
runs-on: macos-latest
needs:
- test-jvm
steps:
- uses: actions/checkout@v3
- name: Gradle Cache Setup
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Library Release Deploy

on:
push:
branches-ignore: [ "main" ]
workflow_dispatch:

jobs:

test-jvm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Gradle Cache Setup
uses: gradle/gradle-build-action@v2.4.2
- name: Gradle Check
run: ./gradlew jvmTest
4 changes: 4 additions & 0 deletions build-logic/src/main/kotlin/kmp-library-convention.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ kotlin {
iosX64()
iosSimulatorArm64()
}

dependencies {
commonTestImplementation(kotlin("test"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public fun <T> ParseResult<T>.getOrFail(context: ParseContext): T {
}
}

public fun <T> ParseResult<T>.getOrThrow(): T {
return when (this) {
is ParseResult.Failure -> error("Parse result is expected to be Success")
is ParseResult.Success -> this.value
}
}

public inline fun <T> ParseResult<T>.getOrElse(
context: ParseContext,
recover: (ParseResult.Failure) -> T
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.3"
calkt = "0.0.4"

[libraries]

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

import me.y9san9.calkt.Expression
import me.y9san9.calkt.math.MathExpression
import me.y9san9.calkt.math.parse.MathParseOperandFunction
import me.y9san9.calkt.number.PreciseNumber
import me.y9san9.calkt.parse.*
import me.y9san9.calkt.parse.base.overrideCause
import me.y9san9.calkt.parse.cause.ExpectedInputCause
Expand All @@ -11,7 +13,6 @@ public class UnitsMathParseOperand(
private val parseUnitKey: UnitsParseUnitKeyFunction,
private val parseOperand: MathParseOperandFunction
) : MathParseOperandFunction {

override fun invoke(context: ParseContext): Expression {
context.tryParse {
context.overrideCause(
Expand All @@ -25,19 +26,34 @@ public class UnitsMathParseOperand(
context.pushNonTerminalCause(failure.cause)
}

val operand = parseOperand.invoke(context)

context.tryParse {
context.overrideCause(
{ failure -> ExpectedInputCause.of("Unit After Number", failure = failure) }
) {
val unitKey = parseUnitKey(context)
return UnitsExpression.Conversion(operand, unitKey)
val operand = parseOperand(context)

context.tryParse {
context.overrideCause(
{ failure -> ExpectedInputCause.of("Unit After Number", failure = failure) }
) {
val unitKey = parseUnitKey(context)
return UnitsExpression.Conversion(operand, unitKey)
}
}.getOrElse(context) { secondFailure ->
context.pushNonTerminalCause(secondFailure.cause)
}
}.getOrElse(context) { secondFailure ->
context.pushNonTerminalCause(secondFailure.cause)

return operand
}.getOrElse(context) { failure ->
context.pushNonTerminalCause(failure.cause)
}

context.tryParse {
val unitKey = parseUnitKey(context)

return UnitsExpression.Conversion(
value = MathExpression.Number(PreciseNumber.Companion.of(1)),
key = unitKey
)
}

return operand
context.failWithNonTerminalCauses()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package me.y9san9.calkt.units.parse

import me.y9san9.calkt.math.MathExpression
import me.y9san9.calkt.math.parse.DefaultMathParseOperand
import me.y9san9.calkt.number.PreciseNumber
import me.y9san9.calkt.parse.base.consume
import me.y9san9.calkt.parse.getOrThrow
import me.y9san9.calkt.parse.tryParse
import me.y9san9.calkt.units.UnitKey
import me.y9san9.calkt.units.UnitsExpression
import me.y9san9.calkt.units.annotation.UnitKeySubclass
import me.y9san9.calkt.units.parse.cause.ExpectedUnitsCause
import kotlin.test.Test
import kotlin.test.assertEquals

class UnitsMathParseOperandTest {

@Test
fun testCanParseNumber() {
val operand = create()

val result = tryParse("1") { context ->
operand(context)
}.getOrThrow()

val expected = MathExpression.Number(number = PreciseNumber.of(int = 1))

assertEquals(expected, result)
}

@Test
fun testCanParseGroup() {
val operand = create()

val result = tryParse("(1 unit)") { context ->
operand(context)
}.getOrThrow()

val expected = UnitsExpression.Conversion(
value = MathExpression.Number(
number = PreciseNumber.of(int = 1)
),
key = TestUnitKey
)

assertEquals(expected, result)
}

@Test
fun testCanUnitBefore() {
val operand = create()

val result = tryParse("unit 1") { context ->
operand(context)
}.getOrThrow()

val expected = UnitsExpression.Conversion(
value = MathExpression.Number(
number = PreciseNumber.of(int = 1)
),
key = TestUnitKey
)

assertEquals(expected, result)
}

@Test
fun testCanParseUnitAfter() {
val operand = create()

val result = tryParse("1 unit") { context ->
operand(context)
}.getOrThrow()

val expected = UnitsExpression.Conversion(
value = MathExpression.Number(
number = PreciseNumber.of(int = 1)
),
key = TestUnitKey
)

assertEquals(expected, result)
}

@Test
fun testCanParseUnitOnly() {
val operand = create()

val result = tryParse("unit") { context ->
operand(context)
}.getOrThrow()

val expected = UnitsExpression.Conversion(
value = MathExpression.Number(
number = PreciseNumber.of(int = 1)
),
key = TestUnitKey
)

assertEquals(expected, result)
}

private fun create(): UnitsMathParseOperand {
val parseUnitKey = UnitsParseUnitKeyFunction { context ->
context.consume("unit") { ExpectedUnitsCause.of("unit") }
TestUnitKey

}
val parseOperand = DefaultMathParseOperand
return UnitsMathParseOperand(parseUnitKey, parseOperand)
}

@OptIn(UnitKeySubclass::class)
private object TestUnitKey : UnitKey
}

0 comments on commit 374319e

Please sign in to comment.