Files

70 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Compteur de profondeur (parentheses) par ligne, strings/comments maskees.
Usage: parencheck.py <fichier.java> [start] [end]
Exige FINAL: 0."""
import sys
path = sys.argv[1]
start = int(sys.argv[2]) if len(sys.argv) > 2 else 1
end = int(sys.argv[3]) if len(sys.argv) > 3 else None
with open(path) as f:
lines = f.readlines()
depth = 0
in_block = False
for i, raw in enumerate(lines, 1):
line = raw.rstrip("\n")
masked = []
j = 0
in_str = False
in_char = False
while j < len(line):
if in_block:
if line[j:j+2] == "*/":
in_block = False
j += 2
else:
j += 1
continue
if in_str:
if line[j] == "\\":
j += 2
continue
if line[j] == '"':
in_str = False
j += 1
continue
if in_char:
if line[j] == "\\":
j += 2
continue
if line[j] == "'":
in_char = False
j += 1
continue
if line[j:j+2] == "//":
break
if line[j:j+2] == "/*":
in_block = True
j += 2
continue
if line[j] == '"':
in_str = True
j += 1
continue
if line[j] == "'":
in_char = True
j += 1
continue
masked.append(line[j])
j += 1
code = "".join(masked)
opens = code.count("(") - code.count(")")
depth += opens
if (end is None or (start <= i <= end)) and (opens != 0 or "Commands.literal" in line
or "dispatcher.register" in line or ".executes(" in line):
print(f"{i:5d} d={depth:+3d} ({opens:+3d}) {line.strip()[:100]}")
print(f"FINAL: {depth}")