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

Go to the source code of this file.

Data Structures

struct  tokenizer_entry
 One name of the token list (the caller's memory). More...
struct  tokenizer
 The tokeniser: the token list as a hash table over the caller's arrays, and the rule set. Read-only while tokenising. More...

Macros

#define TOKENIZER_AB3_COLUMNS   250
 Characters of a line of a main source AmiBlitz3 keeps (MAX_Columns, Shared/common.ab3:96).
#define TOKENIZER_BOUND(len)
 Bytes tokenizer_text and tokenizer_line may write for len bytes of text: a name of one letter becomes a 2-byte token, a string or a comment at the end of the text gets its 0.
#define TOKENIZER_LITHI   0x90
 The first byte of a literal character (TOKENIZER_RULES_AB3_LIT).
#define TOKENIZER_AB3_TEXT   1 /* tokenised as text */
#define TOKENIZER_AB3_HEADER   2 /* its embedded header skipped */
#define TOKENIZER_AB3_INCNAMES   7
 The names AmiBlitz3 tries for an include at one place (ab3_GetIncludeLock, AmiBlitz3.ab3:25796): the name, then its extension replaced by .ab3, .ab2, .bb2, then these behind the old extension (dos_SetExt/dos_RemExt of dos.include.ab3).
#define TOKENIZER_AB3_TOKENISED   2
#define TOKENIZER_AB3_REFUSED   0

Enumerations

enum  tokenizer_rules { TOKENIZER_RULES_AB3 = 1 , TOKENIZER_RULES_AB3_LIT = 2 }
 The rule sets. More...
enum  tokenizer_err {
  TOKENIZER_OK , TOKENIZER_DUPLICATE , TOKENIZER_E_RULES , TOKENIZER_E_BUCKETS ,
  TOKENIZER_E_FULL , TOKENIZER_E_NAME , TOKENIZER_E_SPACE
}
 Results of the functions (TOKENIZER_OK = 0). More...

Functions

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

Macro Definition Documentation

◆ TOKENIZER_AB3_COLUMNS

#define TOKENIZER_AB3_COLUMNS   250

Characters of a line of a main source AmiBlitz3 keeps (MAX_Columns, Shared/common.ab3:96).

◆ TOKENIZER_AB3_HEADER

#define TOKENIZER_AB3_HEADER   2 /* its embedded header skipped */

◆ TOKENIZER_AB3_INCNAMES

#define TOKENIZER_AB3_INCNAMES   7

The names AmiBlitz3 tries for an include at one place (ab3_GetIncludeLock, AmiBlitz3.ab3:25796): the name, then its extension replaced by .ab3, .ab2, .bb2, then these behind the old extension (dos_SetExt/dos_RemExt of dos.include.ab3).

◆ TOKENIZER_AB3_REFUSED

#define TOKENIZER_AB3_REFUSED   0

◆ TOKENIZER_AB3_TEXT

#define TOKENIZER_AB3_TEXT   1 /* tokenised as text */

◆ TOKENIZER_AB3_TOKENISED

#define TOKENIZER_AB3_TOKENISED   2

◆ TOKENIZER_BOUND

#define TOKENIZER_BOUND ( len)
Value:
(2 * (size_t)(len) + 2)
ULONG len
Definition asllib.c:158

Bytes tokenizer_text and tokenizer_line may write for len bytes of text: a name of one letter becomes a 2-byte token, a string or a comment at the end of the text gets its 0.

◆ TOKENIZER_LITHI

#define TOKENIZER_LITHI   0x90

The first byte of a literal character (TOKENIZER_RULES_AB3_LIT).

Enumeration Type Documentation

◆ tokenizer_err

Results of the functions (TOKENIZER_OK = 0).

Enumerator
TOKENIZER_OK 
TOKENIZER_DUPLICATE 
TOKENIZER_E_RULES 
TOKENIZER_E_BUCKETS 
TOKENIZER_E_FULL 
TOKENIZER_E_NAME 
TOKENIZER_E_SPACE 

◆ tokenizer_rules

The rule sets.

Enumerator
TOKENIZER_RULES_AB3 
TOKENIZER_RULES_AB3_LIT 

Function Documentation

◆ 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