libsnobol4 1.0.2
High-performance SNOBOL4-style pattern matching library
Loading...
Searching...
No Matches
ast.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7
8#include <stdbool.h>
9#include <stddef.h>
10#include <stdint.h>
11#include <stdio.h>
12
13#include "snobol/arena.h"
14
28
33#define SNOBOL_AST_VERSION_MAJOR 1
34#define SNOBOL_AST_VERSION_MINOR 0
35#define SNOBOL_AST_VERSION_PATCH 0
36#define SNOBOL_AST_VERSION_STRING "1.0.0"
37
42#define SNOBOL_AST_VERSION_CHECK(major, minor) \
43 ((major) == SNOBOL_AST_VERSION_MAJOR && (minor) <= SNOBOL_AST_VERSION_MINOR)
44
48typedef struct {
49 uint16_t major;
50 uint16_t minor;
51 uint16_t patch;
52 const char *string;
54
58typedef enum {
59 /* Core pattern types */
60 AST_LITERAL, /* Literal string match */
61 AST_CONCAT, /* Concatenation of patterns */
62 AST_ALT, /* Alternation (pattern1 | pattern2) */
63 AST_REPETITION, /* Bounded repetition: repeat(P, min, max) */
64
65 /* Character class types */
66 AST_SPAN, /* Match span of characters in set */
67 AST_BREAK, /* Match until break character */
68 AST_ANY, /* Match any character (optionally in set) */
69 AST_NOTANY, /* Match any character NOT in set */
70
71 /* Quantifiers */
72 AST_ARBNO, /* Arbitrary number (zero or more) */
73
74 /* Capture and assignment */
75 AST_CAP, /* Capture match into register */
76 AST_ASSIGN, /* Assign register to variable */
77
78 /* Length matching */
79 AST_LEN, /* Match exactly N characters */
80
81 /* Dynamic evaluation */
82 AST_EVAL, /* Evaluate function result as pattern */
83 AST_DYNAMIC_EVAL, /* Dynamic pattern from source text */
84
85 /* Anchors */
86 AST_ANCHOR, /* Start/end anchor */
87
88 /* Output/emission */
89 AST_EMIT, /* Emit literal or capture to output */
90
91 /* Control flow */
92 AST_LABEL, /* Label definition */
93 AST_GOTO, /* Goto label reference */
94
95 /* Table operations */
96 AST_TABLE_ACCESS, /* Table lookup */
97 AST_TABLE_UPDATE, /* Table assignment */
98
99 /* Pattern primitives */
100 AST_BREAKX, /* BREAKX – break with retry choice point */
101 AST_BAL, /* BAL – balanced delimiter match */
102 AST_FENCE, /* FENCE – cut choice stack */
103 AST_REM, /* REM – consume remainder */
104 AST_RPOS, /* RPOS(n) – cursor n codepoints from end */
105 AST_RTAB, /* RTAB(n) – advance to n codepoints from end */
106 AST_POS, /* POS(n) – succeed when cursor at n codepoints from start */
107 AST_TAB, /* TAB(n) – advance cursor to n codepoints from start */
108 AST_ABORT, /* ABORT – terminate entire match */
109 AST_FAIL, /* FAIL – force failure / backtrack */
110 AST_SUCCEED, /* SUCCEED – force immediate success */
111
112 /* Register reference (one of the v0..v63 variable registers). Used as a
113 * table key (`TABLE[$vN]`) where the key text is the previously captured
114 * value of the register. Appended last so existing tag values are stable. */
116} ast_type_t;
117
121typedef enum {
122 ANCHOR_START, /* Beginning of string (^) */
123 ANCHOR_END /* End of string ($) */
125
129typedef struct ast_node ast_node_t;
130
139struct ast_node {
141
148 uint32_t depth;
149
158
159 union {
160 /* AST_LITERAL */
161 struct {
162 char *text; /* Owned: literal string content */
163 size_t len; /* Length of text */
165
166 /* AST_CONCAT */
167 struct {
168 ast_node_t **parts; /* Owned array of child nodes */
169 size_t count; /* Number of parts */
171
172 /* AST_ALT */
173 struct {
174 ast_node_t *left; /* Owned: left alternative */
175 ast_node_t *right; /* Owned: right alternative */
177
178 /* AST_REPETITION */
179 struct {
180 ast_node_t *sub; /* Owned: sub-pattern to repeat */
181 int32_t min; /* Minimum repetitions (-1 = unbounded) */
182 int32_t max; /* Maximum repetitions (-1 = unbounded) */
184
185 /* AST_SPAN, AST_BREAK, AST_ANY, AST_NOTANY */
186 struct {
187 char *set; /* Owned: character set string */
188 size_t len; /* Length of set */
190
191 /* AST_ARBNO */
192 struct {
193 ast_node_t *sub; /* Owned: sub-pattern */
195
196 /* AST_CAP */
197 struct {
198 int reg; /* Register number for capture */
199 ast_node_t *sub; /* Owned: sub-pattern to capture */
201
202 /* AST_ASSIGN */
203 struct {
204 int var; /* Variable number */
205 int reg; /* Register number */
207
208 /* AST_LEN */
209 struct {
210 int32_t n; /* Exact length to match */
212
213 /* AST_EVAL */
214 struct {
215 int fn; /* Function ID */
216 int reg; /* Register for result */
218
219 /* AST_DYNAMIC_EVAL */
220 struct {
221 ast_node_t *expr; /* Owned: expression to evaluate */
223
224 /* AST_ANCHOR */
225 struct {
226 anchor_type_t atype; /* Start or end anchor */
228
229 /* AST_EMIT */
230 struct {
231 char *text; /* Owned: literal text to emit (or NULL) */
232 size_t len; /* Byte length of text (NUL-safe; 0 if text is NULL) */
233 int reg; /* Register to emit (-1 if text only) */
235
236 /* AST_LABEL */
237 struct {
238 char *name; /* Owned: label name */
239 ast_node_t *target; /* Owned: target pattern */
241
242 /* AST_GOTO */
243 struct {
244 char *label; /* Owned: target label name */
246
247 /* AST_TABLE_ACCESS */
248 struct {
249 char *table; /* Owned: table name */
250 ast_node_t *key; /* Owned: key expression */
252
253 /* AST_TABLE_UPDATE */
254 struct {
255 char *table; /* Owned: table name */
256 ast_node_t *key; /* Owned: key expression */
257 ast_node_t *value; /* Owned: value expression */
258 uint8_t value_reg; /* Value capture register; 0xFF = compiler-allocated
259 (Builder-built ASTs have no parser allocator) */
261
262 /* AST_BREAKX */
263 struct {
264 char *set; /* Owned: break character set */
265 size_t len;
267
268 /* AST_BAL */
269 struct {
270 uint32_t open_cp; /* Opening delimiter codepoint */
271 uint32_t close_cp; /* Closing delimiter codepoint */
273
274 /* AST_RPOS / AST_RTAB */
275 struct {
276 int32_t n; /* Distance from end */
278
279 /* AST_REG_REF */
280 struct {
281 int reg; /* Register number (0..63) */
284};
285
293ast_node_t *snobol_ast_create_lit(const char *text, size_t len);
294
302
310
317
325
332ast_node_t *snobol_ast_create_span(const char *set, size_t len);
333
340ast_node_t *snobol_ast_create_any(const char *set, size_t len);
341
349ast_node_t *snobol_ast_create_repeat(ast_node_t *sub, int32_t min, int32_t max);
350
358
365
372
378
388uint32_t snobol_ast_depth(const ast_node_t *node);
389
403
412
419
426void snobol_ast_dump(const ast_node_t *node, FILE *out, int indent);
427
433
440bool snobol_ast_version_check(uint16_t required_major, uint16_t required_minor);
441
447
448/* Additional AST creation functions for PHP binding */
449
451ast_node_t *snobol_ast_create_literal(const char *text, size_t len);
452
454ast_node_t *snobol_ast_create_break(const char *set, size_t len);
455
457ast_node_t *snobol_ast_create_notany(const char *set, size_t len);
458
461
464
467
469ast_node_t *snobol_ast_create_emit(const char *text, size_t len, int reg);
470
473
476
479
482 ast_node_t *value);
483
485ast_node_t *snobol_ast_create_breakx(const char *set, size_t len);
486
488ast_node_t *snobol_ast_create_bal(uint32_t open_cp, uint32_t close_cp);
489
492
495
498
501
504
507
510
513
516
519
520#ifdef __cplusplus
521}
522#endif
Bump-allocated arena pool for short-lived scratch allocations (e.g. AST nodes during compilation,...
void snobol_ast_dump(const ast_node_t *node, FILE *out, int indent)
ast_node_t * snobol_ast_create_regref(int reg)
ast_node_t * snobol_ast_create_emit(const char *text, size_t len, int reg)
ast_node_t * snobol_ast_create_rpos(int32_t n)
ast_node_t * snobol_ast_create_tab(int32_t n)
ast_type_t
Definition ast.h:58
@ AST_SUCCEED
Definition ast.h:110
@ AST_NOTANY
Definition ast.h:69
@ AST_TABLE_UPDATE
Definition ast.h:97
@ AST_CAP
Definition ast.h:75
@ AST_RPOS
Definition ast.h:104
@ AST_RTAB
Definition ast.h:105
@ AST_ANY
Definition ast.h:68
@ AST_CONCAT
Definition ast.h:61
@ AST_ALT
Definition ast.h:62
@ AST_EMIT
Definition ast.h:89
@ AST_ARBNO
Definition ast.h:72
@ AST_ABORT
Definition ast.h:108
@ AST_FAIL
Definition ast.h:109
@ AST_POS
Definition ast.h:106
@ AST_LITERAL
Definition ast.h:60
@ AST_EVAL
Definition ast.h:82
@ AST_BREAKX
Definition ast.h:100
@ AST_BREAK
Definition ast.h:67
@ AST_ANCHOR
Definition ast.h:86
@ AST_BAL
Definition ast.h:101
@ AST_LABEL
Definition ast.h:92
@ AST_REG_REF
Definition ast.h:115
@ AST_REPETITION
Definition ast.h:63
@ AST_DYNAMIC_EVAL
Definition ast.h:83
@ AST_FENCE
Definition ast.h:102
@ AST_REM
Definition ast.h:103
@ AST_GOTO
Definition ast.h:93
@ AST_TABLE_ACCESS
Definition ast.h:96
@ AST_LEN
Definition ast.h:79
@ AST_TAB
Definition ast.h:107
@ AST_ASSIGN
Definition ast.h:76
@ AST_SPAN
Definition ast.h:66
ast_node_t * snobol_ast_create_eval(int fn, int reg)
ast_node_t * snobol_ast_create_anchor(anchor_type_t atype)
ast_node_t * snobol_ast_create_assign(int var, int reg)
ast_node_t * snobol_ast_create_pos(int32_t n)
ast_node_t * snobol_ast_create_fail(void)
struct ast_node ast_node_t
Definition ast.h:129
ast_node_t * snobol_ast_create_notany(const char *set, size_t len)
const char * snobol_ast_type_name(ast_type_t type)
ast_node_t * snobol_ast_create_cap(int reg, ast_node_t *sub)
uint32_t snobol_ast_depth(const ast_node_t *node)
void snobol_ast_free(ast_node_t *node)
ast_node_t * snobol_ast_create_any(const char *set, size_t len)
ast_node_t * snobol_ast_create_arbno(ast_node_t *sub)
ast_node_t * snobol_ast_create_dynamic_eval(ast_node_t *expr)
snobol_ast_version_t snobol_ast_get_version(void)
ast_node_t * snobol_ast_create_span(const char *set, size_t len)
snobol_arena_t * snobol_ast_clear_arena(void)
ast_node_t * snobol_ast_create_break(const char *set, size_t len)
ast_node_t * snobol_ast_create_alt(ast_node_t *left, ast_node_t *right)
ast_node_t * snobol_ast_create_bal(uint32_t open_cp, uint32_t close_cp)
anchor_type_t
Definition ast.h:121
@ ANCHOR_END
Definition ast.h:123
@ ANCHOR_START
Definition ast.h:122
ast_node_t * snobol_ast_create_repeat(ast_node_t *sub, int32_t min, int32_t max)
ast_node_t * snobol_ast_create_rem(void)
ast_node_t * snobol_ast_create_breakx(const char *set, size_t len)
ast_node_t * snobol_ast_clone(const ast_node_t *node)
bool snobol_ast_version_check(uint16_t required_major, uint16_t required_minor)
ast_node_t * snobol_ast_create_rtab(int32_t n)
ast_node_t * snobol_ast_create_fence(void)
void snobol_ast_set_arena(snobol_arena_t *arena)
ast_node_t * snobol_ast_create_table_access(const char *table, ast_node_t *key)
ast_node_t * snobol_ast_create_literal(const char *text, size_t len)
ast_node_t * snobol_ast_create_label(char *name, ast_node_t *target)
ast_node_t * snobol_ast_create_concat(ast_node_t **parts, size_t count)
const char * snobol_ast_version_string(void)
ast_node_t * snobol_ast_create_len(int32_t n)
ast_node_t * snobol_ast_create_goto(const char *label)
ast_node_t * snobol_ast_create_lit(const char *text, size_t len)
ast_node_t * snobol_ast_create_table_update(const char *table, ast_node_t *key, ast_node_t *value)
ast_node_t * snobol_ast_create_abort(void)
ast_node_t * snobol_ast_create_succeed(void)
struct ast_node::@033015144052056314115242066137206173337123340163::@155206377163265200264317333172066021261225005052 label
struct ast_node::@033015144052056314115242066137206173337123340163::@332261164025143207061111236210022321310161262313 table_access
struct ast_node::@033015144052056314115242066137206173337123340163::@031060216132031025101060350310134375024266345050 rpos_rtab
struct ast_node::@033015144052056314115242066137206173337123340163::@377372014155322376021133133016057012345260337101 anchor
struct ast_node::@033015144052056314115242066137206173337123340163::@006346151123106165360046352073306115150265312156 emit
struct ast_node::@033015144052056314115242066137206173337123340163::@104022320322376201234131356107147371103164277354 arbno
struct ast_node::@033015144052056314115242066137206173337123340163::@264016357332256145005155021252333112314170037227 goto_stmt
int fn
Definition ast.h:215
struct ast_node::@033015144052056314115242066137206173337123340163::@052231206114155066264222372340300200247013101213 reg_ref
char * table
Definition ast.h:249
ast_node_t * left
Definition ast.h:174
ast_node_t ** parts
Definition ast.h:168
int32_t min
Definition ast.h:181
uint8_t value_reg
Definition ast.h:258
struct ast_node::@033015144052056314115242066137206173337123340163::@300036324266051130306131275221337334107337067330 table_update
int reg
Definition ast.h:198
bool arena_allocated
Definition ast.h:157
int var
Definition ast.h:204
ast_node_t * value
Definition ast.h:257
struct ast_node::@033015144052056314115242066137206173337123340163::@354104241334014355010302174157255242027176060204 alt
ast_node_t * target
Definition ast.h:239
struct ast_node::@033015144052056314115242066137206173337123340163::@014227042236214300164370121154165074327317162131 dynamic_eval
ast_node_t * sub
Definition ast.h:180
ast_node_t * expr
Definition ast.h:221
uint32_t depth
Definition ast.h:148
size_t count
Definition ast.h:169
char * name
Definition ast.h:238
struct ast_node::@033015144052056314115242066137206173337123340163::@204361121105170314033050161356126154140025314020 assign
int32_t max
Definition ast.h:182
anchor_type_t atype
Definition ast.h:226
struct ast_node::@033015144052056314115242066137206173337123340163::@200035045072054337157210160233246075152370215062 breakx
struct ast_node::@033015144052056314115242066137206173337123340163::@046266103355376102226136365305312301070277115225 bal
uint32_t close_cp
Definition ast.h:271
char * set
Definition ast.h:187
struct ast_node::@033015144052056314115242066137206173337123340163::@242153333367173047300261225015306152106011010274 concat
struct ast_node::@033015144052056314115242066137206173337123340163::@271004003310230223217122346372355323003233373363 literal
union ast_node::@033015144052056314115242066137206173337123340163 data
ast_type_t type
Definition ast.h:140
struct ast_node::@033015144052056314115242066137206173337123340163::@240344307064252344225102356310273027325035213037 eval
struct ast_node::@033015144052056314115242066137206173337123340163::@240305076364032370261227026204177176344245122243 repetition
uint32_t open_cp
Definition ast.h:270
size_t len
Definition ast.h:163
char * text
Definition ast.h:162
ast_node_t * key
Definition ast.h:250
struct ast_node::@033015144052056314115242066137206173337123340163::@003373343272351015325337242007337173267370254312 charclass
struct ast_node::@033015144052056314115242066137206173337123340163::@250024176254060122072205026232152227265234136254 cap
int32_t n
Definition ast.h:210
ast_node_t * right
Definition ast.h:175
uint16_t minor
Definition ast.h:50
uint16_t patch
Definition ast.h:51
const char * string
Definition ast.h:52
uint16_t major
Definition ast.h:49