Skip to content

Commit

Permalink
perf(selector): check type error
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 24, 2024
1 parent 8d1ddf7 commit 017422c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
12 changes: 8 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/service/AbExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import li.songe.selector.Selector
import li.songe.selector.Transform
import li.songe.selector.UnknownIdentifierException
import li.songe.selector.UnknownIdentifierMethodException
import li.songe.selector.UnknownIdentifierMethodParamsException
import li.songe.selector.UnknownMemberException
import li.songe.selector.UnknownMemberMethodException
import li.songe.selector.UnknownMemberMethodParamsException
import li.songe.selector.getBooleanInvoke
import li.songe.selector.getCharSequenceAttr
import li.songe.selector.getCharSequenceInvoke
Expand Down Expand Up @@ -163,10 +165,12 @@ fun Selector.checkSelector(): String? {
is MismatchExpressionTypeException -> "不匹配表达式类型:${error.exception.stringify()}"
is MismatchOperatorTypeException -> "不匹配操作符类型:${error.exception.stringify()}"
is MismatchParamTypeException -> "不匹配参数类型:${error.call.stringify()}"
is UnknownIdentifierException -> "未知属性:${error.value.value}"
is UnknownIdentifierMethodException -> "未知方法:${error.value.value}"
is UnknownMemberException -> "未知属性:${error.value.property}"
is UnknownMemberMethodException -> "未知方法:${error.value.property}"
is UnknownIdentifierException -> "未知属性:${error.value.stringify()}"
is UnknownIdentifierMethodException -> "未知方法:${error.value.stringify()}"
is UnknownMemberException -> "未知属性:${error.value.stringify()}"
is UnknownMemberMethodException -> "未知方法:${error.value.stringify()}"
is UnknownIdentifierMethodParamsException -> "未知方法参数:${error.value.stringify()}"
is UnknownMemberMethodParamsException -> "未知方法参数:${error.value.stringify()}"
}
}

Expand Down
20 changes: 15 additions & 5 deletions selector/src/commonMain/kotlin/li/songe/selector/Exception.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,39 @@ sealed class SelectorCheckException(override val message: String) : Exception(me
@JsExport
data class UnknownIdentifierException(
val value: ValueExpression.Identifier,
) : SelectorCheckException("Unknown Identifier: ${value.name}")
) : SelectorCheckException("Unknown Identifier: ${value.stringify()}")

@JsExport
data class UnknownMemberException(
val value: ValueExpression.MemberExpression,
) : SelectorCheckException("Unknown Member: ${value.property}")
) : SelectorCheckException("Unknown Member: ${value.stringify()}")

@JsExport
data class UnknownIdentifierMethodException(
val value: ValueExpression.Identifier,
) : SelectorCheckException("Unknown Identifier Method: ${value.name}")
) : SelectorCheckException("Unknown Identifier Method: ${value.stringify()}")

@JsExport
data class UnknownIdentifierMethodParamsException(
val value: ValueExpression.CallExpression,
) : SelectorCheckException("Unknown Identifier Method Params: ${value.stringify()}")

@JsExport
data class UnknownMemberMethodException(
val value: ValueExpression.MemberExpression,
) : SelectorCheckException("Unknown Member Method: ${value.property}")
) : SelectorCheckException("Unknown Member Method: ${value.stringify()}")

@JsExport
data class UnknownMemberMethodParamsException(
val value: ValueExpression.CallExpression,
) : SelectorCheckException("Unknown Member Method Params: ${value.stringify()}")

@JsExport
data class MismatchParamTypeException(
val call: ValueExpression.CallExpression,
val argument: ValueExpression,
val type: PrimitiveType
) : SelectorCheckException("Mismatch Param Type: ${argument.value} should be ${type.key}")
) : SelectorCheckException("Mismatch Param Type: ${argument.stringify()} should be ${type.key}")

@JsExport
data class MismatchExpressionTypeException(
Expand Down
16 changes: 14 additions & 2 deletions selector/src/commonMain/kotlin/li/songe/selector/Selector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,19 @@ private fun checkVariable(

is ValueExpression.Identifier -> {
// getChild(0)
globalTypeInfo.methods.filter { it.name == value.callee.value && it.params.size == value.arguments.size }
globalTypeInfo.methods
.filter { it.name == value.callee.value }
.apply {
if (isEmpty()) {
throw UnknownIdentifierMethodException(value.callee)
}
}
.filter { it.params.size == value.arguments.size }
.apply {
if (isEmpty()) {
throw UnknownIdentifierMethodParamsException(value)
}
}
}

is ValueExpression.MemberExpression -> {
Expand All @@ -219,11 +226,16 @@ private fun checkVariable(
value.callee.object0,
currentTypeInfo,
globalTypeInfo
).methods.filter { it.name == value.callee.property && it.params.size == value.arguments.size }
).methods
.filter { it.name == value.callee.property }
.apply {
if (isEmpty()) {
throw UnknownMemberMethodException(value.callee)
}
}.filter { it.params.size == value.arguments.size }.apply {
if (isEmpty()) {
throw UnknownMemberMethodParamsException(value)
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion selector/src/jvmTest/kotlin/li/songe/selector/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,12 @@ class ParserTest {
@Test
fun check_type() {
val source =
"[prev!=null&&visibleToUser=true&&equal(index, depth)=true][((parent.getChild(0,).getChild( (0), )=null) && (((2 >= 1)))) || (name=null && desc=null)]"
"[prev.getChild(0,0)=0][prev!=null&&visibleToUser=true&&equal(index, depth)=true][((parent.getChild(0,).getChild( (0), )=null) && (((2 >= 1)))) || (name=null && desc=null)]"
val selector = Selector.parse(source)
val error = selector.checkType(typeInfo)
println("useCache: ${selector.useCache}")
println("error: $error")
println("error_message: ${error?.message}")
println("check_type: ${selector.stringify()}")
}

Expand Down

0 comments on commit 017422c

Please sign in to comment.