-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lex.h
64 lines (55 loc) · 1.16 KB
/
Lex.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
#pragma once
#include <stdlib.h>
#define lex_MAX_ID_LENGTH 255
#define lex_MAX_NUMCONST_LENGTH 12
enum lex_Token {
tokERROR_NUMCONST_OVERFLOW = -4,
tokERROR_ILLEGAL_CHARACTER = -3,
tokERROR_IDENTIFIER_TOO_LONG = -2,
tokEOF = -1,
tokNEWLINE, /* 0 */
tokWHITE,
tokID,
tokNUMCONST,
tokCOLON,
tokCOMMA,
tokLPAR,
tokRPAR,
tokPLUS,
tokMINUS,
tokMULT, /* 10 */
tokDIV,
tokMOD,
tokAND,
tokOR,
tokNOT,
tokEQ,
tokNE,
tokLT,
tokGT,
tokLE,
tokGE,
tokIF, /* 22 */
tokTHEN,
tokELSE,
tokENDIF,
tokGOTO,
tokDONTMATCH = 2000000 /* Lexer will never return this token value */
};
typedef enum lex_Token lex_Token;
typedef struct lex_Context lex_Context;
struct lex_Context
{
const char* sourceCodeBegin;
const char* sourceCodeEnd;
const char* sourceCodeCursor;
int line; /* current line */
int col; /* current column */
int c; /* current character */
lex_Token tok; /* kind of token from enum above */
/* Values */
int numConst; /* tokNUMCONST */
char id[lex_MAX_ID_LENGTH + 1]; /* tokID */
};
lex_Context* lex_initContext(lex_Context* c, const char* sourceCode, size_t sourceCodeLength);
lex_Token lex_nextToken(lex_Context* c);