2025-10-31 20:18:39 +09:00
|
|
|
|
// UsingResolverBox — static, stateful resolver helpers(インスタンス禁止・VM互換)
|
2025-11-21 06:25:17 +09:00
|
|
|
|
// Boundary memo:
|
|
|
|
|
|
// - entry/using_resolver_box: file I/O + using 収集、modules_list を MapBox へ積む役。
|
|
|
|
|
|
// - pipeline_v2/using_resolver_box: modules_json 上で alias/path をテキスト検索で解決する役。
|
|
|
|
|
|
// RegexFlow の単一路ループのみで continue/backedge 分岐を持たないため、Region+next_i 化は不要と判断。
|
2025-10-31 20:18:39 +09:00
|
|
|
|
// State layout (Map): {
|
|
|
|
|
|
// alias_paths: Map, alias_names: Map, alias_keys: Array,
|
|
|
|
|
|
// modules_map: Map, modules_keys: Array
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2025-11-02 04:13:17 +09:00
|
|
|
|
using selfhost.shared.common.string_helpers as StringHelpers
|
|
|
|
|
|
using selfhost.shared.common.box_helpers as BoxHelpers
|
|
|
|
|
|
using lang.compiler.pipeline_v2.regex_flow as RegexFlow
|
2025-10-31 20:18:39 +09:00
|
|
|
|
|
|
|
|
|
|
static box UsingResolverBox {
|
|
|
|
|
|
// Lightweight state as String: holds modules_json only
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Format: modules_json (raw JSON string from nyash.toml [modules])
|
2025-10-31 20:18:39 +09:00
|
|
|
|
state_new() { return "" }
|
|
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Load using.aliases JSON (not used in minimal impl, kept for compatibility)
|
2025-10-31 20:18:39 +09:00
|
|
|
|
load_usings_json(state, usings_json) { return state }
|
|
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Load modules JSON from nyash.toml [modules] section
|
|
|
|
|
|
// Expected format: {"module.path": "file.hako", ...}
|
|
|
|
|
|
load_modules_json(state, mod_json) {
|
|
|
|
|
|
if mod_json == null { return "" }
|
|
|
|
|
|
return ("" + mod_json)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve path from alias using modules map
|
|
|
|
|
|
// Returns the file path if alias matches a module key, null otherwise
|
|
|
|
|
|
resolve_path_alias(state, alias) {
|
|
|
|
|
|
if alias == null { return null }
|
|
|
|
|
|
local s = "" + state
|
|
|
|
|
|
if s.length() == 0 { return null }
|
|
|
|
|
|
|
|
|
|
|
|
// Search for exact match in modules JSON
|
|
|
|
|
|
// Format: "alias":"path"
|
|
|
|
|
|
local search_key = "\"" + alias + "\""
|
|
|
|
|
|
local pos = RegexFlow.find_from(s, search_key, 0)
|
|
|
|
|
|
if pos < 0 { return null }
|
|
|
|
|
|
|
|
|
|
|
|
// Find the colon after the key
|
|
|
|
|
|
local colon_pos = RegexFlow.find_from(s, ":", pos)
|
|
|
|
|
|
if colon_pos < 0 { return null }
|
|
|
|
|
|
|
|
|
|
|
|
// Find the opening quote of the value
|
|
|
|
|
|
local val_start = RegexFlow.find_from(s, "\"", colon_pos)
|
|
|
|
|
|
if val_start < 0 { return null }
|
|
|
|
|
|
|
|
|
|
|
|
// Find the closing quote of the value
|
|
|
|
|
|
local val_end = RegexFlow.find_from(s, "\"", val_start + 1)
|
|
|
|
|
|
if val_end < 0 { return null }
|
2025-10-31 20:18:39 +09:00
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Extract and return the path value
|
|
|
|
|
|
return s.substring(val_start + 1, val_end)
|
|
|
|
|
|
}
|
2025-10-31 20:18:39 +09:00
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Resolve namespace alias by tail matching
|
|
|
|
|
|
// Returns the full module path if unique match found, null if ambiguous or not found
|
2025-10-31 20:18:39 +09:00
|
|
|
|
resolve_namespace_alias(state, alias) {
|
|
|
|
|
|
if alias == null { return null }
|
|
|
|
|
|
local s = "" + state
|
2025-11-13 18:11:25 +09:00
|
|
|
|
if s.length() == 0 { return null }
|
|
|
|
|
|
|
2025-10-31 20:18:39 +09:00
|
|
|
|
// Prefer unique tail match by last segment
|
|
|
|
|
|
local i = 0
|
|
|
|
|
|
local start = 0
|
|
|
|
|
|
local found = null
|
|
|
|
|
|
loop(true) {
|
|
|
|
|
|
local kpos = RegexFlow.find_from(s, "\"", start)
|
|
|
|
|
|
if kpos < 0 { break }
|
|
|
|
|
|
local kend = RegexFlow.find_from(s, "\"", kpos + 1)
|
|
|
|
|
|
if kend < 0 { break }
|
|
|
|
|
|
local key = s.substring(kpos + 1, kend)
|
|
|
|
|
|
local dot = RegexFlow.last_index_of(key, ".")
|
|
|
|
|
|
local last = key
|
2025-11-01 13:28:56 +09:00
|
|
|
|
if dot >= 0 { last = key.substring(dot + 1, key.length()) }
|
2025-10-31 20:18:39 +09:00
|
|
|
|
if last == alias {
|
|
|
|
|
|
if found == null { found = key } else { return null }
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// first-letter case-insensitive match
|
2025-11-01 13:28:56 +09:00
|
|
|
|
if last.length() == alias.length() && last.length() > 0 {
|
2025-10-31 20:18:39 +09:00
|
|
|
|
local l0 = last.substring(0,1)
|
|
|
|
|
|
local a0 = alias.substring(0,1)
|
2025-11-01 13:28:56 +09:00
|
|
|
|
local restl = last.substring(1, last.length())
|
|
|
|
|
|
local resta = alias.substring(1, alias.length())
|
2025-10-31 20:18:39 +09:00
|
|
|
|
if restl == resta {
|
|
|
|
|
|
local U = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; local L = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
local idxL = L.indexOf(l0); local idxU = U.indexOf(l0)
|
|
|
|
|
|
if (idxL >= 0 && U.substring(idxL, idxL+1) == a0) || (idxU >= 0 && L.substring(idxU, idxU+1) == a0) {
|
|
|
|
|
|
if found == null { found = key } else { return null }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
start = kend + 1
|
|
|
|
|
|
}
|
|
|
|
|
|
return found
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Resolve module path from alias (delegates to resolve_path_alias)
|
|
|
|
|
|
resolve_module_path_from_alias(state, alias) {
|
|
|
|
|
|
return me.resolve_path_alias(state, alias)
|
|
|
|
|
|
}
|
2025-10-31 20:18:39 +09:00
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Guess namespace from tail segment (delegates to resolve_namespace_alias)
|
|
|
|
|
|
guess_namespace_from_tail(state, tail) {
|
|
|
|
|
|
return me.resolve_namespace_alias(state, tail)
|
|
|
|
|
|
}
|
2025-10-31 20:18:39 +09:00
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// No-op for minimal implementation
|
2025-10-31 20:18:39 +09:00
|
|
|
|
upgrade_aliases(state) { return 0 }
|
|
|
|
|
|
|
2025-11-13 18:11:25 +09:00
|
|
|
|
// Convert state to context JSON for ParserBox
|
|
|
|
|
|
// Format: {"modules": {...}, "aliases": {}}
|
|
|
|
|
|
to_context_json(state) {
|
|
|
|
|
|
if state == null { return "{\"modules\":{},\"aliases\":{}}" }
|
|
|
|
|
|
local s = "" + state
|
|
|
|
|
|
if s.length() == 0 { return "{\"modules\":{},\"aliases\":{}}" }
|
|
|
|
|
|
|
|
|
|
|
|
// Wrap modules_json in context structure
|
|
|
|
|
|
return "{\"modules\":" + s + ",\"aliases\":{}}"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper to convert map to JSON (minimal stub)
|
2025-10-31 20:18:39 +09:00
|
|
|
|
map_to_json(m) { return "{}" }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static box UsingResolverBoxMain { main(args) { return 0 } }
|