cnhkmcp 2.3.7__py3-none-any.whl → 2.3.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cnhkmcp/__init__.py +1 -1
- cnhkmcp/untracked/APP/Tranformer/validator.py +192 -53
- cnhkmcp/untracked/APP/blueprints/validator.py +192 -53
- cnhkmcp/untracked/APP/trailSomeAlphas/skills/brain-feature-implementation/scripts/validator.py +193 -53
- cnhkmcp/untracked/skills/alpha-expression-verifier/scripts/validator.py +192 -53
- cnhkmcp/untracked/skills/brain-inspectRawTemplate-create-Setting/scripts/validator.py +192 -53
- {cnhkmcp-2.3.7.dist-info → cnhkmcp-2.3.8.dist-info}/METADATA +1 -1
- {cnhkmcp-2.3.7.dist-info → cnhkmcp-2.3.8.dist-info}/RECORD +12 -12
- {cnhkmcp-2.3.7.dist-info → cnhkmcp-2.3.8.dist-info}/WHEEL +0 -0
- {cnhkmcp-2.3.7.dist-info → cnhkmcp-2.3.8.dist-info}/entry_points.txt +0 -0
- {cnhkmcp-2.3.7.dist-info → cnhkmcp-2.3.8.dist-info}/licenses/LICENSE +0 -0
- {cnhkmcp-2.3.7.dist-info → cnhkmcp-2.3.8.dist-info}/top_level.txt +0 -0
cnhkmcp/untracked/APP/trailSomeAlphas/skills/brain-feature-implementation/scripts/validator.py
CHANGED
|
@@ -613,6 +613,16 @@ class ExpressionValidator:
|
|
|
613
613
|
"""验证参数类型是否符合预期"""
|
|
614
614
|
errors = []
|
|
615
615
|
|
|
616
|
+
def _is_number_like(node: ASTNode) -> bool:
|
|
617
|
+
if node is None:
|
|
618
|
+
return False
|
|
619
|
+
if node.node_type == 'number':
|
|
620
|
+
return True
|
|
621
|
+
if node.node_type == 'unop' and isinstance(node.value, dict) and node.value.get('op') in {'-', '+'}:
|
|
622
|
+
if node.children and hasattr(node.children[0], 'node_type'):
|
|
623
|
+
return _is_number_like(node.children[0])
|
|
624
|
+
return False
|
|
625
|
+
|
|
616
626
|
# Unit compatibility check
|
|
617
627
|
# bucket()/group_cartesian_product() output a derived category (grouping key).
|
|
618
628
|
# It can only be consumed where a category/grouping key is expected (typically by group_* operators).
|
|
@@ -634,7 +644,8 @@ class ExpressionValidator:
|
|
|
634
644
|
# 表达式可以是任何有效的AST节点
|
|
635
645
|
pass
|
|
636
646
|
elif expected_type == 'number':
|
|
637
|
-
|
|
647
|
+
# 允许 -1 这类一元负号数字常量(解析为 unop(number))
|
|
648
|
+
if not _is_number_like(arg):
|
|
638
649
|
errors.append(f"参数 {arg_index+1} 应该是一个数字,但得到 {arg.node_type}")
|
|
639
650
|
elif expected_type == 'boolean':
|
|
640
651
|
# 布尔值可以是 true/false 或数字(0/1)
|
|
@@ -901,6 +912,173 @@ class ExpressionValidator:
|
|
|
901
912
|
Returns:
|
|
902
913
|
Tuple[bool, str]: (是否成功, 转换后的表达式或错误信息)
|
|
903
914
|
"""
|
|
915
|
+
def _top_level_equals_positions(stmt: str) -> List[int]:
|
|
916
|
+
"""返回所有“顶层赋值”等号位置。
|
|
917
|
+
|
|
918
|
+
仅统计括号外(()[]{})、引号外、且不属于比较操作符(==,!=,<=,>=)的 '='。
|
|
919
|
+
这样可以避免把关键字参数(如 rettype=0)误判为赋值语句。
|
|
920
|
+
"""
|
|
921
|
+
positions: List[int] = []
|
|
922
|
+
paren_depth = 0
|
|
923
|
+
bracket_depth = 0
|
|
924
|
+
brace_depth = 0
|
|
925
|
+
in_single_quote = False
|
|
926
|
+
in_double_quote = False
|
|
927
|
+
escape = False
|
|
928
|
+
|
|
929
|
+
for i, ch in enumerate(stmt):
|
|
930
|
+
if escape:
|
|
931
|
+
escape = False
|
|
932
|
+
continue
|
|
933
|
+
if ch == '\\':
|
|
934
|
+
escape = True
|
|
935
|
+
continue
|
|
936
|
+
|
|
937
|
+
if in_single_quote:
|
|
938
|
+
if ch == "'":
|
|
939
|
+
in_single_quote = False
|
|
940
|
+
continue
|
|
941
|
+
if in_double_quote:
|
|
942
|
+
if ch == '"':
|
|
943
|
+
in_double_quote = False
|
|
944
|
+
continue
|
|
945
|
+
|
|
946
|
+
if ch == "'":
|
|
947
|
+
in_single_quote = True
|
|
948
|
+
continue
|
|
949
|
+
if ch == '"':
|
|
950
|
+
in_double_quote = True
|
|
951
|
+
continue
|
|
952
|
+
|
|
953
|
+
if ch == '(':
|
|
954
|
+
paren_depth += 1
|
|
955
|
+
continue
|
|
956
|
+
if ch == ')':
|
|
957
|
+
paren_depth = max(0, paren_depth - 1)
|
|
958
|
+
continue
|
|
959
|
+
if ch == '[':
|
|
960
|
+
bracket_depth += 1
|
|
961
|
+
continue
|
|
962
|
+
if ch == ']':
|
|
963
|
+
bracket_depth = max(0, bracket_depth - 1)
|
|
964
|
+
continue
|
|
965
|
+
if ch == '{':
|
|
966
|
+
brace_depth += 1
|
|
967
|
+
continue
|
|
968
|
+
if ch == '}':
|
|
969
|
+
brace_depth = max(0, brace_depth - 1)
|
|
970
|
+
continue
|
|
971
|
+
|
|
972
|
+
if paren_depth or bracket_depth or brace_depth:
|
|
973
|
+
continue
|
|
974
|
+
|
|
975
|
+
if ch != '=':
|
|
976
|
+
continue
|
|
977
|
+
|
|
978
|
+
# 过滤比较操作符(==,!=,<=,>=)
|
|
979
|
+
prev_ch = stmt[i - 1] if i > 0 else ''
|
|
980
|
+
next_ch = stmt[i + 1] if i + 1 < len(stmt) else ''
|
|
981
|
+
if prev_ch in ['=', '!', '<', '>'] or next_ch == '=':
|
|
982
|
+
continue
|
|
983
|
+
|
|
984
|
+
positions.append(i)
|
|
985
|
+
|
|
986
|
+
return positions
|
|
987
|
+
|
|
988
|
+
def _keyword_arg_names(stmt: str):
|
|
989
|
+
"""提取函数调用中的命名参数名(如 rettype=0 中的 rettype)。
|
|
990
|
+
|
|
991
|
+
只收集括号/中括号/大括号内部出现的 name= 形式,避免把脚本级赋值误当作命名参数。
|
|
992
|
+
"""
|
|
993
|
+
names = set()
|
|
994
|
+
paren_depth = 0
|
|
995
|
+
bracket_depth = 0
|
|
996
|
+
brace_depth = 0
|
|
997
|
+
in_single_quote = False
|
|
998
|
+
in_double_quote = False
|
|
999
|
+
escape = False
|
|
1000
|
+
|
|
1001
|
+
i = 0
|
|
1002
|
+
while i < len(stmt):
|
|
1003
|
+
ch = stmt[i]
|
|
1004
|
+
|
|
1005
|
+
if escape:
|
|
1006
|
+
escape = False
|
|
1007
|
+
i += 1
|
|
1008
|
+
continue
|
|
1009
|
+
if ch == '\\':
|
|
1010
|
+
escape = True
|
|
1011
|
+
i += 1
|
|
1012
|
+
continue
|
|
1013
|
+
|
|
1014
|
+
if in_single_quote:
|
|
1015
|
+
if ch == "'":
|
|
1016
|
+
in_single_quote = False
|
|
1017
|
+
i += 1
|
|
1018
|
+
continue
|
|
1019
|
+
if in_double_quote:
|
|
1020
|
+
if ch == '"':
|
|
1021
|
+
in_double_quote = False
|
|
1022
|
+
i += 1
|
|
1023
|
+
continue
|
|
1024
|
+
|
|
1025
|
+
if ch == "'":
|
|
1026
|
+
in_single_quote = True
|
|
1027
|
+
i += 1
|
|
1028
|
+
continue
|
|
1029
|
+
if ch == '"':
|
|
1030
|
+
in_double_quote = True
|
|
1031
|
+
i += 1
|
|
1032
|
+
continue
|
|
1033
|
+
|
|
1034
|
+
if ch == '(':
|
|
1035
|
+
paren_depth += 1
|
|
1036
|
+
i += 1
|
|
1037
|
+
continue
|
|
1038
|
+
if ch == ')':
|
|
1039
|
+
paren_depth = max(0, paren_depth - 1)
|
|
1040
|
+
i += 1
|
|
1041
|
+
continue
|
|
1042
|
+
if ch == '[':
|
|
1043
|
+
bracket_depth += 1
|
|
1044
|
+
i += 1
|
|
1045
|
+
continue
|
|
1046
|
+
if ch == ']':
|
|
1047
|
+
bracket_depth = max(0, bracket_depth - 1)
|
|
1048
|
+
i += 1
|
|
1049
|
+
continue
|
|
1050
|
+
if ch == '{':
|
|
1051
|
+
brace_depth += 1
|
|
1052
|
+
i += 1
|
|
1053
|
+
continue
|
|
1054
|
+
if ch == '}':
|
|
1055
|
+
brace_depth = max(0, brace_depth - 1)
|
|
1056
|
+
i += 1
|
|
1057
|
+
continue
|
|
1058
|
+
|
|
1059
|
+
inside_container = bool(paren_depth or bracket_depth or brace_depth)
|
|
1060
|
+
|
|
1061
|
+
if inside_container and (ch.isalpha() or ch == '_'):
|
|
1062
|
+
start = i
|
|
1063
|
+
i += 1
|
|
1064
|
+
while i < len(stmt) and (stmt[i].isalnum() or stmt[i] == '_'):
|
|
1065
|
+
i += 1
|
|
1066
|
+
name = stmt[start:i]
|
|
1067
|
+
|
|
1068
|
+
j = i
|
|
1069
|
+
while j < len(stmt) and stmt[j].isspace():
|
|
1070
|
+
j += 1
|
|
1071
|
+
|
|
1072
|
+
if j < len(stmt) and stmt[j] == '=':
|
|
1073
|
+
next_ch = stmt[j + 1] if j + 1 < len(stmt) else ''
|
|
1074
|
+
if next_ch != '=':
|
|
1075
|
+
names.add(name.lower())
|
|
1076
|
+
continue
|
|
1077
|
+
|
|
1078
|
+
i += 1
|
|
1079
|
+
|
|
1080
|
+
return names
|
|
1081
|
+
|
|
904
1082
|
# 检查表达式是否以分号结尾
|
|
905
1083
|
if expression.strip().endswith(';'):
|
|
906
1084
|
return False, "表达式不能以分号结尾"
|
|
@@ -915,51 +1093,13 @@ class ExpressionValidator:
|
|
|
915
1093
|
|
|
916
1094
|
# 处理每个赋值语句(除了最后一个)
|
|
917
1095
|
for i, stmt in enumerate(statements[:-1]):
|
|
918
|
-
|
|
919
|
-
if
|
|
920
|
-
return False, f"第{i+1}个语句必须是赋值语句(使用=符号)"
|
|
921
|
-
|
|
922
|
-
# 检查是否是比较操作符(==, !=, <=, >=)
|
|
923
|
-
if any(op in stmt for op in ['==', '!=', '<=', '>=']):
|
|
924
|
-
# 如果包含比较操作符,需要确认是否有赋值符号
|
|
925
|
-
# 使用临时替换法:将比较操作符替换为临时标记,再检查是否还有=
|
|
926
|
-
temp_stmt = stmt
|
|
927
|
-
for op in ['==', '!=', '<=', '>=']:
|
|
928
|
-
temp_stmt = temp_stmt.replace(op, '---')
|
|
929
|
-
|
|
930
|
-
if '=' not in temp_stmt:
|
|
931
|
-
return False, f"第{i+1}个语句必须是赋值语句,不能只是比较表达式"
|
|
932
|
-
|
|
933
|
-
# 找到第一个=符号(不是比较操作符的一部分)
|
|
934
|
-
# 先将比较操作符替换为临时标记,再找=
|
|
935
|
-
temp_stmt = stmt
|
|
936
|
-
for op in ['==', '!=', '<=', '>=']:
|
|
937
|
-
temp_stmt = temp_stmt.replace(op, '---')
|
|
938
|
-
|
|
939
|
-
if '=' not in temp_stmt:
|
|
1096
|
+
eq_positions = _top_level_equals_positions(stmt)
|
|
1097
|
+
if not eq_positions:
|
|
940
1098
|
return False, f"第{i+1}个语句必须是赋值语句(使用=符号)"
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
# 在原始语句中找到对应位置
|
|
946
|
-
real_equals_pos = 0
|
|
947
|
-
temp_count = 0
|
|
948
|
-
for char in stmt:
|
|
949
|
-
if temp_count == equals_pos:
|
|
950
|
-
break
|
|
951
|
-
if char in '!<>':
|
|
952
|
-
# 检查是否是比较操作符的一部分
|
|
953
|
-
if real_equals_pos + 1 < len(stmt) and stmt[real_equals_pos + 1] == '=':
|
|
954
|
-
# 是比较操作符,跳过两个字符
|
|
955
|
-
real_equals_pos += 2
|
|
956
|
-
temp_count += 3 # 因为替换成了三个字符的---
|
|
957
|
-
else:
|
|
958
|
-
real_equals_pos += 1
|
|
959
|
-
temp_count += 1
|
|
960
|
-
else:
|
|
961
|
-
real_equals_pos += 1
|
|
962
|
-
temp_count += 1
|
|
1099
|
+
if len(eq_positions) > 1:
|
|
1100
|
+
return False, f"第{i+1}个语句只能包含一个赋值符号(=)"
|
|
1101
|
+
|
|
1102
|
+
real_equals_pos = eq_positions[0]
|
|
963
1103
|
|
|
964
1104
|
# 分割变量名和值
|
|
965
1105
|
var_name = stmt[:real_equals_pos].strip()
|
|
@@ -976,9 +1116,12 @@ class ExpressionValidator:
|
|
|
976
1116
|
|
|
977
1117
|
# 检查变量值中使用的变量是否已经定义
|
|
978
1118
|
# 简单检查:提取所有可能的变量名
|
|
1119
|
+
kw_names = _keyword_arg_names(var_value)
|
|
979
1120
|
used_vars = re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b', var_value)
|
|
980
1121
|
for used_var in used_vars:
|
|
981
1122
|
used_var_lower = used_var.lower()
|
|
1123
|
+
if used_var_lower in kw_names:
|
|
1124
|
+
continue
|
|
982
1125
|
if used_var_lower not in variables:
|
|
983
1126
|
# 检查是否是函数名
|
|
984
1127
|
if used_var not in supported_functions:
|
|
@@ -1001,19 +1144,16 @@ class ExpressionValidator:
|
|
|
1001
1144
|
final_stmt = statements[-1]
|
|
1002
1145
|
|
|
1003
1146
|
# 检查最后一个语句是否是赋值语句
|
|
1004
|
-
if
|
|
1005
|
-
|
|
1006
|
-
temp_stmt = final_stmt
|
|
1007
|
-
for op in ['==', '!=', '<=', '>=']:
|
|
1008
|
-
temp_stmt = temp_stmt.replace(op, '---')
|
|
1009
|
-
|
|
1010
|
-
if '=' in temp_stmt:
|
|
1011
|
-
return False, "最后一个语句不能是赋值语句"
|
|
1147
|
+
if _top_level_equals_positions(final_stmt):
|
|
1148
|
+
return False, "最后一个语句不能是赋值语句"
|
|
1012
1149
|
|
|
1013
1150
|
# 检查最后一个语句中使用的变量是否已经定义
|
|
1151
|
+
kw_names = _keyword_arg_names(final_stmt)
|
|
1014
1152
|
used_vars = re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b', final_stmt)
|
|
1015
1153
|
for used_var in used_vars:
|
|
1016
1154
|
used_var_lower = used_var.lower()
|
|
1155
|
+
if used_var_lower in kw_names:
|
|
1156
|
+
continue
|
|
1017
1157
|
if used_var_lower not in variables:
|
|
1018
1158
|
# 检查是否是函数名
|
|
1019
1159
|
if used_var not in supported_functions:
|
|
@@ -613,6 +613,16 @@ class ExpressionValidator:
|
|
|
613
613
|
"""验证参数类型是否符合预期"""
|
|
614
614
|
errors = []
|
|
615
615
|
|
|
616
|
+
def _is_number_like(node: ASTNode) -> bool:
|
|
617
|
+
if node is None:
|
|
618
|
+
return False
|
|
619
|
+
if node.node_type == 'number':
|
|
620
|
+
return True
|
|
621
|
+
if node.node_type == 'unop' and isinstance(node.value, dict) and node.value.get('op') in {'-', '+'}:
|
|
622
|
+
if node.children and hasattr(node.children[0], 'node_type'):
|
|
623
|
+
return _is_number_like(node.children[0])
|
|
624
|
+
return False
|
|
625
|
+
|
|
616
626
|
# Unit compatibility check
|
|
617
627
|
# bucket()/group_cartesian_product() output a derived category (grouping key).
|
|
618
628
|
# It can only be consumed where a category/grouping key is expected.
|
|
@@ -633,7 +643,8 @@ class ExpressionValidator:
|
|
|
633
643
|
# 表达式可以是任何有效的AST节点
|
|
634
644
|
pass
|
|
635
645
|
elif expected_type == 'number':
|
|
636
|
-
|
|
646
|
+
# 允许 -1 这类一元负号数字常量(解析为 unop(number))
|
|
647
|
+
if not _is_number_like(arg):
|
|
637
648
|
errors.append(f"参数 {arg_index+1} 应该是一个数字,但得到 {arg.node_type}")
|
|
638
649
|
elif expected_type == 'boolean':
|
|
639
650
|
# 布尔值可以是 true/false 或数字(0/1)
|
|
@@ -894,6 +905,172 @@ class ExpressionValidator:
|
|
|
894
905
|
Returns:
|
|
895
906
|
Tuple[bool, str]: (是否成功, 转换后的表达式或错误信息)
|
|
896
907
|
"""
|
|
908
|
+
def _top_level_equals_positions(stmt: str) -> List[int]:
|
|
909
|
+
"""返回所有“顶层赋值”等号位置。
|
|
910
|
+
|
|
911
|
+
仅统计括号外(()[]{})、引号外、且不属于比较操作符(==,!=,<=,>=)的 '='。
|
|
912
|
+
这样可以避免把关键字参数(如 rettype=0)误判为赋值语句。
|
|
913
|
+
"""
|
|
914
|
+
positions: List[int] = []
|
|
915
|
+
paren_depth = 0
|
|
916
|
+
bracket_depth = 0
|
|
917
|
+
brace_depth = 0
|
|
918
|
+
in_single_quote = False
|
|
919
|
+
in_double_quote = False
|
|
920
|
+
escape = False
|
|
921
|
+
|
|
922
|
+
for i, ch in enumerate(stmt):
|
|
923
|
+
if escape:
|
|
924
|
+
escape = False
|
|
925
|
+
continue
|
|
926
|
+
if ch == '\\':
|
|
927
|
+
escape = True
|
|
928
|
+
continue
|
|
929
|
+
|
|
930
|
+
if in_single_quote:
|
|
931
|
+
if ch == "'":
|
|
932
|
+
in_single_quote = False
|
|
933
|
+
continue
|
|
934
|
+
if in_double_quote:
|
|
935
|
+
if ch == '"':
|
|
936
|
+
in_double_quote = False
|
|
937
|
+
continue
|
|
938
|
+
|
|
939
|
+
if ch == "'":
|
|
940
|
+
in_single_quote = True
|
|
941
|
+
continue
|
|
942
|
+
if ch == '"':
|
|
943
|
+
in_double_quote = True
|
|
944
|
+
continue
|
|
945
|
+
|
|
946
|
+
if ch == '(':
|
|
947
|
+
paren_depth += 1
|
|
948
|
+
continue
|
|
949
|
+
if ch == ')':
|
|
950
|
+
paren_depth = max(0, paren_depth - 1)
|
|
951
|
+
continue
|
|
952
|
+
if ch == '[':
|
|
953
|
+
bracket_depth += 1
|
|
954
|
+
continue
|
|
955
|
+
if ch == ']':
|
|
956
|
+
bracket_depth = max(0, bracket_depth - 1)
|
|
957
|
+
continue
|
|
958
|
+
if ch == '{':
|
|
959
|
+
brace_depth += 1
|
|
960
|
+
continue
|
|
961
|
+
if ch == '}':
|
|
962
|
+
brace_depth = max(0, brace_depth - 1)
|
|
963
|
+
continue
|
|
964
|
+
|
|
965
|
+
if paren_depth or bracket_depth or brace_depth:
|
|
966
|
+
continue
|
|
967
|
+
|
|
968
|
+
if ch != '=':
|
|
969
|
+
continue
|
|
970
|
+
|
|
971
|
+
prev_ch = stmt[i - 1] if i > 0 else ''
|
|
972
|
+
next_ch = stmt[i + 1] if i + 1 < len(stmt) else ''
|
|
973
|
+
if prev_ch in ['=', '!', '<', '>'] or next_ch == '=':
|
|
974
|
+
continue
|
|
975
|
+
|
|
976
|
+
positions.append(i)
|
|
977
|
+
|
|
978
|
+
return positions
|
|
979
|
+
|
|
980
|
+
def _keyword_arg_names(stmt: str):
|
|
981
|
+
"""提取函数调用中的命名参数名(如 rettype=0 中的 rettype)。
|
|
982
|
+
|
|
983
|
+
只收集括号/中括号/大括号内部出现的 name= 形式,避免把脚本级赋值误当作命名参数。
|
|
984
|
+
"""
|
|
985
|
+
names = set()
|
|
986
|
+
paren_depth = 0
|
|
987
|
+
bracket_depth = 0
|
|
988
|
+
brace_depth = 0
|
|
989
|
+
in_single_quote = False
|
|
990
|
+
in_double_quote = False
|
|
991
|
+
escape = False
|
|
992
|
+
|
|
993
|
+
i = 0
|
|
994
|
+
while i < len(stmt):
|
|
995
|
+
ch = stmt[i]
|
|
996
|
+
|
|
997
|
+
if escape:
|
|
998
|
+
escape = False
|
|
999
|
+
i += 1
|
|
1000
|
+
continue
|
|
1001
|
+
if ch == '\\':
|
|
1002
|
+
escape = True
|
|
1003
|
+
i += 1
|
|
1004
|
+
continue
|
|
1005
|
+
|
|
1006
|
+
if in_single_quote:
|
|
1007
|
+
if ch == "'":
|
|
1008
|
+
in_single_quote = False
|
|
1009
|
+
i += 1
|
|
1010
|
+
continue
|
|
1011
|
+
if in_double_quote:
|
|
1012
|
+
if ch == '"':
|
|
1013
|
+
in_double_quote = False
|
|
1014
|
+
i += 1
|
|
1015
|
+
continue
|
|
1016
|
+
|
|
1017
|
+
if ch == "'":
|
|
1018
|
+
in_single_quote = True
|
|
1019
|
+
i += 1
|
|
1020
|
+
continue
|
|
1021
|
+
if ch == '"':
|
|
1022
|
+
in_double_quote = True
|
|
1023
|
+
i += 1
|
|
1024
|
+
continue
|
|
1025
|
+
|
|
1026
|
+
if ch == '(':
|
|
1027
|
+
paren_depth += 1
|
|
1028
|
+
i += 1
|
|
1029
|
+
continue
|
|
1030
|
+
if ch == ')':
|
|
1031
|
+
paren_depth = max(0, paren_depth - 1)
|
|
1032
|
+
i += 1
|
|
1033
|
+
continue
|
|
1034
|
+
if ch == '[':
|
|
1035
|
+
bracket_depth += 1
|
|
1036
|
+
i += 1
|
|
1037
|
+
continue
|
|
1038
|
+
if ch == ']':
|
|
1039
|
+
bracket_depth = max(0, bracket_depth - 1)
|
|
1040
|
+
i += 1
|
|
1041
|
+
continue
|
|
1042
|
+
if ch == '{':
|
|
1043
|
+
brace_depth += 1
|
|
1044
|
+
i += 1
|
|
1045
|
+
continue
|
|
1046
|
+
if ch == '}':
|
|
1047
|
+
brace_depth = max(0, brace_depth - 1)
|
|
1048
|
+
i += 1
|
|
1049
|
+
continue
|
|
1050
|
+
|
|
1051
|
+
inside_container = bool(paren_depth or bracket_depth or brace_depth)
|
|
1052
|
+
|
|
1053
|
+
if inside_container and (ch.isalpha() or ch == '_'):
|
|
1054
|
+
start = i
|
|
1055
|
+
i += 1
|
|
1056
|
+
while i < len(stmt) and (stmt[i].isalnum() or stmt[i] == '_'):
|
|
1057
|
+
i += 1
|
|
1058
|
+
name = stmt[start:i]
|
|
1059
|
+
|
|
1060
|
+
j = i
|
|
1061
|
+
while j < len(stmt) and stmt[j].isspace():
|
|
1062
|
+
j += 1
|
|
1063
|
+
|
|
1064
|
+
if j < len(stmt) and stmt[j] == '=':
|
|
1065
|
+
next_ch = stmt[j + 1] if j + 1 < len(stmt) else ''
|
|
1066
|
+
if next_ch != '=':
|
|
1067
|
+
names.add(name.lower())
|
|
1068
|
+
continue
|
|
1069
|
+
|
|
1070
|
+
i += 1
|
|
1071
|
+
|
|
1072
|
+
return names
|
|
1073
|
+
|
|
897
1074
|
# 检查表达式是否以分号结尾
|
|
898
1075
|
if expression.strip().endswith(';'):
|
|
899
1076
|
return False, "表达式不能以分号结尾"
|
|
@@ -908,51 +1085,13 @@ class ExpressionValidator:
|
|
|
908
1085
|
|
|
909
1086
|
# 处理每个赋值语句(除了最后一个)
|
|
910
1087
|
for i, stmt in enumerate(statements[:-1]):
|
|
911
|
-
|
|
912
|
-
if
|
|
913
|
-
return False, f"第{i+1}个语句必须是赋值语句(使用=符号)"
|
|
914
|
-
|
|
915
|
-
# 检查是否是比较操作符(==, !=, <=, >=)
|
|
916
|
-
if any(op in stmt for op in ['==', '!=', '<=', '>=']):
|
|
917
|
-
# 如果包含比较操作符,需要确认是否有赋值符号
|
|
918
|
-
# 使用临时替换法:将比较操作符替换为临时标记,再检查是否还有=
|
|
919
|
-
temp_stmt = stmt
|
|
920
|
-
for op in ['==', '!=', '<=', '>=']:
|
|
921
|
-
temp_stmt = temp_stmt.replace(op, '---')
|
|
922
|
-
|
|
923
|
-
if '=' not in temp_stmt:
|
|
924
|
-
return False, f"第{i+1}个语句必须是赋值语句,不能只是比较表达式"
|
|
925
|
-
|
|
926
|
-
# 找到第一个=符号(不是比较操作符的一部分)
|
|
927
|
-
# 先将比较操作符替换为临时标记,再找=
|
|
928
|
-
temp_stmt = stmt
|
|
929
|
-
for op in ['==', '!=', '<=', '>=']:
|
|
930
|
-
temp_stmt = temp_stmt.replace(op, '---')
|
|
931
|
-
|
|
932
|
-
if '=' not in temp_stmt:
|
|
1088
|
+
eq_positions = _top_level_equals_positions(stmt)
|
|
1089
|
+
if not eq_positions:
|
|
933
1090
|
return False, f"第{i+1}个语句必须是赋值语句(使用=符号)"
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
# 在原始语句中找到对应位置
|
|
939
|
-
real_equals_pos = 0
|
|
940
|
-
temp_count = 0
|
|
941
|
-
for char in stmt:
|
|
942
|
-
if temp_count == equals_pos:
|
|
943
|
-
break
|
|
944
|
-
if char in '!<>':
|
|
945
|
-
# 检查是否是比较操作符的一部分
|
|
946
|
-
if real_equals_pos + 1 < len(stmt) and stmt[real_equals_pos + 1] == '=':
|
|
947
|
-
# 是比较操作符,跳过两个字符
|
|
948
|
-
real_equals_pos += 2
|
|
949
|
-
temp_count += 3 # 因为替换成了三个字符的---
|
|
950
|
-
else:
|
|
951
|
-
real_equals_pos += 1
|
|
952
|
-
temp_count += 1
|
|
953
|
-
else:
|
|
954
|
-
real_equals_pos += 1
|
|
955
|
-
temp_count += 1
|
|
1091
|
+
if len(eq_positions) > 1:
|
|
1092
|
+
return False, f"第{i+1}个语句只能包含一个赋值符号(=)"
|
|
1093
|
+
|
|
1094
|
+
real_equals_pos = eq_positions[0]
|
|
956
1095
|
|
|
957
1096
|
# 分割变量名和值
|
|
958
1097
|
var_name = stmt[:real_equals_pos].strip()
|
|
@@ -969,9 +1108,12 @@ class ExpressionValidator:
|
|
|
969
1108
|
|
|
970
1109
|
# 检查变量值中使用的变量是否已经定义
|
|
971
1110
|
# 简单检查:提取所有可能的变量名
|
|
1111
|
+
kw_names = _keyword_arg_names(var_value)
|
|
972
1112
|
used_vars = re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b', var_value)
|
|
973
1113
|
for used_var in used_vars:
|
|
974
1114
|
used_var_lower = used_var.lower()
|
|
1115
|
+
if used_var_lower in kw_names:
|
|
1116
|
+
continue
|
|
975
1117
|
if used_var_lower not in variables:
|
|
976
1118
|
# 检查是否是函数名
|
|
977
1119
|
if used_var not in supported_functions:
|
|
@@ -994,19 +1136,16 @@ class ExpressionValidator:
|
|
|
994
1136
|
final_stmt = statements[-1]
|
|
995
1137
|
|
|
996
1138
|
# 检查最后一个语句是否是赋值语句
|
|
997
|
-
if
|
|
998
|
-
|
|
999
|
-
temp_stmt = final_stmt
|
|
1000
|
-
for op in ['==', '!=', '<=', '>=']:
|
|
1001
|
-
temp_stmt = temp_stmt.replace(op, '---')
|
|
1002
|
-
|
|
1003
|
-
if '=' in temp_stmt:
|
|
1004
|
-
return False, "最后一个语句不能是赋值语句"
|
|
1139
|
+
if _top_level_equals_positions(final_stmt):
|
|
1140
|
+
return False, "最后一个语句不能是赋值语句"
|
|
1005
1141
|
|
|
1006
1142
|
# 检查最后一个语句中使用的变量是否已经定义
|
|
1143
|
+
kw_names = _keyword_arg_names(final_stmt)
|
|
1007
1144
|
used_vars = re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b', final_stmt)
|
|
1008
1145
|
for used_var in used_vars:
|
|
1009
1146
|
used_var_lower = used_var.lower()
|
|
1147
|
+
if used_var_lower in kw_names:
|
|
1148
|
+
continue
|
|
1010
1149
|
if used_var_lower not in variables:
|
|
1011
1150
|
# 检查是否是函数名
|
|
1012
1151
|
if used_var not in supported_functions:
|