ed-precompiled_prism 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/BSDmakefile +58 -0
- data/CHANGELOG.md +723 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +58 -0
- data/LICENSE.md +7 -0
- data/Makefile +110 -0
- data/README.md +143 -0
- data/config.yml +4714 -0
- data/docs/build_system.md +119 -0
- data/docs/configuration.md +68 -0
- data/docs/cruby_compilation.md +27 -0
- data/docs/design.md +53 -0
- data/docs/encoding.md +121 -0
- data/docs/fuzzing.md +88 -0
- data/docs/heredocs.md +36 -0
- data/docs/javascript.md +118 -0
- data/docs/local_variable_depth.md +229 -0
- data/docs/mapping.md +117 -0
- data/docs/parser_translation.md +24 -0
- data/docs/parsing_rules.md +22 -0
- data/docs/releasing.md +98 -0
- data/docs/relocation.md +34 -0
- data/docs/ripper_translation.md +72 -0
- data/docs/ruby_api.md +44 -0
- data/docs/ruby_parser_translation.md +19 -0
- data/docs/serialization.md +233 -0
- data/docs/testing.md +55 -0
- data/ext/prism/api_node.c +6941 -0
- data/ext/prism/api_pack.c +276 -0
- data/ext/prism/extconf.rb +127 -0
- data/ext/prism/extension.c +1419 -0
- data/ext/prism/extension.h +19 -0
- data/include/prism/ast.h +8220 -0
- data/include/prism/defines.h +260 -0
- data/include/prism/diagnostic.h +456 -0
- data/include/prism/encoding.h +283 -0
- data/include/prism/node.h +129 -0
- data/include/prism/options.h +482 -0
- data/include/prism/pack.h +163 -0
- data/include/prism/parser.h +933 -0
- data/include/prism/prettyprint.h +34 -0
- data/include/prism/regexp.h +43 -0
- data/include/prism/static_literals.h +121 -0
- data/include/prism/util/pm_buffer.h +236 -0
- data/include/prism/util/pm_char.h +204 -0
- data/include/prism/util/pm_constant_pool.h +218 -0
- data/include/prism/util/pm_integer.h +130 -0
- data/include/prism/util/pm_list.h +103 -0
- data/include/prism/util/pm_memchr.h +29 -0
- data/include/prism/util/pm_newline_list.h +113 -0
- data/include/prism/util/pm_string.h +200 -0
- data/include/prism/util/pm_strncasecmp.h +32 -0
- data/include/prism/util/pm_strpbrk.h +46 -0
- data/include/prism/version.h +29 -0
- data/include/prism.h +408 -0
- data/lib/prism/compiler.rb +801 -0
- data/lib/prism/desugar_compiler.rb +392 -0
- data/lib/prism/dispatcher.rb +2210 -0
- data/lib/prism/dot_visitor.rb +4762 -0
- data/lib/prism/dsl.rb +1003 -0
- data/lib/prism/ffi.rb +570 -0
- data/lib/prism/inspect_visitor.rb +2392 -0
- data/lib/prism/lex_compat.rb +928 -0
- data/lib/prism/mutation_compiler.rb +772 -0
- data/lib/prism/node.rb +18816 -0
- data/lib/prism/node_ext.rb +511 -0
- data/lib/prism/pack.rb +230 -0
- data/lib/prism/parse_result/comments.rb +188 -0
- data/lib/prism/parse_result/errors.rb +66 -0
- data/lib/prism/parse_result/newlines.rb +155 -0
- data/lib/prism/parse_result.rb +911 -0
- data/lib/prism/pattern.rb +269 -0
- data/lib/prism/polyfill/append_as_bytes.rb +15 -0
- data/lib/prism/polyfill/byteindex.rb +13 -0
- data/lib/prism/polyfill/scan_byte.rb +14 -0
- data/lib/prism/polyfill/unpack1.rb +14 -0
- data/lib/prism/polyfill/warn.rb +36 -0
- data/lib/prism/reflection.rb +416 -0
- data/lib/prism/relocation.rb +505 -0
- data/lib/prism/serialize.rb +2398 -0
- data/lib/prism/string_query.rb +31 -0
- data/lib/prism/translation/parser/builder.rb +62 -0
- data/lib/prism/translation/parser/compiler.rb +2234 -0
- data/lib/prism/translation/parser/lexer.rb +820 -0
- data/lib/prism/translation/parser.rb +374 -0
- data/lib/prism/translation/parser33.rb +13 -0
- data/lib/prism/translation/parser34.rb +13 -0
- data/lib/prism/translation/parser35.rb +13 -0
- data/lib/prism/translation/parser_current.rb +24 -0
- data/lib/prism/translation/ripper/sexp.rb +126 -0
- data/lib/prism/translation/ripper/shim.rb +5 -0
- data/lib/prism/translation/ripper.rb +3474 -0
- data/lib/prism/translation/ruby_parser.rb +1929 -0
- data/lib/prism/translation.rb +16 -0
- data/lib/prism/visitor.rb +813 -0
- data/lib/prism.rb +97 -0
- data/prism.gemspec +174 -0
- data/rbi/prism/compiler.rbi +12 -0
- data/rbi/prism/dsl.rbi +524 -0
- data/rbi/prism/inspect_visitor.rbi +12 -0
- data/rbi/prism/node.rbi +8734 -0
- data/rbi/prism/node_ext.rbi +107 -0
- data/rbi/prism/parse_result.rbi +404 -0
- data/rbi/prism/reflection.rbi +58 -0
- data/rbi/prism/string_query.rbi +12 -0
- data/rbi/prism/translation/parser.rbi +11 -0
- data/rbi/prism/translation/parser33.rbi +6 -0
- data/rbi/prism/translation/parser34.rbi +6 -0
- data/rbi/prism/translation/parser35.rbi +6 -0
- data/rbi/prism/translation/ripper.rbi +15 -0
- data/rbi/prism/visitor.rbi +473 -0
- data/rbi/prism.rbi +66 -0
- data/sig/prism/compiler.rbs +9 -0
- data/sig/prism/dispatcher.rbs +19 -0
- data/sig/prism/dot_visitor.rbs +6 -0
- data/sig/prism/dsl.rbs +351 -0
- data/sig/prism/inspect_visitor.rbs +22 -0
- data/sig/prism/lex_compat.rbs +10 -0
- data/sig/prism/mutation_compiler.rbs +159 -0
- data/sig/prism/node.rbs +4028 -0
- data/sig/prism/node_ext.rbs +149 -0
- data/sig/prism/pack.rbs +43 -0
- data/sig/prism/parse_result/comments.rbs +38 -0
- data/sig/prism/parse_result.rbs +196 -0
- data/sig/prism/pattern.rbs +13 -0
- data/sig/prism/reflection.rbs +50 -0
- data/sig/prism/relocation.rbs +185 -0
- data/sig/prism/serialize.rbs +8 -0
- data/sig/prism/string_query.rbs +11 -0
- data/sig/prism/visitor.rbs +169 -0
- data/sig/prism.rbs +254 -0
- data/src/diagnostic.c +850 -0
- data/src/encoding.c +5235 -0
- data/src/node.c +8676 -0
- data/src/options.c +328 -0
- data/src/pack.c +509 -0
- data/src/prettyprint.c +8941 -0
- data/src/prism.c +23361 -0
- data/src/regexp.c +790 -0
- data/src/serialize.c +2268 -0
- data/src/static_literals.c +617 -0
- data/src/token_type.c +703 -0
- data/src/util/pm_buffer.c +357 -0
- data/src/util/pm_char.c +318 -0
- data/src/util/pm_constant_pool.c +342 -0
- data/src/util/pm_integer.c +670 -0
- data/src/util/pm_list.c +49 -0
- data/src/util/pm_memchr.c +35 -0
- data/src/util/pm_newline_list.c +125 -0
- data/src/util/pm_string.c +381 -0
- data/src/util/pm_strncasecmp.c +36 -0
- data/src/util/pm_strpbrk.c +206 -0
- metadata +195 -0
|
@@ -0,0 +1,1419 @@
|
|
|
1
|
+
#include "prism/extension.h"
|
|
2
|
+
|
|
3
|
+
#ifdef _WIN32
|
|
4
|
+
#include <ruby/win32.h>
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
// NOTE: this file should contain only bindings. All non-trivial logic should be
|
|
8
|
+
// in libprism so it can be shared its the various callers.
|
|
9
|
+
|
|
10
|
+
VALUE rb_cPrism;
|
|
11
|
+
VALUE rb_cPrismNode;
|
|
12
|
+
VALUE rb_cPrismSource;
|
|
13
|
+
VALUE rb_cPrismToken;
|
|
14
|
+
VALUE rb_cPrismLocation;
|
|
15
|
+
|
|
16
|
+
VALUE rb_cPrismComment;
|
|
17
|
+
VALUE rb_cPrismInlineComment;
|
|
18
|
+
VALUE rb_cPrismEmbDocComment;
|
|
19
|
+
VALUE rb_cPrismMagicComment;
|
|
20
|
+
VALUE rb_cPrismParseError;
|
|
21
|
+
VALUE rb_cPrismParseWarning;
|
|
22
|
+
VALUE rb_cPrismResult;
|
|
23
|
+
VALUE rb_cPrismParseResult;
|
|
24
|
+
VALUE rb_cPrismLexResult;
|
|
25
|
+
VALUE rb_cPrismParseLexResult;
|
|
26
|
+
VALUE rb_cPrismStringQuery;
|
|
27
|
+
VALUE rb_cPrismScope;
|
|
28
|
+
|
|
29
|
+
VALUE rb_cPrismDebugEncoding;
|
|
30
|
+
|
|
31
|
+
ID rb_id_option_command_line;
|
|
32
|
+
ID rb_id_option_encoding;
|
|
33
|
+
ID rb_id_option_filepath;
|
|
34
|
+
ID rb_id_option_freeze;
|
|
35
|
+
ID rb_id_option_frozen_string_literal;
|
|
36
|
+
ID rb_id_option_line;
|
|
37
|
+
ID rb_id_option_main_script;
|
|
38
|
+
ID rb_id_option_partial_script;
|
|
39
|
+
ID rb_id_option_scopes;
|
|
40
|
+
ID rb_id_option_version;
|
|
41
|
+
ID rb_id_source_for;
|
|
42
|
+
ID rb_id_forwarding_positionals;
|
|
43
|
+
ID rb_id_forwarding_keywords;
|
|
44
|
+
ID rb_id_forwarding_block;
|
|
45
|
+
ID rb_id_forwarding_all;
|
|
46
|
+
|
|
47
|
+
/******************************************************************************/
|
|
48
|
+
/* IO of Ruby code */
|
|
49
|
+
/******************************************************************************/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Check if the given VALUE is a string. If it's not a string, then raise a
|
|
53
|
+
* TypeError. Otherwise return the VALUE as a C string.
|
|
54
|
+
*/
|
|
55
|
+
static const char *
|
|
56
|
+
check_string(VALUE value) {
|
|
57
|
+
// Check if the value is a string. If it's not, then raise a type error.
|
|
58
|
+
if (!RB_TYPE_P(value, T_STRING)) {
|
|
59
|
+
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(value));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Otherwise, return the value as a C string.
|
|
63
|
+
return RSTRING_PTR(value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Load the contents and size of the given string into the given pm_string_t.
|
|
68
|
+
*/
|
|
69
|
+
static void
|
|
70
|
+
input_load_string(pm_string_t *input, VALUE string) {
|
|
71
|
+
// Check if the string is a string. If it's not, then raise a type error.
|
|
72
|
+
if (!RB_TYPE_P(string, T_STRING)) {
|
|
73
|
+
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(string));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
pm_string_constant_init(input, RSTRING_PTR(string), RSTRING_LEN(string));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/******************************************************************************/
|
|
80
|
+
/* Building C options from Ruby options */
|
|
81
|
+
/******************************************************************************/
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Build the scopes associated with the provided Ruby keyword value.
|
|
85
|
+
*/
|
|
86
|
+
static void
|
|
87
|
+
build_options_scopes(pm_options_t *options, VALUE scopes) {
|
|
88
|
+
// Check if the value is an array. If it's not, then raise a type error.
|
|
89
|
+
if (!RB_TYPE_P(scopes, T_ARRAY)) {
|
|
90
|
+
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(scopes));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Initialize the scopes array.
|
|
94
|
+
size_t scopes_count = RARRAY_LEN(scopes);
|
|
95
|
+
if (!pm_options_scopes_init(options, scopes_count)) {
|
|
96
|
+
rb_raise(rb_eNoMemError, "failed to allocate memory");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Iterate over the scopes and add them to the options.
|
|
100
|
+
for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
|
|
101
|
+
VALUE scope = rb_ary_entry(scopes, scope_index);
|
|
102
|
+
|
|
103
|
+
// The scope can be either an array or it can be a Prism::Scope object.
|
|
104
|
+
// Parse out the correct values here from either.
|
|
105
|
+
VALUE locals;
|
|
106
|
+
uint8_t forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
|
|
107
|
+
|
|
108
|
+
if (RB_TYPE_P(scope, T_ARRAY)) {
|
|
109
|
+
locals = scope;
|
|
110
|
+
} else if (rb_obj_is_kind_of(scope, rb_cPrismScope)) {
|
|
111
|
+
locals = rb_ivar_get(scope, rb_intern("@locals"));
|
|
112
|
+
if (!RB_TYPE_P(locals, T_ARRAY)) {
|
|
113
|
+
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(locals));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
VALUE names = rb_ivar_get(scope, rb_intern("@forwarding"));
|
|
117
|
+
if (!RB_TYPE_P(names, T_ARRAY)) {
|
|
118
|
+
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array)", rb_obj_class(names));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
size_t names_count = RARRAY_LEN(names);
|
|
122
|
+
for (size_t name_index = 0; name_index < names_count; name_index++) {
|
|
123
|
+
VALUE name = rb_ary_entry(names, name_index);
|
|
124
|
+
|
|
125
|
+
// Check that the name is a symbol. If it's not, then raise
|
|
126
|
+
// a type error.
|
|
127
|
+
if (!RB_TYPE_P(name, T_SYMBOL)) {
|
|
128
|
+
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(name));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
ID id = SYM2ID(name);
|
|
132
|
+
if (id == rb_id_forwarding_positionals) {
|
|
133
|
+
forwarding |= PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS;
|
|
134
|
+
} else if (id == rb_id_forwarding_keywords) {
|
|
135
|
+
forwarding |= PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS;
|
|
136
|
+
} else if (id == rb_id_forwarding_block) {
|
|
137
|
+
forwarding |= PM_OPTIONS_SCOPE_FORWARDING_BLOCK;
|
|
138
|
+
} else if (id == rb_id_forwarding_all) {
|
|
139
|
+
forwarding |= PM_OPTIONS_SCOPE_FORWARDING_ALL;
|
|
140
|
+
} else {
|
|
141
|
+
rb_raise(rb_eArgError, "invalid forwarding value: %" PRIsVALUE, name);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Array or Prism::Scope)", rb_obj_class(scope));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Initialize the scope array.
|
|
149
|
+
size_t locals_count = RARRAY_LEN(locals);
|
|
150
|
+
pm_options_scope_t *options_scope = &options->scopes[scope_index];
|
|
151
|
+
if (!pm_options_scope_init(options_scope, locals_count)) {
|
|
152
|
+
rb_raise(rb_eNoMemError, "failed to allocate memory");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Iterate over the locals and add them to the scope.
|
|
156
|
+
for (size_t local_index = 0; local_index < locals_count; local_index++) {
|
|
157
|
+
VALUE local = rb_ary_entry(locals, local_index);
|
|
158
|
+
|
|
159
|
+
// Check that the local is a symbol. If it's not, then raise a
|
|
160
|
+
// type error.
|
|
161
|
+
if (!RB_TYPE_P(local, T_SYMBOL)) {
|
|
162
|
+
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Symbol)", rb_obj_class(local));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Add the local to the scope.
|
|
166
|
+
pm_string_t *scope_local = &options_scope->locals[local_index];
|
|
167
|
+
const char *name = rb_id2name(SYM2ID(local));
|
|
168
|
+
pm_string_constant_init(scope_local, name, strlen(name));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Now set the forwarding options.
|
|
172
|
+
pm_options_scope_forwarding_set(options_scope, forwarding);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* An iterator function that is called for each key-value in the keywords hash.
|
|
178
|
+
*/
|
|
179
|
+
static int
|
|
180
|
+
build_options_i(VALUE key, VALUE value, VALUE argument) {
|
|
181
|
+
pm_options_t *options = (pm_options_t *) argument;
|
|
182
|
+
ID key_id = SYM2ID(key);
|
|
183
|
+
|
|
184
|
+
if (key_id == rb_id_option_filepath) {
|
|
185
|
+
if (!NIL_P(value)) pm_options_filepath_set(options, check_string(value));
|
|
186
|
+
} else if (key_id == rb_id_option_encoding) {
|
|
187
|
+
if (!NIL_P(value)) {
|
|
188
|
+
if (value == Qfalse) {
|
|
189
|
+
pm_options_encoding_locked_set(options, true);
|
|
190
|
+
} else {
|
|
191
|
+
pm_options_encoding_set(options, rb_enc_name(rb_to_encoding(value)));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
} else if (key_id == rb_id_option_line) {
|
|
195
|
+
if (!NIL_P(value)) pm_options_line_set(options, NUM2INT(value));
|
|
196
|
+
} else if (key_id == rb_id_option_frozen_string_literal) {
|
|
197
|
+
if (!NIL_P(value)) pm_options_frozen_string_literal_set(options, RTEST(value));
|
|
198
|
+
} else if (key_id == rb_id_option_version) {
|
|
199
|
+
if (!NIL_P(value)) {
|
|
200
|
+
const char *version = check_string(value);
|
|
201
|
+
|
|
202
|
+
if (!pm_options_version_set(options, version, RSTRING_LEN(value))) {
|
|
203
|
+
rb_raise(rb_eArgError, "invalid version: %" PRIsVALUE, value);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
} else if (key_id == rb_id_option_scopes) {
|
|
207
|
+
if (!NIL_P(value)) build_options_scopes(options, value);
|
|
208
|
+
} else if (key_id == rb_id_option_command_line) {
|
|
209
|
+
if (!NIL_P(value)) {
|
|
210
|
+
const char *string = check_string(value);
|
|
211
|
+
uint8_t command_line = 0;
|
|
212
|
+
|
|
213
|
+
for (size_t index = 0; index < strlen(string); index++) {
|
|
214
|
+
switch (string[index]) {
|
|
215
|
+
case 'a': command_line |= PM_OPTIONS_COMMAND_LINE_A; break;
|
|
216
|
+
case 'e': command_line |= PM_OPTIONS_COMMAND_LINE_E; break;
|
|
217
|
+
case 'l': command_line |= PM_OPTIONS_COMMAND_LINE_L; break;
|
|
218
|
+
case 'n': command_line |= PM_OPTIONS_COMMAND_LINE_N; break;
|
|
219
|
+
case 'p': command_line |= PM_OPTIONS_COMMAND_LINE_P; break;
|
|
220
|
+
case 'x': command_line |= PM_OPTIONS_COMMAND_LINE_X; break;
|
|
221
|
+
default: rb_raise(rb_eArgError, "invalid command line flag: '%c'", string[index]); break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
pm_options_command_line_set(options, command_line);
|
|
226
|
+
}
|
|
227
|
+
} else if (key_id == rb_id_option_main_script) {
|
|
228
|
+
if (!NIL_P(value)) pm_options_main_script_set(options, RTEST(value));
|
|
229
|
+
} else if (key_id == rb_id_option_partial_script) {
|
|
230
|
+
if (!NIL_P(value)) pm_options_partial_script_set(options, RTEST(value));
|
|
231
|
+
} else if (key_id == rb_id_option_freeze) {
|
|
232
|
+
if (!NIL_P(value)) pm_options_freeze_set(options, RTEST(value));
|
|
233
|
+
} else {
|
|
234
|
+
rb_raise(rb_eArgError, "unknown keyword: %" PRIsVALUE, key);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return ST_CONTINUE;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* We need a struct here to pass through rb_protect and it has to be a single
|
|
242
|
+
* value. Because the sizeof(VALUE) == sizeof(void *), we're going to pass this
|
|
243
|
+
* through as an opaque pointer and cast it on both sides.
|
|
244
|
+
*/
|
|
245
|
+
struct build_options_data {
|
|
246
|
+
pm_options_t *options;
|
|
247
|
+
VALUE keywords;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Build the set of options from the given keywords. Note that this can raise a
|
|
252
|
+
* Ruby error if the options are not valid.
|
|
253
|
+
*/
|
|
254
|
+
static VALUE
|
|
255
|
+
build_options(VALUE argument) {
|
|
256
|
+
struct build_options_data *data = (struct build_options_data *) argument;
|
|
257
|
+
rb_hash_foreach(data->keywords, build_options_i, (VALUE) data->options);
|
|
258
|
+
return Qnil;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Extract the options from the given keyword arguments.
|
|
263
|
+
*/
|
|
264
|
+
static void
|
|
265
|
+
extract_options(pm_options_t *options, VALUE filepath, VALUE keywords) {
|
|
266
|
+
options->line = 1; // default
|
|
267
|
+
|
|
268
|
+
if (!NIL_P(keywords)) {
|
|
269
|
+
struct build_options_data data = { .options = options, .keywords = keywords };
|
|
270
|
+
struct build_options_data *argument = &data;
|
|
271
|
+
|
|
272
|
+
int state = 0;
|
|
273
|
+
rb_protect(build_options, (VALUE) argument, &state);
|
|
274
|
+
|
|
275
|
+
if (state != 0) {
|
|
276
|
+
pm_options_free(options);
|
|
277
|
+
rb_jump_tag(state);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (!NIL_P(filepath)) {
|
|
282
|
+
if (!RB_TYPE_P(filepath, T_STRING)) {
|
|
283
|
+
pm_options_free(options);
|
|
284
|
+
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected String)", rb_obj_class(filepath));
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
pm_options_filepath_set(options, RSTRING_PTR(filepath));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Read options for methods that look like (source, **options).
|
|
293
|
+
*/
|
|
294
|
+
static void
|
|
295
|
+
string_options(int argc, VALUE *argv, pm_string_t *input, pm_options_t *options) {
|
|
296
|
+
VALUE string;
|
|
297
|
+
VALUE keywords;
|
|
298
|
+
rb_scan_args(argc, argv, "1:", &string, &keywords);
|
|
299
|
+
|
|
300
|
+
extract_options(options, Qnil, keywords);
|
|
301
|
+
input_load_string(input, string);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Read options for methods that look like (filepath, **options).
|
|
306
|
+
*/
|
|
307
|
+
static void
|
|
308
|
+
file_options(int argc, VALUE *argv, pm_string_t *input, pm_options_t *options, VALUE *encoded_filepath) {
|
|
309
|
+
VALUE filepath;
|
|
310
|
+
VALUE keywords;
|
|
311
|
+
rb_scan_args(argc, argv, "1:", &filepath, &keywords);
|
|
312
|
+
|
|
313
|
+
Check_Type(filepath, T_STRING);
|
|
314
|
+
*encoded_filepath = rb_str_encode_ospath(filepath);
|
|
315
|
+
extract_options(options, *encoded_filepath, keywords);
|
|
316
|
+
|
|
317
|
+
const char *source = (const char *) pm_string_source(&options->filepath);
|
|
318
|
+
pm_string_init_result_t result;
|
|
319
|
+
|
|
320
|
+
switch (result = pm_string_file_init(input, source)) {
|
|
321
|
+
case PM_STRING_INIT_SUCCESS:
|
|
322
|
+
break;
|
|
323
|
+
case PM_STRING_INIT_ERROR_GENERIC: {
|
|
324
|
+
pm_options_free(options);
|
|
325
|
+
|
|
326
|
+
#ifdef _WIN32
|
|
327
|
+
int e = rb_w32_map_errno(GetLastError());
|
|
328
|
+
#else
|
|
329
|
+
int e = errno;
|
|
330
|
+
#endif
|
|
331
|
+
|
|
332
|
+
rb_syserr_fail(e, source);
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
case PM_STRING_INIT_ERROR_DIRECTORY:
|
|
336
|
+
pm_options_free(options);
|
|
337
|
+
rb_syserr_fail(EISDIR, source);
|
|
338
|
+
break;
|
|
339
|
+
default:
|
|
340
|
+
pm_options_free(options);
|
|
341
|
+
rb_raise(rb_eRuntimeError, "Unknown error (%d) initializing file: %s", result, source);
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
#ifndef PRISM_EXCLUDE_SERIALIZATION
|
|
347
|
+
|
|
348
|
+
/******************************************************************************/
|
|
349
|
+
/* Serializing the AST */
|
|
350
|
+
/******************************************************************************/
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Dump the AST corresponding to the given input to a string.
|
|
354
|
+
*/
|
|
355
|
+
static VALUE
|
|
356
|
+
dump_input(pm_string_t *input, const pm_options_t *options) {
|
|
357
|
+
pm_buffer_t buffer;
|
|
358
|
+
if (!pm_buffer_init(&buffer)) {
|
|
359
|
+
rb_raise(rb_eNoMemError, "failed to allocate memory");
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
pm_parser_t parser;
|
|
363
|
+
pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
|
|
364
|
+
|
|
365
|
+
pm_node_t *node = pm_parse(&parser);
|
|
366
|
+
pm_serialize(&parser, node, &buffer);
|
|
367
|
+
|
|
368
|
+
VALUE result = rb_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer));
|
|
369
|
+
pm_node_destroy(&parser, node);
|
|
370
|
+
pm_buffer_free(&buffer);
|
|
371
|
+
pm_parser_free(&parser);
|
|
372
|
+
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* call-seq:
|
|
378
|
+
* Prism::dump(source, **options) -> String
|
|
379
|
+
*
|
|
380
|
+
* Dump the AST corresponding to the given string to a string. For supported
|
|
381
|
+
* options, see Prism::parse.
|
|
382
|
+
*/
|
|
383
|
+
static VALUE
|
|
384
|
+
dump(int argc, VALUE *argv, VALUE self) {
|
|
385
|
+
pm_string_t input;
|
|
386
|
+
pm_options_t options = { 0 };
|
|
387
|
+
string_options(argc, argv, &input, &options);
|
|
388
|
+
|
|
389
|
+
#ifdef PRISM_BUILD_DEBUG
|
|
390
|
+
size_t length = pm_string_length(&input);
|
|
391
|
+
char* dup = xmalloc(length);
|
|
392
|
+
memcpy(dup, pm_string_source(&input), length);
|
|
393
|
+
pm_string_constant_init(&input, dup, length);
|
|
394
|
+
#endif
|
|
395
|
+
|
|
396
|
+
VALUE value = dump_input(&input, &options);
|
|
397
|
+
if (options.freeze) rb_obj_freeze(value);
|
|
398
|
+
|
|
399
|
+
#ifdef PRISM_BUILD_DEBUG
|
|
400
|
+
xfree(dup);
|
|
401
|
+
#endif
|
|
402
|
+
|
|
403
|
+
pm_string_free(&input);
|
|
404
|
+
pm_options_free(&options);
|
|
405
|
+
|
|
406
|
+
return value;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* call-seq:
|
|
411
|
+
* Prism::dump_file(filepath, **options) -> String
|
|
412
|
+
*
|
|
413
|
+
* Dump the AST corresponding to the given file to a string. For supported
|
|
414
|
+
* options, see Prism::parse.
|
|
415
|
+
*/
|
|
416
|
+
static VALUE
|
|
417
|
+
dump_file(int argc, VALUE *argv, VALUE self) {
|
|
418
|
+
pm_string_t input;
|
|
419
|
+
pm_options_t options = { 0 };
|
|
420
|
+
|
|
421
|
+
VALUE encoded_filepath;
|
|
422
|
+
file_options(argc, argv, &input, &options, &encoded_filepath);
|
|
423
|
+
|
|
424
|
+
VALUE value = dump_input(&input, &options);
|
|
425
|
+
pm_string_free(&input);
|
|
426
|
+
pm_options_free(&options);
|
|
427
|
+
|
|
428
|
+
return value;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
#endif
|
|
432
|
+
|
|
433
|
+
/******************************************************************************/
|
|
434
|
+
/* Extracting values for the parse result */
|
|
435
|
+
/******************************************************************************/
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* The same as rb_class_new_instance, but accepts an additional boolean to
|
|
439
|
+
* indicate whether or not the resulting class instance should be frozen.
|
|
440
|
+
*/
|
|
441
|
+
static inline VALUE
|
|
442
|
+
rb_class_new_instance_freeze(int argc, const VALUE *argv, VALUE klass, bool freeze) {
|
|
443
|
+
VALUE value = rb_class_new_instance(argc, argv, klass);
|
|
444
|
+
if (freeze) rb_obj_freeze(value);
|
|
445
|
+
return value;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Create a new Location instance from the given parser and bounds.
|
|
450
|
+
*/
|
|
451
|
+
static inline VALUE
|
|
452
|
+
parser_location(const pm_parser_t *parser, VALUE source, bool freeze, const uint8_t *start, size_t length) {
|
|
453
|
+
VALUE argv[] = { source, LONG2FIX(start - parser->start), LONG2FIX(length) };
|
|
454
|
+
return rb_class_new_instance_freeze(3, argv, rb_cPrismLocation, freeze);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Create a new Location instance from the given parser and location.
|
|
459
|
+
*/
|
|
460
|
+
#define PARSER_LOCATION_LOC(parser, source, freeze, loc) \
|
|
461
|
+
parser_location(parser, source, freeze, loc.start, (size_t) (loc.end - loc.start))
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Build a new Comment instance from the given parser and comment.
|
|
465
|
+
*/
|
|
466
|
+
static inline VALUE
|
|
467
|
+
parser_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_comment_t *comment) {
|
|
468
|
+
VALUE argv[] = { PARSER_LOCATION_LOC(parser, source, freeze, comment->location) };
|
|
469
|
+
VALUE type = (comment->type == PM_COMMENT_EMBDOC) ? rb_cPrismEmbDocComment : rb_cPrismInlineComment;
|
|
470
|
+
return rb_class_new_instance_freeze(1, argv, type, freeze);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Extract the comments out of the parser into an array.
|
|
475
|
+
*/
|
|
476
|
+
static VALUE
|
|
477
|
+
parser_comments(const pm_parser_t *parser, VALUE source, bool freeze) {
|
|
478
|
+
VALUE comments = rb_ary_new_capa(parser->comment_list.size);
|
|
479
|
+
|
|
480
|
+
for (
|
|
481
|
+
const pm_comment_t *comment = (const pm_comment_t *) parser->comment_list.head;
|
|
482
|
+
comment != NULL;
|
|
483
|
+
comment = (const pm_comment_t *) comment->node.next
|
|
484
|
+
) {
|
|
485
|
+
VALUE value = parser_comment(parser, source, freeze, comment);
|
|
486
|
+
rb_ary_push(comments, value);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
if (freeze) rb_obj_freeze(comments);
|
|
490
|
+
return comments;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Build a new MagicComment instance from the given parser and magic comment.
|
|
495
|
+
*/
|
|
496
|
+
static inline VALUE
|
|
497
|
+
parser_magic_comment(const pm_parser_t *parser, VALUE source, bool freeze, const pm_magic_comment_t *magic_comment) {
|
|
498
|
+
VALUE key_loc = parser_location(parser, source, freeze, magic_comment->key_start, magic_comment->key_length);
|
|
499
|
+
VALUE value_loc = parser_location(parser, source, freeze, magic_comment->value_start, magic_comment->value_length);
|
|
500
|
+
VALUE argv[] = { key_loc, value_loc };
|
|
501
|
+
return rb_class_new_instance_freeze(2, argv, rb_cPrismMagicComment, freeze);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Extract the magic comments out of the parser into an array.
|
|
506
|
+
*/
|
|
507
|
+
static VALUE
|
|
508
|
+
parser_magic_comments(const pm_parser_t *parser, VALUE source, bool freeze) {
|
|
509
|
+
VALUE magic_comments = rb_ary_new_capa(parser->magic_comment_list.size);
|
|
510
|
+
|
|
511
|
+
for (
|
|
512
|
+
const pm_magic_comment_t *magic_comment = (const pm_magic_comment_t *) parser->magic_comment_list.head;
|
|
513
|
+
magic_comment != NULL;
|
|
514
|
+
magic_comment = (const pm_magic_comment_t *) magic_comment->node.next
|
|
515
|
+
) {
|
|
516
|
+
VALUE value = parser_magic_comment(parser, source, freeze, magic_comment);
|
|
517
|
+
rb_ary_push(magic_comments, value);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (freeze) rb_obj_freeze(magic_comments);
|
|
521
|
+
return magic_comments;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Extract out the data location from the parser into a Location instance if one
|
|
526
|
+
* exists.
|
|
527
|
+
*/
|
|
528
|
+
static VALUE
|
|
529
|
+
parser_data_loc(const pm_parser_t *parser, VALUE source, bool freeze) {
|
|
530
|
+
if (parser->data_loc.end == NULL) {
|
|
531
|
+
return Qnil;
|
|
532
|
+
} else {
|
|
533
|
+
return PARSER_LOCATION_LOC(parser, source, freeze, parser->data_loc);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Extract the errors out of the parser into an array.
|
|
539
|
+
*/
|
|
540
|
+
static VALUE
|
|
541
|
+
parser_errors(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
|
|
542
|
+
VALUE errors = rb_ary_new_capa(parser->error_list.size);
|
|
543
|
+
|
|
544
|
+
for (
|
|
545
|
+
const pm_diagnostic_t *error = (const pm_diagnostic_t *) parser->error_list.head;
|
|
546
|
+
error != NULL;
|
|
547
|
+
error = (const pm_diagnostic_t *) error->node.next
|
|
548
|
+
) {
|
|
549
|
+
VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(error->diag_id)));
|
|
550
|
+
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(error->message, encoding));
|
|
551
|
+
VALUE location = PARSER_LOCATION_LOC(parser, source, freeze, error->location);
|
|
552
|
+
|
|
553
|
+
VALUE level = Qnil;
|
|
554
|
+
switch (error->level) {
|
|
555
|
+
case PM_ERROR_LEVEL_SYNTAX:
|
|
556
|
+
level = ID2SYM(rb_intern("syntax"));
|
|
557
|
+
break;
|
|
558
|
+
case PM_ERROR_LEVEL_ARGUMENT:
|
|
559
|
+
level = ID2SYM(rb_intern("argument"));
|
|
560
|
+
break;
|
|
561
|
+
case PM_ERROR_LEVEL_LOAD:
|
|
562
|
+
level = ID2SYM(rb_intern("load"));
|
|
563
|
+
break;
|
|
564
|
+
default:
|
|
565
|
+
rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, error->level);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
VALUE argv[] = { type, message, location, level };
|
|
569
|
+
VALUE value = rb_class_new_instance_freeze(4, argv, rb_cPrismParseError, freeze);
|
|
570
|
+
rb_ary_push(errors, value);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (freeze) rb_obj_freeze(errors);
|
|
574
|
+
return errors;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Extract the warnings out of the parser into an array.
|
|
579
|
+
*/
|
|
580
|
+
static VALUE
|
|
581
|
+
parser_warnings(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
|
|
582
|
+
VALUE warnings = rb_ary_new_capa(parser->warning_list.size);
|
|
583
|
+
|
|
584
|
+
for (
|
|
585
|
+
const pm_diagnostic_t *warning = (const pm_diagnostic_t *) parser->warning_list.head;
|
|
586
|
+
warning != NULL;
|
|
587
|
+
warning = (const pm_diagnostic_t *) warning->node.next
|
|
588
|
+
) {
|
|
589
|
+
VALUE type = ID2SYM(rb_intern(pm_diagnostic_id_human(warning->diag_id)));
|
|
590
|
+
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(warning->message, encoding));
|
|
591
|
+
VALUE location = PARSER_LOCATION_LOC(parser, source, freeze, warning->location);
|
|
592
|
+
|
|
593
|
+
VALUE level = Qnil;
|
|
594
|
+
switch (warning->level) {
|
|
595
|
+
case PM_WARNING_LEVEL_DEFAULT:
|
|
596
|
+
level = ID2SYM(rb_intern("default"));
|
|
597
|
+
break;
|
|
598
|
+
case PM_WARNING_LEVEL_VERBOSE:
|
|
599
|
+
level = ID2SYM(rb_intern("verbose"));
|
|
600
|
+
break;
|
|
601
|
+
default:
|
|
602
|
+
rb_raise(rb_eRuntimeError, "Unknown level: %" PRIu8, warning->level);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
VALUE argv[] = { type, message, location, level };
|
|
606
|
+
VALUE value = rb_class_new_instance_freeze(4, argv, rb_cPrismParseWarning, freeze);
|
|
607
|
+
rb_ary_push(warnings, value);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (freeze) rb_obj_freeze(warnings);
|
|
611
|
+
return warnings;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* Create a new parse result from the given parser, value, encoding, and source.
|
|
616
|
+
*/
|
|
617
|
+
static VALUE
|
|
618
|
+
parse_result_create(VALUE class, const pm_parser_t *parser, VALUE value, rb_encoding *encoding, VALUE source, bool freeze) {
|
|
619
|
+
VALUE result_argv[] = {
|
|
620
|
+
value,
|
|
621
|
+
parser_comments(parser, source, freeze),
|
|
622
|
+
parser_magic_comments(parser, source, freeze),
|
|
623
|
+
parser_data_loc(parser, source, freeze),
|
|
624
|
+
parser_errors(parser, encoding, source, freeze),
|
|
625
|
+
parser_warnings(parser, encoding, source, freeze),
|
|
626
|
+
source
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
return rb_class_new_instance_freeze(7, result_argv, class, freeze);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/******************************************************************************/
|
|
633
|
+
/* Lexing Ruby code */
|
|
634
|
+
/******************************************************************************/
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* This struct gets stored in the parser and passed in to the lex callback any
|
|
638
|
+
* time a new token is found. We use it to store the necessary information to
|
|
639
|
+
* initialize a Token instance.
|
|
640
|
+
*/
|
|
641
|
+
typedef struct {
|
|
642
|
+
VALUE source;
|
|
643
|
+
VALUE tokens;
|
|
644
|
+
rb_encoding *encoding;
|
|
645
|
+
bool freeze;
|
|
646
|
+
} parse_lex_data_t;
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* This is passed as a callback to the parser. It gets called every time a new
|
|
650
|
+
* token is found. Once found, we initialize a new instance of Token and push it
|
|
651
|
+
* onto the tokens array.
|
|
652
|
+
*/
|
|
653
|
+
static void
|
|
654
|
+
parse_lex_token(void *data, pm_parser_t *parser, pm_token_t *token) {
|
|
655
|
+
parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) parser->lex_callback->data;
|
|
656
|
+
|
|
657
|
+
VALUE value = pm_token_new(parser, token, parse_lex_data->encoding, parse_lex_data->source, parse_lex_data->freeze);
|
|
658
|
+
VALUE yields = rb_assoc_new(value, INT2FIX(parser->lex_state));
|
|
659
|
+
|
|
660
|
+
if (parse_lex_data->freeze) {
|
|
661
|
+
rb_obj_freeze(value);
|
|
662
|
+
rb_obj_freeze(yields);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
rb_ary_push(parse_lex_data->tokens, yields);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* This is called whenever the encoding changes based on the magic comment at
|
|
670
|
+
* the top of the file. We use it to update the encoding that we are using to
|
|
671
|
+
* create tokens.
|
|
672
|
+
*/
|
|
673
|
+
static void
|
|
674
|
+
parse_lex_encoding_changed_callback(pm_parser_t *parser) {
|
|
675
|
+
parse_lex_data_t *parse_lex_data = (parse_lex_data_t *) parser->lex_callback->data;
|
|
676
|
+
parse_lex_data->encoding = rb_enc_find(parser->encoding->name);
|
|
677
|
+
|
|
678
|
+
// Since the encoding changed, we need to go back and change the encoding of
|
|
679
|
+
// the tokens that were already lexed. This is only going to end up being
|
|
680
|
+
// one or two tokens, since the encoding can only change at the top of the
|
|
681
|
+
// file.
|
|
682
|
+
VALUE tokens = parse_lex_data->tokens;
|
|
683
|
+
VALUE next_tokens = rb_ary_new();
|
|
684
|
+
|
|
685
|
+
for (long index = 0; index < RARRAY_LEN(tokens); index++) {
|
|
686
|
+
VALUE yields = rb_ary_entry(tokens, index);
|
|
687
|
+
VALUE token = rb_ary_entry(yields, 0);
|
|
688
|
+
|
|
689
|
+
VALUE value = rb_ivar_get(token, rb_intern("@value"));
|
|
690
|
+
VALUE next_value = rb_str_dup(value);
|
|
691
|
+
|
|
692
|
+
rb_enc_associate(next_value, parse_lex_data->encoding);
|
|
693
|
+
if (parse_lex_data->freeze) rb_obj_freeze(next_value);
|
|
694
|
+
|
|
695
|
+
VALUE next_token_argv[] = {
|
|
696
|
+
parse_lex_data->source,
|
|
697
|
+
rb_ivar_get(token, rb_intern("@type")),
|
|
698
|
+
next_value,
|
|
699
|
+
rb_ivar_get(token, rb_intern("@location"))
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
VALUE next_token = rb_class_new_instance(4, next_token_argv, rb_cPrismToken);
|
|
703
|
+
VALUE next_yields = rb_assoc_new(next_token, rb_ary_entry(yields, 1));
|
|
704
|
+
|
|
705
|
+
if (parse_lex_data->freeze) {
|
|
706
|
+
rb_obj_freeze(next_token);
|
|
707
|
+
rb_obj_freeze(next_yields);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
rb_ary_push(next_tokens, next_yields);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
rb_ary_replace(parse_lex_data->tokens, next_tokens);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Parse the given input and return a ParseResult containing just the tokens or
|
|
718
|
+
* the nodes and tokens.
|
|
719
|
+
*/
|
|
720
|
+
static VALUE
|
|
721
|
+
parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nodes) {
|
|
722
|
+
pm_parser_t parser;
|
|
723
|
+
pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
|
|
724
|
+
pm_parser_register_encoding_changed_callback(&parser, parse_lex_encoding_changed_callback);
|
|
725
|
+
|
|
726
|
+
VALUE source_string = rb_str_new((const char *) pm_string_source(input), pm_string_length(input));
|
|
727
|
+
VALUE offsets = rb_ary_new_capa(parser.newline_list.size);
|
|
728
|
+
VALUE source = rb_funcall(rb_cPrismSource, rb_id_source_for, 3, source_string, LONG2NUM(parser.start_line), offsets);
|
|
729
|
+
|
|
730
|
+
parse_lex_data_t parse_lex_data = {
|
|
731
|
+
.source = source,
|
|
732
|
+
.tokens = rb_ary_new(),
|
|
733
|
+
.encoding = rb_utf8_encoding(),
|
|
734
|
+
.freeze = options->freeze,
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
parse_lex_data_t *data = &parse_lex_data;
|
|
738
|
+
pm_lex_callback_t lex_callback = (pm_lex_callback_t) {
|
|
739
|
+
.data = (void *) data,
|
|
740
|
+
.callback = parse_lex_token,
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
parser.lex_callback = &lex_callback;
|
|
744
|
+
pm_node_t *node = pm_parse(&parser);
|
|
745
|
+
|
|
746
|
+
// Here we need to update the Source object to have the correct
|
|
747
|
+
// encoding for the source string and the correct newline offsets.
|
|
748
|
+
// We do it here because we've already created the Source object and given
|
|
749
|
+
// it over to all of the tokens, and both of these are only set after pm_parse().
|
|
750
|
+
rb_encoding *encoding = rb_enc_find(parser.encoding->name);
|
|
751
|
+
rb_enc_associate(source_string, encoding);
|
|
752
|
+
|
|
753
|
+
for (size_t index = 0; index < parser.newline_list.size; index++) {
|
|
754
|
+
rb_ary_push(offsets, ULONG2NUM(parser.newline_list.offsets[index]));
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
if (options->freeze) {
|
|
758
|
+
rb_obj_freeze(source_string);
|
|
759
|
+
rb_obj_freeze(offsets);
|
|
760
|
+
rb_obj_freeze(source);
|
|
761
|
+
rb_obj_freeze(parse_lex_data.tokens);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
VALUE result;
|
|
765
|
+
if (return_nodes) {
|
|
766
|
+
VALUE value = rb_ary_new_capa(2);
|
|
767
|
+
rb_ary_push(value, pm_ast_new(&parser, node, parse_lex_data.encoding, source, options->freeze));
|
|
768
|
+
rb_ary_push(value, parse_lex_data.tokens);
|
|
769
|
+
if (options->freeze) rb_obj_freeze(value);
|
|
770
|
+
result = parse_result_create(rb_cPrismParseLexResult, &parser, value, parse_lex_data.encoding, source, options->freeze);
|
|
771
|
+
} else {
|
|
772
|
+
result = parse_result_create(rb_cPrismLexResult, &parser, parse_lex_data.tokens, parse_lex_data.encoding, source, options->freeze);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
pm_node_destroy(&parser, node);
|
|
776
|
+
pm_parser_free(&parser);
|
|
777
|
+
|
|
778
|
+
return result;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* call-seq:
|
|
783
|
+
* Prism::lex(source, **options) -> LexResult
|
|
784
|
+
*
|
|
785
|
+
* Return a LexResult instance that contains an array of Token instances
|
|
786
|
+
* corresponding to the given string. For supported options, see Prism::parse.
|
|
787
|
+
*/
|
|
788
|
+
static VALUE
|
|
789
|
+
lex(int argc, VALUE *argv, VALUE self) {
|
|
790
|
+
pm_string_t input;
|
|
791
|
+
pm_options_t options = { 0 };
|
|
792
|
+
string_options(argc, argv, &input, &options);
|
|
793
|
+
|
|
794
|
+
VALUE result = parse_lex_input(&input, &options, false);
|
|
795
|
+
pm_string_free(&input);
|
|
796
|
+
pm_options_free(&options);
|
|
797
|
+
|
|
798
|
+
return result;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* call-seq:
|
|
803
|
+
* Prism::lex_file(filepath, **options) -> LexResult
|
|
804
|
+
*
|
|
805
|
+
* Return a LexResult instance that contains an array of Token instances
|
|
806
|
+
* corresponding to the given file. For supported options, see Prism::parse.
|
|
807
|
+
*/
|
|
808
|
+
static VALUE
|
|
809
|
+
lex_file(int argc, VALUE *argv, VALUE self) {
|
|
810
|
+
pm_string_t input;
|
|
811
|
+
pm_options_t options = { 0 };
|
|
812
|
+
|
|
813
|
+
VALUE encoded_filepath;
|
|
814
|
+
file_options(argc, argv, &input, &options, &encoded_filepath);
|
|
815
|
+
|
|
816
|
+
VALUE value = parse_lex_input(&input, &options, false);
|
|
817
|
+
pm_string_free(&input);
|
|
818
|
+
pm_options_free(&options);
|
|
819
|
+
|
|
820
|
+
return value;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/******************************************************************************/
|
|
824
|
+
/* Parsing Ruby code */
|
|
825
|
+
/******************************************************************************/
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Parse the given input and return a ParseResult instance.
|
|
829
|
+
*/
|
|
830
|
+
static VALUE
|
|
831
|
+
parse_input(pm_string_t *input, const pm_options_t *options) {
|
|
832
|
+
pm_parser_t parser;
|
|
833
|
+
pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
|
|
834
|
+
|
|
835
|
+
pm_node_t *node = pm_parse(&parser);
|
|
836
|
+
rb_encoding *encoding = rb_enc_find(parser.encoding->name);
|
|
837
|
+
|
|
838
|
+
VALUE source = pm_source_new(&parser, encoding, options->freeze);
|
|
839
|
+
VALUE value = pm_ast_new(&parser, node, encoding, source, options->freeze);
|
|
840
|
+
VALUE result = parse_result_create(rb_cPrismParseResult, &parser, value, encoding, source, options->freeze);
|
|
841
|
+
|
|
842
|
+
if (options->freeze) {
|
|
843
|
+
rb_obj_freeze(source);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
pm_node_destroy(&parser, node);
|
|
847
|
+
pm_parser_free(&parser);
|
|
848
|
+
|
|
849
|
+
return result;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* call-seq:
|
|
854
|
+
* Prism::parse(source, **options) -> ParseResult
|
|
855
|
+
*
|
|
856
|
+
* Parse the given string and return a ParseResult instance. The options that
|
|
857
|
+
* are supported are:
|
|
858
|
+
*
|
|
859
|
+
* * `command_line` - either nil or a string of the various options that were
|
|
860
|
+
* set on the command line. Valid values are combinations of "a", "l",
|
|
861
|
+
* "n", "p", and "x".
|
|
862
|
+
* * `encoding` - the encoding of the source being parsed. This should be an
|
|
863
|
+
* encoding or nil.
|
|
864
|
+
* * `filepath` - the filepath of the source being parsed. This should be a
|
|
865
|
+
* string or nil.
|
|
866
|
+
* * `freeze` - whether or not to deeply freeze the AST. This should be a
|
|
867
|
+
* boolean or nil.
|
|
868
|
+
* * `frozen_string_literal` - whether or not the frozen string literal pragma
|
|
869
|
+
* has been set. This should be a boolean or nil.
|
|
870
|
+
* * `line` - the line number that the parse starts on. This should be an
|
|
871
|
+
* integer or nil. Note that this is 1-indexed.
|
|
872
|
+
* * `main_script` - a boolean indicating whether or not the source being parsed
|
|
873
|
+
* is the main script being run by the interpreter. This controls whether
|
|
874
|
+
* or not shebangs are parsed for additional flags and whether or not the
|
|
875
|
+
* parser will attempt to find a matching shebang if the first one does
|
|
876
|
+
* not contain the word "ruby".
|
|
877
|
+
* * `partial_script` - when the file being parsed is considered a "partial"
|
|
878
|
+
* script, jumps will not be marked as errors if they are not contained
|
|
879
|
+
* within loops/blocks. This is used in the case that you're parsing a
|
|
880
|
+
* script that you know will be embedded inside another script later, but
|
|
881
|
+
* you do not have that context yet. For example, when parsing an ERB
|
|
882
|
+
* template that will be evaluated inside another script.
|
|
883
|
+
* * `scopes` - the locals that are in scope surrounding the code that is being
|
|
884
|
+
* parsed. This should be an array of arrays of symbols or nil. Scopes are
|
|
885
|
+
* ordered from the outermost scope to the innermost one.
|
|
886
|
+
* * `version` - the version of Ruby syntax that prism should used to parse Ruby
|
|
887
|
+
* code. By default prism assumes you want to parse with the latest
|
|
888
|
+
* version of Ruby syntax (which you can trigger with `nil` or
|
|
889
|
+
* `"latest"`). You may also restrict the syntax to a specific version of
|
|
890
|
+
* Ruby, e.g., with `"3.3.0"`. To parse with the same syntax version that
|
|
891
|
+
* the current Ruby is running use `version: RUBY_VERSION`. Raises
|
|
892
|
+
* ArgumentError if the version is not currently supported by Prism.
|
|
893
|
+
*/
|
|
894
|
+
static VALUE
|
|
895
|
+
parse(int argc, VALUE *argv, VALUE self) {
|
|
896
|
+
pm_string_t input;
|
|
897
|
+
pm_options_t options = { 0 };
|
|
898
|
+
string_options(argc, argv, &input, &options);
|
|
899
|
+
|
|
900
|
+
#ifdef PRISM_BUILD_DEBUG
|
|
901
|
+
size_t length = pm_string_length(&input);
|
|
902
|
+
char* dup = xmalloc(length);
|
|
903
|
+
memcpy(dup, pm_string_source(&input), length);
|
|
904
|
+
pm_string_constant_init(&input, dup, length);
|
|
905
|
+
#endif
|
|
906
|
+
|
|
907
|
+
VALUE value = parse_input(&input, &options);
|
|
908
|
+
|
|
909
|
+
#ifdef PRISM_BUILD_DEBUG
|
|
910
|
+
xfree(dup);
|
|
911
|
+
#endif
|
|
912
|
+
|
|
913
|
+
pm_string_free(&input);
|
|
914
|
+
pm_options_free(&options);
|
|
915
|
+
return value;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* call-seq:
|
|
920
|
+
* Prism::parse_file(filepath, **options) -> ParseResult
|
|
921
|
+
*
|
|
922
|
+
* Parse the given file and return a ParseResult instance. For supported
|
|
923
|
+
* options, see Prism::parse.
|
|
924
|
+
*/
|
|
925
|
+
static VALUE
|
|
926
|
+
parse_file(int argc, VALUE *argv, VALUE self) {
|
|
927
|
+
pm_string_t input;
|
|
928
|
+
pm_options_t options = { 0 };
|
|
929
|
+
|
|
930
|
+
VALUE encoded_filepath;
|
|
931
|
+
file_options(argc, argv, &input, &options, &encoded_filepath);
|
|
932
|
+
|
|
933
|
+
VALUE value = parse_input(&input, &options);
|
|
934
|
+
pm_string_free(&input);
|
|
935
|
+
pm_options_free(&options);
|
|
936
|
+
|
|
937
|
+
return value;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/**
|
|
941
|
+
* Parse the given input and return nothing.
|
|
942
|
+
*/
|
|
943
|
+
static void
|
|
944
|
+
profile_input(pm_string_t *input, const pm_options_t *options) {
|
|
945
|
+
pm_parser_t parser;
|
|
946
|
+
pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
|
|
947
|
+
|
|
948
|
+
pm_node_t *node = pm_parse(&parser);
|
|
949
|
+
pm_node_destroy(&parser, node);
|
|
950
|
+
pm_parser_free(&parser);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* call-seq:
|
|
955
|
+
* Prism::profile(source, **options) -> nil
|
|
956
|
+
*
|
|
957
|
+
* Parse the given string and return nothing. This method is meant to allow
|
|
958
|
+
* profilers to avoid the overhead of reifying the AST to Ruby. For supported
|
|
959
|
+
* options, see Prism::parse.
|
|
960
|
+
*/
|
|
961
|
+
static VALUE
|
|
962
|
+
profile(int argc, VALUE *argv, VALUE self) {
|
|
963
|
+
pm_string_t input;
|
|
964
|
+
pm_options_t options = { 0 };
|
|
965
|
+
|
|
966
|
+
string_options(argc, argv, &input, &options);
|
|
967
|
+
profile_input(&input, &options);
|
|
968
|
+
pm_string_free(&input);
|
|
969
|
+
pm_options_free(&options);
|
|
970
|
+
|
|
971
|
+
return Qnil;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* call-seq:
|
|
976
|
+
* Prism::profile_file(filepath, **options) -> nil
|
|
977
|
+
*
|
|
978
|
+
* Parse the given file and return nothing. This method is meant to allow
|
|
979
|
+
* profilers to avoid the overhead of reifying the AST to Ruby. For supported
|
|
980
|
+
* options, see Prism::parse.
|
|
981
|
+
*/
|
|
982
|
+
static VALUE
|
|
983
|
+
profile_file(int argc, VALUE *argv, VALUE self) {
|
|
984
|
+
pm_string_t input;
|
|
985
|
+
pm_options_t options = { 0 };
|
|
986
|
+
|
|
987
|
+
VALUE encoded_filepath;
|
|
988
|
+
file_options(argc, argv, &input, &options, &encoded_filepath);
|
|
989
|
+
|
|
990
|
+
profile_input(&input, &options);
|
|
991
|
+
pm_string_free(&input);
|
|
992
|
+
pm_options_free(&options);
|
|
993
|
+
|
|
994
|
+
return Qnil;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
static int
|
|
998
|
+
parse_stream_eof(void *stream) {
|
|
999
|
+
if (rb_funcall((VALUE) stream, rb_intern("eof?"), 0)) {
|
|
1000
|
+
return 1;
|
|
1001
|
+
}
|
|
1002
|
+
return 0;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* An implementation of fgets that is suitable for use with Ruby IO objects.
|
|
1007
|
+
*/
|
|
1008
|
+
static char *
|
|
1009
|
+
parse_stream_fgets(char *string, int size, void *stream) {
|
|
1010
|
+
RUBY_ASSERT(size > 0);
|
|
1011
|
+
|
|
1012
|
+
VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - 1));
|
|
1013
|
+
if (NIL_P(line)) {
|
|
1014
|
+
return NULL;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
const char *cstr = RSTRING_PTR(line);
|
|
1018
|
+
long length = RSTRING_LEN(line);
|
|
1019
|
+
|
|
1020
|
+
memcpy(string, cstr, length);
|
|
1021
|
+
string[length] = '\0';
|
|
1022
|
+
|
|
1023
|
+
return string;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* call-seq:
|
|
1028
|
+
* Prism::parse_stream(stream, **options) -> ParseResult
|
|
1029
|
+
*
|
|
1030
|
+
* Parse the given object that responds to `gets` and return a ParseResult
|
|
1031
|
+
* instance. The options that are supported are the same as Prism::parse.
|
|
1032
|
+
*/
|
|
1033
|
+
static VALUE
|
|
1034
|
+
parse_stream(int argc, VALUE *argv, VALUE self) {
|
|
1035
|
+
VALUE stream;
|
|
1036
|
+
VALUE keywords;
|
|
1037
|
+
rb_scan_args(argc, argv, "1:", &stream, &keywords);
|
|
1038
|
+
|
|
1039
|
+
pm_options_t options = { 0 };
|
|
1040
|
+
extract_options(&options, Qnil, keywords);
|
|
1041
|
+
|
|
1042
|
+
pm_parser_t parser;
|
|
1043
|
+
pm_buffer_t buffer;
|
|
1044
|
+
|
|
1045
|
+
pm_node_t *node = pm_parse_stream(&parser, &buffer, (void *) stream, parse_stream_fgets, parse_stream_eof, &options);
|
|
1046
|
+
rb_encoding *encoding = rb_enc_find(parser.encoding->name);
|
|
1047
|
+
|
|
1048
|
+
VALUE source = pm_source_new(&parser, encoding, options.freeze);
|
|
1049
|
+
VALUE value = pm_ast_new(&parser, node, encoding, source, options.freeze);
|
|
1050
|
+
VALUE result = parse_result_create(rb_cPrismParseResult, &parser, value, encoding, source, options.freeze);
|
|
1051
|
+
|
|
1052
|
+
pm_node_destroy(&parser, node);
|
|
1053
|
+
pm_buffer_free(&buffer);
|
|
1054
|
+
pm_parser_free(&parser);
|
|
1055
|
+
|
|
1056
|
+
return result;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
/**
|
|
1060
|
+
* Parse the given input and return an array of Comment objects.
|
|
1061
|
+
*/
|
|
1062
|
+
static VALUE
|
|
1063
|
+
parse_input_comments(pm_string_t *input, const pm_options_t *options) {
|
|
1064
|
+
pm_parser_t parser;
|
|
1065
|
+
pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
|
|
1066
|
+
|
|
1067
|
+
pm_node_t *node = pm_parse(&parser);
|
|
1068
|
+
rb_encoding *encoding = rb_enc_find(parser.encoding->name);
|
|
1069
|
+
|
|
1070
|
+
VALUE source = pm_source_new(&parser, encoding, options->freeze);
|
|
1071
|
+
VALUE comments = parser_comments(&parser, source, options->freeze);
|
|
1072
|
+
|
|
1073
|
+
pm_node_destroy(&parser, node);
|
|
1074
|
+
pm_parser_free(&parser);
|
|
1075
|
+
|
|
1076
|
+
return comments;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/**
|
|
1080
|
+
* call-seq:
|
|
1081
|
+
* Prism::parse_comments(source, **options) -> Array
|
|
1082
|
+
*
|
|
1083
|
+
* Parse the given string and return an array of Comment objects. For supported
|
|
1084
|
+
* options, see Prism::parse.
|
|
1085
|
+
*/
|
|
1086
|
+
static VALUE
|
|
1087
|
+
parse_comments(int argc, VALUE *argv, VALUE self) {
|
|
1088
|
+
pm_string_t input;
|
|
1089
|
+
pm_options_t options = { 0 };
|
|
1090
|
+
string_options(argc, argv, &input, &options);
|
|
1091
|
+
|
|
1092
|
+
VALUE result = parse_input_comments(&input, &options);
|
|
1093
|
+
pm_string_free(&input);
|
|
1094
|
+
pm_options_free(&options);
|
|
1095
|
+
|
|
1096
|
+
return result;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* call-seq:
|
|
1101
|
+
* Prism::parse_file_comments(filepath, **options) -> Array
|
|
1102
|
+
*
|
|
1103
|
+
* Parse the given file and return an array of Comment objects. For supported
|
|
1104
|
+
* options, see Prism::parse.
|
|
1105
|
+
*/
|
|
1106
|
+
static VALUE
|
|
1107
|
+
parse_file_comments(int argc, VALUE *argv, VALUE self) {
|
|
1108
|
+
pm_string_t input;
|
|
1109
|
+
pm_options_t options = { 0 };
|
|
1110
|
+
|
|
1111
|
+
VALUE encoded_filepath;
|
|
1112
|
+
file_options(argc, argv, &input, &options, &encoded_filepath);
|
|
1113
|
+
|
|
1114
|
+
VALUE value = parse_input_comments(&input, &options);
|
|
1115
|
+
pm_string_free(&input);
|
|
1116
|
+
pm_options_free(&options);
|
|
1117
|
+
|
|
1118
|
+
return value;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* call-seq:
|
|
1123
|
+
* Prism::parse_lex(source, **options) -> ParseLexResult
|
|
1124
|
+
*
|
|
1125
|
+
* Parse the given string and return a ParseLexResult instance that contains a
|
|
1126
|
+
* 2-element array, where the first element is the AST and the second element is
|
|
1127
|
+
* an array of Token instances.
|
|
1128
|
+
*
|
|
1129
|
+
* This API is only meant to be used in the case where you need both the AST and
|
|
1130
|
+
* the tokens. If you only need one or the other, use either Prism::parse or
|
|
1131
|
+
* Prism::lex.
|
|
1132
|
+
*
|
|
1133
|
+
* For supported options, see Prism::parse.
|
|
1134
|
+
*/
|
|
1135
|
+
static VALUE
|
|
1136
|
+
parse_lex(int argc, VALUE *argv, VALUE self) {
|
|
1137
|
+
pm_string_t input;
|
|
1138
|
+
pm_options_t options = { 0 };
|
|
1139
|
+
string_options(argc, argv, &input, &options);
|
|
1140
|
+
|
|
1141
|
+
VALUE value = parse_lex_input(&input, &options, true);
|
|
1142
|
+
pm_string_free(&input);
|
|
1143
|
+
pm_options_free(&options);
|
|
1144
|
+
|
|
1145
|
+
return value;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* call-seq:
|
|
1150
|
+
* Prism::parse_lex_file(filepath, **options) -> ParseLexResult
|
|
1151
|
+
*
|
|
1152
|
+
* Parse the given file and return a ParseLexResult instance that contains a
|
|
1153
|
+
* 2-element array, where the first element is the AST and the second element is
|
|
1154
|
+
* an array of Token instances.
|
|
1155
|
+
*
|
|
1156
|
+
* This API is only meant to be used in the case where you need both the AST and
|
|
1157
|
+
* the tokens. If you only need one or the other, use either Prism::parse_file
|
|
1158
|
+
* or Prism::lex_file.
|
|
1159
|
+
*
|
|
1160
|
+
* For supported options, see Prism::parse.
|
|
1161
|
+
*/
|
|
1162
|
+
static VALUE
|
|
1163
|
+
parse_lex_file(int argc, VALUE *argv, VALUE self) {
|
|
1164
|
+
pm_string_t input;
|
|
1165
|
+
pm_options_t options = { 0 };
|
|
1166
|
+
|
|
1167
|
+
VALUE encoded_filepath;
|
|
1168
|
+
file_options(argc, argv, &input, &options, &encoded_filepath);
|
|
1169
|
+
|
|
1170
|
+
VALUE value = parse_lex_input(&input, &options, true);
|
|
1171
|
+
pm_string_free(&input);
|
|
1172
|
+
pm_options_free(&options);
|
|
1173
|
+
|
|
1174
|
+
return value;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
/**
|
|
1178
|
+
* Parse the given input and return true if it parses without errors.
|
|
1179
|
+
*/
|
|
1180
|
+
static VALUE
|
|
1181
|
+
parse_input_success_p(pm_string_t *input, const pm_options_t *options) {
|
|
1182
|
+
pm_parser_t parser;
|
|
1183
|
+
pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
|
|
1184
|
+
|
|
1185
|
+
pm_node_t *node = pm_parse(&parser);
|
|
1186
|
+
pm_node_destroy(&parser, node);
|
|
1187
|
+
|
|
1188
|
+
VALUE result = parser.error_list.size == 0 ? Qtrue : Qfalse;
|
|
1189
|
+
pm_parser_free(&parser);
|
|
1190
|
+
|
|
1191
|
+
return result;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* call-seq:
|
|
1196
|
+
* Prism::parse_success?(source, **options) -> bool
|
|
1197
|
+
*
|
|
1198
|
+
* Parse the given string and return true if it parses without errors. For
|
|
1199
|
+
* supported options, see Prism::parse.
|
|
1200
|
+
*/
|
|
1201
|
+
static VALUE
|
|
1202
|
+
parse_success_p(int argc, VALUE *argv, VALUE self) {
|
|
1203
|
+
pm_string_t input;
|
|
1204
|
+
pm_options_t options = { 0 };
|
|
1205
|
+
string_options(argc, argv, &input, &options);
|
|
1206
|
+
|
|
1207
|
+
VALUE result = parse_input_success_p(&input, &options);
|
|
1208
|
+
pm_string_free(&input);
|
|
1209
|
+
pm_options_free(&options);
|
|
1210
|
+
|
|
1211
|
+
return result;
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
/**
|
|
1215
|
+
* call-seq:
|
|
1216
|
+
* Prism::parse_failure?(source, **options) -> bool
|
|
1217
|
+
*
|
|
1218
|
+
* Parse the given string and return true if it parses with errors. For
|
|
1219
|
+
* supported options, see Prism::parse.
|
|
1220
|
+
*/
|
|
1221
|
+
static VALUE
|
|
1222
|
+
parse_failure_p(int argc, VALUE *argv, VALUE self) {
|
|
1223
|
+
return RTEST(parse_success_p(argc, argv, self)) ? Qfalse : Qtrue;
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
/**
|
|
1227
|
+
* call-seq:
|
|
1228
|
+
* Prism::parse_file_success?(filepath, **options) -> bool
|
|
1229
|
+
*
|
|
1230
|
+
* Parse the given file and return true if it parses without errors. For
|
|
1231
|
+
* supported options, see Prism::parse.
|
|
1232
|
+
*/
|
|
1233
|
+
static VALUE
|
|
1234
|
+
parse_file_success_p(int argc, VALUE *argv, VALUE self) {
|
|
1235
|
+
pm_string_t input;
|
|
1236
|
+
pm_options_t options = { 0 };
|
|
1237
|
+
|
|
1238
|
+
VALUE encoded_filepath;
|
|
1239
|
+
file_options(argc, argv, &input, &options, &encoded_filepath);
|
|
1240
|
+
|
|
1241
|
+
VALUE result = parse_input_success_p(&input, &options);
|
|
1242
|
+
pm_string_free(&input);
|
|
1243
|
+
pm_options_free(&options);
|
|
1244
|
+
|
|
1245
|
+
return result;
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
/**
|
|
1249
|
+
* call-seq:
|
|
1250
|
+
* Prism::parse_file_failure?(filepath, **options) -> bool
|
|
1251
|
+
*
|
|
1252
|
+
* Parse the given file and return true if it parses with errors. For
|
|
1253
|
+
* supported options, see Prism::parse.
|
|
1254
|
+
*/
|
|
1255
|
+
static VALUE
|
|
1256
|
+
parse_file_failure_p(int argc, VALUE *argv, VALUE self) {
|
|
1257
|
+
return RTEST(parse_file_success_p(argc, argv, self)) ? Qfalse : Qtrue;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
/******************************************************************************/
|
|
1261
|
+
/* String query methods */
|
|
1262
|
+
/******************************************************************************/
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* Process the result of a call to a string query method and return an
|
|
1266
|
+
* appropriate value.
|
|
1267
|
+
*/
|
|
1268
|
+
static VALUE
|
|
1269
|
+
string_query(pm_string_query_t result) {
|
|
1270
|
+
switch (result) {
|
|
1271
|
+
case PM_STRING_QUERY_ERROR:
|
|
1272
|
+
rb_raise(rb_eArgError, "Invalid or non ascii-compatible encoding");
|
|
1273
|
+
return Qfalse;
|
|
1274
|
+
case PM_STRING_QUERY_FALSE:
|
|
1275
|
+
return Qfalse;
|
|
1276
|
+
case PM_STRING_QUERY_TRUE:
|
|
1277
|
+
return Qtrue;
|
|
1278
|
+
}
|
|
1279
|
+
return Qfalse;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* call-seq:
|
|
1284
|
+
* Prism::StringQuery::local?(string) -> bool
|
|
1285
|
+
*
|
|
1286
|
+
* Returns true if the string constitutes a valid local variable name. Note that
|
|
1287
|
+
* this means the names that can be set through Binding#local_variable_set, not
|
|
1288
|
+
* necessarily the ones that can be set through a local variable assignment.
|
|
1289
|
+
*/
|
|
1290
|
+
static VALUE
|
|
1291
|
+
string_query_local_p(VALUE self, VALUE string) {
|
|
1292
|
+
const uint8_t *source = (const uint8_t *) check_string(string);
|
|
1293
|
+
return string_query(pm_string_query_local(source, RSTRING_LEN(string), rb_enc_get(string)->name));
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* call-seq:
|
|
1298
|
+
* Prism::StringQuery::constant?(string) -> bool
|
|
1299
|
+
*
|
|
1300
|
+
* Returns true if the string constitutes a valid constant name. Note that this
|
|
1301
|
+
* means the names that can be set through Module#const_set, not necessarily the
|
|
1302
|
+
* ones that can be set through a constant assignment.
|
|
1303
|
+
*/
|
|
1304
|
+
static VALUE
|
|
1305
|
+
string_query_constant_p(VALUE self, VALUE string) {
|
|
1306
|
+
const uint8_t *source = (const uint8_t *) check_string(string);
|
|
1307
|
+
return string_query(pm_string_query_constant(source, RSTRING_LEN(string), rb_enc_get(string)->name));
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* call-seq:
|
|
1312
|
+
* Prism::StringQuery::method_name?(string) -> bool
|
|
1313
|
+
*
|
|
1314
|
+
* Returns true if the string constitutes a valid method name.
|
|
1315
|
+
*/
|
|
1316
|
+
static VALUE
|
|
1317
|
+
string_query_method_name_p(VALUE self, VALUE string) {
|
|
1318
|
+
const uint8_t *source = (const uint8_t *) check_string(string);
|
|
1319
|
+
return string_query(pm_string_query_method_name(source, RSTRING_LEN(string), rb_enc_get(string)->name));
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
/******************************************************************************/
|
|
1323
|
+
/* Initialization of the extension */
|
|
1324
|
+
/******************************************************************************/
|
|
1325
|
+
|
|
1326
|
+
/**
|
|
1327
|
+
* The init function that Ruby calls when loading this extension.
|
|
1328
|
+
*/
|
|
1329
|
+
RUBY_FUNC_EXPORTED void
|
|
1330
|
+
Init_prism(void) {
|
|
1331
|
+
// Make sure that the prism library version matches the expected version.
|
|
1332
|
+
// Otherwise something was compiled incorrectly.
|
|
1333
|
+
if (strcmp(pm_version(), EXPECTED_PRISM_VERSION) != 0) {
|
|
1334
|
+
rb_raise(
|
|
1335
|
+
rb_eRuntimeError,
|
|
1336
|
+
"The prism library version (%s) does not match the expected version (%s)",
|
|
1337
|
+
pm_version(),
|
|
1338
|
+
EXPECTED_PRISM_VERSION
|
|
1339
|
+
);
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
|
1343
|
+
// Mark this extension as Ractor-safe.
|
|
1344
|
+
rb_ext_ractor_safe(true);
|
|
1345
|
+
#endif
|
|
1346
|
+
|
|
1347
|
+
// Grab up references to all of the constants that we're going to need to
|
|
1348
|
+
// reference throughout this extension.
|
|
1349
|
+
rb_cPrism = rb_define_module("Prism");
|
|
1350
|
+
rb_cPrismNode = rb_define_class_under(rb_cPrism, "Node", rb_cObject);
|
|
1351
|
+
rb_cPrismSource = rb_define_class_under(rb_cPrism, "Source", rb_cObject);
|
|
1352
|
+
rb_cPrismToken = rb_define_class_under(rb_cPrism, "Token", rb_cObject);
|
|
1353
|
+
rb_cPrismLocation = rb_define_class_under(rb_cPrism, "Location", rb_cObject);
|
|
1354
|
+
rb_cPrismComment = rb_define_class_under(rb_cPrism, "Comment", rb_cObject);
|
|
1355
|
+
rb_cPrismInlineComment = rb_define_class_under(rb_cPrism, "InlineComment", rb_cPrismComment);
|
|
1356
|
+
rb_cPrismEmbDocComment = rb_define_class_under(rb_cPrism, "EmbDocComment", rb_cPrismComment);
|
|
1357
|
+
rb_cPrismMagicComment = rb_define_class_under(rb_cPrism, "MagicComment", rb_cObject);
|
|
1358
|
+
rb_cPrismParseError = rb_define_class_under(rb_cPrism, "ParseError", rb_cObject);
|
|
1359
|
+
rb_cPrismParseWarning = rb_define_class_under(rb_cPrism, "ParseWarning", rb_cObject);
|
|
1360
|
+
rb_cPrismResult = rb_define_class_under(rb_cPrism, "Result", rb_cObject);
|
|
1361
|
+
rb_cPrismParseResult = rb_define_class_under(rb_cPrism, "ParseResult", rb_cPrismResult);
|
|
1362
|
+
rb_cPrismLexResult = rb_define_class_under(rb_cPrism, "LexResult", rb_cPrismResult);
|
|
1363
|
+
rb_cPrismParseLexResult = rb_define_class_under(rb_cPrism, "ParseLexResult", rb_cPrismResult);
|
|
1364
|
+
rb_cPrismStringQuery = rb_define_class_under(rb_cPrism, "StringQuery", rb_cObject);
|
|
1365
|
+
rb_cPrismScope = rb_define_class_under(rb_cPrism, "Scope", rb_cObject);
|
|
1366
|
+
|
|
1367
|
+
// Intern all of the IDs eagerly that we support so that we don't have to do
|
|
1368
|
+
// it every time we parse.
|
|
1369
|
+
rb_id_option_command_line = rb_intern_const("command_line");
|
|
1370
|
+
rb_id_option_encoding = rb_intern_const("encoding");
|
|
1371
|
+
rb_id_option_filepath = rb_intern_const("filepath");
|
|
1372
|
+
rb_id_option_freeze = rb_intern_const("freeze");
|
|
1373
|
+
rb_id_option_frozen_string_literal = rb_intern_const("frozen_string_literal");
|
|
1374
|
+
rb_id_option_line = rb_intern_const("line");
|
|
1375
|
+
rb_id_option_main_script = rb_intern_const("main_script");
|
|
1376
|
+
rb_id_option_partial_script = rb_intern_const("partial_script");
|
|
1377
|
+
rb_id_option_scopes = rb_intern_const("scopes");
|
|
1378
|
+
rb_id_option_version = rb_intern_const("version");
|
|
1379
|
+
rb_id_source_for = rb_intern("for");
|
|
1380
|
+
rb_id_forwarding_positionals = rb_intern("*");
|
|
1381
|
+
rb_id_forwarding_keywords = rb_intern("**");
|
|
1382
|
+
rb_id_forwarding_block = rb_intern("&");
|
|
1383
|
+
rb_id_forwarding_all = rb_intern("...");
|
|
1384
|
+
|
|
1385
|
+
/**
|
|
1386
|
+
* The version of the prism library.
|
|
1387
|
+
*/
|
|
1388
|
+
rb_define_const(rb_cPrism, "VERSION", rb_str_freeze(rb_str_new_cstr(EXPECTED_PRISM_VERSION)));
|
|
1389
|
+
|
|
1390
|
+
// First, the functions that have to do with lexing and parsing.
|
|
1391
|
+
rb_define_singleton_method(rb_cPrism, "lex", lex, -1);
|
|
1392
|
+
rb_define_singleton_method(rb_cPrism, "lex_file", lex_file, -1);
|
|
1393
|
+
rb_define_singleton_method(rb_cPrism, "parse", parse, -1);
|
|
1394
|
+
rb_define_singleton_method(rb_cPrism, "parse_file", parse_file, -1);
|
|
1395
|
+
rb_define_singleton_method(rb_cPrism, "profile", profile, -1);
|
|
1396
|
+
rb_define_singleton_method(rb_cPrism, "profile_file", profile_file, -1);
|
|
1397
|
+
rb_define_singleton_method(rb_cPrism, "parse_stream", parse_stream, -1);
|
|
1398
|
+
rb_define_singleton_method(rb_cPrism, "parse_comments", parse_comments, -1);
|
|
1399
|
+
rb_define_singleton_method(rb_cPrism, "parse_file_comments", parse_file_comments, -1);
|
|
1400
|
+
rb_define_singleton_method(rb_cPrism, "parse_lex", parse_lex, -1);
|
|
1401
|
+
rb_define_singleton_method(rb_cPrism, "parse_lex_file", parse_lex_file, -1);
|
|
1402
|
+
rb_define_singleton_method(rb_cPrism, "parse_success?", parse_success_p, -1);
|
|
1403
|
+
rb_define_singleton_method(rb_cPrism, "parse_failure?", parse_failure_p, -1);
|
|
1404
|
+
rb_define_singleton_method(rb_cPrism, "parse_file_success?", parse_file_success_p, -1);
|
|
1405
|
+
rb_define_singleton_method(rb_cPrism, "parse_file_failure?", parse_file_failure_p, -1);
|
|
1406
|
+
|
|
1407
|
+
#ifndef PRISM_EXCLUDE_SERIALIZATION
|
|
1408
|
+
rb_define_singleton_method(rb_cPrism, "dump", dump, -1);
|
|
1409
|
+
rb_define_singleton_method(rb_cPrism, "dump_file", dump_file, -1);
|
|
1410
|
+
#endif
|
|
1411
|
+
|
|
1412
|
+
rb_define_singleton_method(rb_cPrismStringQuery, "local?", string_query_local_p, 1);
|
|
1413
|
+
rb_define_singleton_method(rb_cPrismStringQuery, "constant?", string_query_constant_p, 1);
|
|
1414
|
+
rb_define_singleton_method(rb_cPrismStringQuery, "method_name?", string_query_method_name_p, 1);
|
|
1415
|
+
|
|
1416
|
+
// Next, initialize the other APIs.
|
|
1417
|
+
Init_prism_api_node();
|
|
1418
|
+
Init_prism_pack();
|
|
1419
|
+
}
|