Collection of tokens and operations. More...
Classes | |
struct | tokset_t |
Container for multiple tokens. More... | |
Typedefs | |
typedef struct tokset_t | tokset_t |
Functions | |
tokset_t * | tokset_ctor () |
Allocates and returns a new token set object. | |
tokset_t * | tokset_ptor (const size_t toksz) |
Creates and initializes a token set with a specified number of tokens. | |
void | tokset_dtor (tokset_t *set) |
Frees the memory associated with a token set. | |
size_t | cnt_toktyp (const tokset_t *const set, const tok_e type) |
Counts the number of tokens of a specific type in a token set. | |
void | printf_tokset (const tokset_t *const set) |
Prints the contents of a token set to the standard output. | |
bool | fwrite_tokset (FILE *fp, const tokset_t *const set) |
Writes the contents of a token set to a file. | |
Collection of tokens and operations.
This group contains the token set structure (tokset_t) and all functions for managing collections of tokens, including:
Counts the number of tokens of a specific type in a token set.
This function iterates through the token set and counts how many tokens match the specified type. It compares each token's type with the provided type
argument.
set | A constant pointer to the tokset_t object containing the tokens to be checked. |
type | The specific token type (from tok_e ) to count in the token set. |
bool fwrite_tokset | ( | FILE * | fp, |
const tokset_t *const | set ) |
Writes the contents of a token set to a file.
This function writes a summary of the token set to the specified file, including the total number of tokens and a breakdown of the counts for each token type. It then writes each individual token’s details to the file. If any write operation fails, it reports the error and returns false
.
fp | A valid file pointer where the token set will be written. Must not be NULL . |
set | A constant pointer to the tokset_t object containing the tokens to be written. |
true
if all write operations succeed, false
otherwise. void printf_tokset | ( | const tokset_t *const | set | ) |
Prints the contents of a token set to the standard output.
This function iterates through the token set and prints each token's value, type, line, and column information using the printf_tok
function. It formats the output such that each token is printed on a new line, except for the last token.
set | A constant pointer to the tokset_t object containing the tokens to be printed. |
tokset_t * tokset_ctor | ( | ) |
Allocates and returns a new token set object.
This constructor allocates memory for a new tokset_t
object, which will hold a collection of tokens. However, the token array itself (toks
) is not yet initialized and will need further allocation.
tokset_t
object. The caller is responsible for freeing the memory when done. void tokset_dtor | ( | tokset_t * | set | ) |
Frees the memory associated with a token set.
This destructor deallocates the memory used by a tokset_t
object, including freeing each individual token in the token set (using tok_dtor
) and the token array itself. After calling this function, the token set pointer should no longer be used.
set | A pointer to the tokset_t object to be destroyed. If set is NULL , the function does nothing. |
tokset_t * tokset_ptor | ( | const size_t | toksz | ) |
Creates and initializes a token set with a specified number of tokens.
This parameterized constructor allocates memory for a tokset_t
object and initializes the toks
array to hold a specified number of tokens. It also sets the size of the token set (toksz
). If memory allocation fails at any point, it reports the error and returns NULL
.
toksz | The number of tokens that the token set should be able to hold. |
tokset_t
object. Returns NULL
if memory allocation fails.