Decaf Compiler
Loading...
Searching...
No Matches
token.h
Go to the documentation of this file.
1
9#ifndef __TOKENS_H
10#define __TOKENS_H
11
12#include "common.h"
13
25typedef regex_t Regex;
26
33Regex* Regex_new (const char* regex);
34
46bool Regex_match (Regex *regex, const char *text, char *match);
47
53void Regex_free (Regex* regex);
54
69typedef enum TokenType {
70 ID, DECLIT, HEXLIT, STRLIT, KEY, SYM
72
78typedef struct Token
79{
84
89
93 int line;
94
98 struct Token* next;
99
101
109
118bool token_str_eq(const char* str1, const char* str2);
119
131Token* Token_new (TokenType type, const char* text, int line);
132
138void Token_free (Token* token);
139
152typedef struct TokenQueue
153{
158
163
165
172
179void TokenQueue_add (TokenQueue* queue, Token* token);
180
189
197
204bool TokenQueue_is_empty (TokenQueue* queue);
205
212size_t TokenQueue_size (TokenQueue* queue);
213
220void TokenQueue_print (TokenQueue* queue, FILE* out);
221
229void TokenQueue_free (TokenQueue* queue);
230
231#endif
Includes, constants, declarations, and macros used across the compiler.
#define MAX_TOKEN_LEN
Maximum length (in characters) of any single token.
Definition common.h:47
Single token.
Definition token.h:79
struct Token * next
Pointer to next token (used to store in a list)
Definition token.h:98
int line
Source line number.
Definition token.h:93
TokenType type
Type of the token.
Definition token.h:83
char text[256]
Raw text of the token.
Definition token.h:88
Linked list of tokens.
Definition token.h:153
Token * tail
Back of list (or NULL if list is empty)
Definition token.h:162
Token * head
Front of list (or NULL if list is empty)
Definition token.h:157
void TokenQueue_print(TokenQueue *queue, FILE *out)
Print a queue to the given file descriptor (debug output)
Definition token.c:120
TokenQueue * TokenQueue_new(void)
Allocate and initialize a new, empty queue of tokens.
Definition token.c:65
size_t TokenQueue_size(TokenQueue *queue)
Calculate size of the queue.
Definition token.c:111
const char * TokenType_to_string(TokenType type)
Convert a token type to a string for output.
Definition token.c:31
void Token_free(Token *token)
Deallocate a token.
Definition token.c:60
Regex * Regex_new(const char *regex)
Allocate and compile a new regular expression.
Definition token.c:3
bool Regex_match(Regex *regex, const char *text, char *match)
Match a regular expression against some text.
Definition token.c:12
void TokenQueue_free(TokenQueue *queue)
Deallocate a token queue.
Definition token.c:129
bool token_str_eq(const char *str1, const char *str2)
Check string equality for tokens. Limits comparison to MAX_TOKEN_LEN for safety.
Definition token.c:44
bool TokenQueue_is_empty(TokenQueue *queue)
Check whether a queue is empty.
Definition token.c:106
TokenType
Valid token types.
Definition token.h:69
Token * Token_new(TokenType type, const char *text, int line)
Allocate and initialize a new token.
Definition token.c:49
regex_t Regex
Compiled regular expression.
Definition token.h:25
void Regex_free(Regex *regex)
Deallocate a regular expression.
Definition token.c:25
Token * TokenQueue_peek(TokenQueue *queue)
Return the next token from a queue without removing it (first-in-first-out)
Definition token.c:85
void TokenQueue_add(TokenQueue *queue, Token *token)
Add a token to a queue.
Definition token.c:72
Token * TokenQueue_remove(TokenQueue *queue)
Remove a token from a queue (first-in-first-out)
Definition token.c:90