parser.h 942 B raw
1
#ifndef PARSER_H
2
#define PARSER_H
3
4
#include "ast.h"
5
#include "limits.h"
6
#include "scanner.h"
7
8
/* Parsing context to handle ambiguities */
9
typedef enum {
10
    PARSE_CTX_NORMAL,    /* Normal expression context */
11
    PARSE_CTX_CONDITION, /* Inside condition where { starts block */
12
} parse_ctx_t;
13
14
/* Parser state */
15
typedef struct parser_t {
16
    scanner_t   scanner;
17
    token_t     current;
18
    token_t     previous;
19
    node_t     *root;
20
    u32         errors;
21
    node_t      nodes[MAX_NODES];
22
    u32         nnodes;
23
    parse_ctx_t context;
24
25
    /* Pool for variable-length node pointer arrays.
26
     * Nodes store an index + count into this pool instead of
27
     * embedding large arrays, keeping node_t small. */
28
    struct node_t *ptrs[MAX_NODEPTR_POOL];
29
    u32            nptrs;
30
} parser_t;
31
32
/* Initialize parser with scanner */
33
void parser_init(parser_t *p);
34
35
/* Parse a complete program */
36
node_t *parser_parse(parser_t *p);
37
38
#endif