summaryrefslogtreecommitdiff
path: root/src/services/quick-answers/math-lexer.c
blob: b0d24b737ee6cba73731b9f7857a243f5d6b0d33 (plain)
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
#include "services/quick-answers/math.h"

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 );

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 );
}

gboolean char_is_whitespace( char c )
{
	return c == ' ' || c == '\t' || c == '\r';
}

gboolean char_is_digit( char c )
{
	return c >= '0' && c <= '9';
}

gboolean char_is_identifier( char c )
{
	// Identifiers are all lowercase ASCII.

	return c >= 'a' && c <= 'z';
}

char lexer_peek(
	Lexer const* ctx,
	gsize        offset )
{
	gsize position = ctx->position + offset;
	if ( position >= ctx->input_length ) { return 0; }

	return ctx->input[position];
}

char lexer_pop( Lexer* ctx )
{
	if ( ctx->position >= ctx->input_length ) { return 0; }

	return ctx->input[ctx->position++];
}

void lexer_begin_token( Lexer* ctx )
{
	ctx->token_start = ctx->position;
}

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 );
}

void lexer_commit_single(
	Lexer*              ctx,
	FoobarMathTokenType type )
{
	lexer_begin_token( ctx );
	lexer_pop( ctx );
	lexer_commit_token( ctx, type );
}