prism 0.15.1 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +35 -1
- data/Makefile +12 -0
- data/README.md +3 -1
- data/config.yml +66 -50
- data/docs/configuration.md +2 -0
- data/docs/fuzzing.md +1 -1
- data/docs/javascript.md +90 -0
- data/docs/releasing.md +27 -0
- data/docs/ruby_api.md +2 -0
- data/docs/serialization.md +28 -29
- data/ext/prism/api_node.c +856 -826
- data/ext/prism/api_pack.c +20 -9
- data/ext/prism/extension.c +494 -119
- data/ext/prism/extension.h +1 -1
- data/include/prism/ast.h +3157 -747
- data/include/prism/defines.h +40 -8
- data/include/prism/diagnostic.h +36 -3
- data/include/prism/enc/pm_encoding.h +119 -28
- data/include/prism/node.h +38 -30
- data/include/prism/options.h +204 -0
- data/include/prism/pack.h +44 -33
- data/include/prism/parser.h +445 -199
- data/include/prism/prettyprint.h +26 -0
- data/include/prism/regexp.h +16 -2
- data/include/prism/util/pm_buffer.h +102 -18
- data/include/prism/util/pm_char.h +162 -48
- data/include/prism/util/pm_constant_pool.h +128 -34
- data/include/prism/util/pm_list.h +68 -38
- data/include/prism/util/pm_memchr.h +18 -3
- data/include/prism/util/pm_newline_list.h +71 -28
- data/include/prism/util/pm_state_stack.h +25 -7
- data/include/prism/util/pm_string.h +115 -27
- data/include/prism/util/pm_string_list.h +25 -6
- data/include/prism/util/pm_strncasecmp.h +32 -0
- data/include/prism/util/pm_strpbrk.h +31 -17
- data/include/prism/version.h +28 -3
- data/include/prism.h +229 -36
- data/lib/prism/compiler.rb +5 -5
- data/lib/prism/debug.rb +43 -13
- data/lib/prism/desugar_compiler.rb +1 -1
- data/lib/prism/dispatcher.rb +27 -26
- data/lib/prism/dsl.rb +16 -16
- data/lib/prism/ffi.rb +138 -61
- data/lib/prism/lex_compat.rb +26 -16
- data/lib/prism/mutation_compiler.rb +11 -11
- data/lib/prism/node.rb +426 -227
- data/lib/prism/node_ext.rb +23 -16
- data/lib/prism/node_inspector.rb +1 -1
- data/lib/prism/pack.rb +79 -40
- data/lib/prism/parse_result/comments.rb +7 -2
- data/lib/prism/parse_result/newlines.rb +4 -0
- data/lib/prism/parse_result.rb +157 -21
- data/lib/prism/pattern.rb +14 -3
- data/lib/prism/ripper_compat.rb +28 -10
- data/lib/prism/serialize.rb +935 -307
- data/lib/prism/visitor.rb +9 -5
- data/lib/prism.rb +20 -2
- data/prism.gemspec +11 -2
- data/rbi/prism.rbi +7305 -0
- data/rbi/prism_static.rbi +196 -0
- data/sig/prism.rbs +4468 -0
- data/sig/prism_static.rbs +123 -0
- data/src/diagnostic.c +56 -53
- data/src/enc/pm_big5.c +1 -0
- data/src/enc/pm_euc_jp.c +1 -0
- data/src/enc/pm_gbk.c +1 -0
- data/src/enc/pm_shift_jis.c +1 -0
- data/src/enc/pm_tables.c +316 -80
- data/src/enc/pm_unicode.c +54 -9
- data/src/enc/pm_windows_31j.c +1 -0
- data/src/node.c +357 -345
- data/src/options.c +170 -0
- data/src/prettyprint.c +7697 -1643
- data/src/prism.c +1964 -1125
- data/src/regexp.c +153 -95
- data/src/serialize.c +432 -397
- data/src/token_type.c +3 -1
- data/src/util/pm_buffer.c +88 -23
- data/src/util/pm_char.c +103 -57
- data/src/util/pm_constant_pool.c +52 -22
- data/src/util/pm_list.c +12 -4
- data/src/util/pm_memchr.c +5 -3
- data/src/util/pm_newline_list.c +25 -63
- data/src/util/pm_state_stack.c +9 -3
- data/src/util/pm_string.c +95 -85
- data/src/util/pm_string_list.c +14 -15
- data/src/util/pm_strncasecmp.c +10 -3
- data/src/util/pm_strpbrk.c +25 -19
- metadata +12 -3
- data/docs/prism.png +0 -0
data/src/options.c
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
#include "prism/options.h"
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Set the filepath option on the given options struct.
|
5
|
+
*/
|
6
|
+
PRISM_EXPORTED_FUNCTION void
|
7
|
+
pm_options_filepath_set(pm_options_t *options, const char *filepath) {
|
8
|
+
pm_string_constant_init(&options->filepath, filepath, strlen(filepath));
|
9
|
+
}
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Set the encoding option on the given options struct.
|
13
|
+
*/
|
14
|
+
PRISM_EXPORTED_FUNCTION void
|
15
|
+
pm_options_encoding_set(pm_options_t *options, const char *encoding) {
|
16
|
+
pm_string_constant_init(&options->encoding, encoding, strlen(encoding));
|
17
|
+
}
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Set the line option on the given options struct.
|
21
|
+
*/
|
22
|
+
PRISM_EXPORTED_FUNCTION void
|
23
|
+
pm_options_line_set(pm_options_t *options, uint32_t line) {
|
24
|
+
options->line = line;
|
25
|
+
}
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Set the frozen string literal option on the given options struct.
|
29
|
+
*/
|
30
|
+
PRISM_EXPORTED_FUNCTION void
|
31
|
+
pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) {
|
32
|
+
options->frozen_string_literal = frozen_string_literal;
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Set the suppress warnings option on the given options struct.
|
37
|
+
*/
|
38
|
+
PRISM_EXPORTED_FUNCTION void
|
39
|
+
pm_options_suppress_warnings_set(pm_options_t *options, bool suppress_warnings) {
|
40
|
+
options->suppress_warnings = suppress_warnings;
|
41
|
+
}
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Allocate and zero out the scopes array on the given options struct.
|
45
|
+
*/
|
46
|
+
PRISM_EXPORTED_FUNCTION void
|
47
|
+
pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
|
48
|
+
options->scopes_count = scopes_count;
|
49
|
+
options->scopes = calloc(scopes_count, sizeof(pm_options_scope_t));
|
50
|
+
if (options->scopes == NULL) abort();
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
* Return a pointer to the scope at the given index within the given options.
|
55
|
+
*/
|
56
|
+
PRISM_EXPORTED_FUNCTION const pm_options_scope_t *
|
57
|
+
pm_options_scope_get(const pm_options_t *options, size_t index) {
|
58
|
+
return &options->scopes[index];
|
59
|
+
}
|
60
|
+
|
61
|
+
/**
|
62
|
+
* Create a new options scope struct. This will hold a set of locals that are in
|
63
|
+
* scope surrounding the code that is being parsed.
|
64
|
+
*/
|
65
|
+
PRISM_EXPORTED_FUNCTION void
|
66
|
+
pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
|
67
|
+
scope->locals_count = locals_count;
|
68
|
+
scope->locals = calloc(locals_count, sizeof(pm_string_t));
|
69
|
+
if (scope->locals == NULL) abort();
|
70
|
+
}
|
71
|
+
|
72
|
+
/**
|
73
|
+
* Return a pointer to the local at the given index within the given scope.
|
74
|
+
*/
|
75
|
+
PRISM_EXPORTED_FUNCTION const pm_string_t *
|
76
|
+
pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index) {
|
77
|
+
return &scope->locals[index];
|
78
|
+
}
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Free the internal memory associated with the options.
|
82
|
+
*/
|
83
|
+
PRISM_EXPORTED_FUNCTION void
|
84
|
+
pm_options_free(pm_options_t *options) {
|
85
|
+
pm_string_free(&options->filepath);
|
86
|
+
pm_string_free(&options->encoding);
|
87
|
+
|
88
|
+
for (size_t scope_index = 0; scope_index < options->scopes_count; scope_index++) {
|
89
|
+
pm_options_scope_t *scope = &options->scopes[scope_index];
|
90
|
+
|
91
|
+
for (size_t local_index = 0; local_index < scope->locals_count; local_index++) {
|
92
|
+
pm_string_free(&scope->locals[local_index]);
|
93
|
+
}
|
94
|
+
|
95
|
+
free(scope->locals);
|
96
|
+
}
|
97
|
+
|
98
|
+
free(options->scopes);
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Read a 32-bit unsigned integer from a pointer. This function is used to read
|
103
|
+
* the options that are passed into the parser from the Ruby implementation. It
|
104
|
+
* handles aligned and unaligned reads.
|
105
|
+
*/
|
106
|
+
static uint32_t
|
107
|
+
pm_options_read_u32(const char *data) {
|
108
|
+
if (((uintptr_t) data) % sizeof(uint32_t) == 0) {
|
109
|
+
return *((uint32_t *) data);
|
110
|
+
} else {
|
111
|
+
uint32_t value;
|
112
|
+
memcpy(&value, data, sizeof(uint32_t));
|
113
|
+
return value;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
/**
|
118
|
+
* Deserialize an options struct from the given binary string. This is used to
|
119
|
+
* pass options to the parser from an FFI call so that consumers of the library
|
120
|
+
* from an FFI perspective don't have to worry about the structure of our
|
121
|
+
* options structs. Since the source of these calls will be from Ruby
|
122
|
+
* implementation internals we assume it is from a trusted source.
|
123
|
+
*/
|
124
|
+
void
|
125
|
+
pm_options_read(pm_options_t *options, const char *data) {
|
126
|
+
uint32_t filepath_length = pm_options_read_u32(data);
|
127
|
+
data += 4;
|
128
|
+
|
129
|
+
if (filepath_length > 0) {
|
130
|
+
pm_string_constant_init(&options->filepath, data, filepath_length);
|
131
|
+
data += filepath_length;
|
132
|
+
}
|
133
|
+
|
134
|
+
options->line = pm_options_read_u32(data);
|
135
|
+
data += 4;
|
136
|
+
|
137
|
+
uint32_t encoding_length = pm_options_read_u32(data);
|
138
|
+
data += 4;
|
139
|
+
|
140
|
+
if (encoding_length > 0) {
|
141
|
+
pm_string_constant_init(&options->encoding, data, encoding_length);
|
142
|
+
data += encoding_length;
|
143
|
+
}
|
144
|
+
|
145
|
+
options->frozen_string_literal = *data++;
|
146
|
+
options->suppress_warnings = *data++;
|
147
|
+
|
148
|
+
uint32_t scopes_count = pm_options_read_u32(data);
|
149
|
+
data += 4;
|
150
|
+
|
151
|
+
if (scopes_count > 0) {
|
152
|
+
pm_options_scopes_init(options, scopes_count);
|
153
|
+
|
154
|
+
for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
|
155
|
+
uint32_t locals_count = pm_options_read_u32(data);
|
156
|
+
data += 4;
|
157
|
+
|
158
|
+
pm_options_scope_t *scope = &options->scopes[scope_index];
|
159
|
+
pm_options_scope_init(scope, locals_count);
|
160
|
+
|
161
|
+
for (size_t local_index = 0; local_index < locals_count; local_index++) {
|
162
|
+
uint32_t local_length = pm_options_read_u32(data);
|
163
|
+
data += 4;
|
164
|
+
|
165
|
+
pm_string_constant_init(&scope->locals[local_index], data, local_length);
|
166
|
+
data += local_length;
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
}
|