Skip to content

Commit 7960c48

Browse files
committed
introduce yp_string_t, use it instead of yp_token_t as node field
1 parent b8c7130 commit 7960c48

File tree

16 files changed

+284
-159
lines changed

16 files changed

+284
-159
lines changed

Gemfile.lock

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ GEM
1515
power_assert
1616

1717
PLATFORMS
18-
arm64-darwin-21
19-
x86_64-linux
18+
ruby
2019

2120
DEPENDENCIES
2221
bundler (~> 2)

bin/template

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ class NodeListParam < Struct.new(:name)
4747
end
4848

4949
# This represents a parameter to a node that is a token. We pass them as
50-
# references and store them by copying.
51-
class TokenParam < Struct.new(:name)
52-
def decl = "yp_token_t #{name}"
50+
# references and store by converting to a string.
51+
class StringParam < Struct.new(:name)
52+
def decl = "yp_string_t #{name}"
5353
def param = "yp_token_t *#{name}"
54-
def assign = "*#{name}"
55-
def rbs_class = "Token"
54+
def assign = "token_to_string(*#{name})"
55+
def rbs_class = "String"
5656
def child_nodes = nil
5757

5858
def start_location(params) = "#{name}->start - parser->start"
@@ -85,10 +85,10 @@ class NodeType
8585
OptionalNodeParam.new(name, $1)
8686
when "node[]"
8787
NodeListParam.new(name)
88-
when "token"
89-
TokenParam.new(name)
88+
when "string"
89+
StringParam.new(name)
9090
else
91-
raise
91+
raise "Unknown field type #{type.inspect}"
9292
end
9393
end
9494

bin/templates/extension.c.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
2121
for (size_t index = 0; index < node->as.<%= node.human %>.<%= param.name %>->size; index++) {
2222
rb_ary_push(argv[<%= index %>], node_new(parser, node->as.<%= node.human %>.<%= param.name %>->nodes[index]));
2323
}
24-
<%- when TokenParam -%>
25-
argv[<%= index %>] = token_new(parser, &node->as.<%= node.human %>.<%= param.name %>);
24+
<%- when StringParam -%>
25+
argv[<%= index %>] = string_new(parser, &node->as.<%= node.human %>.<%= param.name %>);
2626
<%- else -%>
27-
<%- raise -%>
27+
<%- raise "Unsupported param type #{param.class}" -%>
2828
<%- end -%>
2929
<%- end -%>
3030

bin/templates/yarp.c.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ yp_node_dealloc(yp_parser_t *parser, yp_node_t *node) {
3131
case <%= node.type %>:
3232
<%- node.params.each do |param| -%>
3333
<%- case param -%>
34-
<%- when TokenParam -%>
34+
<%- when StringParam -%>
3535
<%- when NodeParam -%>
3636
yp_node_dealloc(parser, node->as.<%= node.human %>.<%= param.name %>);
3737
<%- when OptionalNodeParam -%>

config.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -278,45 +278,45 @@ nodes:
278278
- name: Assignment
279279
child_nodes:
280280
- node target
281-
- token operator
281+
- string operator
282282
- node value
283283
location: target->value
284284
- name: Binary
285285
child_nodes:
286286
- node left
287-
- token operator
287+
- string operator
288288
- node right
289289
location: left->right
290290
- name: CharacterLiteral
291291
child_nodes:
292-
- token value
292+
- string value
293293
location: value
294294
- name: FloatLiteral
295295
child_nodes:
296-
- token value
296+
- string value
297297
location: value
298298
- name: Identifier
299299
child_nodes:
300-
- token value
300+
- string value
301301
location: value
302302
- name: IfModifier
303303
child_nodes:
304304
- node statement
305-
- token keyword
305+
- string keyword
306306
- node predicate
307307
location: statement->predicate
308308
- name: ImaginaryLiteral
309309
child_nodes:
310-
- token value
310+
- string value
311311
location: value
312312
- name: IntegerLiteral
313313
child_nodes:
314-
- token value
314+
- string value
315315
location: value
316316
- name: OperatorAssignment
317317
child_nodes:
318318
- node target
319-
- token operator
319+
- string operator
320320
- node value
321321
location: target->value
322322
- name: Program
@@ -325,21 +325,21 @@ nodes:
325325
location: statements
326326
- name: RationalLiteral
327327
child_nodes:
328-
- token value
328+
- string value
329329
location: value
330330
- name: Range
331331
child_nodes:
332332
- node?operator left
333-
- token operator
333+
- string operator
334334
- node?operator right
335335
location: left->right
336336
- name: Redo
337337
child_nodes:
338-
- token value
338+
- string value
339339
location: value
340340
- name: Retry
341341
child_nodes:
342-
- token value
342+
- string value
343343
location: value
344344
- name: Statements
345345
child_nodes:
@@ -348,30 +348,30 @@ nodes:
348348
- name: Ternary
349349
child_nodes:
350350
- node predicate
351-
- token question_mark
351+
- string question_mark
352352
- node true_expression
353-
- token colon
353+
- string colon
354354
- node false_expression
355355
location: predicate->false_expression
356356
- name: UnlessModifier
357357
child_nodes:
358358
- node statement
359-
- token keyword
359+
- string keyword
360360
- node predicate
361361
location: statement->predicate
362362
- name: UntilModifier
363363
child_nodes:
364364
- node statement
365-
- token keyword
365+
- string keyword
366366
- node predicate
367367
location: statement->predicate
368368
- name: VariableReference
369369
child_nodes:
370-
- token value
370+
- string value
371371
location: value
372372
- name: WhileModifier
373373
child_nodes:
374374
- node statement
375-
- token keyword
375+
- string keyword
376376
- node predicate
377377
location: statement->predicate

ext/yarp/extconf.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
require "mkmf"
44
$INCFLAGS << " -I$(top_srcdir)" if $extmk
5+
6+
# There are complains about missing memcpy and strlen
7+
$CFLAGS << ' -Wno-implicit-function-declaration'
8+
59
create_makefile "yarp/yarp"

ext/yarp/extension.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <string.h>
2+
13
#include "gen_token_type.h"
24
#include "yarp.h"
35
#include <ruby.h>
@@ -39,6 +41,11 @@ token_new(yp_parser_t *parser, yp_token_t *token) {
3941
return rb_class_new_instance(3, argv, rb_cYARPToken);
4042
}
4143

44+
static VALUE
45+
string_new(yp_parser_t *parser, yp_string_t *string) {
46+
return rb_str_new(string_ptr(string), string_length(string));
47+
}
48+
4249
/******************************************************************************/
4350
/* BEGIN TEMPLATE */
4451
/******************************************************************************/
@@ -53,7 +60,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
5360
argv[0] = node_new(parser, node->as.assignment.target);
5461

5562
// operator
56-
argv[1] = token_new(parser, &node->as.assignment.operator);
63+
argv[1] = string_new(parser, &node->as.assignment.operator);
5764

5865
// value
5966
argv[2] = node_new(parser, node->as.assignment.value);
@@ -70,7 +77,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
7077
argv[0] = node_new(parser, node->as.binary.left);
7178

7279
// operator
73-
argv[1] = token_new(parser, &node->as.binary.operator);
80+
argv[1] = string_new(parser, &node->as.binary.operator);
7481

7582
// right
7683
argv[2] = node_new(parser, node->as.binary.right);
@@ -84,7 +91,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
8491
VALUE argv[2];
8592

8693
// value
87-
argv[0] = token_new(parser, &node->as.character_literal.value);
94+
argv[0] = string_new(parser, &node->as.character_literal.value);
8895

8996
// location
9097
argv[1] = location_new(&node->location);
@@ -95,7 +102,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
95102
VALUE argv[2];
96103

97104
// value
98-
argv[0] = token_new(parser, &node->as.float_literal.value);
105+
argv[0] = string_new(parser, &node->as.float_literal.value);
99106

100107
// location
101108
argv[1] = location_new(&node->location);
@@ -106,7 +113,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
106113
VALUE argv[2];
107114

108115
// value
109-
argv[0] = token_new(parser, &node->as.identifier.value);
116+
argv[0] = string_new(parser, &node->as.identifier.value);
110117

111118
// location
112119
argv[1] = location_new(&node->location);
@@ -120,7 +127,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
120127
argv[0] = node_new(parser, node->as.if_modifier.statement);
121128

122129
// keyword
123-
argv[1] = token_new(parser, &node->as.if_modifier.keyword);
130+
argv[1] = string_new(parser, &node->as.if_modifier.keyword);
124131

125132
// predicate
126133
argv[2] = node_new(parser, node->as.if_modifier.predicate);
@@ -134,7 +141,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
134141
VALUE argv[2];
135142

136143
// value
137-
argv[0] = token_new(parser, &node->as.imaginary_literal.value);
144+
argv[0] = string_new(parser, &node->as.imaginary_literal.value);
138145

139146
// location
140147
argv[1] = location_new(&node->location);
@@ -145,7 +152,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
145152
VALUE argv[2];
146153

147154
// value
148-
argv[0] = token_new(parser, &node->as.integer_literal.value);
155+
argv[0] = string_new(parser, &node->as.integer_literal.value);
149156

150157
// location
151158
argv[1] = location_new(&node->location);
@@ -159,7 +166,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
159166
argv[0] = node_new(parser, node->as.operator_assignment.target);
160167

161168
// operator
162-
argv[1] = token_new(parser, &node->as.operator_assignment.operator);
169+
argv[1] = string_new(parser, &node->as.operator_assignment.operator);
163170

164171
// value
165172
argv[2] = node_new(parser, node->as.operator_assignment.value);
@@ -187,7 +194,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
187194
argv[0] = node->as.range.left == NULL ? Qnil : node_new(parser, node->as.range.left);
188195

189196
// operator
190-
argv[1] = token_new(parser, &node->as.range.operator);
197+
argv[1] = string_new(parser, &node->as.range.operator);
191198

192199
// right
193200
argv[2] = node->as.range.right == NULL ? Qnil : node_new(parser, node->as.range.right);
@@ -201,7 +208,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
201208
VALUE argv[2];
202209

203210
// value
204-
argv[0] = token_new(parser, &node->as.rational_literal.value);
211+
argv[0] = string_new(parser, &node->as.rational_literal.value);
205212

206213
// location
207214
argv[1] = location_new(&node->location);
@@ -212,7 +219,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
212219
VALUE argv[2];
213220

214221
// value
215-
argv[0] = token_new(parser, &node->as.redo.value);
222+
argv[0] = string_new(parser, &node->as.redo.value);
216223

217224
// location
218225
argv[1] = location_new(&node->location);
@@ -223,7 +230,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
223230
VALUE argv[2];
224231

225232
// value
226-
argv[0] = token_new(parser, &node->as.retry.value);
233+
argv[0] = string_new(parser, &node->as.retry.value);
227234

228235
// location
229236
argv[1] = location_new(&node->location);
@@ -251,13 +258,13 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
251258
argv[0] = node_new(parser, node->as.ternary.predicate);
252259

253260
// question_mark
254-
argv[1] = token_new(parser, &node->as.ternary.question_mark);
261+
argv[1] = string_new(parser, &node->as.ternary.question_mark);
255262

256263
// true_expression
257264
argv[2] = node_new(parser, node->as.ternary.true_expression);
258265

259266
// colon
260-
argv[3] = token_new(parser, &node->as.ternary.colon);
267+
argv[3] = string_new(parser, &node->as.ternary.colon);
261268

262269
// false_expression
263270
argv[4] = node_new(parser, node->as.ternary.false_expression);
@@ -274,7 +281,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
274281
argv[0] = node_new(parser, node->as.unless_modifier.statement);
275282

276283
// keyword
277-
argv[1] = token_new(parser, &node->as.unless_modifier.keyword);
284+
argv[1] = string_new(parser, &node->as.unless_modifier.keyword);
278285

279286
// predicate
280287
argv[2] = node_new(parser, node->as.unless_modifier.predicate);
@@ -291,7 +298,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
291298
argv[0] = node_new(parser, node->as.until_modifier.statement);
292299

293300
// keyword
294-
argv[1] = token_new(parser, &node->as.until_modifier.keyword);
301+
argv[1] = string_new(parser, &node->as.until_modifier.keyword);
295302

296303
// predicate
297304
argv[2] = node_new(parser, node->as.until_modifier.predicate);
@@ -305,7 +312,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
305312
VALUE argv[2];
306313

307314
// value
308-
argv[0] = token_new(parser, &node->as.variable_reference.value);
315+
argv[0] = string_new(parser, &node->as.variable_reference.value);
309316

310317
// location
311318
argv[1] = location_new(&node->location);
@@ -319,7 +326,7 @@ node_new(yp_parser_t *parser, yp_node_t *node) {
319326
argv[0] = node_new(parser, node->as.while_modifier.statement);
320327

321328
// keyword
322-
argv[1] = token_new(parser, &node->as.while_modifier.keyword);
329+
argv[1] = string_new(parser, &node->as.while_modifier.keyword);
323330

324331
// predicate
325332
argv[2] = node_new(parser, node->as.while_modifier.predicate);

0 commit comments

Comments
 (0)