diff options
Diffstat (limited to 'scripts/ts_parser.py')
| -rw-r--r-- | scripts/ts_parser.py | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/scripts/ts_parser.py b/scripts/ts_parser.py index 2ad3629..31d926d 100644 --- a/scripts/ts_parser.py +++ b/scripts/ts_parser.py @@ -41,6 +41,19 @@ class FuncMeta: self.struct_fields = struct_fields or [] +def _strip_strings_comments(text): + """Remove C string literals and comments to avoid false brace matches.""" + # Remove block comments + text = re.sub(r"/\*.*?\*/", "", text, flags=re.DOTALL) + # Remove line comments + text = re.sub(r"//.*", "", text) + # Remove string literals + text = re.sub(r'"(?:[^"\\]|\\.)*"', '""', text) + # Remove character literals + text = re.sub(r"'(?:[^'\\]|\\.)*'", "''", text) + return text + + def _detect_actual_x_n(content, match_end, name): """Detect actual x[N] access in function body.""" remaining = content[match_end:] @@ -48,18 +61,21 @@ def _detect_actual_x_n(content, match_end, name): if not func_def: return 0 brace_pos = remaining.find("{", func_def.start()) + # Strip strings and comments for safe brace matching + cleaned = _strip_strings_comments(remaining[brace_pos:]) depth = 0 - func_body = "" - for i in range(brace_pos, len(remaining)): - if remaining[i] == "{": + end_pos = 0 + for i, ch in enumerate(cleaned): + if ch == "{": depth += 1 - elif remaining[i] == "}": + elif ch == "}": depth -= 1 if depth == 0: - func_body = remaining[brace_pos : i + 1] + end_pos = i break + func_body = remaining[brace_pos : brace_pos + end_pos + 1] if end_pos else "" indices = [ - int(xm.group(1)) for xm in re.finditer(r"x\s*\[\s*(\d+)\s*\]", func_body) + int(xm.group(1)) for xm in re.finditer(r"\bx\s*\[\s*(\d+)\s*\]", func_body) ] return max(indices) + 1 if indices else 0 @@ -133,11 +149,6 @@ def parse_all(detect_x_access=True): actual = _detect_actual_x_n(content, m.end(), name) if actual > x_n: x_n = actual - if x_n < OHLCV_COUNT and re.search( - r"\bx\s*\[\s*" + str(VOLUME_INDEX) + r"\s*\]", - content[m.end() :], - ): - x_n = OHLCV_COUNT struct_fields = _parse_struct_fields(content, name) if c_n > 0 else [] @@ -153,7 +164,7 @@ def parse_names_only(): Dict mapping name -> {"x_n": int, "y_n": int}. """ meta = {} - for fm in parse_all(detect_x_access=True): + for fm in parse_all(detect_x_access=False): if fm.name not in meta: meta[fm.name] = {"x_n": fm.x_n, "y_n": fm.y_n} return meta |