-
Notifications
You must be signed in to change notification settings - Fork 11
/
Lexer.HC
140 lines (126 loc) · 2.71 KB
/
Lexer.HC
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// NOTE: This version of the lexer is very slow and isn't currently
// used. See FastLexer.HC for the real one. I may get around to
// writing a proper lexer generator eventually.
#ifndef LEXER_HC
#define LEXER_HC
#include "Array.HC"
#include "PArray.HC"
#include "Regex.HC"
#include "String.HC"
#include "Util.HC"
class Lexer
{
PArray *_regexes; // Array of pointers to Regex terms
PArray *_aux_regexes; // Auxiliary regexes with '.*' appended to the end
};
class Token
{
INT i;
String *s;
};
Token *TokenMk(INT i, String *s)
{
Token *t = MAlloc(sizeof(Token));
t->i = i;
t->s = s;
return t;
}
VOID TokenDelete(Token *t)
{
NullCheck(t, "token");
Free(t);
}
VOID LexerDelete(Lexer *l)
{
NullCheck(l, "lexer");
NullCheck(l->_regexes, "lexer regexes");
INT i;
for (i = 0; i < PArraySize(l->_regexes); ++i) {
RegexDelete(PArrayGet(l->_regexes, i));
}
Free(l);
}
Lexer *LexerEmpty()
{
Lexer *l = MAlloc(sizeof(Lexer));
l->_regexes = PArrayEmpty;
l->_aux_regexes = PArrayEmpty;
return l;
}
// expects [patterns] to be an PArray of Strings
Lexer *LexerMk(PArray *patterns)
{
NullCheck(patterns, "regex patterns");
INT i;
Lexer *l = LexerEmpty;
String *p;
Regex *r;
for (i = 0; i < PArraySize(patterns); ++i) {
p = PArrayGet(patterns, i);
r = RegexParse(p);
PArrayPush(l->_regexes, r);
PArrayPush(l->_aux_regexes, RegexMkPrefix(r));
}
return l;
}
// expects [patterns] to be an Array of c-strings
Lexer *LexerMkC(PArray *patterns)
{
INT i;
PArray *strings = PArrayEmpty;
for (i = 0; i < PArraySize(patterns); ++i) {
PArrayPush(strings, StringMk(PArrayGet(patterns, i)));
}
return LexerMk(strings);
}
INT _greedyMatch(Regex *r, String *s)
{
INT i, match;
INT max_i = 0;
String *sub;
for (i = 1; i <= StringLen(s); ++i) {
sub = StringSubstr(s, 0, i);
match = RegexMatch(r, sub, 0);
if (match)
max_i = i;
}
return max_i;
}
Token *LexerMatch(Lexer *l, String *s)
{
NullCheck(l, "lexer");
NullCheck(s, "input string");
INT i, j;
Regex *r, *r_aux;
for (i = 0; i < PArraySize(l->_regexes); ++i) {
r_aux = PArrayGet(l->_aux_regexes, i);
if (RegexMatch(r_aux, s, 0)) {
r = PArrayGet(l->_regexes, i);
j = _greedyMatch(r, s);
return TokenMk(i, StringSubstr(s, 0, j));
}
}
return NULL;
}
PArray *LexerLex(Lexer *l, String *s)
{
INT i = 0;
NullCheck(l, "lexer");
NullCheck(s, "input string");
s = StringCopy(s);
PArray *a = PArrayEmpty;
Token *t;
while (StringLen(s)) {
t = LexerMatch(l, s);
if (!t) {
for (i = 0; i < PArraySize(a); ++i) {
TokenDelete(PArrayGet(a, i));
}
return NULL;
}
PArrayPush(a, t);
StringErase(s, 0, StringLen(t->s));
}
return a;
}
#endif