/* =====================================
 * Generated by Mod-c 0.26.5
 * HEADER parser_main MODULE
 * ===================================== */

#ifndef MODC_MODULE_parser_main_H
#define MODC_MODULE_parser_main_H

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

#define nil ((void *)0)
#define NIL nil
typedef uint8_t byte;
typedef int integer;
typedef unsigned int cardinal;
typedef float real;

/**
 * TMod-c
 * By M. Scott Reynolds
 * Date 28 March 2026
 *
 * Parser.mc - Recursive descent parser for Mod-c source code.
 * 10 August 2026 - Conversion to .mc
 */

/* === IMPORT === */
#include "Lexer.h"

/* === IMPORT === */
#include "arena.h"

/* === IMPORT === */
#include "dynbuf.h"

/* === IMPORT === */
#include "ttype.h"

/* === IMPORT === */
#include "parser_common.h"

/* === IMPORT === */
#include "declaration.h"

/* === IMPORT === */
#include "expression.h"

/* === IMPORT === */
#include "statement.h"

/* === IMPORT === */
#include "utils.h"

/* === IMPORT === */
#include "node.h"

/* === IMPORT === */
#include "stddef.h"

/* === IMPORT === */
#include "stdio.h"

/* === IMPORT === */
#include "stdlib.h"

/* === IMPORT === */
#include "string.h"

/**
 * Skip simple type 
 */

/**
 * skipped balanced parens
 */

/**
 * Skip type RHS
 */

/**
 * Skip to EOS
 */

/**
 * Skip BEGIN END block
 */

/**
 * Skip PROC decl
 */

/**
 * Prescan type names
 */

/**
 * Parse doc comments
 */

/**
 * Parse a return type.
 * 
 * This is the most restricted of the parsing functions. It only accepts 
 * syntax that is valid (or can be reasonably lowered) as a C return type.
 *
 * In particular:
 *  - Direct array types (T[], T[N], array[...] of T) are accepted syntactically,
 *    but are expected to be lowered to pointer types during code generation.
 *  - Method types (PROCEDURE / FUNCTION signatures) are NOT accepted here.
 *
 * Intended usage:
 *  - Return types of FUNCTION declarations
 *  - Return type of PROGRAM/MODULE.
 *
 * For parameter types, use parse_parameter_type().
 * For general type expressions (including method types, struct types, etc.),
 * use parse_type_expression().
 *
 * Allow opaque: true only for TYPE RHS (parse_type_expression)
 *
 * Exits on fatal parse error. 
 */

/**
 * Initialize parser state.
 * Primes the lexer and advances to the first token.
 * Does NOT take ownership of source or out - they must remain valid.
 *
 * @param p         Parser to initialize
 * @param arena     Arena for all AST node allocations (must be initialized)
 * @param source    Null-terminated source code buffer (must remain valid)
 * @param out       Optional output buffer for debug/codegen (may be NULL)
 */

/* === PROC/FUNC EXPORT === */
void TParser__init(TParser* p, pArena arena, const char* source, pDynBuf out);

/**
 * Register a user TYPE spelling for sizeof() operand disambiguation.
 * Case-sensitive; duplicates are ignored.
 */

/* === PROC/FUNC EXPORT === */
void TParser__register_type_name(TParser* p, const char* start, size_t length);

/**
 * Is user type name?
 * True when start/len is a registered user TYPE name (not a builtin).
 */

/* === PROC/FUNC EXPORT === */
bool TParser__is_user_type_name(TParser* p, const char* start, size_t length);

/**
 * Parse expression
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse_expr(TParser* p);

/**
 * Parse a return type
 *
 * Narrow parser used only where a C return type is expected.
 * See the header declaration for full rationale and restrictions.
 */

/* === PROC/FUNC EXPORT === */
pTType TParser__parse_return_type(TParser* p);

/**
 * Parse parameter types: Type specified in formal parameters, grammar may
 * allow more than just parse_return_type. Returns fully constructed ^TType
 * Exits on fatal parse error.
 */

/* === PROC/FUNC EXPORT === */
pTType TParser__parse_parameter_type(TParser* p);

/**
 * Parse full type expression. Used for full type declarations.
 * Returns fully constructed ^TType.
 * Exits on fatal parse error.
 */

/* === PROC/FUNC EXPORT === */
pTType TParser__parse_type_expression(TParser* p);

/**
 * Parse a const expression.
 * Same as normal expression for now. Semantics will enforce later.
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse_const_expression(TParser* p);

/**
 * Parse a designator (for left-hand side of assignment, etc.)
 * Supports: ident, ident.field, ident[index], ^designator
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse_designator(TParser* p);

/**
 * Parse block: BEGIN statement-list END
 * 
 * Allocates and grows stmts array dynamically.
 * Exits on fatal parse errors.
 *
 * @param p     Parser state (current token should be BEGIN)
 * @return      BLOCK node with parsed statements.
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse_block(TParser* p);

/**
 * Parse statement_sequence - statements until a terminating key word (NED, UNTIL, etc.)
 * This is what IF/THEN, FOR/DO, REPEAT, ELSIF, ELSE use.
 * No BEGIN is required.
 * This is similar to TParser::parse_block; any changes there will need to also go here.
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse_statement_sequence(TParser* p);

/**
 * Parse a single statement.
 * Recognizes:
 *  - expression / assignment statements / call statements
 *  - contorl structure (if, for, repeat, loop) -- TODO
 *  - empty statements (just ; or newline)
 *
 * Returns statement node or nil for empty / skiped lines.
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse_stmt(TParser* p);

/**
 * Parse built-in statements: ASSERT 
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse_builtin_stmt(TParser* p);

/**
 * Entry point. Parse the entire source into an AST.
 * Returns the root PROGRAM node (or NULL on fatal error - exits via error handlers).
 * Caller owns the returned node (allocated in p->arena).
 */

/* === PROC/FUNC EXPORT === */
pNode TParser__parse(TParser* p);

void parser_main(void);

#endif /* MODC_MODULE_parser_main_H */
