2025-11-07 19:32:44 +09:00
|
|
|
static box RuleStaticTopAssignBox {
|
|
|
|
|
apply(text, path, out) {
|
2025-11-08 00:46:34 +09:00
|
|
|
local n = text.length(); local line = 1
|
2025-11-07 19:32:44 +09:00
|
|
|
local in_static = 0; local brace = 0; local in_method = 0
|
|
|
|
|
local i = 0
|
|
|
|
|
while i < n {
|
|
|
|
|
local c = text.substring(i, i+1)
|
|
|
|
|
if c == "\n" { line = line + 1 }
|
|
|
|
|
if in_static == 0 {
|
|
|
|
|
if me._match_kw(text, i, "static box ") { in_static = 1; in_method = 0 }
|
|
|
|
|
}
|
|
|
|
|
if in_static == 1 {
|
|
|
|
|
if in_method == 0 && me._match_kw(text, i, "method ") { in_method = 1 }
|
|
|
|
|
if c == "{" { brace = brace + 1 }
|
|
|
|
|
if c == "}" { brace = brace - 1; if brace <= 0 { in_static = 0; in_method = 0 } }
|
|
|
|
|
if in_method == 0 {
|
|
|
|
|
if me._is_line_head(text, i) == 1 {
|
|
|
|
|
if me._is_ident_start(c) == 1 {
|
|
|
|
|
// find '=' before EOL
|
|
|
|
|
local seen_eq = 0
|
2025-11-07 21:04:01 +09:00
|
|
|
local off = 0; while off < n {
|
2025-11-07 19:32:44 +09:00
|
|
|
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 }
|
2025-11-07 21:04:01 +09:00
|
|
|
off = off + 1 }
|
2025-11-07 19:32:44 +09:00
|
|
|
if seen_eq == 1 {
|
2025-11-08 00:46:34 +09:00
|
|
|
out.push("[HC001] top-level assignment in static box (use lazy init in method): " + path + ":" + ("" + line))
|
2025-11-07 19:32:44 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i = i + 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-08 00:46:34 +09:00
|
|
|
_match_kw(s,i,kw) { local k=kw.length(); if i+k>s.length() { return 0 }; if s.substring(i,i+k)==kw { return 1 } return 0 }
|
2025-11-07 19:32:44 +09:00
|
|
|
_is_ident_start(c) { if c=="_" {return 1}; if c>="A"&&c<="Z" {return 1}; if c>="a"&&c<="z" {return 1}; return 0 }
|
|
|
|
|
_is_line_head(s,i) {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static box RuleStaticTopAssignMain { method main(args) { return 0 } }
|