Lexical Analyzer v0.1.0
 
Loading...
Searching...
No Matches
Token Set

Collection of tokens and operations. More...

Classes

struct  tokset_t
 Container for multiple tokens. More...
 

Typedefs

typedef struct tokset_t tokset_t
 

Functions

tokset_ttokset_ctor ()
 Allocates and returns a new token set object.
 
tokset_ttokset_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.
 

Detailed Description

Collection of tokens and operations.

This group contains the token set structure (tokset_t) and all functions for managing collections of tokens, including:

Function Documentation

◆ cnt_toktyp()

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.

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.

Parameters
setA constant pointer to the tokset_t object containing the tokens to be checked.
typeThe specific token type (from tok_e) to count in the token set.
Returns
The number of tokens of the specified type found in the token set.

◆ fwrite_tokset()

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.

Parameters
fpA valid file pointer where the token set will be written. Must not be NULL.
setA constant pointer to the tokset_t object containing the tokens to be written.
Returns
true if all write operations succeed, false otherwise.

◆ printf_tokset()

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.

Parameters
setA constant pointer to the tokset_t object containing the tokens to be printed.

◆ tokset_ctor()

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.

Returns
A pointer to the newly allocated tokset_t object. The caller is responsible for freeing the memory when done.

◆ tokset_dtor()

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.

Parameters
setA pointer to the tokset_t object to be destroyed. If set is NULL, the function does nothing.

◆ tokset_ptor()

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.

Parameters
tokszThe number of tokens that the token set should be able to hold.
Returns
A pointer to the newly created and initialized tokset_t object. Returns NULL if memory allocation fails.