import t, c
from stdint import *
import memhub
import string

# 导入子模块
from .tokens import (
    Token, TokenType, Keyword, TokOp, _init_tables,
)
# 公共部分：基类、枚举、常量、辅助函数、dump
from .base import (
    AST, ASTKind, ASTCtx, OpKind, ASTFlag,
    # 常量
    CONST_INT, CONST_FLOAT, CONST_STR, CONST_BOOL, CONST_NONE,
    FLAG_IS_ASYNC, FLAG_SIMPLE, FLAG_HAS_STAR,
    # 辅助函数
    _init_ast, _copy_str, _set_pos, _inherit_pos, _set_parent_list,
    _binop_from_op, _augop_from_op, _cmpop_from_op,
    _emit, _emit_str, _emit_int, _dump_list,
    dump,
)
# 语句节点
from .stmts import (
    # 模块层
    Module, Expression, Interactive, FunctionType,
    # 语句
    FunctionDef, ClassDef, Return, Delete, Assign, AugAssign, AnnAssign,
    For, While, If, With, Raise, Try, Assert, Global, Nonlocal,
    Pass, Break, Continue, Expr, Import, ImportFrom, Match,
)
# 表达式节点
from .exprs import (
    # 表达式
    BoolOp, BinOp, UnaryOp, Lambda, IfExp, Dict, Set,
    ListComp, SetComp, DictComp, GeneratorExp,
    Await, Yield, YieldFrom, FormattedValue, JoinedStr,
    Constant, NamedExpr, Attribute, Subscript, Starred, Name,
    List, Tuple, Slice,
    Call, Compare, OpNode,
)
# 辅助节点
from .astaux import (
    ExceptHandler, Arguments, Arg, Keyword, Alias, WithItem,
    Comprehension,
)
# 模式匹配
from .match import (
    MatchCase, MatchValue, MatchSingleton, MatchSequence,
    MatchMapping, MatchClass, MatchStar, MatchAs, MatchOr,
)
from .visitor import ASTVisitor
from .lexer import Lexer, _lexer_init, tokenize, new_lexer
from .parser import Parser, _parser_init, parse_tokens, new_parser
from .parser import _parse_test

# ============================================================
# 全局内存池指针（遵循项目规则：声明 _mbuddy）
# 用户在 main 中初始化：ast._mbuddy = memhub.MemBuddy(arena, size)
# ============================================================
_mbuddy: memhub.MemBuddy | t.CPtr


# ============================================================
# 公共 API: parse
# ============================================================

def parse(src: str, pool: memhub.MemManager | t.CPtr) -> AST | t.CPtr:
    """解析 Python 源代码，返回 AST Module 节点。

    参数:
        src: NUL 结尾的源代码字符串
        pool: memhub.MemManager 实例，用于分配 AST 节点

    返回:
        AST Module 节点，失败返回 None
    """
    if src is None or pool is None:
        return None
    _init_tables(pool)
    lx: Lexer | t.CPtr = new_lexer(pool)
    if lx is None:
        return None
    _lexer_init(lx, src, pool)
    tokens: Token | t.CPtr = tokenize(lx)
    if tokens is None:
        return None
    tree: AST | t.CPtr = parse_tokens(pool, tokens)
    return tree


def parse_expression(src: str, pool: memhub.MemManager | t.CPtr) -> AST | t.CPtr:
    """解析单个表达式，返回 Expression 节点"""
    if src is None or pool is None:
        return None
    _init_tables(pool)
    lx: Lexer | t.CPtr = new_lexer(pool)
    if lx is None: return None
    _lexer_init(lx, src, pool)
    tokens: Token | t.CPtr = tokenize(lx)
    if tokens is None: return None
    ps: Parser | t.CPtr = new_parser(pool)
    if ps is None: return None
    _parser_init(ps, tokens, pool)
    body: AST | t.CPtr = _parse_test(ps)
    return Expression(pool, body)
