prism 0.29.0 → 1.3.0

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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +115 -1
  3. data/CONTRIBUTING.md +0 -4
  4. data/Makefile +1 -1
  5. data/README.md +4 -0
  6. data/config.yml +920 -148
  7. data/docs/build_system.md +8 -11
  8. data/docs/fuzzing.md +1 -1
  9. data/docs/parsing_rules.md +4 -1
  10. data/docs/relocation.md +34 -0
  11. data/docs/ripper_translation.md +22 -0
  12. data/docs/serialization.md +3 -0
  13. data/ext/prism/api_node.c +2863 -2079
  14. data/ext/prism/extconf.rb +14 -37
  15. data/ext/prism/extension.c +241 -391
  16. data/ext/prism/extension.h +2 -2
  17. data/include/prism/ast.h +2156 -453
  18. data/include/prism/defines.h +58 -7
  19. data/include/prism/diagnostic.h +24 -6
  20. data/include/prism/node.h +0 -21
  21. data/include/prism/options.h +94 -3
  22. data/include/prism/parser.h +82 -40
  23. data/include/prism/regexp.h +18 -8
  24. data/include/prism/static_literals.h +3 -2
  25. data/include/prism/util/pm_char.h +1 -2
  26. data/include/prism/util/pm_constant_pool.h +0 -8
  27. data/include/prism/util/pm_integer.h +22 -15
  28. data/include/prism/util/pm_newline_list.h +11 -0
  29. data/include/prism/util/pm_string.h +28 -12
  30. data/include/prism/version.h +3 -3
  31. data/include/prism.h +47 -11
  32. data/lib/prism/compiler.rb +3 -0
  33. data/lib/prism/desugar_compiler.rb +111 -74
  34. data/lib/prism/dispatcher.rb +16 -1
  35. data/lib/prism/dot_visitor.rb +55 -34
  36. data/lib/prism/dsl.rb +660 -468
  37. data/lib/prism/ffi.rb +113 -8
  38. data/lib/prism/inspect_visitor.rb +296 -64
  39. data/lib/prism/lex_compat.rb +1 -1
  40. data/lib/prism/mutation_compiler.rb +11 -6
  41. data/lib/prism/node.rb +4262 -5023
  42. data/lib/prism/node_ext.rb +91 -14
  43. data/lib/prism/parse_result/comments.rb +0 -7
  44. data/lib/prism/parse_result/errors.rb +65 -0
  45. data/lib/prism/parse_result/newlines.rb +101 -11
  46. data/lib/prism/parse_result.rb +183 -6
  47. data/lib/prism/reflection.rb +12 -10
  48. data/lib/prism/relocation.rb +504 -0
  49. data/lib/prism/serialize.rb +496 -609
  50. data/lib/prism/string_query.rb +30 -0
  51. data/lib/prism/translation/parser/compiler.rb +185 -155
  52. data/lib/prism/translation/parser/lexer.rb +26 -4
  53. data/lib/prism/translation/parser.rb +9 -4
  54. data/lib/prism/translation/ripper.rb +23 -25
  55. data/lib/prism/translation/ruby_parser.rb +86 -17
  56. data/lib/prism/visitor.rb +3 -0
  57. data/lib/prism.rb +6 -8
  58. data/prism.gemspec +9 -5
  59. data/rbi/prism/dsl.rbi +521 -0
  60. data/rbi/prism/node.rbi +1115 -1120
  61. data/rbi/prism/parse_result.rbi +29 -0
  62. data/rbi/prism/string_query.rbi +12 -0
  63. data/rbi/prism/visitor.rbi +3 -0
  64. data/rbi/prism.rbi +36 -30
  65. data/sig/prism/dsl.rbs +190 -303
  66. data/sig/prism/mutation_compiler.rbs +1 -0
  67. data/sig/prism/node.rbs +678 -632
  68. data/sig/prism/parse_result.rbs +22 -0
  69. data/sig/prism/relocation.rbs +185 -0
  70. data/sig/prism/string_query.rbs +11 -0
  71. data/sig/prism/visitor.rbs +1 -0
  72. data/sig/prism.rbs +103 -64
  73. data/src/diagnostic.c +64 -28
  74. data/src/node.c +502 -1739
  75. data/src/options.c +76 -27
  76. data/src/prettyprint.c +188 -112
  77. data/src/prism.c +3376 -2293
  78. data/src/regexp.c +208 -71
  79. data/src/serialize.c +182 -50
  80. data/src/static_literals.c +64 -85
  81. data/src/token_type.c +4 -4
  82. data/src/util/pm_char.c +1 -1
  83. data/src/util/pm_constant_pool.c +0 -8
  84. data/src/util/pm_integer.c +53 -25
  85. data/src/util/pm_newline_list.c +29 -0
  86. data/src/util/pm_string.c +131 -80
  87. data/src/util/pm_strpbrk.c +32 -6
  88. metadata +11 -7
  89. data/include/prism/util/pm_string_list.h +0 -44
  90. data/lib/prism/debug.rb +0 -249
  91. data/lib/prism/translation/parser/rubocop.rb +0 -73
  92. data/src/util/pm_string_list.c +0 -28
data/src/options.c CHANGED
@@ -1,5 +1,14 @@
1
1
  #include "prism/options.h"
2
2
 
3
+ /**
4
+ * Set the shebang callback option on the given options struct.
5
+ */
6
+ PRISM_EXPORTED_FUNCTION void
7
+ pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data) {
8
+ options->shebang_callback = shebang_callback;
9
+ options->shebang_callback_data = shebang_callback_data;
10
+ }
11
+
3
12
  /**
4
13
  * Set the filepath option on the given options struct.
5
14
  */
@@ -16,6 +25,14 @@ pm_options_encoding_set(pm_options_t *options, const char *encoding) {
16
25
  pm_string_constant_init(&options->encoding, encoding, strlen(encoding));
17
26
  }
18
27
 
28
+ /**
29
+ * Set the encoding_locked option on the given options struct.
30
+ */
31
+ PRISM_EXPORTED_FUNCTION void
32
+ pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked) {
33
+ options->encoding_locked = encoding_locked;
34
+ }
35
+
19
36
  /**
20
37
  * Set the line option on the given options struct.
21
38
  */
@@ -40,6 +57,14 @@ pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
40
57
  options->command_line = command_line;
41
58
  }
42
59
 
60
+ /**
61
+ * Checks if the given slice represents a number.
62
+ */
63
+ static inline bool
64
+ is_number(const char *string, size_t length) {
65
+ return pm_strspn_decimal_digit((const uint8_t *) string, (ptrdiff_t) length) == length;
66
+ }
67
+
43
68
  /**
44
69
  * Set the version option on the given options struct by parsing the given
45
70
  * string. If the string contains an invalid option, this returns false.
@@ -47,40 +72,61 @@ pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
47
72
  */
48
73
  PRISM_EXPORTED_FUNCTION bool
49
74
  pm_options_version_set(pm_options_t *options, const char *version, size_t length) {
50
- switch (length) {
51
- case 0:
52
- if (version == NULL) {
53
- options->version = PM_OPTIONS_VERSION_LATEST;
54
- return true;
55
- }
75
+ if (version == NULL) {
76
+ options->version = PM_OPTIONS_VERSION_LATEST;
77
+ return true;
78
+ }
56
79
 
57
- return false;
58
- case 5:
59
- assert(version != NULL);
80
+ if (length == 3) {
81
+ if (strncmp(version, "3.3", 3) == 0) {
82
+ options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
83
+ return true;
84
+ }
60
85
 
61
- if ((strncmp(version, "3.3.0", length) == 0) || (strncmp(version, "3.3.1", length) == 0)) {
62
- options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
63
- return true;
64
- }
86
+ if (strncmp(version, "3.4", 3) == 0) {
87
+ options->version = PM_OPTIONS_VERSION_LATEST;
88
+ return true;
89
+ }
65
90
 
66
- if (strncmp(version, "3.4.0", length) == 0) {
67
- options->version = PM_OPTIONS_VERSION_LATEST;
68
- return true;
69
- }
91
+ return false;
92
+ }
70
93
 
71
- return false;
72
- case 6:
73
- assert(version != NULL);
94
+ if (length >= 4) {
95
+ if (strncmp(version, "3.3.", 4) == 0 && is_number(version + 4, length - 4)) {
96
+ options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
97
+ return true;
98
+ }
74
99
 
75
- if (strncmp(version, "latest", length) == 0) {
76
- options->version = PM_OPTIONS_VERSION_LATEST;
77
- return true;
78
- }
100
+ if (strncmp(version, "3.4.", 4) == 0 && is_number(version + 4, length - 4)) {
101
+ options->version = PM_OPTIONS_VERSION_LATEST;
102
+ return true;
103
+ }
104
+ }
79
105
 
80
- return false;
81
- default:
82
- return false;
106
+ if (length >= 6) {
107
+ if (strncmp(version, "latest", 7) == 0) { // 7 to compare the \0 as well
108
+ options->version = PM_OPTIONS_VERSION_LATEST;
109
+ return true;
110
+ }
83
111
  }
112
+
113
+ return false;
114
+ }
115
+
116
+ /**
117
+ * Set the main script option on the given options struct.
118
+ */
119
+ PRISM_EXPORTED_FUNCTION void
120
+ pm_options_main_script_set(pm_options_t *options, bool main_script) {
121
+ options->main_script = main_script;
122
+ }
123
+
124
+ /**
125
+ * Set the partial script option on the given options struct.
126
+ */
127
+ PRISM_EXPORTED_FUNCTION void
128
+ pm_options_partial_script_set(pm_options_t *options, bool partial_script) {
129
+ options->partial_script = partial_script;
84
130
  }
85
131
 
86
132
  // For some reason, GCC analyzer thinks we're leaking allocated scopes and
@@ -215,6 +261,9 @@ pm_options_read(pm_options_t *options, const char *data) {
215
261
  options->frozen_string_literal = (int8_t) *data++;
216
262
  options->command_line = (uint8_t) *data++;
217
263
  options->version = (pm_options_version_t) *data++;
264
+ options->encoding_locked = ((uint8_t) *data++) > 0;
265
+ options->main_script = ((uint8_t) *data++) > 0;
266
+ options->partial_script = ((uint8_t) *data++) > 0;
218
267
 
219
268
  uint32_t scopes_count = pm_options_read_u32(data);
220
269
  data += 4;