-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathextension.c
More file actions
480 lines (366 loc) · 13.2 KB
/
Copy pathextension.c
File metadata and controls
480 lines (366 loc) · 13.2 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <string.h>
#include "gen_token_type.h"
#include "yarp.h"
#include <ruby.h>
VALUE rb_cYARP;
VALUE rb_cYARPToken;
VALUE rb_cYARPLocation;
static VALUE
location_new(yp_location_t *location) {
VALUE argv[] = { LONG2FIX(location->start), LONG2FIX(location->end) };
return rb_class_new_instance(2, argv, rb_cYARPLocation);
}
static VALUE
token_type(yp_token_t *token) {
if (token->type == YP_TOKEN_INVALID) {
// We're going to special-case the invalid token here since that doesn't
// actually exist in Ripper. This is going to give us a little more
// information when our tests fail.
// fprintf(stderr, "Invalid token: %.*s\n", (int) (token.end - token.start), token.start);
return ID2SYM(rb_intern("INVALID"));
}
return ID2SYM(rb_intern(token_type_to_str(token->type)));
}
static VALUE
token_new(yp_parser_t *parser, yp_token_t *token) {
VALUE argv[] = {
token_type(token),
rb_str_new(token->start, token->end - token->start),
location_new(&(yp_location_t) {
.start = token->start - parser->start,
.end = token->end - parser->start,
}),
};
return rb_class_new_instance(3, argv, rb_cYARPToken);
}
static VALUE
string_new(yp_parser_t *parser, yp_string_t *string) {
return rb_str_new(string_ptr(string), string_length(string));
}
/******************************************************************************/
/* BEGIN TEMPLATE */
/******************************************************************************/
static VALUE
node_new(yp_parser_t *parser, yp_node_t *node) {
switch (node->type) {
case YP_NODE_ASSIGNMENT: {
VALUE argv[4];
// target
argv[0] = node_new(parser, node->as.assignment.target);
// operator
argv[1] = string_new(parser, &node->as.assignment.operator);
// value
argv[2] = node_new(parser, node->as.assignment.value);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("Assignment")));
}
case YP_NODE_BINARY: {
VALUE argv[4];
// left
argv[0] = node_new(parser, node->as.binary.left);
// operator
argv[1] = string_new(parser, &node->as.binary.operator);
// right
argv[2] = node_new(parser, node->as.binary.right);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("Binary")));
}
case YP_NODE_CHARACTER_LITERAL: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.character_literal.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("CharacterLiteral")));
}
case YP_NODE_FLOAT_LITERAL: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.float_literal.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("FloatLiteral")));
}
case YP_NODE_IDENTIFIER: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.identifier.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("Identifier")));
}
case YP_NODE_IF_MODIFIER: {
VALUE argv[4];
// statement
argv[0] = node_new(parser, node->as.if_modifier.statement);
// keyword
argv[1] = string_new(parser, &node->as.if_modifier.keyword);
// predicate
argv[2] = node_new(parser, node->as.if_modifier.predicate);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("IfModifier")));
}
case YP_NODE_IMAGINARY_LITERAL: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.imaginary_literal.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("ImaginaryLiteral")));
}
case YP_NODE_INTEGER_LITERAL: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.integer_literal.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("IntegerLiteral")));
}
case YP_NODE_OPERATOR_ASSIGNMENT: {
VALUE argv[4];
// target
argv[0] = node_new(parser, node->as.operator_assignment.target);
// operator
argv[1] = string_new(parser, &node->as.operator_assignment.operator);
// value
argv[2] = node_new(parser, node->as.operator_assignment.value);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("OperatorAssignment")));
}
case YP_NODE_PROGRAM: {
VALUE argv[2];
// statements
argv[0] = node_new(parser, node->as.program.statements);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("Program")));
}
case YP_NODE_RANGE: {
VALUE argv[4];
// left
argv[0] = node->as.range.left == NULL ? Qnil : node_new(parser, node->as.range.left);
// operator
argv[1] = string_new(parser, &node->as.range.operator);
// right
argv[2] = node->as.range.right == NULL ? Qnil : node_new(parser, node->as.range.right);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("Range")));
}
case YP_NODE_RATIONAL_LITERAL: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.rational_literal.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("RationalLiteral")));
}
case YP_NODE_REDO: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.redo.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("Redo")));
}
case YP_NODE_RETRY: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.retry.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("Retry")));
}
case YP_NODE_STATEMENTS: {
VALUE argv[2];
// body
argv[0] = rb_ary_new();
for (size_t index = 0; index < node->as.statements.body->size; index++) {
rb_ary_push(argv[0], node_new(parser, node->as.statements.body->nodes[index]));
}
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("Statements")));
}
case YP_NODE_TERNARY: {
VALUE argv[6];
// predicate
argv[0] = node_new(parser, node->as.ternary.predicate);
// question_mark
argv[1] = string_new(parser, &node->as.ternary.question_mark);
// true_expression
argv[2] = node_new(parser, node->as.ternary.true_expression);
// colon
argv[3] = string_new(parser, &node->as.ternary.colon);
// false_expression
argv[4] = node_new(parser, node->as.ternary.false_expression);
// location
argv[5] = location_new(&node->location);
return rb_class_new_instance(6, argv, rb_const_get_at(rb_cYARP, rb_intern("Ternary")));
}
case YP_NODE_UNLESS_MODIFIER: {
VALUE argv[4];
// statement
argv[0] = node_new(parser, node->as.unless_modifier.statement);
// keyword
argv[1] = string_new(parser, &node->as.unless_modifier.keyword);
// predicate
argv[2] = node_new(parser, node->as.unless_modifier.predicate);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("UnlessModifier")));
}
case YP_NODE_UNTIL_MODIFIER: {
VALUE argv[4];
// statement
argv[0] = node_new(parser, node->as.until_modifier.statement);
// keyword
argv[1] = string_new(parser, &node->as.until_modifier.keyword);
// predicate
argv[2] = node_new(parser, node->as.until_modifier.predicate);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("UntilModifier")));
}
case YP_NODE_VARIABLE_REFERENCE: {
VALUE argv[2];
// value
argv[0] = string_new(parser, &node->as.variable_reference.value);
// location
argv[1] = location_new(&node->location);
return rb_class_new_instance(2, argv, rb_const_get_at(rb_cYARP, rb_intern("VariableReference")));
}
case YP_NODE_WHILE_MODIFIER: {
VALUE argv[4];
// statement
argv[0] = node_new(parser, node->as.while_modifier.statement);
// keyword
argv[1] = string_new(parser, &node->as.while_modifier.keyword);
// predicate
argv[2] = node_new(parser, node->as.while_modifier.predicate);
// location
argv[3] = location_new(&node->location);
return rb_class_new_instance(4, argv, rb_const_get_at(rb_cYARP, rb_intern("WhileModifier")));
}
default:
rb_raise(rb_eRuntimeError, "unknown node type: %d", node->type);
}
}
/******************************************************************************/
/* END TEMPLATE */
/******************************************************************************/
// Represents a source of Ruby code. It can either be coming from a file or a
// string. If it's a file, it's going to mmap the contents of the file. If it's
// a string it's going to just point to the contents of the string.
typedef struct {
enum { SOURCE_FILE, SOURCE_STRING } type;
const char *source;
off_t size;
} source_t;
// Read the file indicated by the filepath parameter into source and load its
// contents and size into the given source_t.
int
source_file_load(source_t *source, VALUE filepath) {
// Open the file for reading
int fd = open(StringValueCStr(filepath), O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
// Stat the file to get the file size
struct stat sb;
if (fstat(fd, &sb) == -1) {
close(fd);
perror("fstat");
return 1;
}
// mmap the file descriptor to virtually get the contents
source->size = sb.st_size;
source->source = mmap(NULL, source->size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (source == MAP_FAILED) {
perror("mmap");
return 1;
}
return 0;
}
// Load the contents and size of the given string into the given source_t.
void
source_string_load(source_t *source, VALUE string) {
*source = (source_t) {
.type = SOURCE_STRING,
.source = StringValueCStr(string),
.size = RSTRING_LEN(string),
};
}
// Free any resources associated with the given source_t.
void
source_file_unload(source_t *source) {
munmap((void *) source->source, source->size);
}
// Return an array of tokens corresponding to the given source.
static VALUE
lex_source(source_t *source) {
yp_parser_t parser;
yp_parser_init(&parser, source->source, source->size);
VALUE ary = rb_ary_new();
for (yp_lex_token(&parser); parser.current.type != YP_TOKEN_EOF; yp_lex_token(&parser)) {
rb_ary_push(ary, token_new(&parser, &parser.current));
}
return ary;
}
// Return an array of tokens corresponding to the given string.
static VALUE
lex(VALUE self, VALUE string) {
source_t source;
source_string_load(&source, string);
return lex_source(&source);
}
// Return an array of tokens corresponding to the given file.
static VALUE
lex_file(VALUE self, VALUE filepath) {
source_t source;
if (source_file_load(&source, filepath) != 0) return Qnil;
VALUE value = lex_source(&source);
source_file_unload(&source);
return value;
}
static VALUE
parse_source(source_t *source) {
yp_parser_t parser;
yp_parser_init(&parser, source->source, source->size);
yp_node_t *node = yp_parse(&parser);
VALUE value = node_new(&parser, node);
yp_node_dealloc(&parser, node);
return value;
}
static VALUE
parse(VALUE self, VALUE string) {
source_t source;
source_string_load(&source, string);
return parse_source(&source);
}
static VALUE
parse_file(VALUE self, VALUE rb_filepath) {
source_t source;
if (source_file_load(&source, rb_filepath) != 0) {
return Qnil;
}
VALUE value = parse_source(&source);
source_file_unload(&source);
return value;
}
void
Init_yarp(void) {
rb_cYARP = rb_define_module("YARP");
rb_cYARPToken = rb_define_class_under(rb_cYARP, "Token", rb_cObject);
rb_cYARPLocation = rb_define_class_under(rb_cYARP, "Location", rb_cObject);
rb_define_singleton_method(rb_cYARP, "lex", lex, 1);
rb_define_singleton_method(rb_cYARP, "lex_file", lex_file, 1);
rb_define_singleton_method(rb_cYARP, "parse", parse, 1);
rb_define_singleton_method(rb_cYARP, "parse_file", parse_file, 1);
}