This repository was archived by the owner on Sep 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.h
More file actions
108 lines (95 loc) · 3.96 KB
/
lexer.h
File metadata and controls
108 lines (95 loc) · 3.96 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef LEXER_H
#define LEXER_H
#include "clomy.h"
#include "utils.h"
/* Lexer flags. */
#define READ_BLOCK_COMMENT (1 << 0)
#define READ_STR_LIT (1 << 1)
#define READ_INT_LIT (1 << 2)
#define READ_FLOAT_LIT (1 << 3)
/* Handle operators which takes 2 characters. */
#define LEX_HANDLE_OP(ctx, ch1, ch2, token) \
if ((ctx)->src->data[(ctx)->pos] == (ch1) \
&& (ctx)->src->data[(ctx)->pos + 1] == (ch2)) \
{ \
sbreset (&(ctx)->sb); \
(ctx)->pos += 2; \
return (token); \
}
/* Return known keywords. */
#define LEX_FLUSH_IDENTF() \
do \
{ \
if (ctx->sb.size > 0) \
{ \
ctx->str = sbflush (&ctx->sb); \
if (streq (ctx->str->data, "program")) \
return TOKEN_PROGRAM; \
else if (streq (ctx->str->data, "then")) \
return TOKEN_THEN; \
else if (streq (ctx->str->data, "else")) \
return TOKEN_ELSE; \
else if (streq (ctx->str->data, "do")) \
return TOKEN_DO; \
else if (streq (ctx->str->data, "var")) \
return TOKEN_VAR; \
else if (streq (ctx->str->data, "begin")) \
return TOKEN_BEGIN; \
else if (streq (ctx->str->data, "end")) \
return TOKEN_BLOCK_END; \
else if (streq (ctx->str->data, "while")) \
return TOKEN_WHILE; \
else if (streq (ctx->str->data, "if")) \
return TOKEN_IF; \
return TOKEN_IDENTF; \
} \
sbreset (&ctx->sb); \
} \
while (0);
typedef struct lex
{
arena ar;
stringbuilder sb;
string *path;
string *src;
string *str;
double float_num;
long int_num;
U32 line;
U32 col;
U32 pos;
} lex;
enum lex_token
{
TOKEN_END = 256,
TOKEN_IDENTF,
TOKEN_STRLIT,
TOKEN_FLOATLIT,
TOKEN_INTLIT,
TOKEN_INFEQ,
TOKEN_GEQ,
TOKEN_LEQ,
TOKEN_NEQ,
TOKEN_PROGRAM,
TOKEN_THEN,
TOKEN_ELSE,
TOKEN_DO,
TOKEN_VAR,
TOKEN_BEGIN,
TOKEN_BLOCK_END,
TOKEN_WHILE,
TOKEN_IF
};
/* Initialize the lexer. */
int lex_init (lex *ctx, char *path);
/* Move the lexer forward and get next token. */
int lex_next_token (lex *ctx);
/* Peek the next token without moving forward. */
int lex_peek (lex *ctx);
/* Print token in Human-friendly way. */
void lex_print_token (lex *ctx, int tok);
/* Report error at current lexer position. */
void lex_error (lex *ctx, char *msg);
/* Cleanup the lexer. */
void lex_fold (lex *ctx);
#endif /* not LEXER_H */