jruby-prism-parser 0.24.0-java → 1.4.0-java
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 +4 -4
- data/BSDmakefile +58 -0
- data/CHANGELOG.md +269 -1
- data/CONTRIBUTING.md +0 -4
- data/Makefile +25 -18
- data/README.md +57 -6
- data/config.yml +1724 -140
- data/docs/build_system.md +39 -11
- data/docs/configuration.md +4 -0
- data/docs/cruby_compilation.md +1 -1
- data/docs/fuzzing.md +1 -1
- data/docs/parser_translation.md +14 -9
- data/docs/parsing_rules.md +4 -1
- data/docs/releasing.md +8 -10
- data/docs/relocation.md +34 -0
- data/docs/ripper_translation.md +72 -0
- data/docs/ruby_api.md +2 -1
- data/docs/serialization.md +29 -5
- data/ext/prism/api_node.c +3395 -1999
- data/ext/prism/api_pack.c +9 -0
- data/ext/prism/extconf.rb +55 -34
- data/ext/prism/extension.c +597 -346
- data/ext/prism/extension.h +6 -5
- data/include/prism/ast.h +2612 -455
- data/include/prism/defines.h +160 -2
- data/include/prism/diagnostic.h +188 -76
- data/include/prism/encoding.h +22 -4
- data/include/prism/node.h +89 -17
- data/include/prism/options.h +224 -12
- data/include/prism/pack.h +11 -0
- data/include/prism/parser.h +267 -66
- data/include/prism/prettyprint.h +8 -0
- data/include/prism/regexp.h +18 -8
- data/include/prism/static_literals.h +121 -0
- data/include/prism/util/pm_buffer.h +75 -2
- data/include/prism/util/pm_char.h +1 -2
- data/include/prism/util/pm_constant_pool.h +18 -9
- data/include/prism/util/pm_integer.h +126 -0
- data/include/prism/util/pm_list.h +1 -1
- data/include/prism/util/pm_newline_list.h +19 -0
- data/include/prism/util/pm_string.h +48 -8
- data/include/prism/version.h +3 -3
- data/include/prism.h +99 -5
- data/jruby-prism.jar +0 -0
- data/lib/prism/compiler.rb +11 -1
- data/lib/prism/desugar_compiler.rb +113 -74
- data/lib/prism/dispatcher.rb +45 -1
- data/lib/prism/dot_visitor.rb +201 -77
- data/lib/prism/dsl.rb +673 -461
- data/lib/prism/ffi.rb +233 -45
- data/lib/prism/inspect_visitor.rb +2389 -0
- data/lib/prism/lex_compat.rb +35 -16
- data/lib/prism/mutation_compiler.rb +24 -8
- data/lib/prism/node.rb +7731 -8460
- data/lib/prism/node_ext.rb +328 -32
- data/lib/prism/pack.rb +4 -0
- data/lib/prism/parse_result/comments.rb +34 -24
- data/lib/prism/parse_result/errors.rb +65 -0
- data/lib/prism/parse_result/newlines.rb +102 -12
- data/lib/prism/parse_result.rb +448 -44
- data/lib/prism/pattern.rb +28 -10
- data/lib/prism/polyfill/append_as_bytes.rb +15 -0
- data/lib/prism/polyfill/byteindex.rb +13 -0
- data/lib/prism/polyfill/unpack1.rb +14 -0
- data/lib/prism/reflection.rb +413 -0
- data/lib/prism/relocation.rb +504 -0
- data/lib/prism/serialize.rb +1940 -1198
- data/lib/prism/string_query.rb +30 -0
- data/lib/prism/translation/parser/builder.rb +61 -0
- data/lib/prism/translation/parser/compiler.rb +569 -195
- data/lib/prism/translation/parser/lexer.rb +516 -39
- data/lib/prism/translation/parser.rb +177 -12
- data/lib/prism/translation/parser33.rb +1 -1
- data/lib/prism/translation/parser34.rb +1 -1
- data/lib/prism/translation/parser35.rb +12 -0
- data/lib/prism/translation/ripper/sexp.rb +125 -0
- data/lib/prism/translation/ripper/shim.rb +5 -0
- data/lib/prism/translation/ripper.rb +3224 -462
- data/lib/prism/translation/ruby_parser.rb +194 -69
- data/lib/prism/translation.rb +4 -1
- data/lib/prism/version.rb +1 -1
- data/lib/prism/visitor.rb +13 -0
- data/lib/prism.rb +17 -27
- data/prism.gemspec +57 -17
- 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 +8722 -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 +44 -7745
- data/sig/prism/compiler.rbs +9 -0
- data/sig/prism/dispatcher.rbs +16 -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 +3614 -0
- data/sig/prism/node_ext.rbs +82 -0
- data/sig/prism/pack.rbs +43 -0
- data/sig/prism/parse_result.rbs +192 -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 +248 -4767
- data/src/diagnostic.c +672 -230
- data/src/encoding.c +211 -108
- data/src/node.c +7541 -1653
- data/src/options.c +135 -20
- data/src/pack.c +33 -17
- data/src/prettyprint.c +1543 -1485
- data/src/prism.c +7813 -3050
- data/src/regexp.c +225 -73
- data/src/serialize.c +101 -77
- data/src/static_literals.c +617 -0
- data/src/token_type.c +14 -13
- data/src/util/pm_buffer.c +187 -20
- data/src/util/pm_char.c +5 -5
- data/src/util/pm_constant_pool.c +39 -19
- data/src/util/pm_integer.c +670 -0
- data/src/util/pm_list.c +1 -1
- data/src/util/pm_newline_list.c +43 -5
- data/src/util/pm_string.c +213 -33
- data/src/util/pm_strncasecmp.c +13 -1
- data/src/util/pm_strpbrk.c +32 -6
- metadata +55 -19
- data/docs/ripper.md +0 -36
- data/include/prism/util/pm_state_stack.h +0 -42
- data/include/prism/util/pm_string_list.h +0 -44
- data/lib/prism/debug.rb +0 -206
- data/lib/prism/node_inspector.rb +0 -68
- data/lib/prism/translation/parser/rubocop.rb +0 -45
- data/rbi/prism_static.rbi +0 -207
- data/sig/prism_static.rbs +0 -201
- data/src/util/pm_state_stack.c +0 -25
- data/src/util/pm_string_list.c +0 -28
data/src/token_type.c
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
|
1
|
+
/*----------------------------------------------------------------------------*/
|
2
2
|
/* This file is generated by the templates/template.rb script and should not */
|
3
3
|
/* be modified manually. See */
|
4
4
|
/* templates/src/token_type.c.erb */
|
5
5
|
/* if you are looking to modify the */
|
6
6
|
/* template */
|
7
|
-
|
7
|
+
/*----------------------------------------------------------------------------*/
|
8
|
+
|
8
9
|
#include <string.h>
|
9
10
|
|
10
11
|
#include "prism/ast.h"
|
@@ -361,7 +362,7 @@ const char *
|
|
361
362
|
pm_token_type_human(pm_token_type_t token_type) {
|
362
363
|
switch (token_type) {
|
363
364
|
case PM_TOKEN_EOF:
|
364
|
-
return "end
|
365
|
+
return "end-of-input";
|
365
366
|
case PM_TOKEN_MISSING:
|
366
367
|
return "missing token";
|
367
368
|
case PM_TOKEN_NOT_PROVIDED:
|
@@ -421,9 +422,9 @@ pm_token_type_human(pm_token_type_t token_type) {
|
|
421
422
|
case PM_TOKEN_DOT:
|
422
423
|
return "'.'";
|
423
424
|
case PM_TOKEN_DOT_DOT:
|
424
|
-
return "
|
425
|
+
return "..";
|
425
426
|
case PM_TOKEN_DOT_DOT_DOT:
|
426
|
-
return "
|
427
|
+
return "...";
|
427
428
|
case PM_TOKEN_EMBDOC_BEGIN:
|
428
429
|
return "'=begin'";
|
429
430
|
case PM_TOKEN_EMBDOC_END:
|
@@ -461,9 +462,9 @@ pm_token_type_human(pm_token_type_t token_type) {
|
|
461
462
|
case PM_TOKEN_GREATER_EQUAL:
|
462
463
|
return "'>='";
|
463
464
|
case PM_TOKEN_GREATER_GREATER:
|
464
|
-
return "
|
465
|
+
return ">>";
|
465
466
|
case PM_TOKEN_GREATER_GREATER_EQUAL:
|
466
|
-
return "
|
467
|
+
return ">>=";
|
467
468
|
case PM_TOKEN_HEREDOC_END:
|
468
469
|
return "heredoc ending";
|
469
470
|
case PM_TOKEN_HEREDOC_START:
|
@@ -539,7 +540,7 @@ pm_token_type_human(pm_token_type_t token_type) {
|
|
539
540
|
case PM_TOKEN_KEYWORD_RESCUE:
|
540
541
|
return "'rescue'";
|
541
542
|
case PM_TOKEN_KEYWORD_RESCUE_MODIFIER:
|
542
|
-
return "'rescue'";
|
543
|
+
return "'rescue' modifier";
|
543
544
|
case PM_TOKEN_KEYWORD_RETRY:
|
544
545
|
return "'retry'";
|
545
546
|
case PM_TOKEN_KEYWORD_RETURN:
|
@@ -589,9 +590,9 @@ pm_token_type_human(pm_token_type_t token_type) {
|
|
589
590
|
case PM_TOKEN_LESS_EQUAL_GREATER:
|
590
591
|
return "'<=>'";
|
591
592
|
case PM_TOKEN_LESS_LESS:
|
592
|
-
return "
|
593
|
+
return "<<";
|
593
594
|
case PM_TOKEN_LESS_LESS_EQUAL:
|
594
|
-
return "
|
595
|
+
return "<<=";
|
595
596
|
case PM_TOKEN_METHOD_NAME:
|
596
597
|
return "method name";
|
597
598
|
case PM_TOKEN_MINUS:
|
@@ -657,13 +658,13 @@ pm_token_type_human(pm_token_type_t token_type) {
|
|
657
658
|
case PM_TOKEN_STAR_STAR_EQUAL:
|
658
659
|
return "'**='";
|
659
660
|
case PM_TOKEN_STRING_BEGIN:
|
660
|
-
return "string
|
661
|
+
return "string literal";
|
661
662
|
case PM_TOKEN_STRING_CONTENT:
|
662
663
|
return "string content";
|
663
664
|
case PM_TOKEN_STRING_END:
|
664
665
|
return "string ending";
|
665
666
|
case PM_TOKEN_SYMBOL_BEGIN:
|
666
|
-
return "symbol
|
667
|
+
return "symbol literal";
|
667
668
|
case PM_TOKEN_TILDE:
|
668
669
|
return "'~'";
|
669
670
|
case PM_TOKEN_UAMPERSAND:
|
@@ -683,7 +684,7 @@ pm_token_type_human(pm_token_type_t token_type) {
|
|
683
684
|
case PM_TOKEN_USTAR:
|
684
685
|
return "*";
|
685
686
|
case PM_TOKEN_USTAR_STAR:
|
686
|
-
return "
|
687
|
+
return "**";
|
687
688
|
case PM_TOKEN_WORDS_SEP:
|
688
689
|
return "string separator";
|
689
690
|
case PM_TOKEN___END__:
|
data/src/util/pm_buffer.c
CHANGED
@@ -16,7 +16,7 @@ pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity) {
|
|
16
16
|
buffer->length = 0;
|
17
17
|
buffer->capacity = capacity;
|
18
18
|
|
19
|
-
buffer->value = (char *)
|
19
|
+
buffer->value = (char *) xmalloc(capacity);
|
20
20
|
return buffer->value != NULL;
|
21
21
|
}
|
22
22
|
|
@@ -32,7 +32,7 @@ pm_buffer_init(pm_buffer_t *buffer) {
|
|
32
32
|
* Return the value of the buffer.
|
33
33
|
*/
|
34
34
|
char *
|
35
|
-
pm_buffer_value(pm_buffer_t *buffer) {
|
35
|
+
pm_buffer_value(const pm_buffer_t *buffer) {
|
36
36
|
return buffer->value;
|
37
37
|
}
|
38
38
|
|
@@ -40,14 +40,14 @@ pm_buffer_value(pm_buffer_t *buffer) {
|
|
40
40
|
* Return the length of the buffer.
|
41
41
|
*/
|
42
42
|
size_t
|
43
|
-
pm_buffer_length(pm_buffer_t *buffer) {
|
43
|
+
pm_buffer_length(const pm_buffer_t *buffer) {
|
44
44
|
return buffer->length;
|
45
45
|
}
|
46
46
|
|
47
47
|
/**
|
48
48
|
* Append the given amount of space to the buffer.
|
49
49
|
*/
|
50
|
-
static inline
|
50
|
+
static inline bool
|
51
51
|
pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
|
52
52
|
size_t next_length = buffer->length + length;
|
53
53
|
|
@@ -60,10 +60,12 @@ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
|
|
60
60
|
buffer->capacity *= 2;
|
61
61
|
}
|
62
62
|
|
63
|
-
buffer->value =
|
63
|
+
buffer->value = xrealloc(buffer->value, buffer->capacity);
|
64
|
+
if (buffer->value == NULL) return false;
|
64
65
|
}
|
65
66
|
|
66
67
|
buffer->length = next_length;
|
68
|
+
return true;
|
67
69
|
}
|
68
70
|
|
69
71
|
/**
|
@@ -72,8 +74,9 @@ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
|
|
72
74
|
static inline void
|
73
75
|
pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) {
|
74
76
|
size_t cursor = buffer->length;
|
75
|
-
pm_buffer_append_length(buffer, length)
|
76
|
-
|
77
|
+
if (pm_buffer_append_length(buffer, length)) {
|
78
|
+
memcpy(buffer->value + cursor, source, length);
|
79
|
+
}
|
77
80
|
}
|
78
81
|
|
79
82
|
/**
|
@@ -82,8 +85,9 @@ pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) {
|
|
82
85
|
void
|
83
86
|
pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length) {
|
84
87
|
size_t cursor = buffer->length;
|
85
|
-
pm_buffer_append_length(buffer, length)
|
86
|
-
|
88
|
+
if (pm_buffer_append_length(buffer, length)) {
|
89
|
+
memset(buffer->value + cursor, 0, length);
|
90
|
+
}
|
87
91
|
}
|
88
92
|
|
89
93
|
/**
|
@@ -100,13 +104,12 @@ pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) {
|
|
100
104
|
size_t length = (size_t) (result + 1);
|
101
105
|
|
102
106
|
size_t cursor = buffer->length;
|
103
|
-
pm_buffer_append_length(buffer, length)
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
buffer->length--;
|
107
|
+
if (pm_buffer_append_length(buffer, length)) {
|
108
|
+
va_start(arguments, format);
|
109
|
+
vsnprintf(buffer->value + cursor, length, format, arguments);
|
110
|
+
va_end(arguments);
|
111
|
+
buffer->length--;
|
112
|
+
}
|
110
113
|
}
|
111
114
|
|
112
115
|
/**
|
@@ -160,15 +163,135 @@ pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value) {
|
|
160
163
|
pm_buffer_append_varuint(buffer, unsigned_int);
|
161
164
|
}
|
162
165
|
|
166
|
+
/**
|
167
|
+
* Append a double to the buffer.
|
168
|
+
*/
|
169
|
+
void
|
170
|
+
pm_buffer_append_double(pm_buffer_t *buffer, double value) {
|
171
|
+
const void *source = &value;
|
172
|
+
pm_buffer_append(buffer, source, sizeof(double));
|
173
|
+
}
|
174
|
+
|
175
|
+
/**
|
176
|
+
* Append a unicode codepoint to the buffer.
|
177
|
+
*/
|
178
|
+
bool
|
179
|
+
pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value) {
|
180
|
+
if (value <= 0x7F) {
|
181
|
+
pm_buffer_append_byte(buffer, (uint8_t) value); // 0xxxxxxx
|
182
|
+
return true;
|
183
|
+
} else if (value <= 0x7FF) {
|
184
|
+
uint8_t bytes[] = {
|
185
|
+
(uint8_t) (0xC0 | ((value >> 6) & 0x3F)), // 110xxxxx
|
186
|
+
(uint8_t) (0x80 | (value & 0x3F)) // 10xxxxxx
|
187
|
+
};
|
188
|
+
|
189
|
+
pm_buffer_append_bytes(buffer, bytes, 2);
|
190
|
+
return true;
|
191
|
+
} else if (value <= 0xFFFF) {
|
192
|
+
uint8_t bytes[] = {
|
193
|
+
(uint8_t) (0xE0 | ((value >> 12) & 0x3F)), // 1110xxxx
|
194
|
+
(uint8_t) (0x80 | ((value >> 6) & 0x3F)), // 10xxxxxx
|
195
|
+
(uint8_t) (0x80 | (value & 0x3F)) // 10xxxxxx
|
196
|
+
};
|
197
|
+
|
198
|
+
pm_buffer_append_bytes(buffer, bytes, 3);
|
199
|
+
return true;
|
200
|
+
} else if (value <= 0x10FFFF) {
|
201
|
+
uint8_t bytes[] = {
|
202
|
+
(uint8_t) (0xF0 | ((value >> 18) & 0x3F)), // 11110xxx
|
203
|
+
(uint8_t) (0x80 | ((value >> 12) & 0x3F)), // 10xxxxxx
|
204
|
+
(uint8_t) (0x80 | ((value >> 6) & 0x3F)), // 10xxxxxx
|
205
|
+
(uint8_t) (0x80 | (value & 0x3F)) // 10xxxxxx
|
206
|
+
};
|
207
|
+
|
208
|
+
pm_buffer_append_bytes(buffer, bytes, 4);
|
209
|
+
return true;
|
210
|
+
} else {
|
211
|
+
return false;
|
212
|
+
}
|
213
|
+
}
|
214
|
+
|
215
|
+
/**
|
216
|
+
* Append a slice of source code to the buffer.
|
217
|
+
*/
|
218
|
+
void
|
219
|
+
pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping) {
|
220
|
+
for (size_t index = 0; index < length; index++) {
|
221
|
+
const uint8_t byte = source[index];
|
222
|
+
|
223
|
+
if ((byte <= 0x06) || (byte >= 0x0E && byte <= 0x1F) || (byte >= 0x7F)) {
|
224
|
+
if (escaping == PM_BUFFER_ESCAPING_RUBY) {
|
225
|
+
pm_buffer_append_format(buffer, "\\x%02X", byte);
|
226
|
+
} else {
|
227
|
+
pm_buffer_append_format(buffer, "\\u%04X", byte);
|
228
|
+
}
|
229
|
+
} else {
|
230
|
+
switch (byte) {
|
231
|
+
case '\a':
|
232
|
+
if (escaping == PM_BUFFER_ESCAPING_RUBY) {
|
233
|
+
pm_buffer_append_string(buffer, "\\a", 2);
|
234
|
+
} else {
|
235
|
+
pm_buffer_append_format(buffer, "\\u%04X", byte);
|
236
|
+
}
|
237
|
+
break;
|
238
|
+
case '\b':
|
239
|
+
pm_buffer_append_string(buffer, "\\b", 2);
|
240
|
+
break;
|
241
|
+
case '\t':
|
242
|
+
pm_buffer_append_string(buffer, "\\t", 2);
|
243
|
+
break;
|
244
|
+
case '\n':
|
245
|
+
pm_buffer_append_string(buffer, "\\n", 2);
|
246
|
+
break;
|
247
|
+
case '\v':
|
248
|
+
if (escaping == PM_BUFFER_ESCAPING_RUBY) {
|
249
|
+
pm_buffer_append_string(buffer, "\\v", 2);
|
250
|
+
} else {
|
251
|
+
pm_buffer_append_format(buffer, "\\u%04X", byte);
|
252
|
+
}
|
253
|
+
break;
|
254
|
+
case '\f':
|
255
|
+
pm_buffer_append_string(buffer, "\\f", 2);
|
256
|
+
break;
|
257
|
+
case '\r':
|
258
|
+
pm_buffer_append_string(buffer, "\\r", 2);
|
259
|
+
break;
|
260
|
+
case '"':
|
261
|
+
pm_buffer_append_string(buffer, "\\\"", 2);
|
262
|
+
break;
|
263
|
+
case '#': {
|
264
|
+
if (escaping == PM_BUFFER_ESCAPING_RUBY && index + 1 < length) {
|
265
|
+
const uint8_t next_byte = source[index + 1];
|
266
|
+
if (next_byte == '{' || next_byte == '@' || next_byte == '$') {
|
267
|
+
pm_buffer_append_byte(buffer, '\\');
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
pm_buffer_append_byte(buffer, '#');
|
272
|
+
break;
|
273
|
+
}
|
274
|
+
case '\\':
|
275
|
+
pm_buffer_append_string(buffer, "\\\\", 2);
|
276
|
+
break;
|
277
|
+
default:
|
278
|
+
pm_buffer_append_byte(buffer, byte);
|
279
|
+
break;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
}
|
283
|
+
}
|
284
|
+
|
163
285
|
/**
|
164
286
|
* Prepend the given string to the buffer.
|
165
287
|
*/
|
166
288
|
void
|
167
289
|
pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length) {
|
168
290
|
size_t cursor = buffer->length;
|
169
|
-
pm_buffer_append_length(buffer, length)
|
170
|
-
|
171
|
-
|
291
|
+
if (pm_buffer_append_length(buffer, length)) {
|
292
|
+
memmove(buffer->value + length, buffer->value, cursor);
|
293
|
+
memcpy(buffer->value, value, length);
|
294
|
+
}
|
172
295
|
}
|
173
296
|
|
174
297
|
/**
|
@@ -181,10 +304,54 @@ pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source) {
|
|
181
304
|
}
|
182
305
|
}
|
183
306
|
|
307
|
+
/**
|
308
|
+
* Clear the buffer by reducing its size to 0. This does not free the allocated
|
309
|
+
* memory, but it does allow the buffer to be reused.
|
310
|
+
*/
|
311
|
+
void
|
312
|
+
pm_buffer_clear(pm_buffer_t *buffer) {
|
313
|
+
buffer->length = 0;
|
314
|
+
}
|
315
|
+
|
316
|
+
/**
|
317
|
+
* Strip the whitespace from the end of the buffer.
|
318
|
+
*/
|
319
|
+
void
|
320
|
+
pm_buffer_rstrip(pm_buffer_t *buffer) {
|
321
|
+
while (buffer->length > 0 && pm_char_is_whitespace((uint8_t) buffer->value[buffer->length - 1])) {
|
322
|
+
buffer->length--;
|
323
|
+
}
|
324
|
+
}
|
325
|
+
|
326
|
+
/**
|
327
|
+
* Checks if the buffer includes the given value.
|
328
|
+
*/
|
329
|
+
size_t
|
330
|
+
pm_buffer_index(const pm_buffer_t *buffer, char value) {
|
331
|
+
const char *first = memchr(buffer->value, value, buffer->length);
|
332
|
+
return (first == NULL) ? SIZE_MAX : (size_t) (first - buffer->value);
|
333
|
+
}
|
334
|
+
|
335
|
+
/**
|
336
|
+
* Insert the given string into the buffer at the given index.
|
337
|
+
*/
|
338
|
+
void
|
339
|
+
pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length) {
|
340
|
+
assert(index <= buffer->length);
|
341
|
+
|
342
|
+
if (index == buffer->length) {
|
343
|
+
pm_buffer_append_string(buffer, value, length);
|
344
|
+
} else {
|
345
|
+
pm_buffer_append_zeroes(buffer, length);
|
346
|
+
memmove(buffer->value + index + length, buffer->value + index, buffer->length - length - index);
|
347
|
+
memcpy(buffer->value + index, value, length);
|
348
|
+
}
|
349
|
+
}
|
350
|
+
|
184
351
|
/**
|
185
352
|
* Free the memory associated with the buffer.
|
186
353
|
*/
|
187
354
|
void
|
188
355
|
pm_buffer_free(pm_buffer_t *buffer) {
|
189
|
-
|
356
|
+
xfree(buffer->value);
|
190
357
|
}
|
data/src/util/pm_char.c
CHANGED
@@ -19,10 +19,10 @@ static const uint8_t pm_byte_table[256] = {
|
|
19
19
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
|
20
20
|
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x
|
21
21
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3x
|
22
|
-
0,
|
23
|
-
|
24
|
-
0,
|
25
|
-
|
22
|
+
0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 4x
|
23
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, // 5x
|
24
|
+
0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 6x
|
25
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, // 7x
|
26
26
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
|
27
27
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x
|
28
28
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax
|
@@ -185,7 +185,7 @@ pm_strspn_number_kind_underscores(const uint8_t *string, ptrdiff_t length, const
|
|
185
185
|
size++;
|
186
186
|
}
|
187
187
|
|
188
|
-
if (string[size - 1] == '_') *invalid = string + size - 1;
|
188
|
+
if (size > 0 && string[size - 1] == '_') *invalid = string + size - 1;
|
189
189
|
return size;
|
190
190
|
}
|
191
191
|
|
data/src/util/pm_constant_pool.c
CHANGED
@@ -10,6 +10,22 @@ pm_constant_id_list_init(pm_constant_id_list_t *list) {
|
|
10
10
|
list->capacity = 0;
|
11
11
|
}
|
12
12
|
|
13
|
+
/**
|
14
|
+
* Initialize a list of constant ids with a given capacity.
|
15
|
+
*/
|
16
|
+
void
|
17
|
+
pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity) {
|
18
|
+
if (capacity) {
|
19
|
+
list->ids = xcalloc(capacity, sizeof(pm_constant_id_t));
|
20
|
+
if (list->ids == NULL) abort();
|
21
|
+
} else {
|
22
|
+
list->ids = NULL;
|
23
|
+
}
|
24
|
+
|
25
|
+
list->size = 0;
|
26
|
+
list->capacity = capacity;
|
27
|
+
}
|
28
|
+
|
13
29
|
/**
|
14
30
|
* Append a constant id to a list of constant ids. Returns false if any
|
15
31
|
* potential reallocations fail.
|
@@ -18,7 +34,7 @@ bool
|
|
18
34
|
pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id) {
|
19
35
|
if (list->size >= list->capacity) {
|
20
36
|
list->capacity = list->capacity == 0 ? 8 : list->capacity * 2;
|
21
|
-
list->ids = (pm_constant_id_t *)
|
37
|
+
list->ids = (pm_constant_id_t *) xrealloc(list->ids, sizeof(pm_constant_id_t) * list->capacity);
|
22
38
|
if (list->ids == NULL) return false;
|
23
39
|
}
|
24
40
|
|
@@ -26,6 +42,18 @@ pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id) {
|
|
26
42
|
return true;
|
27
43
|
}
|
28
44
|
|
45
|
+
/**
|
46
|
+
* Insert a constant id into a list of constant ids at the specified index.
|
47
|
+
*/
|
48
|
+
void
|
49
|
+
pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id) {
|
50
|
+
assert(index < list->capacity);
|
51
|
+
assert(list->ids[index] == PM_CONSTANT_ID_UNSET);
|
52
|
+
|
53
|
+
list->ids[index] = id;
|
54
|
+
list->size++;
|
55
|
+
}
|
56
|
+
|
29
57
|
/**
|
30
58
|
* Checks if the current constant id list includes the given constant id.
|
31
59
|
*/
|
@@ -37,21 +65,13 @@ pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id) {
|
|
37
65
|
return false;
|
38
66
|
}
|
39
67
|
|
40
|
-
/**
|
41
|
-
* Get the memory size of a list of constant ids.
|
42
|
-
*/
|
43
|
-
size_t
|
44
|
-
pm_constant_id_list_memsize(pm_constant_id_list_t *list) {
|
45
|
-
return sizeof(pm_constant_id_list_t) + (list->capacity * sizeof(pm_constant_id_t));
|
46
|
-
}
|
47
|
-
|
48
68
|
/**
|
49
69
|
* Free the memory associated with a list of constant ids.
|
50
70
|
*/
|
51
71
|
void
|
52
72
|
pm_constant_id_list_free(pm_constant_id_list_t *list) {
|
53
73
|
if (list->ids != NULL) {
|
54
|
-
|
74
|
+
xfree(list->ids);
|
55
75
|
}
|
56
76
|
}
|
57
77
|
|
@@ -111,7 +131,7 @@ pm_constant_pool_resize(pm_constant_pool_t *pool) {
|
|
111
131
|
const uint32_t mask = next_capacity - 1;
|
112
132
|
const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
|
113
133
|
|
114
|
-
void *next =
|
134
|
+
void *next = xcalloc(next_capacity, element_size);
|
115
135
|
if (next == NULL) return false;
|
116
136
|
|
117
137
|
pm_constant_pool_bucket_t *next_buckets = next;
|
@@ -145,7 +165,7 @@ pm_constant_pool_resize(pm_constant_pool_t *pool) {
|
|
145
165
|
|
146
166
|
// pool->constants and pool->buckets are allocated out of the same chunk
|
147
167
|
// of memory, with the buckets coming first.
|
148
|
-
|
168
|
+
xfree(pool->buckets);
|
149
169
|
pool->constants = next_constants;
|
150
170
|
pool->buckets = next_buckets;
|
151
171
|
pool->capacity = next_capacity;
|
@@ -162,7 +182,7 @@ pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity) {
|
|
162
182
|
|
163
183
|
capacity = next_power_of_two(capacity);
|
164
184
|
const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
|
165
|
-
void *memory =
|
185
|
+
void *memory = xcalloc(capacity, element_size);
|
166
186
|
if (memory == NULL) return false;
|
167
187
|
|
168
188
|
pool->buckets = memory;
|
@@ -237,12 +257,12 @@ pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t l
|
|
237
257
|
// an existing constant, then either way we don't want the given
|
238
258
|
// memory. Either it's duplicated with the existing constant or
|
239
259
|
// it's not necessary because we have a shared version.
|
240
|
-
|
260
|
+
xfree((void *) start);
|
241
261
|
} else if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
|
242
262
|
// If we're attempting to insert a shared constant and the
|
243
263
|
// existing constant is owned, then we can free the owned
|
244
264
|
// constant and replace it with the shared constant.
|
245
|
-
|
265
|
+
xfree((void *) constant->start);
|
246
266
|
constant->start = start;
|
247
267
|
bucket->type = (unsigned int) (PM_CONSTANT_POOL_BUCKET_DEFAULT & 0x3);
|
248
268
|
}
|
@@ -253,7 +273,7 @@ pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t l
|
|
253
273
|
index = (index + 1) & mask;
|
254
274
|
}
|
255
275
|
|
256
|
-
// IDs are allocated starting at 1, since the value 0 denotes a non-
|
276
|
+
// IDs are allocated starting at 1, since the value 0 denotes a non-existent
|
257
277
|
// constant.
|
258
278
|
uint32_t id = ++pool->size;
|
259
279
|
assert(pool->size < ((uint32_t) (1 << 30)));
|
@@ -287,7 +307,7 @@ pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, s
|
|
287
307
|
* potential calls to resize fail.
|
288
308
|
*/
|
289
309
|
pm_constant_id_t
|
290
|
-
pm_constant_pool_insert_owned(pm_constant_pool_t *pool,
|
310
|
+
pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length) {
|
291
311
|
return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED);
|
292
312
|
}
|
293
313
|
|
@@ -314,9 +334,9 @@ pm_constant_pool_free(pm_constant_pool_t *pool) {
|
|
314
334
|
// If an id is set on this constant, then we know we have content here.
|
315
335
|
if (bucket->id != PM_CONSTANT_ID_UNSET && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
|
316
336
|
pm_constant_t *constant = &pool->constants[bucket->id - 1];
|
317
|
-
|
337
|
+
xfree((void *) constant->start);
|
318
338
|
}
|
319
339
|
}
|
320
340
|
|
321
|
-
|
341
|
+
xfree(pool->buckets);
|
322
342
|
}
|