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_t * | tok_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_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. | |
| 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. | |
Token instance representation and operations.
This group contains the token container structure (tok_t) and all functions for creating, managing, and destroying individual tokens.
| 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.
| fp | A valid file pointer to which the token will be written. Must not be NULL. |
| tok | A constant pointer to the tok_t object to be written. |
true if all write operations succeed, false otherwise. | 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.
| tok | A constant pointer to the tok_t object to be printed. |
| tok_t * tok_ctor | ( | ) |
| 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.
| tok | A pointer to the tok_t object to be destroyed. If tok or tok->val is NULL, the function does nothing. |
| 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.
| n | The number of tokens to allocate. |
tok_t tokens. 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.
| value | The string value of the token (lexeme). |
| type | The specific token type from tok_e. |
| line | The line number where the token appears in the source code. |
| col | The column number where the token starts in the line. |
tok_t object. The caller is responsible for freeing the memory.