Files
hakorune/tools/hako_check/rules/rule_brace_heuristics.hako

119 lines
2.9 KiB
Plaintext
Raw Normal View History

// tools/hako_check/rules/rule_brace_heuristics.hako — HC031: Brace Heuristics
// Detects rough brace mismatch ({/}) for early warning.
// This is a heuristic check, not a full parser.
static box RuleBraceHeuristicsBox {
method apply(text, path, out) {
if text == null { return 0 }
local open_count = 0
local close_count = 0
local lines = me._split_lines(text)
local i = 0
while i < lines.size() {
local ln = lines.get(i)
// Skip comments
local comment_pos = ln.indexOf("//")
local scan_text = ln
if comment_pos >= 0 {
scan_text = ln.substring(0, comment_pos)
}
// Remove string literals (simple heuristic: remove content between quotes)
scan_text = me._remove_strings(scan_text)
// Count braces
local j = 0
while j < scan_text.length() {
local ch = scan_text.substring(j, j+1)
if ch == "{" { open_count = open_count + 1 }
if ch == "}" { close_count = close_count + 1 }
j = j + 1
}
i = i + 1
}
// Check for mismatch
if open_count != close_count {
local msg = "[HC031] brace mismatch - " + me._itoa(open_count) + " open, "
msg = msg + me._itoa(close_count) + " close"
// File-level diagnostic, report on line 1
out.push(msg + " :: " + path + ":1")
}
return out.size()
}
_remove_strings(s) {
// Simple heuristic: remove content between double quotes
if s == null { return "" }
local result = ""
local in_string = 0
local i = 0
while i < s.length() {
local ch = s.substring(i, i+1)
// Toggle string mode on unescaped quotes
if ch == "\"" {
// Simple check: not escaped (not preceded by \)
local escaped = 0
if i > 0 {
local prev = s.substring(i-1, i)
if prev == "\\" { escaped = 1 }
}
if escaped == 0 {
if in_string == 0 { in_string = 1 } else { in_string = 0 }
}
}
// Keep character if not in string
if in_string == 0 {
result = result + ch
} else {
// Replace string content with space to preserve structure
result = result + " "
}
i = i + 1
}
return result
}
_split_lines(s) {
local arr = new ArrayBox()
if s == null { return arr }
local n = s.length()
local last = 0
local i = 0
loop(i < n) {
local ch = s.substring(i, i+1)
if ch == "\n" {
arr.push(s.substring(last, i))
last = i + 1
}
i = i + 1
}
if last <= n { arr.push(s.substring(last)) }
return arr
}
_itoa(n) {
local v = 0 + n
if v == 0 { return "0" }
local out = ""
local digits = "0123456789"
local tmp = ""
while v > 0 {
local d = v % 10
tmp = digits.substring(d, d+1) + tmp
v = v / 10
}
out = tmp
return out
}
}
static box RuleBraceHeuristicsMain { method main(args) { return 0 } }