#
#   Imandra Inc.
#
#   speclogician/modeling/__init__.py
#


import re
from typing import List

IML_TYPE_REGEX = re.compile(
    r"""
    ^\s*type\s+                    # 'type' keyword
    (?P<name>[a-zA-Z_][a-zA-Z0-9_]*)  # type name
    \s*=                            # '=' sign
    """,
    re.MULTILINE | re.VERBOSE,
)

def strip_iml_comments(text: str) -> str:
    """Remove OCaml-style (* ... *) and // comments."""
    # Remove block comments
    text = re.sub(r"\(\*.*?\*\)", "", text, flags=re.DOTALL)
    # Remove line comments
    text = re.sub(r"//.*", "", text)
    return text

def extract_declared_types(iml_text: str) -> List[str]:
    """Return all declared type names in an IML file."""
    clean = strip_iml_comments(iml_text)
    return [m.group("name") for m in IML_TYPE_REGEX.finditer(clean)]
