BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
tokenizer.h
Go to the documentation of this file.
1/*
2 * tokenizer.h - turning source text into tokenised lines, as an object
3 * with a rule set (design D2 of the change ab3-mode-g2, spec
4 * ab3-compat/tokenizer).
5 *
6 * A struct tokenizer holds a token list (names and token numbers, in the
7 * order the compiler hands them to the editor: its own tokens, then the
8 * libraries' - the first entry of a name wins) and a rule set:
9 *
10 * TOKENIZER_RULES_AB3 AmiBlitz3 v3.14.0's preproc_Tokenize
11 * (Sourcecodes/Amiblitz3/Shared/
12 * preprocessor.include.ab3:358 at 58a11658): names
13 * match byte for byte (case-sensitive), only whole
14 * words that start after a separator, "name$"
15 * before "name", strings and comments copied
16 * untouched.
17 *
18 * (A rule set for TED's tokeniser, ted.s:4198 tokit, is reserved for the
19 * change that lets TED use this component; TED keeps its own for now.)
20 *
21 * The functions around it follow AmiBlitz3's way of loading a source
22 * (CLIModeSupport.ab3 LoadSource, AmiBlitz3.ab3 read_includeline): the end
23 * of the embedded header of a main source and of an include, and the
24 * lines of a tokenised main source, cut at 250 characters.
25 *
26 * Shared code like shared/xtra: every user compiles tokenizer.c itself
27 * with its own options (ab3cli with TED's, the host tools natively). C99
28 * on the freestanding headers: no libc, no libgcc (no 32-bit multiply or
29 * divide on the 68000), no writable static data, no allocation - the
30 * caller gives the memory of the object.
31 */
32#ifndef TOKENIZER_H
33#define TOKENIZER_H
34
35#include <stddef.h>
36#include <stdint.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/** @brief The rule sets. */
44 TOKENIZER_RULES_AB3 = 1, /* AmiBlitz3 v3.14.0 (preproc_Tokenize) */
45 TOKENIZER_RULES_AB3_LIT = 2 /* the same, and inside a string or a
46 comment a byte from 128 becomes the
47 literal character $90 byte
48 (docs/formats.md 3.2; blitz2-ab3's
49 loader, spec ab3-compat/strings) */
50};
51
52/** @brief Results of the functions (TOKENIZER_OK = 0). */
55 TOKENIZER_DUPLICATE, /* tokenizer_add: the name is there already,
56 the first entry stays (not an error) */
57 TOKENIZER_E_RULES, /* not an enum tokenizer_rules */
58 TOKENIZER_E_BUCKETS, /* bucket count not a power of 2 (1..65536) */
59 TOKENIZER_E_FULL, /* no entry left */
60 TOKENIZER_E_NAME, /* empty name, or longer than 65535 bytes */
61 TOKENIZER_E_SPACE /* the output buffer is too small */
62};
63
64/** @brief Characters of a line of a main source AmiBlitz3 keeps
65 (#MAX_Columns, Shared/common.ab3:96). */
66#define TOKENIZER_AB3_COLUMNS 250
67
68/** @brief Bytes tokenizer_text and tokenizer_line may write for len bytes
69 of text: a name of one letter becomes a 2-byte token, a string or a
70 comment at the end of the text gets its 0. */
71#define TOKENIZER_BOUND(len) (2 * (size_t)(len) + 2)
72
73/** @brief The first byte of a literal character (TOKENIZER_RULES_AB3_LIT). */
74#define TOKENIZER_LITHI 0x90
75
76/** @brief One name of the token list (the caller's memory). */
78 const uint8_t *name; /* the name's bytes (no 0 needed); must stay */
79 uint16_t len; /* its length */
80 uint16_t num; /* the token: written high byte first */
81 uint16_t next; /* the next entry of its bucket + 1, 0 = none */
82};
83
84/** @brief The tokeniser: the token list as a hash table over the caller's
85 arrays, and the rule set. Read-only while tokenising. */
86struct tokenizer {
88 uint16_t *bucket; /* first entry + 1 of each bucket, 0 = none */
89 uint32_t n, cap; /* entries used, entries there are */
90 uint32_t mask; /* buckets - 1 */
91 uint8_t rules; /* enum tokenizer_rules */
92};
93
94/**
95 * @brief Set up an empty tokeniser.
96 * @param t the object
97 * @param rules an enum tokenizer_rules
98 * @param entry room for cap names (at most 65535)
99 * @param cap the entries there are
100 * @param bucket the hash buckets: nbucket words
101 * @param nbucket a power of 2, 1..65536
102 * @return TOKENIZER_OK, TOKENIZER_E_RULES or TOKENIZER_E_BUCKETS
103 */
104int tokenizer_init(struct tokenizer *t, int rules, struct tokenizer_entry *entry, uint32_t cap,
105 uint16_t *bucket, uint32_t nbucket);
106
107/**
108 * @brief Add a name to the token list, in the compiler's order.
109 * A name that is there already keeps its first token (AmiBlitz3's
110 * preproc_QueryTokens): the call changes nothing.
111 * @param t the tokeniser
112 * @param name the name's bytes (kept by reference)
113 * @param len its length
114 * @param num the token number (bit 15 set as in a tokenised source, or
115 * not: it is written as given)
116 * @return TOKENIZER_OK, TOKENIZER_DUPLICATE, TOKENIZER_E_NAME or
117 * TOKENIZER_E_FULL
118 */
119int tokenizer_add(struct tokenizer *t, const uint8_t *name, size_t len, uint16_t num);
120
121/**
122 * @brief Look a name up, byte for byte.
123 * @param t the tokeniser
124 * @param name the bytes
125 * @param len their number
126 * @param num the token if found (may be NULL)
127 * @return 1 if found, 0 if not
128 */
129int tokenizer_find(const struct tokenizer *t, const uint8_t *name, size_t len, uint16_t *num);
130
131/**
132 * @brief Tokenise a text as AmiBlitz3 tokenises a whole source buffer.
133 * Every LF (and every 0 byte) of the text ends a line with a 0; a TAB
134 * outside strings and comments becomes a space, CR, DEL and the other
135 * control characters are dropped, a byte from 128 becomes '?'; inside a
136 * string or a comment every byte stays except TAB (a space). A word that
137 * ends the text (no character behind it) is not looked up, a string or
138 * comment that ends it gets its 0 (AmiBlitz3 reads the 0 behind its
139 * buffer). With TOKENIZER_RULES_AB3_LIT a byte from 128 inside a string or
140 * a comment is written as the pair TOKENIZER_LITHI byte.
141 * @param t the tokeniser
142 * @param src the text
143 * @param len its length
144 * @param dst the output (not overlapping src)
145 * @param cap its size; TOKENIZER_BOUND(len) is always enough
146 * @param out the bytes written
147 * @return TOKENIZER_OK, TOKENIZER_E_SPACE or TOKENIZER_E_RULES
148 */
149int tokenizer_text(const struct tokenizer *t, const uint8_t *src, size_t len, uint8_t *dst,
150 size_t cap, size_t *out);
151
152/**
153 * @brief Tokenise one line: as tokenizer_text of the line followed by its
154 * LF, so the output ends with the line's 0.
155 * @param t the tokeniser
156 * @param src the line without its LF
157 * @param len its length
158 * @param dst the output (not overlapping src)
159 * @param cap its size; TOKENIZER_BOUND(len) is always enough
160 * @param out the bytes written, the 0 included
161 * @return as tokenizer_text
162 */
163int tokenizer_line(const struct tokenizer *t, const uint8_t *src, size_t len, uint8_t *dst,
164 size_t cap, size_t *out);
165
166/**
167 * @brief The end of the embedded header of a main source, as AmiBlitz3's
168 * ParseAB3Header finds it (CLIModeSupport.ab3:576): the lines up to
169 * the first one whose letters, '_', '/' and '-' before a '=' read
170 * "/xtra" in any case, that line with its LF included.
171 * @param src the source text
172 * @param len its length
173 * @return the bytes of the header, 0 if there is no such line
174 */
175size_t tokenizer_ab3_header(const uint8_t *src, size_t len);
176
177/**
178 * @brief The end of the embedded header of an include, as AmiBlitz3's
179 * SkipXtraHeader finds it (CLIModeSupport.ab3:401): leading lines
180 * with a ';' up to a "/xtra", "/XTRA" or "/Xtra", that line with
181 * its LF included.
182 * @param src the include's text
183 * @param len its length
184 * @return the bytes of the header, 0 if there is none (also if a line
185 * without ';' comes first or the end is reached)
186 */
187size_t tokenizer_ab3_incheader(const uint8_t *src, size_t len);
188
189/**
190 * @brief The lines a header of len bytes has (each LF ends one, a last
191 * line without LF counts).
192 */
193size_t tokenizer_lines(const uint8_t *src, size_t len);
194
195/**
196 * @brief One line of a tokenised main source as AmiBlitz3's LoadSource
197 * takes it (CLIModeSupport.ab3:480): the bytes from pos up to the
198 * next 0 or the end, of which at most TOKENIZER_AB3_COLUMNS are
199 * kept.
200 * @param tok the tokenised text (tokenizer_text)
201 * @param len its length
202 * @param pos the start of the line (< len)
203 * @param keep the bytes of the line kept (without its 0)
204 * @return the start of the next line (len after the last one)
205 */
206size_t tokenizer_ab3_mainline(const uint8_t *tok, size_t len, size_t pos, size_t *keep);
207
208/**
209 * @brief tokenizer_ab3_mainline for a text tokenised with
210 * TOKENIZER_RULES_AB3_LIT: a literal character (TOKENIZER_LITHI and
211 * a byte from 128) counts as the one byte AmiBlitz3 keeps for it,
212 * and the cut never falls between its two bytes; every other byte
213 * counts one, as there. The line kept has at most
214 * TOKENIZER_AB3_COLUMNS bytes as well (the editor's line length is
215 * a byte): a line with literal characters may keep fewer
216 * characters than AmiBlitz3.
217 */
218size_t tokenizer_ab3_mainline_lit(const uint8_t *tok, size_t len, size_t pos, size_t *keep);
219
220/**
221 * @brief The extension of a file name as AmiBlitz3's dos_GetExt finds it
222 * (Sourcecodes/Includes/dos.include.ab3:2353): the part behind the
223 * last '.' among the last 10 characters, not a '.' at the start.
224 * @param name the name
225 * @param len its length
226 * @return the offset of the extension in name (behind the '.'); len if
227 * there is none
228 */
229size_t tokenizer_ab3_getext(const char *name, size_t len);
230
231/**
232 * @brief Whether AmiBlitz3 tokenises a main source of this name as text
233 * (CLIModeSupport.ab3 LoadSource: not a .bb or .bb2, in any case)
234 * and skips its embedded header (not a .ab2 either, whose settings
235 * are in its companion .xtra).
236 * @param name the source's name
237 * @param len its length
238 * @return a bit set: TOKENIZER_AB3_TEXT, TOKENIZER_AB3_HEADER
239 */
240int tokenizer_ab3_source(const char *name, size_t len);
241#define TOKENIZER_AB3_TEXT 1 /* tokenised as text */
242#define TOKENIZER_AB3_HEADER 2 /* its embedded header skipped */
243
244/** @brief The names AmiBlitz3 tries for an include at one place
245 (ab3_GetIncludeLock, AmiBlitz3.ab3:25796): the name, then its
246 extension replaced by .ab3, .ab2, .bb2, then these behind the old
247 extension (dos_SetExt/dos_RemExt of dos.include.ab3). */
248#define TOKENIZER_AB3_INCNAMES 7
249
250/**
251 * @brief One of the names AmiBlitz3 tries for an include.
252 * @param name the name at that place (path + include name)
253 * @param len its length
254 * @param k which one: 0..TOKENIZER_AB3_INCNAMES-1
255 * @param out the name, with a 0
256 * @param cap the size of out
257 * @return its length; 0 if k is out of range or it does not fit
258 */
259size_t tokenizer_ab3_incname(const char *name, size_t len, int k, char *out, size_t cap);
260
261/**
262 * @brief How AmiBlitz3 includes a file of this (full) name
263 * (processInclude, AmiBlitz3.ab3:20754): by its last three
264 * characters, case-sensitively.
265 * @param name the name
266 * @param len its length
267 * @return TOKENIZER_AB3_TEXT ("ab3", "AB3", "ab2": tokenised as text),
268 * TOKENIZER_AB3_TOKENISED ("bb2", ".bb": read as it is) or
269 * TOKENIZER_AB3_REFUSED (any other: "You can only include
270 * tokenized files")
271 */
272int tokenizer_ab3_inctype(const char *name, size_t len);
273#define TOKENIZER_AB3_TOKENISED 2
274#define TOKENIZER_AB3_REFUSED 0
275
276#ifdef __cplusplus
277}
278#endif
279
280#endif
ULONG len
Definition asllib.c:158
char * name
Definition asllib.c:150
static struct snowfield * entry(ULONG n)
Definition risnowlib.c:205
Definition ab3args.c:62
One name of the token list (the caller's memory).
Definition tokenizer.h:77
uint16_t num
Definition tokenizer.h:80
uint16_t len
Definition tokenizer.h:79
const uint8_t * name
Definition tokenizer.h:78
uint16_t next
Definition tokenizer.h:81
The tokeniser: the token list as a hash table over the caller's arrays, and the rule set....
Definition tokenizer.h:86
uint32_t mask
Definition tokenizer.h:90
uint16_t * bucket
Definition tokenizer.h:88
uint32_t cap
Definition tokenizer.h:89
struct tokenizer_entry * entry
Definition tokenizer.h:87
uint32_t n
Definition tokenizer.h:89
uint8_t rules
Definition tokenizer.h:91
tokenizer_rules
The rule sets.
Definition tokenizer.h:43
@ TOKENIZER_RULES_AB3_LIT
Definition tokenizer.h:45
@ TOKENIZER_RULES_AB3
Definition tokenizer.h:44
size_t tokenizer_ab3_getext(const char *name, size_t len)
The extension of a file name as AmiBlitz3's dos_GetExt finds it (Sourcecodes/Includes/dos....
Definition tokenizer.c:382
int tokenizer_init(struct tokenizer *t, int rules, struct tokenizer_entry *entry, uint32_t cap, uint16_t *bucket, uint32_t nbucket)
Set up an empty tokeniser.
Definition tokenizer.c:32
int tokenizer_line(const struct tokenizer *t, const uint8_t *src, size_t len, uint8_t *dst, size_t cap, size_t *out)
Tokenise one line: as tokenizer_text of the line followed by its LF, so the output ends with the line...
Definition tokenizer.c:257
size_t tokenizer_ab3_mainline(const uint8_t *tok, size_t len, size_t pos, size_t *keep)
One line of a tokenised main source as AmiBlitz3's LoadSource takes it (CLIModeSupport....
Definition tokenizer.c:351
size_t tokenizer_ab3_incheader(const uint8_t *src, size_t len)
The end of the embedded header of an include, as AmiBlitz3's SkipXtraHeader finds it (CLIModeSupport....
Definition tokenizer.c:296
int tokenizer_add(struct tokenizer *t, const uint8_t *name, size_t len, uint16_t num)
Add a name to the token list, in the compiler's order. A name that is there already keeps its first t...
Definition tokenizer.c:67
tokenizer_err
Results of the functions (TOKENIZER_OK = 0).
Definition tokenizer.h:53
@ TOKENIZER_OK
Definition tokenizer.h:54
@ TOKENIZER_E_FULL
Definition tokenizer.h:59
@ TOKENIZER_E_NAME
Definition tokenizer.h:60
@ TOKENIZER_DUPLICATE
Definition tokenizer.h:55
@ TOKENIZER_E_RULES
Definition tokenizer.h:57
@ TOKENIZER_E_BUCKETS
Definition tokenizer.h:58
@ TOKENIZER_E_SPACE
Definition tokenizer.h:61
int tokenizer_ab3_inctype(const char *name, size_t len)
How AmiBlitz3 includes a file of this (full) name (processInclude, AmiBlitz3.ab3:20754): by its last ...
Definition tokenizer.c:454
size_t tokenizer_ab3_incname(const char *name, size_t len, int k, char *out, size_t cap)
One of the names AmiBlitz3 tries for an include.
Definition tokenizer.c:434
size_t tokenizer_lines(const uint8_t *src, size_t len)
The lines a header of len bytes has (each LF ends one, a last line without LF counts).
Definition tokenizer.c:339
size_t tokenizer_ab3_mainline_lit(const uint8_t *tok, size_t len, size_t pos, size_t *keep)
tokenizer_ab3_mainline for a text tokenised with TOKENIZER_RULES_AB3_LIT: a literal character (TOKENI...
Definition tokenizer.c:361
size_t tokenizer_ab3_header(const uint8_t *src, size_t len)
The end of the embedded header of a main source, as AmiBlitz3's ParseAB3Header finds it (CLIModeSuppo...
Definition tokenizer.c:265
int tokenizer_text(const struct tokenizer *t, const uint8_t *src, size_t len, uint8_t *dst, size_t cap, size_t *out)
Tokenise a text as AmiBlitz3 tokenises a whole source buffer. Every LF (and every 0 byte) of the text...
Definition tokenizer.c:251
int tokenizer_find(const struct tokenizer *t, const uint8_t *name, size_t len, uint16_t *num)
Look a name up, byte for byte.
Definition tokenizer.c:88
int tokenizer_ab3_source(const char *name, size_t len)
Whether AmiBlitz3 tokenises a main source of this name as text (CLIModeSupport.ab3 LoadSource: not a ...
Definition tokenizer.c:484
static void num(struct out *o, uint32_t v)
Definition xtra_ab3.c:509