主な変更: - ✅ HC011 (dead methods) 実装・テスト緑 - ✅ HC016 (unused alias) 実装・テスト緑 - ✅ HC017 (non-ascii quotes) 実装完了 - 🔧 tokenizer/parser_core 強化(AST優先ルート) - 🛡️ plugin_guard.rs 追加(stderr専用出力) - 📋 テストインフラ整備(run_tests.sh改善) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
98 lines
4.2 KiB
Plaintext
98 lines
4.2 KiB
Plaintext
// tools/hako_parser/parser_core.hako — HakoParserCoreBox (token-based MVP)
|
|
using selfhost.shared.common.string_helpers as Str
|
|
using tools.hako_parser.tokenizer as HakoTokenizerBox
|
|
|
|
static box HakoParserCoreBox {
|
|
// Parse .hako source into minimal AST map:
|
|
// {
|
|
// uses: Array<String>,
|
|
// boxes: Array<{name,is_static,methods:Array<{name,arity,span}>}>
|
|
// }
|
|
parse(text) {
|
|
local ast = new MapBox()
|
|
ast.set("uses", new ArrayBox())
|
|
ast.set("boxes", new ArrayBox())
|
|
ast.set("includes", new ArrayBox())
|
|
if text == null { return ast }
|
|
|
|
local toks = HakoTokenizerBox.tokenize(text)
|
|
local p = 0
|
|
local N = toks.size()
|
|
|
|
// Parse stream (single pass, tolerant)
|
|
while p < N {
|
|
local t = me._peek(toks, p, N)
|
|
if me._eq(t, "USING") == 1 {
|
|
// using "mod" (as Alias)?
|
|
p = me._advance(p, N)
|
|
local t1 = me._peek(toks, p, N)
|
|
if me._eq(t1, "STRING") == 1 {
|
|
ast.get("uses").push(t1.get("lexeme")); p = me._advance(p, N)
|
|
// optional: as Alias
|
|
local t2 = me._peek(toks, p, N); if me._eq(t2, "AS") == 1 { p = me._advance(p, N); local t3=me._peek(toks, p, N); if me._eq(t3, "IDENT")==1 { p = me._advance(p, N) } }
|
|
} else {
|
|
// tolerate malformed using; skip token
|
|
}
|
|
continue
|
|
}
|
|
if me._eq(t, "INCLUDE") == 1 {
|
|
// include "path"
|
|
p = me._advance(p, N); local s=me._peek(toks, p, N); if me._eq(s, "STRING") == 1 { ast.get("includes").push(Str.int_to_str(s.get("line"))); p = me._advance(p, N) }
|
|
continue
|
|
}
|
|
if me._eq(t, "STATIC") == 1 {
|
|
// static box Name { methods }
|
|
// STATIC BOX IDENT LBRACE ... RBRACE
|
|
local save = p
|
|
p = me._advance(p, N) // STATIC
|
|
local tb = me._peek(toks, p, N); if me._eq(tb, "BOX") == 0 { p = save + 1; continue } p = me._advance(p, N)
|
|
local tn = me._peek(toks, p, N); if me._eq(tn, "IDENT") == 0 { continue }
|
|
local box_name = tn.get("lexeme"); p = me._advance(p, N)
|
|
// expect '{'
|
|
local tl = me._peek(toks, p, N); if me._eq(tl, "LBRACE") == 0 { continue } p = me._advance(p, N)
|
|
// register box
|
|
local b = new MapBox(); b.set("name", box_name); b.set("is_static", 1); b.set("methods", new ArrayBox()); ast.get("boxes").push(b)
|
|
// scan until matching RBRACE (flat, tolerate nested braces count)
|
|
local depth = 1
|
|
while p < N && depth > 0 {
|
|
local tk = me._peek(toks, p, N)
|
|
if me._eq(tk, "LBRACE") == 1 { depth = depth + 1; p = me._advance(p, N); continue }
|
|
if me._eq(tk, "RBRACE") == 1 { depth = depth - 1; p = me._advance(p, N); if depth == 0 { break } else { continue } }
|
|
// method
|
|
if me._eq(tk, "METHOD") == 1 {
|
|
local mline = tk.get("line"); p = me._advance(p, N)
|
|
local mid = me._peek(toks, p, N); if me._eq(mid, "IDENT") == 0 { continue }
|
|
local mname = mid.get("lexeme"); p = me._advance(p, N)
|
|
// params
|
|
local lp = me._peek(toks, p, N); if me._eq(lp, "LPAREN") == 0 { continue } p = me._advance(p, N)
|
|
// count commas until RPAREN (no nesting inside params for MVP)
|
|
local arity = 0; local any = 0
|
|
while p < N {
|
|
local tt = me._peek(toks, p, N)
|
|
if me._eq(tt, "RPAREN") == 1 { p = me._advance(p, N); break }
|
|
if me._eq(tt, "COMMA") == 1 { arity = arity + 1; p = me._advance(p, N); any = 1; continue }
|
|
// consume any token inside params
|
|
p = me._advance(p, N); any = 1
|
|
}
|
|
if any == 1 && arity == 0 { arity = 1 }
|
|
// record method
|
|
local m = new MapBox(); m.set("name", mname); m.set("arity", arity); m.set("span", mline)
|
|
b.get("methods").push(m)
|
|
continue
|
|
}
|
|
p = me._advance(p, N)
|
|
}
|
|
continue
|
|
}
|
|
// skip unhandled token
|
|
p = me._advance(p, N)
|
|
}
|
|
return ast
|
|
}
|
|
_peek(toks, idx, N) { if idx >= N { return null } return toks.get(idx) }
|
|
_eq(t, kind) { if t == null { return 0 } if t.get("type") == kind { return 1 } return 0 }
|
|
_advance(p, N) { if p < N { return p + 1 } return p }
|
|
}
|
|
|
|
static box HakoParserCoreMain { method main(args) { return 0 } }
|