-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
64 lines (54 loc) · 1.16 KB
/
lexer.h
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
#ifndef LEXER_H
#define LEXER_H
#include <stdio.h>
#include "buffer.h"
#ifndef LEXER_BLOCK_SIZE
#define LEXER_BLOCK_SIZE 512
#endif
#ifndef LEXER_TBUF_SIZE
#define LEXER_TBUF_SIZE 64
#endif
typedef enum {
LEXER_ERROR_NO_ERROR,
LEXER_ERROR_INVALID_TOKEN,
LEXER_ERROR_UNEXPECTED_EOF,
} LexerErrorType;
typedef struct {
LexerErrorType type;
char text[128];
int line;
int column;
} LexerError;
typedef struct {
FILE *stream;
LexerBuffer *block;
LexerBuffer *tbuf;
LexerError *error;
int eof;
int line;
int column;
} Lexer;
typedef enum {
UNKNOWN,
NUMBER,
IDENTIFIER,
PAREN_LEFT,
PAREN_RIGHT,
OPERATOR_ARITHMETIC,
OPERATOR_RELATIONAL,
OPERATOR_LOGICAL,
} TokenType;
typedef struct {
TokenType type;
const char *text;
int line;
int column;
} Token;
Lexer *lexer_new(FILE *stream);
void lexer_destroy(Lexer *lex);
Token *lexer_next_token(Lexer *lex);
Token *lexer_token_new(TokenType type, const char *text, int line, int column);
void lexer_token_destroy(Token *t);
LexerError *lexer_error_new(Lexer *lex, LexerErrorType type);
void lexer_error_destroy(LexerError *err);
#endif