Decaf Compiler
token.h
Go to the documentation of this file.
1 
9 #ifndef __TOKENS_H
10 #define __TOKENS_H
11 
12 #include "common.h"
13 
25 typedef regex_t Regex;
26 
33 Regex* Regex_new (const char* regex);
34 
46 bool Regex_match (Regex *regex, const char *text, char *match);
47 
53 void Regex_free (Regex* regex);
54 
69 typedef enum TokenType {
70  ID, DECLIT, HEXLIT, STRLIT, KEY, SYM
72 
78 typedef struct Token
79 {
84 
89 
93  int line;
94 
98  struct Token* next;
99 
101 
108 const char* TokenType_to_string(TokenType type);
109 
118 bool token_str_eq(const char* str1, const char* str2);
119 
131 Token* Token_new (TokenType type, const char* text, int line);
132 
138 void Token_free (Token* token);
139 
152 typedef struct TokenQueue
153 {
158 
163 
165 
172 
179 void TokenQueue_add (TokenQueue* queue, Token* token);
180 
189 
197 
204 bool TokenQueue_is_empty (TokenQueue* queue);
205 
212 size_t TokenQueue_size (TokenQueue* queue);
213 
220 void TokenQueue_print (TokenQueue* queue, FILE* out);
221 
229 void 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
Token * TokenQueue_remove(TokenQueue *queue)
Remove a token from a queue (first-in-first-out)
Definition: token.c:90
void TokenQueue_print(TokenQueue *queue, FILE *out)
Print a queue to the given file descriptor (debug output)
Definition: token.c:120
const char * TokenType_to_string(TokenType type)
Convert a token type to a string for output.
Definition: token.c:31
size_t TokenQueue_size(TokenQueue *queue)
Calculate size of the queue.
Definition: token.c:111
Token * TokenQueue_peek(TokenQueue *queue)
Return the next token from a queue without removing it (first-in-first-out)
Definition: token.c:85
Token * Token_new(TokenType type, const char *text, int line)
Allocate and initialize a new token.
Definition: token.c:49
void Token_free(Token *token)
Deallocate a token.
Definition: token.c:60
TokenQueue * TokenQueue_new()
Allocate and initialize a new, empty queue of tokens.
Definition: token.c:65
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
regex_t Regex
Compiled regular expression.
Definition: token.h:25
void Regex_free(Regex *regex)
Deallocate a regular expression.
Definition: token.c:25
struct Token Token
Single token.
struct TokenQueue TokenQueue
Linked list of tokens.
void TokenQueue_add(TokenQueue *queue, Token *token)
Add a token to a queue.
Definition: token.c:72
Regex * Regex_new(const char *regex)
Allocate and compile a new regular expression.
Definition: token.c:3