feat: single compact notification and on-disk code auto-healing for semicolons
This commit is contained in:
1 parent
e8b93c7d7e
commit
774b8193a5
2 files changed
+68
-35
No files matched your search
+38
-2
@@ -94,7 +94,41 @@ def is_balanced(code):
|
||||
|
||||
return (braces <= 0 and parens <= 0 and brackets <= 0 and not in_single_quote and not in_double_quote)
|
||||
|
||||
def run_code(source_code, evaluator=None):
|
||||
def auto_insert_semicolons_into_file(filepath, lines_to_fix):
|
||||
if not filepath or not os.path.exists(filepath):
|
||||
return
|
||||
try:
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
content = f.readlines()
|
||||
modified = False
|
||||
for line_no in lines_to_fix:
|
||||
idx = line_no - 1
|
||||
if 0 <= idx < len(content):
|
||||
raw_line = content[idx]
|
||||
newline_char = "\n" if raw_line.endswith("\n") else ""
|
||||
stripped = raw_line.rstrip("\r\n")
|
||||
|
||||
code_part = stripped
|
||||
comment_part = ""
|
||||
for sep in ("//", "#"):
|
||||
if sep in stripped:
|
||||
parts = stripped.split(sep, 1)
|
||||
code_part = parts[0]
|
||||
comment_part = sep + parts[1]
|
||||
break
|
||||
code_stripped = code_part.rstrip()
|
||||
if code_stripped and not code_stripped.endswith(";"):
|
||||
code_stripped += ";"
|
||||
spacing = " " if comment_part else ""
|
||||
content[idx] = f"{code_stripped}{spacing}{comment_part}{newline_char}"
|
||||
modified = True
|
||||
if modified:
|
||||
with open(filepath, "w", encoding="utf-8") as f:
|
||||
f.writelines(content)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def run_code(source_code, evaluator=None, filepath=None):
|
||||
if evaluator is None:
|
||||
evaluator = Evaluator()
|
||||
try:
|
||||
@@ -102,6 +136,8 @@ def run_code(source_code, evaluator=None):
|
||||
tokens = lexer.tokenize()
|
||||
parser = Parser(tokens)
|
||||
ast = parser.parse()
|
||||
if filepath and parser.missing_semicolon_lines:
|
||||
auto_insert_semicolons_into_file(filepath, parser.missing_semicolon_lines)
|
||||
return evaluator.eval(ast)
|
||||
except SyntaxError as e:
|
||||
print(f"\033[1;31m[!] SyntaxError:\033[0m {e}")
|
||||
@@ -123,7 +159,7 @@ def run_file(filepath):
|
||||
pass
|
||||
with open(filepath, "r", encoding="utf-8") as f:
|
||||
code = f.read()
|
||||
run_code(code)
|
||||
run_code(code, filepath=filepath)
|
||||
|
||||
def start_repl():
|
||||
print(ASCII_BANNER)
|
||||
|
||||
+30
-33
@@ -5,7 +5,7 @@ class Parser:
|
||||
def __init__(self, tokens):
|
||||
self.tokens = tokens
|
||||
self.pos = 0
|
||||
self.warned_semicolons = set()
|
||||
self.missing_semicolon_lines = []
|
||||
|
||||
def current(self):
|
||||
return self.tokens[self.pos]
|
||||
@@ -34,38 +34,7 @@ class Parser:
|
||||
return
|
||||
if line is None:
|
||||
line = self.tokens[self.pos - 1].line if self.pos > 0 else self.current().line
|
||||
self.trigger_semicolon_recovery(line)
|
||||
|
||||
def trigger_semicolon_recovery(self, line):
|
||||
if line in self.warned_semicolons:
|
||||
return
|
||||
self.warned_semicolons.add(line)
|
||||
|
||||
CYAN = "\033[1;36m"
|
||||
YELLOW = "\033[1;33m"
|
||||
MAGENTA = "\033[1;35m"
|
||||
RESET = "\033[0m"
|
||||
BOLD = "\033[1m"
|
||||
|
||||
ai_tag = ""
|
||||
try:
|
||||
from stdlib_modules import _GLOBAL_AI_STATE
|
||||
has_key = any(v for k, v in _GLOBAL_AI_STATE["keys"].items() if k != "ollama" and v)
|
||||
if has_key:
|
||||
active_p = _GLOBAL_AI_STATE.get("active_provider", "ai").upper()
|
||||
ai_tag = f" [AI Engine: {active_p}]"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
warning = (
|
||||
f"\n{CYAN}✨ [Aira Compiler Auto-Fix{ai_tag}]:{RESET}\n"
|
||||
f"{YELLOW}Hey there! I am Aira From Aira Group Of Technology by Adam Eehan.{RESET}\n"
|
||||
f"Heyyy, here you missed a semicolon (;) at {BOLD}line {line}{RESET}!\n"
|
||||
f"Please concentrate on your code, now you can relax, I've put it automatically.\n"
|
||||
f"{MAGENTA}Take care and Enjoy your Coding 💻✨🚀{RESET}\n"
|
||||
)
|
||||
sys.stderr.write(warning)
|
||||
sys.stderr.flush()
|
||||
self.missing_semicolon_lines.append(line)
|
||||
|
||||
def parse_block(self):
|
||||
self.expect("LBRACE")
|
||||
@@ -83,6 +52,34 @@ class Parser:
|
||||
stmt = self.parse_statement()
|
||||
if stmt:
|
||||
statements.append(stmt)
|
||||
|
||||
if self.missing_semicolon_lines and not getattr(self, "suppress_semicolon_notice", False):
|
||||
unique_lines = sorted(set(self.missing_semicolon_lines))
|
||||
lines_str = ", ".join(str(l) for l in unique_lines)
|
||||
|
||||
CYAN = "\033[1;36m"
|
||||
YELLOW = "\033[1;33m"
|
||||
MAGENTA = "\033[1;35m"
|
||||
RESET = "\033[0m"
|
||||
BOLD = "\033[1m"
|
||||
|
||||
ai_tag = ""
|
||||
try:
|
||||
from stdlib_modules import _GLOBAL_AI_STATE
|
||||
has_key = any(v for k, v in _GLOBAL_AI_STATE["keys"].items() if k != "ollama" and v)
|
||||
if has_key:
|
||||
active_p = _GLOBAL_AI_STATE.get("active_provider", "ai").upper()
|
||||
ai_tag = f" [{active_p}]"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
single_notice = (
|
||||
f"\n{CYAN}✨ [Aira Auto-Fix{ai_tag}]:{RESET} {YELLOW}Hey there! Semicolon (;) missed at line {lines_str}.{RESET} "
|
||||
f"I've put it automatically into your code — relax & enjoy coding! {MAGENTA}💻✨🚀{RESET}\n"
|
||||
)
|
||||
sys.stderr.write(single_notice)
|
||||
sys.stderr.flush()
|
||||
|
||||
return ProgramNode(statements)
|
||||
|
||||
def parse_statement(self):
|
||||
|
||||
Reference in new issue
Block a user