Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
token_table.h
1/*
2** Thread-safe Token Table
3**
4** Provides concurrent token lookup via reader-writer locking.
5** Multiple readers can look up tokens simultaneously; writers
6** acquire exclusive access for modifications. A version counter
7** is maintained for external change detection.
8*/
9#ifndef TOKEN_TABLE_H
10#define TOKEN_TABLE_H
11
12#include <stdint.h>
13#include <stddef.h>
14#include <stdbool.h>
15#include <stdatomic.h>
16#include <pthread.h>
17
18#define INVALID_INDEX 0xFFFFFFFF
19
20typedef uint32_t ExtensionID;
21
25typedef struct TokenDefinition {
26 const char *lexeme;
27 size_t lexeme_len;
29 ExtensionID extension_id;
30 uint32_t next_in_chain;
32
40typedef struct TokenTable {
42 uint32_t ntokens;
43 uint32_t capacity;
45 uint32_t *hash_table;
46 uint32_t hash_capacity;
48 atomic_uint_fast32_t version;
49 pthread_rwlock_t lock;
51
52/*
53** Create a new token table with the given initial capacity.
54** Returns NULL on allocation failure.
55*/
56TokenTable *create_token_table(uint32_t initial_capacity);
57
58/*
59** Destroy a token table and free all associated memory.
60*/
61void destroy_token_table(TokenTable *table);
62
63/*
64** Look up a token by string. Acquires read lock internally, so
65** multiple threads can look up tokens concurrently.
66** Returns the token_code if found, or -1 if not found.
67*/
68int lookup_token(TokenTable *table, const char *str, size_t len);
69
70/*
71** Add a token to the table. Acquires write lock internally.
72** Returns true on success, false on failure (allocation error or duplicate).
73*/
74bool add_token(TokenTable *table, const char *lexeme, int token_code,
75 ExtensionID ext_id);
76
77/*
78** Remove all tokens belonging to a given extension.
79** Acquires write lock internally. Rebuilds hash chains.
80** Returns true on success, false on failure.
81*/
82bool remove_tokens_by_extension(TokenTable *table, ExtensionID ext_id);
83
84#endif /* TOKEN_TABLE_H */
uint32_t ExtensionID
Opaque ID identifying a registered extension.
Definition conflict.h:39
A single token definition in the table.
Definition token_table.h:25
const char * lexeme
Token string (e.g., "SELECT")
Definition token_table.h:26
size_t lexeme_len
Length of lexeme.
Definition token_table.h:27
ExtensionID extension_id
Which extension added it (0 = base)
Definition token_table.h:29
int token_code
Token ID (e.g., TK_SELECT)
Definition token_table.h:28
uint32_t next_in_chain
Hash collision chain link.
Definition token_table.h:30
Thread-safe token lookup table.
Definition token_table.h:40
pthread_rwlock_t lock
Reader/writer lock guarding the table.
Definition token_table.h:49
uint32_t capacity
Allocated slots in tokens.
Definition token_table.h:43
uint32_t hash_capacity
Length of hash_table.
Definition token_table.h:46
TokenDefinition * tokens
Dense array of tokens.
Definition token_table.h:41
atomic_uint_fast32_t version
RCU-style version counter.
Definition token_table.h:48
uint32_t ntokens
Number of tokens in tokens.
Definition token_table.h:42
uint32_t * hash_table
Hash bucket table mapping to indices.
Definition token_table.h:45