BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
tokenizer.c File Reference
#include "tokenizer.h"

Macros

#define F_STOP   0x001 /* ends a word: look it up */
#define F_NEW   0x002 /* a token may start behind it */
#define F_IS   0x004 /* belongs to a word */
#define F_FIRST   0x008 /* starts a word */
#define F_COPY   0x010 /* copied to the output */
#define F_QUOT   0x020 /* starts a string */
#define F_COMMENT   0x040 /* starts a comment */
#define F_EOL   0x080 /* ends a line: a 0 */
#define F_TAB   0x100 /* a space */
#define GET(i)
#define PUT(b)
#define PUTLIT(b)
#define AT(i)

Functions

static uint32_t hash (const uint8_t *s, size_t len)
static int same (const uint8_t *a, const uint8_t *b, size_t len)
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.
static uint32_t lookup (const struct tokenizer *t, const uint8_t *name, size_t len)
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 token (AmiBlitz3's preproc_QueryTokens): the call changes nothing.
int tokenizer_find (const struct tokenizer *t, const uint8_t *name, size_t len, uint16_t *num)
 Look a name up, byte for byte.
static unsigned flags (unsigned c)
static int run (const struct tokenizer *t, const uint8_t *src, size_t len, int lf, uint8_t *dst, size_t cap, size_t *out)
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 ends a line with a 0; a TAB outside strings and comments becomes a space, CR, DEL and the other control characters are dropped, a byte from 128 becomes '?'; inside a string or a comment every byte stays except TAB (a space). A word that ends the text (no character behind it) is not looked up, a string or comment that ends it gets its 0 (AmiBlitz3 reads the 0 behind its buffer). With TOKENIZER_RULES_AB3_LIT a byte from 128 inside a string or a comment is written as the pair TOKENIZER_LITHI byte.
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's 0.
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 (CLIModeSupport.ab3:576): the lines up to the first one whose letters, '_', '/' and '-' before a '=' read "/xtra" in any case, that line with its LF included.
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.ab3:401): leading lines with a ';' up to a "/xtra", "/XTRA" or "/Xtra", that line with its LF included.
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).
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.ab3:480): the bytes from pos up to the next 0 or the end, of which at most TOKENIZER_AB3_COLUMNS are kept.
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 (TOKENIZER_LITHI and a byte from 128) counts as the one byte AmiBlitz3 keeps for it, and the cut never falls between its two bytes; every other byte counts one, as there. The line kept has at most TOKENIZER_AB3_COLUMNS bytes as well (the editor's line length is a byte): a line with literal characters may keep fewer characters than AmiBlitz3.
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.include.ab3:2353): the part behind the last '.' among the last 10 characters, not a '.' at the start.
static size_t remext (const char *name, size_t len)
static size_t setext (const char *name, size_t len, const char *e1, size_t n1, const char *e2, char *out, size_t cap)
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.
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 three characters, case-sensitively.
static int isext (const char *name, size_t len, const char *ext)
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 .bb or .bb2, in any case) and skips its embedded header (not a .ab2 either, whose settings are in its companion .xtra).

Macro Definition Documentation

◆ AT

#define AT ( i)
Value:
((i) < len ? src[i] : 0u)
ULONG len
Definition asllib.c:158

◆ F_COMMENT

#define F_COMMENT   0x040 /* starts a comment */

◆ F_COPY

#define F_COPY   0x010 /* copied to the output */

◆ F_EOL

#define F_EOL   0x080 /* ends a line: a 0 */

◆ F_FIRST

#define F_FIRST   0x008 /* starts a word */

◆ F_IS

#define F_IS   0x004 /* belongs to a word */

◆ F_NEW

#define F_NEW   0x002 /* a token may start behind it */

◆ F_QUOT

#define F_QUOT   0x020 /* starts a string */

◆ F_STOP

#define F_STOP   0x001 /* ends a word: look it up */

◆ F_TAB

#define F_TAB   0x100 /* a space */

◆ GET

#define GET ( i)
Value:
((i) < len ? src[i] : (lf && (i) == len) ? 10u : 0u)

◆ PUT

#define PUT ( b)
Value:
do { \
if (dp >= cap) \
goto space; \
dst[dp++] = (uint8_t)(b); \
} while (0)

◆ PUTLIT

#define PUTLIT ( b)
Value:
do { \
if (lit && (b) >= 128) \
PUT(b); \
} while (0)
#define TOKENIZER_LITHI
The first byte of a literal character (TOKENIZER_RULES_AB3_LIT).
Definition tokenizer.h:74

Function Documentation

◆ flags()

unsigned flags ( unsigned c)
static

◆ hash()

uint32_t hash ( const uint8_t * s,
size_t len )
static

◆ isext()

int isext ( const char * name,
size_t len,
const char * ext )
static

◆ lookup()

uint32_t lookup ( const struct tokenizer * t,
const uint8_t * name,
size_t len )
static

◆ remext()

size_t remext ( const char * name,
size_t len )
static

◆ run()

int run ( const struct tokenizer * t,
const uint8_t * src,
size_t len,
int lf,
uint8_t * dst,
size_t cap,
size_t * out )
static

◆ same()

int same ( const uint8_t * a,
const uint8_t * b,
size_t len )
static

◆ setext()

size_t setext ( const char * name,
size_t len,
const char * e1,
size_t n1,
const char * e2,
char * out,
size_t cap )
static

◆ tokenizer_ab3_getext()

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.include.ab3:2353): the part behind the last '.' among the last 10 characters, not a '.' at the start.

Parameters
namethe name
lenits length
Returns
the offset of the extension in name (behind the '.'); len if there is none

◆ tokenizer_ab3_header()

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 (CLIModeSupport.ab3:576): the lines up to the first one whose letters, '_', '/' and '-' before a '=' read "/xtra" in any case, that line with its LF included.

Parameters
srcthe source text
lenits length
Returns
the bytes of the header, 0 if there is no such line

◆ tokenizer_ab3_incheader()

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.ab3:401): leading lines with a ';' up to a "/xtra", "/XTRA" or "/Xtra", that line with its LF included.

Parameters
srcthe include's text
lenits length
Returns
the bytes of the header, 0 if there is none (also if a line without ';' comes first or the end is reached)

◆ tokenizer_ab3_incname()

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.

Parameters
namethe name at that place (path + include name)
lenits length
kwhich one: 0..TOKENIZER_AB3_INCNAMES-1
outthe name, with a 0
capthe size of out
Returns
its length; 0 if k is out of range or it does not fit

◆ tokenizer_ab3_inctype()

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 three characters, case-sensitively.

Parameters
namethe name
lenits length
Returns
TOKENIZER_AB3_TEXT ("ab3", "AB3", "ab2": tokenised as text), TOKENIZER_AB3_TOKENISED ("bb2", ".bb": read as it is) or TOKENIZER_AB3_REFUSED (any other: "You can only include tokenized files")

◆ tokenizer_ab3_mainline()

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.ab3:480): the bytes from pos up to the next 0 or the end, of which at most TOKENIZER_AB3_COLUMNS are kept.

Parameters
tokthe tokenised text (tokenizer_text)
lenits length
posthe start of the line (< len)
keepthe bytes of the line kept (without its 0)
Returns
the start of the next line (len after the last one)

◆ tokenizer_ab3_mainline_lit()

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 (TOKENIZER_LITHI and a byte from 128) counts as the one byte AmiBlitz3 keeps for it, and the cut never falls between its two bytes; every other byte counts one, as there. The line kept has at most TOKENIZER_AB3_COLUMNS bytes as well (the editor's line length is a byte): a line with literal characters may keep fewer characters than AmiBlitz3.

◆ tokenizer_ab3_source()

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 .bb or .bb2, in any case) and skips its embedded header (not a .ab2 either, whose settings are in its companion .xtra).

Parameters
namethe source's name
lenits length
Returns
a bit set: TOKENIZER_AB3_TEXT, TOKENIZER_AB3_HEADER

◆ tokenizer_add()

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 token (AmiBlitz3's preproc_QueryTokens): the call changes nothing.

Parameters
tthe tokeniser
namethe name's bytes (kept by reference)
lenits length
numthe token number (bit 15 set as in a tokenised source, or not: it is written as given)
Returns
TOKENIZER_OK, TOKENIZER_DUPLICATE, TOKENIZER_E_NAME or TOKENIZER_E_FULL

◆ tokenizer_find()

int tokenizer_find ( const struct tokenizer * t,
const uint8_t * name,
size_t len,
uint16_t * num )

Look a name up, byte for byte.

Parameters
tthe tokeniser
namethe bytes
lentheir number
numthe token if found (may be NULL)
Returns
1 if found, 0 if not

◆ tokenizer_init()

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.

Parameters
tthe object
rulesan enum tokenizer_rules
entryroom for cap names (at most 65535)
capthe entries there are
bucketthe hash buckets: nbucket words
nbucketa power of 2, 1..65536
Returns
TOKENIZER_OK, TOKENIZER_E_RULES or TOKENIZER_E_BUCKETS

◆ tokenizer_line()

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's 0.

Parameters
tthe tokeniser
srcthe line without its LF
lenits length
dstthe output (not overlapping src)
capits size; TOKENIZER_BOUND(len) is always enough
outthe bytes written, the 0 included
Returns
as tokenizer_text

◆ tokenizer_lines()

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).

◆ tokenizer_text()

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 ends a line with a 0; a TAB outside strings and comments becomes a space, CR, DEL and the other control characters are dropped, a byte from 128 becomes '?'; inside a string or a comment every byte stays except TAB (a space). A word that ends the text (no character behind it) is not looked up, a string or comment that ends it gets its 0 (AmiBlitz3 reads the 0 behind its buffer). With TOKENIZER_RULES_AB3_LIT a byte from 128 inside a string or a comment is written as the pair TOKENIZER_LITHI byte.

Parameters
tthe tokeniser
srcthe text
lenits length
dstthe output (not overlapping src)
capits size; TOKENIZER_BOUND(len) is always enough
outthe bytes written
Returns
TOKENIZER_OK, TOKENIZER_E_SPACE or TOKENIZER_E_RULES