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
71 } TokenType;
72 
78 typedef struct Token
79 {
84 
89 
93  int line;
94 
98  struct Token* next;
99 
100 } Token;
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 
164 } TokenQueue;
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
TokenQueue_remove
Token * TokenQueue_remove(TokenQueue *queue)
Remove a token from a queue (first-in-first-out)
Definition: token.c:90
Token::next
struct Token * next
Pointer to next token (used to store in a list)
Definition: token.h:98
TokenQueue_new
TokenQueue * TokenQueue_new()
Allocate and initialize a new, empty queue of tokens.
Definition: token.c:65
TokenQueue_free
void TokenQueue_free(TokenQueue *queue)
Deallocate a token queue.
Definition: token.c:129
Token::text
char text[256]
Raw text of the token.
Definition: token.h:88
TokenQueue
Linked list of tokens.
Definition: token.h:152
Token
struct Token Token
Single token.
Token::line
int line
Source line number.
Definition: token.h:93
Token
Single token.
Definition: token.h:78
TokenQueue_is_empty
bool TokenQueue_is_empty(TokenQueue *queue)
Check whether a queue is empty.
Definition: token.c:106
TokenQueue_add
void TokenQueue_add(TokenQueue *queue, Token *token)
Add a token to a queue.
Definition: token.c:72
Regex_free
void Regex_free(Regex *regex)
Deallocate a regular expression.
Definition: token.c:25
TokenQueue_size
size_t TokenQueue_size(TokenQueue *queue)
Calculate size of the queue.
Definition: token.c:111
Token::type
TokenType type
Type of the token.
Definition: token.h:83
TokenQueue_print
void TokenQueue_print(TokenQueue *queue, FILE *out)
Print a queue to the given file descriptor (debug output)
Definition: token.c:120
MAX_TOKEN_LEN
#define MAX_TOKEN_LEN
Maximum length (in characters) of any single token.
Definition: common.h:47
TokenType
TokenType
Valid token types.
Definition: token.h:69
Regex
regex_t Regex
Compiled regular expression.
Definition: token.h:25
TokenQueue::head
Token * head
Front of list (or NULL if list is empty)
Definition: token.h:157
Token_free
void Token_free(Token *token)
Deallocate a token.
Definition: token.c:60
Regex_new
Regex * Regex_new(const char *regex)
Allocate and compile a new regular expression.
Definition: token.c:3
common.h
Includes, constants, declarations, and macros used across the compiler.
TokenQueue_peek
Token * TokenQueue_peek(TokenQueue *queue)
Return the next token from a queue without removing it (first-in-first-out)
Definition: token.c:85
Token_new
Token * Token_new(TokenType type, const char *text, int line)
Allocate and initialize a new token.
Definition: token.c:49
TokenQueue::tail
Token * tail
Back of list (or NULL if list is empty)
Definition: token.h:162
token_str_eq
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
TokenType_to_string
const char * TokenType_to_string(TokenType type)
Convert a token type to a string for output.
Definition: token.c:31
TokenQueue
struct TokenQueue TokenQueue
Linked list of tokens.
Regex_match
bool Regex_match(Regex *regex, const char *text, char *match)
Match a regular expression against some text.
Definition: token.c:12