prism 0.30.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/README.md +3 -1
  4. data/config.yml +185 -126
  5. data/docs/serialization.md +3 -0
  6. data/ext/prism/api_node.c +2843 -2085
  7. data/ext/prism/extconf.rb +1 -1
  8. data/ext/prism/extension.c +35 -25
  9. data/ext/prism/extension.h +2 -2
  10. data/include/prism/ast.h +1048 -69
  11. data/include/prism/defines.h +9 -0
  12. data/include/prism/diagnostic.h +11 -3
  13. data/include/prism/options.h +55 -1
  14. data/include/prism/parser.h +27 -3
  15. data/include/prism/regexp.h +2 -1
  16. data/include/prism/util/pm_integer.h +6 -6
  17. data/include/prism/util/pm_newline_list.h +11 -0
  18. data/include/prism/util/pm_string.h +1 -0
  19. data/include/prism/version.h +3 -3
  20. data/lib/prism/desugar_compiler.rb +111 -74
  21. data/lib/prism/dispatcher.rb +2 -1
  22. data/lib/prism/dot_visitor.rb +21 -31
  23. data/lib/prism/dsl.rb +656 -471
  24. data/lib/prism/ffi.rb +3 -0
  25. data/lib/prism/inspect_visitor.rb +285 -57
  26. data/lib/prism/mutation_compiler.rb +5 -5
  27. data/lib/prism/node.rb +2282 -4754
  28. data/lib/prism/node_ext.rb +72 -11
  29. data/lib/prism/parse_result/errors.rb +65 -0
  30. data/lib/prism/parse_result/newlines.rb +28 -28
  31. data/lib/prism/parse_result.rb +25 -2
  32. data/lib/prism/reflection.rb +7 -7
  33. data/lib/prism/serialize.rb +468 -610
  34. data/lib/prism/translation/parser/compiler.rb +18 -18
  35. data/lib/prism/translation/parser/lexer.rb +1 -1
  36. data/lib/prism/translation/parser.rb +3 -3
  37. data/lib/prism/translation/ripper.rb +14 -14
  38. data/lib/prism/translation/ruby_parser.rb +43 -7
  39. data/prism.gemspec +3 -1
  40. data/rbi/prism/dsl.rbi +521 -0
  41. data/rbi/prism/node.rbi +1456 -5616
  42. data/rbi/prism.rbi +16 -16
  43. data/sig/prism/dsl.rbs +189 -305
  44. data/sig/prism/node.rbs +702 -603
  45. data/sig/prism/parse_result.rbs +2 -0
  46. data/src/diagnostic.c +22 -6
  47. data/src/node.c +277 -284
  48. data/src/options.c +18 -0
  49. data/src/prettyprint.c +99 -108
  50. data/src/prism.c +1282 -760
  51. data/src/regexp.c +72 -4
  52. data/src/serialize.c +165 -50
  53. data/src/token_type.c +2 -2
  54. data/src/util/pm_integer.c +14 -14
  55. data/src/util/pm_newline_list.c +29 -0
  56. data/src/util/pm_string.c +9 -5
  57. metadata +4 -2
data/src/token_type.c CHANGED
@@ -1,10 +1,10 @@
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
9
  #include <string.h>
10
10
 
@@ -43,7 +43,7 @@ big_add(pm_integer_t *destination, pm_integer_t *left, pm_integer_t *right, uint
43
43
  length++;
44
44
  }
45
45
 
46
- *destination = (pm_integer_t) { 0, length, values, false };
46
+ *destination = (pm_integer_t) { length, values, 0, false };
47
47
  }
48
48
 
49
49
  /**
@@ -87,7 +87,7 @@ big_sub2(pm_integer_t *destination, pm_integer_t *a, pm_integer_t *b, pm_integer
87
87
  }
88
88
 
89
89
  while (a_length > 1 && values[a_length - 1] == 0) a_length--;
90
- *destination = (pm_integer_t) { 0, a_length, values, false };
90
+ *destination = (pm_integer_t) { a_length, values, 0, false };
91
91
  }
92
92
 
93
93
  /**
@@ -130,7 +130,7 @@ karatsuba_multiply(pm_integer_t *destination, pm_integer_t *left, pm_integer_t *
130
130
  }
131
131
 
132
132
  while (length > 1 && values[length - 1] == 0) length--;
133
- *destination = (pm_integer_t) { 0, length, values, false };
133
+ *destination = (pm_integer_t) { length, values, 0, false };
134
134
  return;
135
135
  }
136
136
 
@@ -142,16 +142,16 @@ karatsuba_multiply(pm_integer_t *destination, pm_integer_t *left, pm_integer_t *
142
142
  if (end_offset > right_length) end_offset = right_length;
143
143
 
144
144
  pm_integer_t sliced_left = {
145
- .value = 0,
146
145
  .length = left_length,
147
146
  .values = left_values,
147
+ .value = 0,
148
148
  .negative = false
149
149
  };
150
150
 
151
151
  pm_integer_t sliced_right = {
152
- .value = 0,
153
152
  .length = end_offset - start_offset,
154
153
  .values = right_values + start_offset,
154
+ .value = 0,
155
155
  .negative = false
156
156
  };
157
157
 
@@ -169,15 +169,15 @@ karatsuba_multiply(pm_integer_t *destination, pm_integer_t *left, pm_integer_t *
169
169
  pm_integer_free(&product);
170
170
  }
171
171
 
172
- *destination = (pm_integer_t) { 0, left_length + right_length, values, false };
172
+ *destination = (pm_integer_t) { left_length + right_length, values, 0, false };
173
173
  return;
174
174
  }
175
175
 
176
176
  size_t half = left_length / 2;
177
- pm_integer_t x0 = { 0, half, left_values, false };
178
- pm_integer_t x1 = { 0, left_length - half, left_values + half, false };
179
- pm_integer_t y0 = { 0, half, right_values, false };
180
- pm_integer_t y1 = { 0, right_length - half, right_values + half, false };
177
+ pm_integer_t x0 = { half, left_values, 0, false };
178
+ pm_integer_t x1 = { left_length - half, left_values + half, 0, false };
179
+ pm_integer_t y0 = { half, right_values, 0, false };
180
+ pm_integer_t y1 = { right_length - half, right_values + half, 0, false };
181
181
 
182
182
  pm_integer_t z0 = { 0 };
183
183
  karatsuba_multiply(&z0, &x0, &y0, base);
@@ -229,7 +229,7 @@ karatsuba_multiply(pm_integer_t *destination, pm_integer_t *left, pm_integer_t *
229
229
  pm_integer_free(&y01);
230
230
  pm_integer_free(&xy);
231
231
 
232
- *destination = (pm_integer_t) { 0, length, values, false };
232
+ *destination = (pm_integer_t) { length, values, 0, false };
233
233
  }
234
234
 
235
235
  /**
@@ -323,7 +323,7 @@ pm_integer_normalize(pm_integer_t *integer) {
323
323
  bool negative = integer->negative && value != 0;
324
324
 
325
325
  pm_integer_free(integer);
326
- *integer = (pm_integer_t) { .value = value, .length = 0, .values = NULL, .negative = negative };
326
+ *integer = (pm_integer_t) { .values = NULL, .value = value, .length = 0, .negative = negative };
327
327
  }
328
328
 
329
329
  /**
@@ -412,7 +412,7 @@ pm_integer_parse_powof2(pm_integer_t *integer, uint32_t base, const uint8_t *dig
412
412
  }
413
413
 
414
414
  while (length > 1 && values[length - 1] == 0) length--;
415
- *integer = (pm_integer_t) { .value = 0, .length = length, .values = values, .negative = false };
415
+ *integer = (pm_integer_t) { .length = length, .values = values, .value = 0, .negative = false };
416
416
  pm_integer_normalize(integer);
417
417
  }
418
418
 
@@ -438,7 +438,7 @@ pm_integer_parse_decimal(pm_integer_t *integer, const uint8_t *digits, size_t di
438
438
  }
439
439
 
440
440
  // Convert base from 10**9 to 1<<32.
441
- pm_integer_convert_base(integer, &((pm_integer_t) { .value = 0, .length = length, .values = values, .negative = false }), 1000000000, ((uint64_t) 1 << 32));
441
+ pm_integer_convert_base(integer, &((pm_integer_t) { .length = length, .values = values, .value = 0, .negative = false }), 1000000000, ((uint64_t) 1 << 32));
442
442
  xfree(values);
443
443
  }
444
444
 
@@ -54,6 +54,35 @@ pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor) {
54
54
  return true;
55
55
  }
56
56
 
57
+ /**
58
+ * Returns the line of the given offset. If the offset is not in the list, the
59
+ * line of the closest offset less than the given offset is returned.
60
+ */
61
+ int32_t
62
+ pm_newline_list_line(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line) {
63
+ assert(cursor >= list->start);
64
+ size_t offset = (size_t) (cursor - list->start);
65
+
66
+ size_t left = 0;
67
+ size_t right = list->size - 1;
68
+
69
+ while (left <= right) {
70
+ size_t mid = left + (right - left) / 2;
71
+
72
+ if (list->offsets[mid] == offset) {
73
+ return ((int32_t) mid) + start_line;
74
+ }
75
+
76
+ if (list->offsets[mid] < offset) {
77
+ left = mid + 1;
78
+ } else {
79
+ right = mid - 1;
80
+ }
81
+ }
82
+
83
+ return ((int32_t) left) + start_line - 1;
84
+ }
85
+
57
86
  /**
58
87
  * Returns the line and column of the given offset. If the offset is not in the
59
88
  * list, the line and column of the closest offset less than the given offset
data/src/util/pm_string.c CHANGED
@@ -116,6 +116,13 @@ pm_string_mapped_init(pm_string_t *string, const char *filepath) {
116
116
  return false;
117
117
  }
118
118
 
119
+ // Ensure it is a file and not a directory
120
+ if (S_ISDIR(sb.st_mode)) {
121
+ errno = EISDIR;
122
+ close(fd);
123
+ return false;
124
+ }
125
+
119
126
  // mmap the file descriptor to virtually get the contents
120
127
  size_t size = (size_t) sb.st_size;
121
128
  uint8_t *source = NULL;
@@ -136,10 +143,7 @@ pm_string_mapped_init(pm_string_t *string, const char *filepath) {
136
143
  *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size };
137
144
  return true;
138
145
  #else
139
- (void) string;
140
- (void) filepath;
141
- perror("pm_string_mapped_init is not implemented for this platform");
142
- return false;
146
+ return pm_string_file_init(string, filepath);
143
147
  #endif
144
148
  }
145
149
 
@@ -198,7 +202,7 @@ pm_string_file_init(pm_string_t *string, const char *filepath) {
198
202
  CloseHandle(file);
199
203
  *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = (size_t) file_size };
200
204
  return true;
201
- #elif defined(_POSIX_MAPPED_FILES)
205
+ #elif defined(PRISM_HAS_FILESYSTEM)
202
206
  FILE *file = fopen(filepath, "rb");
203
207
  if (file == NULL) {
204
208
  return false;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prism
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-07 00:00:00.000000000 Z
11
+ date: 2024-08-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -87,6 +87,7 @@ files:
87
87
  - lib/prism/pack.rb
88
88
  - lib/prism/parse_result.rb
89
89
  - lib/prism/parse_result/comments.rb
90
+ - lib/prism/parse_result/errors.rb
90
91
  - lib/prism/parse_result/newlines.rb
91
92
  - lib/prism/pattern.rb
92
93
  - lib/prism/polyfill/byteindex.rb
@@ -108,6 +109,7 @@ files:
108
109
  - prism.gemspec
109
110
  - rbi/prism.rbi
110
111
  - rbi/prism/compiler.rbi
112
+ - rbi/prism/dsl.rbi
111
113
  - rbi/prism/inspect_visitor.rbi
112
114
  - rbi/prism/node.rbi
113
115
  - rbi/prism/node_ext.rbi