-
Notifications
You must be signed in to change notification settings - Fork 4
/
inmind.h
298 lines (268 loc) · 8.02 KB
/
inmind.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* \file inmind.h
*
* <!-- Created on: Jul 24, 2017
* Author: asaparov -->
*/
#ifndef INMIND_H_
#define INMIND_H_
#include <core/lex.h>
#include <core/utility.h>
#include "datalog.h"
using namespace core;
/**
* A simple lexer for S-expression-like InMind data.
*/
enum class inmind_token_type {
LPAREN,
RPAREN,
IDENTIFIER,
STRING,
KEYWORD_CALL
};
typedef lexical_token<inmind_token_type> inmind_token;
template<typename Stream>
inline bool print(inmind_token_type type, Stream& stream) {
switch (type) {
case inmind_token_type::LPAREN:
return print('(', stream);
case inmind_token_type::RPAREN:
return print(')', stream);
case inmind_token_type::IDENTIFIER:
return fprintf(stream, "IDENTIFIER") > 0;
case inmind_token_type::STRING:
return fprintf(stream, "STRING") > 0;
case inmind_token_type::KEYWORD_CALL:
return fprintf(stream, "call KEYWORD") > 0;
}
fprintf(stderr, "print ERROR: Unknown inmind_token_type.\n");
return false;
}
enum class inmind_lexer_state {
START,
IDENTIFIER,
STRING
};
bool inmind_emit_symbol(array<inmind_token>& tokens, const position& start, char symbol) {
switch (symbol) {
case '(':
return emit_token(tokens, start, start + 1, inmind_token_type::LPAREN);
case ')':
return emit_token(tokens, start, start + 1, inmind_token_type::RPAREN);
default:
fprintf(stderr, "inmind_emit_symbol ERROR: Unexpected symbol.\n");
return false;
}
}
inline bool inmind_emit_identifier(
array<inmind_token>& tokens, array<char>& token,
const position& start, const position& current)
{
if (compare_strings(token, "call"))
return emit_token(tokens, start, current, inmind_token_type::KEYWORD_CALL);
return emit_token(tokens, token, start, current, inmind_token_type::IDENTIFIER);
}
bool inmind_lex(array<inmind_token>& tokens, FILE* input) {
position start = position(1, 1);
position current = position(1, 1);
inmind_lexer_state state = inmind_lexer_state::START;
array<char> token = array<char>(1024);
int next = fgetc(input);
bool new_line = false;
while (next != -1) {
switch (state) {
case inmind_lexer_state::STRING:
if (next == '\\') {
/* escape character */
next = fgetc(input); current.column++;
if (next == -1) {
read_error("Unexpected end of stream", current);
return false;
}
if (!token.add(next)) return false;
} else if (next == '"') {
if (!emit_token(tokens, token, start, current, inmind_token_type::STRING)) return false;
state = inmind_lexer_state::START;
token.clear();
} else {
if (!token.add(next)) return false;
}
break;
case inmind_lexer_state::IDENTIFIER:
if (next == '(' || next == ')') {
if (!inmind_emit_identifier(tokens, token, start, current)
|| !inmind_emit_symbol(tokens, current, next))
return false;
state = inmind_lexer_state::START;
token.clear();
} else if (next == '\'') {
read_error("Unexpected quote after identifier", current);
return false;
} else if (next == '\\') {
read_error("Unexpected backslash after identifier", current);
return false;
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
if (!inmind_emit_identifier(tokens, token, start, current))
return false;
state = inmind_lexer_state::START;
token.clear();
new_line = (next == '\n');
} else {
if (!token.add(next)) return false;
}
break;
case inmind_lexer_state::START:
if (next == '(' || next == ')') {
if (!inmind_emit_symbol(tokens, current, next))
return false;
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
new_line = (next == '\n');
} else if (next == '"') {
state = inmind_lexer_state::STRING;
} else {
if (!token.add(next)) return false;
state = inmind_lexer_state::IDENTIFIER;
start = current;
}
break;
}
if (new_line) {
current.line++;
current.column = 1;
new_line = false;
} else current.column++;
next = fgetc(input);
}
if (state == inmind_lexer_state::STRING) {
read_error("Expected closing quote", current);
return false;
} else if (state == inmind_lexer_state::IDENTIFIER) {
return inmind_emit_identifier(tokens, token, start, current);
}
return true;
}
/**
* A recursive descent parser for S-expression-like InMind data.
*/
inline bool tokenize(
const string& sentence, sequence& output,
hash_map<string, unsigned int>& names)
{
bool whitespace = true;
unsigned int token_start = 0;
array<unsigned int> tokens = array<unsigned int>(16);
for (unsigned int i = 0; i < sentence.length; i++) {
if (whitespace) {
if (!isspace(sentence[i])) {
token_start = i;
whitespace = false;
}
} else {
if (sentence[i] == '\'' || sentence[i] == '?' || isspace(sentence[i])) {
unsigned int id;
if (!get_token(string(sentence.data + token_start, i - token_start), id, names)
|| !tokens.add(id))
return false;
if (isspace(sentence[i])) {
whitespace = true;
} else {
if (!get_token(string(sentence.data + i, 1), id, names)
|| !tokens.add(id))
return false;
whitespace = true;
}
}
}
}
if (!whitespace) {
unsigned int id;
if (!get_token(string(sentence.data + token_start, sentence.length - token_start), id, names)
|| !tokens.add(id))
return false;
}
if (!init(output, 1))
return false;
swap(output.tokens, tokens.data);
output.length = tokens.length;
return true;
}
bool inmind_interpret_expression(
const array<inmind_token>& tokens,
unsigned int& index,
datalog_expression& exp,
hash_map<string, unsigned int>& names)
{
if (index >= tokens.length) {
read_error("Expected a string or open parenthesis", tokens.last().end);
return false;
} else if (tokens[index].type == inmind_token_type::STRING) {
if (!init(exp.str, tokens[index].text)) return false;
exp.type = DATALOG_STRING;
exp.reference_count = 1;
index++; return true;
} else if (tokens[index].type != inmind_token_type::LPAREN) {
read_error("Expected a string or open parenthesis", tokens[index].start);
return false;
}
index++;
if (index >= tokens.length) {
read_error("Expected an identifier after an open parenthesis", tokens.last().end);
return false;
} else if (tokens[index].type == inmind_token_type::KEYWORD_CALL) {
index++;
}
if (!expect_token(tokens, index, inmind_token_type::IDENTIFIER, "identifier")
|| !get_token(tokens[index].text, exp.pred.function, names))
return false;
index++;
for (unsigned int i = 0; i < array_length(exp.pred.args); i++)
exp.pred.args[i] = NULL;
exp.pred.excluded_count = 0;
exp.type = DATALOG_PREDICATE;
exp.reference_count = 1;
/* parse the arguments */
unsigned int arg_index = 0;
while (true) {
if (index >= tokens.length) {
read_error("Unexpected end of input", tokens.last().end);
return false;
} else if (tokens[index].type == inmind_token_type::RPAREN) {
index++;
return true;
} else if (arg_index == array_length(exp.pred.args)) {
read_error("Too many arguments", tokens[index].start);
return false;
} else if (!new_expression(exp.pred.args[arg_index])) {
return false;
} else if (!inmind_interpret_expression(tokens, index, *exp.pred.args[arg_index], names)) {
free(exp.pred.args[arg_index]);
exp.pred.args[arg_index] = NULL;
}
arg_index++;
}
}
bool inmind_interpret(
const array<inmind_token>& tokens,
array<datalog_expression_root*>& expressions,
hash_map<string, unsigned int>& names)
{
unsigned int index = 0;
while (index < tokens.length) {
if (!expressions.ensure_capacity(expressions.length + 1))
return false;
datalog_expression_root*& next_expression = expressions[(unsigned int) expressions.length];
next_expression = (datalog_expression_root*) malloc(sizeof(datalog_expression_root));
if (next_expression == NULL) {
fprintf(stderr, "inmind_interpret ERROR: Out of memory.\n");
return false;
}
next_expression->index = NUMBER_ALL;
next_expression->concord = NUMBER_NONE;
next_expression->inf = INFLECTION_NONE;
if (!inmind_interpret_expression(tokens, index, next_expression->root, names))
return false;
expressions.length++;
}
return true;
}
#endif /* INMIND_H_ */