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

Token instance representation and operations. More...

Classes

struct  tok_t
 Container for token data and metadata. More...
 

Typedefs

typedef struct tok_t tok_t
 

Functions

tok_ttok_ctor ()
 Allocates and returns a new token object.
 
tok_t ** tok_nctor (const size_t n)
 Allocates and returns an array of token objects.
 
tok_ttok_ptor (char *value, const tok_e type, const size_t line, const size_t col)
 Creates and initializes a token with given values.
 
void tok_dtor (tok_t *tok)
 Frees the memory associated with a token.
 
void printf_tok (const tok_t *const tok)
 Prints the contents of a token to the standard output.
 
bool fwrite_tok (FILE *fp, const tok_t *const tok)
 Writes the contents of a token to a file.
 

Detailed Description

Token instance representation and operations.

This group contains the token container structure (tok_t) and all functions for creating, managing, and destroying individual tokens.

Function Documentation

◆ fwrite_tok()

bool fwrite_tok ( FILE * fp,
const tok_t *const tok )

Writes the contents of a token to a file.

This function prints the token's value, type, line number, and column number to the specified file. It performs error checking on each write operation and reports issues using perror.

Parameters
fpA valid file pointer to which the token will be written. Must not be NULL.
tokA constant pointer to the tok_t object to be written.
Returns
true if all write operations succeed, false otherwise.

◆ printf_tok()

void printf_tok ( const tok_t *const tok)

Prints the contents of a token to the standard output.

This utility function displays the token's value, type (as a string), line number, and column number in a human-readable format using printf.

Parameters
tokA constant pointer to the tok_t object to be printed.

◆ tok_ctor()

tok_t * tok_ctor ( )

Allocates and returns a new token object.

This is the default constructor for a tok_t token. It dynamically allocates memory for a new token instance and returns a pointer to it.

Returns
A pointer to a newly allocated tok_t object. The memory should be freed by the caller when no longer needed.

◆ tok_dtor()

void tok_dtor ( tok_t * tok)

Frees the memory associated with a token.

This destructor deallocates the memory used by a tok_t object, including its dynamically allocated value string. After calling this function, the token pointer should no longer be used.

Parameters
tokA pointer to the tok_t object to be destroyed. If tok or tok->val is NULL, the function does nothing.

◆ tok_nctor()

tok_t ** tok_nctor ( const size_t n)

Allocates and returns an array of token objects.

This function acts as a constructor for multiple tok_t tokens. It allocates memory for an array of n tokens and returns a pointer to the array.

Parameters
nThe number of tokens to allocate.
Returns
A pointer to array. of tok_t tokens.
Note
The memory should be freed by the caller when no longer needed.

◆ tok_ptor()

tok_t * tok_ptor ( char * value,
const tok_e type,
const size_t line,
const size_t col )

Creates and initializes a token with given values.

This parameterized constructor allocates a new tok_t object and initializes it with the provided token value, type, line number, and column number.

Parameters
valueThe string value of the token (lexeme).
typeThe specific token type from tok_e.
lineThe line number where the token appears in the source code.
colThe column number where the token starts in the line.
Returns
A pointer to the newly created and initialized tok_t object. The caller is responsible for freeing the memory.