Files
hakorune/tools/hako_check/hako_source_checker.hako
nyash-codex 86489ffe43 Phase 21.3 WIP: Hako Source Checker improvements
## 🎯 Checker/Analyzer拡張

###  実装追加
- テストフレームワーク追加(tools/hako_check/tests/)
- ルール改善(HC003グローバルassign、HC040静的箱トップレベルassign)
- テストランナー(run_tests.sh)

### 🔧 Rust側修正
- AST utilities拡張(src/ast/utils.rs)
- MIR lowerers新設(src/mir/lowerers/)
- Parser制御フロー改善(src/parser/statements/control_flow.rs)
- Tokenizer識別子処理改善(src/tokenizer/lex_ident.rs)

### 📁 主要変更
- tools/hako_check/cli.hako - CLI改善
- tools/hako_check/hako_source_checker.hako - Checker core更新
- tools/hako_check/tests/ - NEW (テストケース追加)
- tools/hako_check/run_tests.sh - NEW (テストランナー)
- src/mir/lowerers/ - NEW (MIR lowering utilities)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-07 21:04:01 +09:00

142 lines
5.6 KiB
Plaintext

// hako_source_checker.hako — HakoSourceCheckerBox
// Purpose: Lint/structure checks for .hako sources (Phase 21.3)
// Rules (MVP):
// HC001: Forbid top-level assignment inside static box (before any method)
// HC002: Forbid include "..." lines (using+alias only)
// HC003: Using must be quoted (using "pkg.name" as Alias)
// HC004: Encourage JsonFragBox helpers for JSON scans (warn when substring/indexOf used with seg/inst_json)
using selfhost.shared.common.string_helpers as Str
using selfhost.shared.json.utils.json_frag as JsonFragBox
static box HakoSourceCheckerBox {
// Public: check a file path. Returns 0 on success; >0 on issues.
check_file(path) {
local f = new FileBox()
if f.open(path) == 0 { print("[lint/error] cannot open: " + path); return 2 }
local text = f.read(); f.close()
return me.check_source(text, path)
}
// Public: check raw source
check_source(text, path) {
local issues = new ArrayBox()
me._rule_include_forbidden(text, path, issues)
me._rule_using_quoted(text, path, issues)
me._rule_static_top_assign(text, path, issues)
me._rule_jsonfrag_usage(text, path, issues)
local n = issues.size()
if n > 0 {
local i=0; while i<n { print(issues.get(i)); i=i+1 }
return n
}
return 0
}
// HC002: include is forbidden
_rule_include_forbidden(text, path, out) {
local lines = text.split("\n")
local i=0; while i<lines.size() { local ln=lines.get(i); local trimmed=me._ltrim(ln); if trimmed.indexOf("include \"") == 0 { out.push("[HC002] include is forbidden (use using+alias): " + path + ":" + Str.int_to_str(i+1)) } i=i+1 }
}
// HC003: using must be quoted
_rule_using_quoted(text, path, out) {
local lines = text.split("\n")
local i=0; while i<lines.size() { local ln=lines.get(i); local t=me._ltrim(ln); if t.indexOf("using ") == 0 { if t.indexOf("using \"") != 0 { out.push("[HC003] using must be quoted: " + path + ":" + Str.int_to_str(i+1)) } } i=i+1 }
}
// HC001: static box top-level assignment (before any method) is forbidden
_rule_static_top_assign(text, path, out) {
local n = Str.len(text); local line = 1
local in_static = 0; local brace = 0; local in_method = 0
local i=0; while i<n { local c = text.substring(i, i+1)
// crude line counting
if c == "\n" { line = line + 1 }
// detect "static box"
if in_static == 0 {
if me._match_kw(text, i, "static box ") { in_static = 1; in_method = 0 }
}
if in_static == 1 {
// method start
if in_method == 0 && me._match_kw(text, i, "method ") { in_method = 1 }
// brace tracking
if c == "{" { brace = brace + 1 }
if c == "}" {
brace = brace - 1
if brace <= 0 { in_static = 0; in_method = 0 }
}
// assignment at column start (rough heuristic): letter at i and next '=' later
if in_method == 0 {
// find line start segment
local lstart = me._line_start(text, i)
local head = text.substring(lstart, i+1)
// only check at the first non-space of the line
if me._is_line_head(text, i) == 1 {
// identifier = ... is suspicious
if me._is_ident_start(c) == 1 {
// scan next few chars for '=' (up to EOL)
local seen_eq = 0
local off=0; while off<n { local j = i + 1 + off; if j>=n { break }; local cj=text.substring(j,j+1); if cj=="\n" { break }; if cj=="=" { seen_eq=1; break }; off=off+1 }
if seen_eq == 1 {
out.push("[HC001] top-level assignment in static box (use lazy init in method): " + path + ":" + Str.int_to_str(line))
}
}
}
}
i=i+1 }
}
// HC004: encourage JsonFragBox for JSON scans
_rule_jsonfrag_usage(text, path, out) {
// If the file manipulates mir_call/inst_json/seg and uses indexOf/substring heavily, warn.
local suspicious = 0
if text.indexOf("\"mir_call\"") >= 0 || text.indexOf("inst_json") >= 0 || text.indexOf(" seg") >= 0 {
if text.indexOf(".indexOf(") >= 0 || text.indexOf(".substring(") >= 0 { suspicious = 1 }
}
if suspicious == 1 && text.indexOf("JsonFragBox.") < 0 {
out.push("[HC004] JSON scan likely brittle; prefer JsonFragBox helpers: " + path)
}
}
// helpers
_ltrim(s) { return me._ltrim_chars(s, " \t") }
_ltrim_chars(s, cs) {
local n = Str.len(s)
local head = 0
local i=0; while i<n { local ch=s.substring(i,i+1); if ch!=" " && ch!="\t" { head=i; break }; if i==n-1 { head=n }; i=i+1 }
return s.substring(head)
}
_match_kw(s, i, kw) {
local k = Str.len(kw)
if i + k > Str.len(s) { return 0 }
if s.substring(i, i+k) == kw { return 1 }
return 0
}
_is_ident_start(c) {
// ASCII alpha or _
if c >= "A" && c <= "Z" { return 1 }
if c >= "a" && c <= "z" { return 1 }
if c == "_" { return 1 }
return 0
}
_is_line_head(s, i) {
// true if all chars before i on same line are spaces/tabs
local r=0; while r<=i { if i==0 { return 1 }; local j=i - 1 - r; local cj=s.substring(j,j+1); if cj=="\n" { return 1 }; if cj!=" " && cj!="\t" { return 0 }; if j==0 { return 1 }; r=r+1 }
return 1
}
_line_start(s, i) {
local r=0; while r<=i { local j=i-r; if j==0 { return 0 }; local cj=s.substring(j-1,j); if cj=="\n" { return j }; r=r+1 }
return 0
}
}
static box HakoSourceCheckerMain { method main(args) {
if args == null || args.size() < 1 {
print("[lint/error] require at least one path argument")
return 2
}
local fail = 0
local i=0; while i<args.size() { local p=args.get(i); local rc=HakoSourceCheckerBox.check_file(p); if rc!=0 { fail=fail+1 }; i=i+1 }
return fail
} }