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

#ifndef MODC_MODULE_parser_common_H
#define MODC_MODULE_parser_common_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_common.mc - Common routines used by parser, declaration, expression, and statement
 * 22 August 2026 - Conversion to .mc
 */

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

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

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

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

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

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

/**
 * One user TYPE name registered for sizeof() disambiguation at parse time.
 * Points into the source buffer (case-sensitive spelling).
 */

/* === TYPE EXPORT === */
typedef struct TParserTypeName TParserTypeName;
typedef struct TParserTypeName {
    const char* start;
    size_t length;
} TParserTypeName;

/* === TYPE EXPORT === */
typedef TParserTypeName* pParserTypeName;

/* === PROC/FUNC EXPORT === */
size_t SIZE_PARSER_TYPE_NAME(void);

/**
 * Parser state for recursive descent parsing.
 * Holds lexer state, current token, arena for AST nodes, and optional output buffer.
 */

/* === TYPE EXPORT === */
typedef struct TParser TParser;
typedef struct TParser {
    TLexer lexer;
    TToken current;
    pArena arena;
    pDynBuf out;
    bool show_tokens;
    bool debug_enabled;
    pParserTypeName type_names;
    size_t type_name_count;
    size_t type_name_capacity;
} TParser;

/* === TYPE EXPORT === */
typedef TParser* pParser;

/**
 * Advance to the next token (and print it if debug set show-tokens).
 */

/* === PROC/FUNC EXPORT === */
void advance(pParser p);

/**
 * Advance to the next token (and print it if debug set show-tokens).
 */

/* === PROC/FUNC EXPORT === */
void TParser__advance(TParser* p);

/**
 * Check if the current token matches kind without consuming it.
 */

/* === PROC/FUNC EXPORT === */
bool check(pParser p, TokenKind kind);

/**
 * Check if the current token matches kind without consuming it.
 */

/* === PROC/FUNC EXPORT === */
bool TParser__check(TParser* p, TokenKind kind);

/**
 * If current token matches, consume it and return true
 */

/* === PROC/FUNC EXPORT === */
bool match(pParser p, TokenKind kind);

/**
 * If current token matches, consume it and return true
 */

/* === PROC/FUNC EXPORT === */
bool TParser__match(TParser* p, TokenKind kind);

/**
 * Check if the current token is one of the comments.
 * Doc comments are processed into their own nodes.
 */

/* === PROC/FUNC EXPORT === */
bool check_comment(pParser p);

/**
 * Check if the current token is one of the comments.
 * Doc comments are processed into their own nodes.
 */

/* === PROC/FUNC EXPORT === */
bool TParser__check_comment(TParser* p);

/**
 * Match if one of the comment types
 */

/* === PROC/FUNC EXPORT === */
bool match_comment(pParser p);

/**
 * Match if one of the comment types
 */

/* === PROC/FUNC EXPORT === */
bool TParser__match_comment(TParser* p);

/**
 * Return true if end-of-statement (EOS)
 */

/* === PROC/FUNC EXPORT === */
bool checkEOS(pParser p);

/**
 * Return true if end-of-statement (EOS)
 */

/* === PROC/FUNC EXPORT === */
bool TParser__checkEOS(TParser* p);

/**
 * Match end-of-statement (semicolon or newline)
 * Return true if either was consumed.
 * This allows comments to appear after a statement on the same line.
 */

/* === PROC/FUNC EXPORT === */
bool matchEOS(pParser p);

/**
 * Match end-of-statement (semicolon or newline)
 * Return true if either was consumed.
 * This allows comments to appear after a statement on the same line.
 */

/* === PROC/FUNC EXPORT === */
bool TParser__matchEOS(TParser* p);

/**
 * Skip empty statements: blank lines, comments, etc.
 */

/* === PROC/FUNC EXPORT === */
void skip_empty_statements(pParser p);

/**
 * Skip empty statements: blank lines, comments, etc.
 */

/* === PROC/FUNC EXPORT === */
void TParser__skip_empty_statements(TParser* p);

/**
 * Skip layout-only breaks between list items (newlines, comments).
 * Does not consume ';' - that remains a statement terminator.
 */

/* === PROC/FUNC EXPORT === */
void skip_layout_breaks(pParser p);

/**
 * Skip layout-only breaks between list items (newlines, comments).
 * Does not consume ';' - that remains a statement terminator.
 */

/* === PROC/FUNC EXPORT === */
void TParser__skip_layout_breaks(TParser* p);

/**
 * Check if current token is an assignment
 */

/* === PROC/FUNC EXPORT === */
bool check_assignment(pParser p);

/**
 * Check if current token is an assignment
 */

/* === PROC/FUNC EXPORT === */
bool TParser__check_assignment(TParser* p);

/**
 * Report error. Fatal errors cause immediate program exit.
 */

/* === PROC/FUNC EXPORT === */
void error_at(pParser p, const char* msg, bool fatal);

/**
 * Report error. Fatal errors cause immediate program exit.
 */

/* === PROC/FUNC EXPORT === */
void TParser__error_at(TParser* p, const char* msg, bool fatal);

/**
 * Consume a token of the expected kind or report fata error.
 */

/* === PROC/FUNC EXPORT === */
void consume(pParser p, TokenKind kind, const char* msg);

/**
 * Consume a token of the expected kind or report fata error.
 */

/* === PROC/FUNC EXPORT === */
void TParser__consume(TParser* p, TokenKind kind, const char* msg);

/**
 * Return true if we've reached the end of the input.
 */

/* === PROC/FUNC EXPORT === */
bool isEOF(pParser p);

/**
 * Return true if we've reached the end of the input.
 */

/* === PROC/FUNC EXPORT === */
bool TParser__isEOF(TParser* p);

void parser_common(void);

#endif /* MODC_MODULE_parser_common_H */
