1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
/* Copyright (c) 2024 Freya Murphy */
#ifndef __LEX_H__
#define __LEX_H__
#include <mlimits.h>
#include <stdio.h>
#include <stdint.h>
struct lexer {
FILE *file;
int peek;
int x;
int y;
};
struct lexer_state {
long offset;
int peek;
int x;
int y;
};
enum token_type {
TOK_IDENT,
TOK_REG,
TOK_LABEL,
TOK_STRING,
TOK_COMMA,
TOK_EQUAL,
TOK_LPAREN,
TOK_RPAREN,
TOK_NUMBER,
TOK_EOF,
TOK_NL,
TOK_DIRECTIVE,
};
struct token {
enum token_type type;
union {
int64_t number;
char text[MAX_LEX_LENGTH];
};
int x;
int y;
};
/* initalize a lexer */
int lexer_init(const char *file, struct lexer *lexer);
/* free the lxer */
int lexer_free(struct lexer *lexer);
/* lexes the next token, returns M_ERROR on error,
* and TOK_EOF on EOF */
int lexer_next(struct lexer *lexer, struct token *token);
/* token type to string */
char *token_str(enum token_type);
/* save the state of a lexer */
void lexer_save(struct lexer *lexer, struct lexer_state *state);
/* load a different state into a lexer */
void lexer_load(struct lexer *lexer, const struct lexer_state *state);
#endif /* __LEX_H__ */
|