summaryrefslogtreecommitdiff
path: root/prism/templates/ext/prism/api_node.c.erb
blob: 419236ef782013113ad125e8fc5eda426a898aa7 (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
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
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
#include "prism/extension.h"

extern VALUE rb_cPrism;
extern VALUE rb_cPrismNode;
extern VALUE rb_cPrismSource;
extern VALUE rb_cPrismToken;
extern VALUE rb_cPrismLocation;

<%- nodes.each do |node| -%>
static VALUE rb_cPrism<%= node.name %>;
<%- end -%>

static VALUE
pm_location_new(const pm_parser_t *parser, const uint8_t *start, const uint8_t *end) {
    uint64_t value = ((((uint64_t) (start - parser->start)) << 32) | ((uint32_t) (end - start)));
    return ULL2NUM(value);
}

VALUE
pm_token_new(const pm_parser_t *parser, const pm_token_t *token, rb_encoding *encoding, VALUE source) {
    ID type = rb_intern(pm_token_type_name(token->type));
    VALUE location = pm_location_new(parser, token->start, token->end);

    VALUE argv[] = {
        source,
        ID2SYM(type),
        rb_enc_str_new((const char *) token->start, token->end - token->start, encoding),
        location
    };

    return rb_class_new_instance(4, argv, rb_cPrismToken);
}

static VALUE
pm_string_new(const pm_string_t *string, rb_encoding *encoding) {
    return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), encoding);
}

VALUE
pm_integer_new(const pm_integer_t *integer) {
    VALUE result;
    if (integer->values == NULL) {
        result = UINT2NUM(integer->value);
    } else {
        VALUE string = rb_str_new(NULL, integer->length * 8);
        unsigned char *bytes = (unsigned char *) RSTRING_PTR(string);

        size_t offset = integer->length * 8;
        for (size_t value_index = 0; value_index < integer->length; value_index++) {
            uint32_t value = integer->values[value_index];

            for (int index = 0; index < 8; index++) {
                int byte = (value >> (4 * index)) & 0xf;
                bytes[--offset] = byte < 10 ? byte + '0' : byte - 10 + 'a';
            }
        }

        result = rb_funcall(string, rb_intern("to_i"), 1, UINT2NUM(16));
    }

    if (integer->negative) {
        result = rb_funcall(result, rb_intern("-@"), 0);
    }

    return result;
}

// Create a Prism::Source object from the given parser, after pm_parse() was called.
VALUE
pm_source_new(const pm_parser_t *parser, rb_encoding *encoding) {
    VALUE source_string = rb_enc_str_new((const char *) parser->start, parser->end - parser->start, encoding);

    VALUE offsets = rb_ary_new_capa(parser->newline_list.size);
    for (size_t index = 0; index < parser->newline_list.size; index++) {
        rb_ary_push(offsets, ULONG2NUM(parser->newline_list.offsets[index]));
    }

    VALUE source_argv[] = { source_string, LONG2NUM(parser->start_line), offsets };
    return rb_class_new_instance(3, source_argv, rb_cPrismSource);
}

typedef struct pm_node_stack_node {
    struct pm_node_stack_node *prev;
    const pm_node_t *visit;
    bool visited;
} pm_node_stack_node_t;

static void
pm_node_stack_push(pm_node_stack_node_t **stack, const pm_node_t *visit) {
    pm_node_stack_node_t *node = xmalloc(sizeof(pm_node_stack_node_t));
    node->prev = *stack;
    node->visit = visit;
    node->visited = false;
    *stack = node;
}

static const pm_node_t *
pm_node_stack_pop(pm_node_stack_node_t **stack) {
    pm_node_stack_node_t *current = *stack;
    const pm_node_t *visit = current->visit;

    *stack = current->prev;
    xfree(current);

    return visit;
}

VALUE
pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encoding, VALUE source) {
    VALUE constants = rb_ary_new_capa(parser->constant_pool.size);

    for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
        pm_constant_t *constant = &parser->constant_pool.constants[index];
        int state = 0;

        VALUE string = rb_enc_str_new((const char *) constant->start, constant->length, encoding);
        VALUE value = rb_protect(rb_str_intern, string, &state);

        if (state != 0) {
            value = ID2SYM(rb_intern_const("?"));
            rb_set_errinfo(Qnil);
        }

        rb_ary_push(constants, value);
    }

    pm_node_stack_node_t *node_stack = NULL;
    pm_node_stack_push(&node_stack, node);
    VALUE value_stack = rb_ary_new();

    while (node_stack != NULL) {
        if (!node_stack->visited) {
            if (node_stack->visit == NULL) {
                pm_node_stack_pop(&node_stack);
                rb_ary_push(value_stack, Qnil);
                continue;
            }

            const pm_node_t *node = node_stack->visit;
            node_stack->visited = true;

            switch (PM_NODE_TYPE(node)) {
                <%- nodes.each do |node| -%>
                <%- if node.fields.any? { |field| [Prism::Template::NodeField, Prism::Template::OptionalNodeField, Prism::Template::NodeListField].include?(field.class) } -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                case <%= node.type %>: {
                    pm_<%= node.human %>_t *cast = (pm_<%= node.human %>_t *) node;
                    <%- node.fields.each do |field| -%>
                    <%- case field -%>
                    <%- when Prism::Template::NodeField, Prism::Template::OptionalNodeField -%>
                    pm_node_stack_push(&node_stack, (pm_node_t *) cast-><%= field.name %>);
                    <%- when Prism::Template::NodeListField -%>
                    for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
                        pm_node_stack_push(&node_stack, (pm_node_t *) cast-><%= field.name %>.nodes[index]);
                    }
                    <%- end -%>
                    <%- end -%>
                    break;
                }
                <%- end -%>
                <%- end -%>
                default:
                    break;
            }
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
        } else {
            const pm_node_t *node = pm_node_stack_pop(&node_stack);

            switch (PM_NODE_TYPE(node)) {
                <%- nodes.each do |node| -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                case <%= node.type %>: {
                    <%- if node.fields.any? { |field| ![Prism::Template::NodeField, Prism::Template::OptionalNodeField, Prism::Template::FlagsField].include?(field.class) } -%>
                    pm_<%= node.human %>_t *cast = (pm_<%= node.human %>_t *) node;
                    <%- end -%>
                    VALUE argv[<%= node.fields.length + 2 %>];

                    // source
                    argv[0] = source;
                    <%- node.fields.each.with_index(1) do |field, index| -%>

                    // <%= field.name %>
                    <%- case field -%>
                    <%- when Prism::Template::NodeField, Prism::Template::OptionalNodeField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = rb_ary_pop(value_stack);
                    <%- when Prism::Template::NodeListField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = rb_ary_new_capa(cast-><%= field.name %>.size);
                    for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
                        rb_ary_push(argv[<%= index %>], rb_ary_pop(value_stack));
                    }
                    <%- when Prism::Template::StringField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = pm_string_new(&cast-><%= field.name %>, encoding);
                    <%- when Prism::Template::ConstantField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    assert(cast-><%= field.name %> != 0);
                    argv[<%= index %>] = RARRAY_AREF(constants, cast-><%= field.name %> - 1);
                    <%- when Prism::Template::OptionalConstantField -%>
                    argv[<%= index %>] = cast-><%= field.name %> == 0 ? Qnil : RARRAY_AREF(constants, cast-><%= field.name %> - 1);
                    <%- when Prism::Template::ConstantListField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = rb_ary_new_capa(cast-><%= field.name %>.size);
                    for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
                        assert(cast-><%= field.name %>.ids[index] != 0);
                        rb_ary_push(argv[<%= index %>], RARRAY_AREF(constants, cast-><%= field.name %>.ids[index] - 1));
                    }
                    <%- when Prism::Template::LocationField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = pm_location_new(parser, cast-><%= field.name %>.start, cast-><%= field.name %>.end);
                    <%- when Prism::Template::OptionalLocationField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = cast-><%= field.name %>.start == NULL ? Qnil : pm_location_new(parser, cast-><%= field.name %>.start, cast-><%= field.name %>.end);
                    <%- when Prism::Template::UInt8Field -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = UINT2NUM(cast-><%= field.name %>);
                    <%- when Prism::Template::UInt32Field -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = ULONG2NUM(cast-><%= field.name %>);
                    <%- when Prism::Template::FlagsField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = ULONG2NUM(node->flags & ~PM_NODE_FLAG_COMMON_MASK);
                    <%- when Prism::Template::IntegerField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = pm_integer_new(&cast-><%= field.name %>);
                    <%- when Prism::Template::DoubleField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
                    argv[<%= index %>] = DBL2NUM(cast-><%= field.name %>);
                    <%- else -%>
                    <%- raise -%>
                    <%- end -%>
                    <%- end -%>

                    // location
                    argv[<%= node.fields.length + 1 %>] = pm_location_new(parser, node->location.start, node->location.end);

                    rb_ary_push(value_stack, rb_class_new_instance(<%= node.fields.length + 2 %>, argv, rb_cPrism<%= node.name %>));
                    break;
                }
                <%- end -%>
                default:
                    rb_raise(rb_eRuntimeError, "unknown node type: %d", PM_NODE_TYPE(node));
            }
        }
    }

    return rb_ary_pop(value_stack);
}

void
Init_prism_api_node(void) {
    <%- nodes.each do |node| -%>
    rb_cPrism<%= node.name %> = rb_define_class_under(rb_cPrism, "<%= node.name %>", rb_cPrismNode);
    <%- end -%>
}