prism 0.24.0 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/BSDmakefile +58 -0
  3. data/CHANGELOG.md +50 -1
  4. data/Makefile +5 -2
  5. data/README.md +45 -6
  6. data/config.yml +499 -4
  7. data/docs/build_system.md +31 -0
  8. data/docs/configuration.md +2 -0
  9. data/docs/cruby_compilation.md +1 -1
  10. data/docs/parser_translation.md +14 -9
  11. data/docs/releasing.md +2 -2
  12. data/docs/ripper_translation.md +50 -0
  13. data/docs/ruby_api.md +1 -0
  14. data/docs/serialization.md +26 -5
  15. data/ext/prism/api_node.c +911 -815
  16. data/ext/prism/api_pack.c +9 -0
  17. data/ext/prism/extconf.rb +27 -11
  18. data/ext/prism/extension.c +313 -66
  19. data/ext/prism/extension.h +5 -4
  20. data/include/prism/ast.h +213 -64
  21. data/include/prism/defines.h +106 -2
  22. data/include/prism/diagnostic.h +134 -71
  23. data/include/prism/encoding.h +22 -4
  24. data/include/prism/node.h +93 -0
  25. data/include/prism/options.h +82 -7
  26. data/include/prism/pack.h +11 -0
  27. data/include/prism/parser.h +198 -53
  28. data/include/prism/prettyprint.h +8 -0
  29. data/include/prism/static_literals.h +118 -0
  30. data/include/prism/util/pm_buffer.h +65 -2
  31. data/include/prism/util/pm_constant_pool.h +18 -1
  32. data/include/prism/util/pm_integer.h +119 -0
  33. data/include/prism/util/pm_list.h +1 -1
  34. data/include/prism/util/pm_newline_list.h +8 -0
  35. data/include/prism/util/pm_string.h +26 -2
  36. data/include/prism/version.h +2 -2
  37. data/include/prism.h +59 -1
  38. data/lib/prism/compiler.rb +8 -1
  39. data/lib/prism/debug.rb +46 -3
  40. data/lib/prism/desugar_compiler.rb +1 -1
  41. data/lib/prism/dispatcher.rb +29 -0
  42. data/lib/prism/dot_visitor.rb +87 -16
  43. data/lib/prism/dsl.rb +24 -12
  44. data/lib/prism/ffi.rb +67 -12
  45. data/lib/prism/lex_compat.rb +17 -15
  46. data/lib/prism/mutation_compiler.rb +11 -0
  47. data/lib/prism/node.rb +2096 -2499
  48. data/lib/prism/node_ext.rb +77 -29
  49. data/lib/prism/pack.rb +4 -0
  50. data/lib/prism/parse_result/comments.rb +34 -17
  51. data/lib/prism/parse_result/newlines.rb +3 -1
  52. data/lib/prism/parse_result.rb +78 -32
  53. data/lib/prism/pattern.rb +16 -4
  54. data/lib/prism/polyfill/string.rb +12 -0
  55. data/lib/prism/serialize.rb +439 -102
  56. data/lib/prism/translation/parser/compiler.rb +152 -50
  57. data/lib/prism/translation/parser/lexer.rb +103 -22
  58. data/lib/prism/translation/parser/rubocop.rb +41 -13
  59. data/lib/prism/translation/parser.rb +119 -7
  60. data/lib/prism/translation/parser33.rb +1 -1
  61. data/lib/prism/translation/parser34.rb +1 -1
  62. data/lib/prism/translation/ripper/sexp.rb +125 -0
  63. data/lib/prism/translation/ripper/shim.rb +5 -0
  64. data/lib/prism/translation/ripper.rb +3212 -462
  65. data/lib/prism/translation/ruby_parser.rb +35 -18
  66. data/lib/prism/translation.rb +3 -1
  67. data/lib/prism/visitor.rb +10 -0
  68. data/lib/prism.rb +8 -2
  69. data/prism.gemspec +33 -4
  70. data/rbi/prism/compiler.rbi +14 -0
  71. data/rbi/prism/desugar_compiler.rbi +5 -0
  72. data/rbi/prism/mutation_compiler.rbi +5 -0
  73. data/rbi/prism/node.rbi +8221 -0
  74. data/rbi/prism/node_ext.rbi +102 -0
  75. data/rbi/prism/parse_result.rbi +304 -0
  76. data/rbi/prism/translation/parser/compiler.rbi +13 -0
  77. data/rbi/prism/translation/ripper/ripper_compiler.rbi +5 -0
  78. data/rbi/prism/translation/ripper.rbi +25 -0
  79. data/rbi/prism/translation/ruby_parser.rbi +11 -0
  80. data/rbi/prism/visitor.rbi +470 -0
  81. data/rbi/prism.rbi +39 -7749
  82. data/sig/prism/compiler.rbs +9 -0
  83. data/sig/prism/dispatcher.rbs +16 -0
  84. data/sig/prism/dot_visitor.rbs +6 -0
  85. data/sig/prism/dsl.rbs +462 -0
  86. data/sig/prism/mutation_compiler.rbs +158 -0
  87. data/sig/prism/node.rbs +3529 -0
  88. data/sig/prism/node_ext.rbs +78 -0
  89. data/sig/prism/pack.rbs +43 -0
  90. data/sig/prism/parse_result.rbs +127 -0
  91. data/sig/prism/pattern.rbs +13 -0
  92. data/sig/prism/serialize.rbs +7 -0
  93. data/sig/prism/visitor.rbs +168 -0
  94. data/sig/prism.rbs +188 -4767
  95. data/src/diagnostic.c +575 -230
  96. data/src/encoding.c +211 -108
  97. data/src/node.c +7526 -447
  98. data/src/options.c +36 -12
  99. data/src/pack.c +33 -17
  100. data/src/prettyprint.c +1294 -1385
  101. data/src/prism.c +3628 -1099
  102. data/src/regexp.c +17 -2
  103. data/src/serialize.c +47 -28
  104. data/src/static_literals.c +552 -0
  105. data/src/token_type.c +1 -0
  106. data/src/util/pm_buffer.c +147 -20
  107. data/src/util/pm_char.c +4 -4
  108. data/src/util/pm_constant_pool.c +35 -11
  109. data/src/util/pm_integer.c +629 -0
  110. data/src/util/pm_list.c +1 -1
  111. data/src/util/pm_newline_list.c +14 -5
  112. data/src/util/pm_string.c +134 -5
  113. data/src/util/pm_string_list.c +2 -2
  114. metadata +35 -6
  115. data/docs/ripper.md +0 -36
  116. data/rbi/prism_static.rbi +0 -207
  117. data/sig/prism_static.rbs +0 -201
data/src/options.c CHANGED
@@ -29,7 +29,15 @@ pm_options_line_set(pm_options_t *options, int32_t line) {
29
29
  */
30
30
  PRISM_EXPORTED_FUNCTION void
31
31
  pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) {
32
- options->frozen_string_literal = frozen_string_literal;
32
+ options->frozen_string_literal = frozen_string_literal ? PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED : PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED;
33
+ }
34
+
35
+ /**
36
+ * Sets the command line option on the given options struct.
37
+ */
38
+ PRISM_EXPORTED_FUNCTION void
39
+ pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
40
+ options->command_line = command_line;
33
41
  }
34
42
 
35
43
  /**
@@ -64,14 +72,22 @@ pm_options_version_set(pm_options_t *options, const char *version, size_t length
64
72
  return false;
65
73
  }
66
74
 
75
+ // For some reason, GCC analyzer thinks we're leaking allocated scopes and
76
+ // locals here, even though we definitely aren't. This is a false positive.
77
+ // Ideally we wouldn't need to suppress this.
78
+ #if defined(__GNUC__) && (__GNUC__ >= 10)
79
+ #pragma GCC diagnostic push
80
+ #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
81
+ #endif
82
+
67
83
  /**
68
84
  * Allocate and zero out the scopes array on the given options struct.
69
85
  */
70
- PRISM_EXPORTED_FUNCTION void
86
+ PRISM_EXPORTED_FUNCTION bool
71
87
  pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
72
88
  options->scopes_count = scopes_count;
73
- options->scopes = calloc(scopes_count, sizeof(pm_options_scope_t));
74
- if (options->scopes == NULL) abort();
89
+ options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t));
90
+ return options->scopes != NULL;
75
91
  }
76
92
 
77
93
  /**
@@ -86,11 +102,11 @@ pm_options_scope_get(const pm_options_t *options, size_t index) {
86
102
  * Create a new options scope struct. This will hold a set of locals that are in
87
103
  * scope surrounding the code that is being parsed.
88
104
  */
89
- PRISM_EXPORTED_FUNCTION void
105
+ PRISM_EXPORTED_FUNCTION bool
90
106
  pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
91
107
  scope->locals_count = locals_count;
92
- scope->locals = calloc(locals_count, sizeof(pm_string_t));
93
- if (scope->locals == NULL) abort();
108
+ scope->locals = xcalloc(locals_count, sizeof(pm_string_t));
109
+ return scope->locals != NULL;
94
110
  }
95
111
 
96
112
  /**
@@ -116,10 +132,10 @@ pm_options_free(pm_options_t *options) {
116
132
  pm_string_free(&scope->locals[local_index]);
117
133
  }
118
134
 
119
- free(scope->locals);
135
+ xfree(scope->locals);
120
136
  }
121
137
 
122
- free(options->scopes);
138
+ xfree(options->scopes);
123
139
  }
124
140
 
125
141
  /**
@@ -185,21 +201,25 @@ pm_options_read(pm_options_t *options, const char *data) {
185
201
  data += encoding_length;
186
202
  }
187
203
 
188
- options->frozen_string_literal = *data++;
204
+ options->frozen_string_literal = (int8_t) *data++;
205
+ options->command_line = (uint8_t) *data++;
189
206
  options->version = (pm_options_version_t) *data++;
190
207
 
191
208
  uint32_t scopes_count = pm_options_read_u32(data);
192
209
  data += 4;
193
210
 
194
211
  if (scopes_count > 0) {
195
- pm_options_scopes_init(options, scopes_count);
212
+ if (!pm_options_scopes_init(options, scopes_count)) return;
196
213
 
197
214
  for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
198
215
  uint32_t locals_count = pm_options_read_u32(data);
199
216
  data += 4;
200
217
 
201
218
  pm_options_scope_t *scope = &options->scopes[scope_index];
202
- pm_options_scope_init(scope, locals_count);
219
+ if (!pm_options_scope_init(scope, locals_count)) {
220
+ pm_options_free(options);
221
+ return;
222
+ }
203
223
 
204
224
  for (size_t local_index = 0; local_index < locals_count; local_index++) {
205
225
  uint32_t local_length = pm_options_read_u32(data);
@@ -211,3 +231,7 @@ pm_options_read(pm_options_t *options, const char *data) {
211
231
  }
212
232
  }
213
233
  }
234
+
235
+ #if defined(__GNUC__) && (__GNUC__ >= 10)
236
+ #pragma GCC diagnostic pop
237
+ #endif
data/src/pack.c CHANGED
@@ -1,16 +1,43 @@
1
1
  #include "prism/pack.h"
2
2
 
3
+ // We optionally support parsing String#pack templates. For systems that don't
4
+ // want or need this functionality, it can be turned off with the
5
+ // PRISM_EXCLUDE_PACK define.
6
+ #ifdef PRISM_EXCLUDE_PACK
7
+
8
+ void pm_pack_parse(void) {}
9
+
10
+ #else
11
+
3
12
  #include <stdbool.h>
4
13
  #include <errno.h>
5
14
 
6
15
  static uintmax_t
7
- strtoumaxc(const char **format);
16
+ strtoumaxc(const char **format) {
17
+ uintmax_t value = 0;
18
+ while (**format >= '0' && **format <= '9') {
19
+ if (value > UINTMAX_MAX / 10) {
20
+ errno = ERANGE;
21
+ }
22
+ value = value * 10 + ((uintmax_t) (**format - '0'));
23
+ (*format)++;
24
+ }
25
+ return value;
26
+ }
8
27
 
9
28
  PRISM_EXPORTED_FUNCTION pm_pack_result
10
- pm_pack_parse(pm_pack_variant variant, const char **format, const char *format_end,
11
- pm_pack_type *type, pm_pack_signed *signed_type, pm_pack_endian *endian, pm_pack_size *size,
12
- pm_pack_length_type *length_type, uint64_t *length, pm_pack_encoding *encoding) {
13
-
29
+ pm_pack_parse(
30
+ pm_pack_variant variant,
31
+ const char **format,
32
+ const char *format_end,
33
+ pm_pack_type *type,
34
+ pm_pack_signed *signed_type,
35
+ pm_pack_endian *endian,
36
+ pm_pack_size *size,
37
+ pm_pack_length_type *length_type,
38
+ uint64_t *length,
39
+ pm_pack_encoding *encoding
40
+ ) {
14
41
  if (*encoding == PM_PACK_ENCODING_START) {
15
42
  *encoding = PM_PACK_ENCODING_US_ASCII;
16
43
  }
@@ -479,15 +506,4 @@ pm_size_to_native(pm_pack_size size) {
479
506
  }
480
507
  }
481
508
 
482
- static uintmax_t
483
- strtoumaxc(const char **format) {
484
- uintmax_t value = 0;
485
- while (**format >= '0' && **format <= '9') {
486
- if (value > UINTMAX_MAX / 10) {
487
- errno = ERANGE;
488
- }
489
- value = value * 10 + ((uintmax_t) (**format - '0'));
490
- (*format)++;
491
- }
492
- return value;
493
- }
509
+ #endif