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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <assert.h>
#include "prelexer.h"
/* Creates a scanner state which will be useful for accessing the lexer later. */
PreLexerState*
pl_create_scanner(const gchar* input)
{
PreLexerState* state;
assert(input != NULL);
assert(g_utf8_validate(input, -1, NULL));
state = (PreLexerState *) malloc(sizeof(PreLexerState));
assert(state != NULL);
state->stream = g_strdup(input);
state->length = strlen(state->stream); /* Can't find a GLib replacement of strlen. The mailing list discussion says, it is not implemented because strlen is perfectly capable. :) */
state->next_index = 0;
state->mark_index = 0;
return state;
}
/* Destroy and free memory used by LexerState object. */
void
pl_destroy_scanner(PreLexerState* state)
{
free(state->stream);
free(state);
}
/* Roll back last scanned unichar. */
void
pl_roll_back(PreLexerState* state)
{
gchar* tmp;
tmp = g_utf8_find_prev_char(state->stream, state->stream + state->next_index);
if(tmp == NULL)
/* Already at the beginning of the stram. Reset index. */
state->next_index = 0;
else
state->next_index = tmp - state->stream;
}
/* Get validated gunichar from input stream. */
gunichar
pl_get_next_gunichar(PreLexerState* state)
{
gunichar ret;
if(state->next_index >= state->length)
{
/* To prevent scanning last letter multiple times, when a single unconditional rollback is used. */
if(state->next_index == state->length)
state->next_index++;
return 0;
}
ret = g_utf8_get_char_validated(state->stream + state->next_index, -1);
state->next_index = g_utf8_next_char(state->stream + state->next_index) - state->stream;
return ret;
}
/* Set marker index. To be used for highlighting and error reporting. */
void
pl_set_marker(PreLexerState* state)
{
state->mark_index = state->next_index;
}
/* Get marked substring. To be used for error reporting. */
gchar*
pl_get_marked_substring(const PreLexerState* state)
{
return g_strndup(state->stream + state->mark_index, state->next_index - state->mark_index);
}
/* Compares a list of strings with given unichar. To be used by pl_get_next_token() only. */
static gboolean
pl_compare_all(const gunichar ch, const gint count, gchar *arr[])
{
gint l;
for(l = 0; l < count; l++)
{
if(ch == g_utf8_get_char_validated(arr[l], -1))
return TRUE;
}
return FALSE;
}
/* Pre-Lexer tokanizer. To be called only by Lexer. */
LexerTokenType
pl_get_next_token(PreLexerState* state)
{
gunichar ch = pl_get_next_gunichar(state);
if(pl_compare_all(ch, 2, (gchar*[]){",","."}))
return PL_DECIMAL;
if(g_unichar_isdigit(ch) || pl_compare_all(ch, 10, (gchar*[]){"〇","〡","〢","〣","〤","〥","〦","〧","〨","〩"}))
return PL_DIGIT; /* 0-9 */
if(g_unichar_isxdigit(ch))
return PL_HEX; /* This is supposed to report just the A-F. */
if(pl_compare_all(ch, 10, (gchar*[]){"⁰","¹","²","³","⁴","⁵","⁶","⁷","⁸","⁹"}))
return PL_SUPER_DIGIT;
if(pl_compare_all(ch, 1, (gchar*[]){"⁻"}))
return PL_SUPER_MINUS;
if(pl_compare_all(ch, 10, (gchar*[]){"₀","₁","₂","₃","₄","₅","₆","₇","₈","₉"}))
return PL_SUB_DIGIT;
if(pl_compare_all(ch, 15, (gchar*[]){"½","⅓","⅔","¼","¾","⅕","⅖","⅗","⅘","⅙","⅚","⅛","⅜","⅝","⅞"}))
return PL_FRACTION;
if(pl_compare_all(ch, 1, (gchar*[]){"°"}))
return PL_DEGREE;
if(pl_compare_all(ch, 1, (gchar*[]){"'"}))
return PL_MINUTE;
if(pl_compare_all(ch, 1, (gchar*[]){"\""}))
return PL_SECOND;
if(g_unichar_isalpha(ch))
return PL_LETTER; /* All alphabets excluding A-F. [a-fA-F] are reported as PL_HEX. */
if(pl_compare_all(ch, 1, (gchar*[]){"∧"}))
return T_AND;
if(pl_compare_all(ch, 1, (gchar*[]){"∨"}))
return T_OR;
if(pl_compare_all(ch, 2, (gchar*[]){"⊻","⊕"}))
return T_XOR;
if(pl_compare_all(ch, 2, (gchar*[]){"¬","~"}))
return T_NOT;
if(pl_compare_all(ch, 1, (gchar*[]){"+"}))
return T_ADD;
if(pl_compare_all(ch, 3, (gchar*[]){"-","−","–"}))
return T_SUBTRACT;
if(pl_compare_all(ch, 2, (gchar*[]){"*","×"}))
return T_MULTIPLY;
if(pl_compare_all(ch, 3, (gchar*[]){"/","∕","÷"}))
return T_DIV;
if(pl_compare_all(ch, 1, (gchar*[]){"⌊"}))
return T_L_FLOOR;
if(pl_compare_all(ch, 1, (gchar*[]){"⌋"}))
return T_R_FLOOR;
if(pl_compare_all(ch, 1, (gchar*[]){"⌈"}))
return T_L_CEILING;
if(pl_compare_all(ch, 1, (gchar*[]){"⌉"}))
return T_R_CEILING;
if(pl_compare_all(ch, 1, (gchar*[]){"√"}))
return T_ROOT;
if(pl_compare_all(ch, 1, (gchar*[]){"∛"}))
return T_ROOT_3;
if(pl_compare_all(ch, 1, (gchar*[]){"∜"}))
return T_ROOT_4;
if(pl_compare_all(ch, 1, (gchar*[]){"="}))
return T_ASSIGN;
if(pl_compare_all(ch, 1, (gchar*[]){"("}))
return T_L_R_BRACKET;
if(pl_compare_all(ch, 1, (gchar*[]){")"}))
return T_R_R_BRACKET;
if(pl_compare_all(ch, 1, (gchar*[]){"["}))
return T_L_S_BRACKET;
if(pl_compare_all(ch, 1, (gchar*[]){"]"}))
return T_R_S_BRACKET;
if(pl_compare_all(ch, 1, (gchar*[]){"{"}))
return T_L_C_BRACKET;
if(pl_compare_all(ch, 1, (gchar*[]){"}"}))
return T_R_C_BRACKET;
if(pl_compare_all(ch, 1, (gchar*[]){"|"}))
return T_ABS;
if(pl_compare_all(ch, 1, (gchar*[]){"^"}))
return T_POWER;
if(pl_compare_all(ch, 1, (gchar*[]){"!"}))
return T_FACTORIAL;
if(pl_compare_all(ch, 1, (gchar*[]){"%"}))
return T_PERCENTAGE;
if(pl_compare_all(ch, 4, (gchar*[]){" ","\r","\t","\n"}))
/* Gotta ignore'Em all!!! ;) */
return PL_SKIP;
if(ch == 0)
return PL_EOS;
/* There is no spoon. */
return T_UNKNOWN;
}
|