This commit is contained in:
2026-05-25 22:24:53 -06:00
parent ed8a2f575f
commit c6b368dc82
8 changed files with 116 additions and 40 deletions

View File

@@ -2,15 +2,32 @@ from .base import BaseEngine
class CountNodeEngine(BaseEngine):
def check(self, tree, rule, language, mapping):
def _message(self, rule, count):
target = rule["target"]
node_type = mapping.get(target, target)
nodes = self.collect_nodes(tree.root_node, node_type)
count = len(nodes)
min_count = rule.get("min")
max_count = rule.get("max")
if min_count is not None and count < min_count:
return [rule.get("message", f"{target} 至少出现 {min_count} 次,当前 {count}")]
return rule.get("message") or f"{target} 至少出现 {min_count} 次,当前 {count}"
if max_count is not None and count > max_count:
return [rule.get("message", f"{target} 至多出现 {max_count} 次,当前 {count}")]
return []
return rule.get("message") or f"{target} 至多出现 {max_count} 次,当前 {count}"
return None
def check(self, tree, rule, language, mapping):
target = rule["target"]
node_type = mapping.get(target, target)
count = len(self.collect_nodes(tree.root_node, node_type))
msg = self._message(rule, count)
return [msg] if msg else []
def describe(self, rule, language, mapping):
target = rule["target"]
min_count = rule.get("min")
max_count = rule.get("max")
if rule.get("message"):
return rule["message"]
parts = []
if min_count is not None:
parts.append(f"至少 {min_count}")
if max_count is not None:
parts.append(f"至多 {max_count}")
return f"{target} " + "".join(parts)