perf(selector): check type error
Some checks failed
Build-Apk / build (push) Has been cancelled

This commit is contained in:
lisonge 2024-07-24 21:48:57 +08:00
parent 8d1ddf78bf
commit 017422cc03
4 changed files with 39 additions and 12 deletions

View File

@ -17,8 +17,10 @@ import li.songe.selector.Selector
import li.songe.selector.Transform import li.songe.selector.Transform
import li.songe.selector.UnknownIdentifierException import li.songe.selector.UnknownIdentifierException
import li.songe.selector.UnknownIdentifierMethodException import li.songe.selector.UnknownIdentifierMethodException
import li.songe.selector.UnknownIdentifierMethodParamsException
import li.songe.selector.UnknownMemberException import li.songe.selector.UnknownMemberException
import li.songe.selector.UnknownMemberMethodException import li.songe.selector.UnknownMemberMethodException
import li.songe.selector.UnknownMemberMethodParamsException
import li.songe.selector.getBooleanInvoke import li.songe.selector.getBooleanInvoke
import li.songe.selector.getCharSequenceAttr import li.songe.selector.getCharSequenceAttr
import li.songe.selector.getCharSequenceInvoke import li.songe.selector.getCharSequenceInvoke
@ -163,10 +165,12 @@ fun Selector.checkSelector(): String? {
is MismatchExpressionTypeException -> "不匹配表达式类型:${error.exception.stringify()}" is MismatchExpressionTypeException -> "不匹配表达式类型:${error.exception.stringify()}"
is MismatchOperatorTypeException -> "不匹配操作符类型:${error.exception.stringify()}" is MismatchOperatorTypeException -> "不匹配操作符类型:${error.exception.stringify()}"
is MismatchParamTypeException -> "不匹配参数类型:${error.call.stringify()}" is MismatchParamTypeException -> "不匹配参数类型:${error.call.stringify()}"
is UnknownIdentifierException -> "未知属性:${error.value.value}" is UnknownIdentifierException -> "未知属性:${error.value.stringify()}"
is UnknownIdentifierMethodException -> "未知方法:${error.value.value}" is UnknownIdentifierMethodException -> "未知方法:${error.value.stringify()}"
is UnknownMemberException -> "未知属性:${error.value.property}" is UnknownMemberException -> "未知属性:${error.value.stringify()}"
is UnknownMemberMethodException -> "未知方法:${error.value.property}" is UnknownMemberMethodException -> "未知方法:${error.value.stringify()}"
is UnknownIdentifierMethodParamsException -> "未知方法参数:${error.value.stringify()}"
is UnknownMemberMethodParamsException -> "未知方法参数:${error.value.stringify()}"
} }
} }

View File

@ -8,29 +8,39 @@ sealed class SelectorCheckException(override val message: String) : Exception(me
@JsExport @JsExport
data class UnknownIdentifierException( data class UnknownIdentifierException(
val value: ValueExpression.Identifier, val value: ValueExpression.Identifier,
) : SelectorCheckException("Unknown Identifier: ${value.name}") ) : SelectorCheckException("Unknown Identifier: ${value.stringify()}")
@JsExport @JsExport
data class UnknownMemberException( data class UnknownMemberException(
val value: ValueExpression.MemberExpression, val value: ValueExpression.MemberExpression,
) : SelectorCheckException("Unknown Member: ${value.property}") ) : SelectorCheckException("Unknown Member: ${value.stringify()}")
@JsExport @JsExport
data class UnknownIdentifierMethodException( data class UnknownIdentifierMethodException(
val value: ValueExpression.Identifier, 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 @JsExport
data class UnknownMemberMethodException( data class UnknownMemberMethodException(
val value: ValueExpression.MemberExpression, 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 @JsExport
data class MismatchParamTypeException( data class MismatchParamTypeException(
val call: ValueExpression.CallExpression, val call: ValueExpression.CallExpression,
val argument: ValueExpression, val argument: ValueExpression,
val type: PrimitiveType val type: PrimitiveType
) : SelectorCheckException("Mismatch Param Type: ${argument.value} should be ${type.key}") ) : SelectorCheckException("Mismatch Param Type: ${argument.stringify()} should be ${type.key}")
@JsExport @JsExport
data class MismatchExpressionTypeException( data class MismatchExpressionTypeException(

View File

@ -205,12 +205,19 @@ private fun checkVariable(
is ValueExpression.Identifier -> { is ValueExpression.Identifier -> {
// getChild(0) // 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 { .apply {
if (isEmpty()) { if (isEmpty()) {
throw UnknownIdentifierMethodException(value.callee) throw UnknownIdentifierMethodException(value.callee)
} }
} }
.filter { it.params.size == value.arguments.size }
.apply {
if (isEmpty()) {
throw UnknownIdentifierMethodParamsException(value)
}
}
} }
is ValueExpression.MemberExpression -> { is ValueExpression.MemberExpression -> {
@ -219,11 +226,16 @@ private fun checkVariable(
value.callee.object0, value.callee.object0,
currentTypeInfo, currentTypeInfo,
globalTypeInfo globalTypeInfo
).methods.filter { it.name == value.callee.property && it.params.size == value.arguments.size } ).methods
.filter { it.name == value.callee.property }
.apply { .apply {
if (isEmpty()) { if (isEmpty()) {
throw UnknownMemberMethodException(value.callee) throw UnknownMemberMethodException(value.callee)
} }
}.filter { it.params.size == value.arguments.size }.apply {
if (isEmpty()) {
throw UnknownMemberMethodParamsException(value)
}
} }
} }
} }

View File

@ -268,11 +268,12 @@ class ParserTest {
@Test @Test
fun check_type() { fun check_type() {
val source = 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 selector = Selector.parse(source)
val error = selector.checkType(typeInfo) val error = selector.checkType(typeInfo)
println("useCache: ${selector.useCache}") println("useCache: ${selector.useCache}")
println("error: $error") println("error: $error")
println("error_message: ${error?.message}")
println("check_type: ${selector.stringify()}") println("check_type: ${selector.stringify()}")
} }