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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#include "services/quick-answers/math.h"
//
// FoobarMathToken:
//
// A token in a mathematical expression. The token contents are left as-is and need not be null-terminated. For example,
// "sin(pi) - 3" would be represented as:
// - IDENTIFIER: "sin"
// - PAREN_OPEN: "("
// - IDENTIFIER: "pi"
// - PAREN_CLOSE: ")"
// - MINUS: "-"
// - NUMBER: "3"
//
//
// Lexer:
//
// The lexer context/state which is passed around.
//
typedef struct _Lexer Lexer;
struct _Lexer
{
GArray* result;
gchar const* input;
gsize input_length;
gsize position;
gsize token_start;
};
static gboolean char_is_whitespace( char c );
static gboolean char_is_digit ( char c );
static gboolean char_is_identifier( char c );
static char lexer_peek ( Lexer const* ctx,
gsize offset );
static char lexer_pop ( Lexer* ctx );
static void lexer_begin_token ( Lexer* ctx );
static void lexer_commit_token ( Lexer* ctx,
FoobarMathTokenType type );
static void lexer_commit_single( Lexer* ctx,
FoobarMathTokenType type );
// ---------------------------------------------------------------------------------------------------------------------
// Lexing
// ---------------------------------------------------------------------------------------------------------------------
//
// Divide an input string into a list of tokens.
//
// On success, this will return a newly allocated array of tokens which should be freed using g_free. The number of
// elements will be written into out_count.
//
// On error, this will return NULL.
//
FoobarMathToken* foobar_math_lex(
gchar const* input,
gsize input_length,
gsize* out_count )
{
g_autoptr( GArray ) result = g_array_new( FALSE, TRUE, sizeof( FoobarMathToken ) );
Lexer ctx = { 0 };
ctx.result = result;
ctx.input = input;
ctx.input_length = input_length;
while ( ctx.position < input_length )
{
if ( char_is_whitespace( lexer_peek( &ctx, 0 ) ) )
{
// Skip whitespace.
lexer_pop( &ctx );
}
else if ( char_is_digit( lexer_peek( &ctx, 0 ) ) )
{
// Process numbers as a whole, don't allow whitespace inbetween.
lexer_begin_token( &ctx );
while ( char_is_digit( lexer_peek( &ctx, 0 ) ) )
{
lexer_pop( &ctx );
}
if ( lexer_peek( &ctx, 0 ) == '.' && char_is_digit( lexer_peek( &ctx, 1 ) ) )
{
lexer_pop( &ctx );
while ( char_is_digit( lexer_peek( &ctx, 0 ) ) )
{
lexer_pop( &ctx );
}
}
lexer_commit_token( &ctx, FOOBAR_MATH_TOKEN_NUMBER );
}
else if ( char_is_identifier( lexer_peek( &ctx, 0 ) ) )
{
// Don't validate identifiers, leave that to the parser.
lexer_begin_token( &ctx );
while ( char_is_identifier( lexer_peek( &ctx, 0 ) ) )
{
lexer_pop( &ctx );
}
lexer_commit_token( &ctx, FOOBAR_MATH_TOKEN_IDENTIFIER );
}
else if ( lexer_peek( &ctx, 0 ) == '(' )
{
lexer_commit_single( &ctx, FOOBAR_MATH_TOKEN_PAREN_OPEN );
}
else if ( lexer_peek( &ctx, 0 ) == ')' )
{
lexer_commit_single( &ctx, FOOBAR_MATH_TOKEN_PAREN_CLOSE );
}
else if ( lexer_peek( &ctx, 0 ) == '+' )
{
lexer_commit_single( &ctx, FOOBAR_MATH_TOKEN_PLUS );
}
else if ( lexer_peek( &ctx, 0 ) == '-' )
{
lexer_commit_single( &ctx, FOOBAR_MATH_TOKEN_MINUS );
}
else if ( lexer_peek( &ctx, 0 ) == '*' )
{
lexer_commit_single( &ctx, FOOBAR_MATH_TOKEN_MUL );
}
else if ( lexer_peek( &ctx, 0 ) == '/' )
{
lexer_commit_single( &ctx, FOOBAR_MATH_TOKEN_DIV );
}
else if ( lexer_peek( &ctx, 0 ) == '^' )
{
lexer_commit_single( &ctx, FOOBAR_MATH_TOKEN_POW );
}
else
{
// Unknown character.
return NULL;
}
}
return g_array_steal( result, out_count );
}
// ---------------------------------------------------------------------------------------------------------------------
// Character Classification
// ---------------------------------------------------------------------------------------------------------------------
//
// Check if a character is a whitespace symbol (space, tab, carriage return).
//
gboolean char_is_whitespace( char c )
{
return c == ' ' || c == '\t' || c == '\r';
}
//
// Check if a character is a decimal digit.
//
gboolean char_is_digit( char c )
{
return c >= '0' && c <= '9';
}
//
// Check if a character belongs to an identifier (like "sin", "pi", etc.).
//
gboolean char_is_identifier( char c )
{
// Identifiers are all lowercase ASCII.
return c >= 'a' && c <= 'z';
}
// ---------------------------------------------------------------------------------------------------------------------
// State Helpers
// ---------------------------------------------------------------------------------------------------------------------
//
// Peek at the current character without consuming it.
//
// If this is the end of the input string, return 0.
//
char lexer_peek(
Lexer const* ctx,
gsize offset )
{
gsize position = ctx->position + offset;
if ( position >= ctx->input_length ) { return 0; }
return ctx->input[position];
}
//
// Consume the current character, moving forward to the next one.
//
// If this is the end of the input string, return 0.
//
char lexer_pop( Lexer* ctx )
{
if ( ctx->position >= ctx->input_length ) { return 0; }
return ctx->input[ctx->position++];
}
//
// Start recording a token by remembering the current position.
//
// The current character will be the first one in the token.
//
void lexer_begin_token( Lexer* ctx )
{
ctx->token_start = ctx->position;
}
//
// Stop recording a token, saving the string between the call to lexer_begin_token as the specified type.
//
// The current character will not be a part of the token.
//
void lexer_commit_token(
Lexer* ctx,
FoobarMathTokenType type )
{
FoobarMathToken token = { 0 };
token.data = &ctx->input[ctx->token_start];
token.length = ctx->position - ctx->token_start;
token.type = type;
g_array_append_val( ctx->result, token );
}
//
// Consume the current character and record it as a token.
//
// This is equivalent to calling lexer_begin_token, lexer_pop, and lexer_commit_token.
//
void lexer_commit_single(
Lexer* ctx,
FoobarMathTokenType type )
{
lexer_begin_token( ctx );
lexer_pop( ctx );
lexer_commit_token( ctx, type );
}
|