prism 2.0.0.rc2-x86_64-linux-gnu → 2.1.0-x86_64-linux-gnu

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -41
  3. data/Makefile +8 -8
  4. data/README.md +3 -1
  5. data/docs/fuzzing.md +5 -5
  6. data/docs/serialization.md +19 -6
  7. data/ext/prism/api_node.c +80 -80
  8. data/ext/prism/extension.c +34 -67
  9. data/ext/prism/extension.h +1 -1
  10. data/include/prism/arena.h +8 -0
  11. data/include/prism/arena.hpp +42 -0
  12. data/include/prism/ast.h +11 -1
  13. data/include/prism/ast.hpp +641 -0
  14. data/include/prism/buffer.h +8 -0
  15. data/include/prism/buffer.hpp +55 -0
  16. data/include/prism/comments.h +17 -0
  17. data/include/prism/compiler/branch.h +26 -0
  18. data/include/prism/compiler/cplusplus.hpp +16 -0
  19. data/include/prism/compiler/exceptions.hpp +17 -0
  20. data/include/prism/compiler/format.h +2 -0
  21. data/include/prism/compiler/lifetimebound.hpp +25 -0
  22. data/include/prism/constant_pool.h +25 -0
  23. data/include/prism/diagnostic.h +18 -0
  24. data/include/prism/errors_format.h +8 -0
  25. data/include/prism/internal/comments.h +2 -1
  26. data/include/prism/internal/node.h +8 -4
  27. data/include/prism/internal/parser.h +26 -15
  28. data/include/prism/internal/static_literals.h +24 -64
  29. data/include/prism/internal/stringy.h +1 -6
  30. data/include/prism/json.h +8 -0
  31. data/include/prism/line_offset_list.h +8 -0
  32. data/include/prism/magic_comments.h +18 -0
  33. data/include/prism/node.h +55 -10
  34. data/include/prism/node.hpp +272 -0
  35. data/include/prism/node_type.hpp +38 -0
  36. data/include/prism/options.h +10 -1
  37. data/include/prism/options.hpp +43 -0
  38. data/include/prism/parser.h +72 -64
  39. data/include/prism/parser.hpp +274 -0
  40. data/include/prism/prettyprint.h +8 -0
  41. data/include/prism/serialize.h +8 -0
  42. data/include/prism/source.h +8 -0
  43. data/include/prism/stream.h +8 -0
  44. data/include/prism/string_query.h +8 -0
  45. data/include/prism/stringy.h +16 -27
  46. data/include/prism/stringy.hpp +31 -0
  47. data/include/prism/version.h +10 -2
  48. data/include/prism.h +0 -8
  49. data/include/prism.hpp +24 -0
  50. data/lib/prism/3.3/prism.so +0 -0
  51. data/lib/prism/3.4/prism.so +0 -0
  52. data/lib/prism/4.0/prism.so +0 -0
  53. data/lib/prism/serialize.rb +2550 -3282
  54. data/prism.gemspec +14 -1
  55. data/rbi/generated/prism/node.rbi +736 -0
  56. data/rbi/generated/prism/serialize.rbi +6 -5
  57. data/sig/generated/prism/node.rbs +736 -0
  58. data/sig/generated/prism/serialize.rbs +7 -5
  59. data/src/arena.c +6 -1
  60. data/src/comments.c +11 -10
  61. data/src/diagnostic.c +24 -4
  62. data/src/integer.c +6 -1
  63. data/src/node.c +216 -368
  64. data/src/options.c +7 -13
  65. data/src/parser.c +52 -48
  66. data/src/prism.c +222 -240
  67. data/src/regexp.c +0 -1
  68. data/src/serialize.c +2300 -1950
  69. data/src/source.c +8 -2
  70. data/src/static_literals.c +280 -405
  71. data/src/stringy.c +3 -33
  72. metadata +14 -1
@@ -152,9 +152,6 @@ module Prism
152
152
  # : () -> Integer
153
153
  def load_uint64: () -> Integer
154
154
 
155
- # : (ConstantPool constant_pool, Encoding encoding, bool freeze) -> node?
156
- def load_optional_node: (ConstantPool constant_pool, Encoding encoding, bool freeze) -> node?
157
-
158
155
  # : (Encoding encoding) -> String
159
156
  def load_string: (Encoding encoding) -> String
160
157
 
@@ -184,8 +181,13 @@ module Prism
184
181
  # : (ConstantPool constant_pool, Encoding encoding) -> Symbol?
185
182
  def load_optional_constant: (ConstantPool constant_pool, Encoding encoding) -> Symbol?
186
183
 
187
- # : (ConstantPool constant_pool, Encoding encoding, bool freeze) -> node
188
- def load_node: (ConstantPool constant_pool, Encoding encoding, bool freeze) -> node
184
+ # Load the nodes that are serialized before the given offset and return
185
+ # the root. Nodes are serialized in post-order, so the children of each
186
+ # node are the topmost values on the stack, in field order, by the time
187
+ # its record is read.
188
+ #
189
+ # : (ConstantPool constant_pool, Encoding encoding, bool freeze, Integer finish) -> node
190
+ def load_nodes: (ConstantPool constant_pool, Encoding encoding, bool freeze, Integer finish) -> node
189
191
 
190
192
  @load_node_lambdas: Array[Proc]
191
193
 
data/src/arena.c CHANGED
@@ -12,8 +12,13 @@
12
12
  */
13
13
  #define PM_ARENA_BLOCK_SIZE(data_size) (offsetof(pm_arena_block_t, data) + (data_size))
14
14
 
15
- /** Initial block data size: 8 KB. */
15
+ /**
16
+ * Initial block data size: 8 KB. An embedder on a small heap can define a
17
+ * smaller one; a parse of a short program then takes blocks of that size.
18
+ */
19
+ #ifndef PM_ARENA_INITIAL_SIZE
16
20
  #define PM_ARENA_INITIAL_SIZE 8192
21
+ #endif
17
22
 
18
23
  /** Double the block size every this many blocks. */
19
24
  #define PM_ARENA_GROWTH_INTERVAL 8
data/src/comments.c CHANGED
@@ -11,7 +11,10 @@
11
11
 
12
12
  /* The state used while walking the tree to find the owner of each comment. */
13
13
  typedef struct {
14
- /* The arena for memory that is only needed during the walk. */
14
+ /*
15
+ * The arena for memory that is only needed during the walk, which is the
16
+ * scratch arena of the parser.
17
+ */
15
18
  pm_arena_t *arena;
16
19
 
17
20
  /* The start of the source that was parsed. */
@@ -223,7 +226,7 @@ pm_comments_blank_after(const pm_comments_walk_t *walk, uint32_t offset, uint32_
223
226
  * descends itself, so that it can visit children in source order and handle
224
227
  * each node after all of its descendants.
225
228
  */
226
- static bool
229
+ static pm_visit_result_t
227
230
  pm_comments_push(const pm_node_t *node, void *data) {
228
231
  pm_comments_walk_t *walk = (pm_comments_walk_t *) data;
229
232
 
@@ -237,7 +240,7 @@ pm_comments_push(const pm_node_t *node, void *data) {
237
240
  }
238
241
 
239
242
  walk->stack.nodes[walk->stack.size++] = node;
240
- return false;
243
+ return PM_VISIT_SKIP;
241
244
  }
242
245
 
243
246
  /*
@@ -389,15 +392,15 @@ pm_comments_attach(pm_parser_t *parser, const pm_node_t *root) {
389
392
  uint32_t size = (uint32_t) parser->comment_list.size;
390
393
  if (size == 0) return;
391
394
 
392
- pm_arena_t arena = { 0 };
395
+ pm_arena_t *arena = &parser->scratch_arena;
393
396
  pm_comments_walk_t walk = {
394
- .arena = &arena,
397
+ .arena = arena,
395
398
  .source = parser->start,
396
399
  .comments = pm_comments_sort(parser, size),
397
400
  .size = size,
398
- .starts = (uint32_t *) pm_arena_alloc(&arena, sizeof(uint32_t) * size, PRISM_ALIGNOF(uint32_t)),
399
- .attached = (const pm_node_t **) pm_arena_zalloc(&arena, sizeof(pm_node_t *) * size, PRISM_ALIGNOF(pm_node_t *)),
400
- .unclaimed = (uint32_t *) pm_arena_alloc(&arena, sizeof(uint32_t) * (size + 1), PRISM_ALIGNOF(uint32_t))
401
+ .starts = (uint32_t *) pm_arena_alloc(arena, sizeof(uint32_t) * size, PRISM_ALIGNOF(uint32_t)),
402
+ .attached = (const pm_node_t **) pm_arena_zalloc(arena, sizeof(pm_node_t *) * size, PRISM_ALIGNOF(pm_node_t *)),
403
+ .unclaimed = (uint32_t *) pm_arena_alloc(arena, sizeof(uint32_t) * (size + 1), PRISM_ALIGNOF(uint32_t))
401
404
  };
402
405
 
403
406
  for (uint32_t index = 0; index < size; index++) walk.starts[index] = walk.comments[index]->location.start;
@@ -443,8 +446,6 @@ pm_comments_attach(pm_parser_t *parser, const pm_node_t *root) {
443
446
  attachments[range->index + range->length++] = index;
444
447
  }
445
448
 
446
- pm_arena_cleanup(&arena);
447
-
448
449
  parser->comment_attachments = (pm_comment_attachments_t) {
449
450
  .comments = walk.comments,
450
451
  .attachments = attachments,
data/src/diagnostic.c CHANGED
@@ -20,6 +20,7 @@
20
20
  #include <stdarg.h>
21
21
  #include <stdio.h>
22
22
  #include <stdlib.h>
23
+ #include <string.h>
23
24
 
24
25
  #define PM_DIAGNOSTIC_ID_MAX 327
25
26
 
@@ -832,6 +833,14 @@ pm_diagnostic_warning_level(const pm_diagnostic_t *diagnostic) {
832
833
  return (pm_warning_level_t) pm_diagnostic_id_level(diagnostic->diag_id);
833
834
  }
834
835
 
836
+ /**
837
+ * Get the diagnostic that follows the given diagnostic.
838
+ */
839
+ const pm_diagnostic_t *
840
+ pm_diagnostic_next(const pm_diagnostic_t *diagnostic) {
841
+ return (const pm_diagnostic_t *) diagnostic->node.next;
842
+ }
843
+
835
844
  /**
836
845
  * Append an error to the given list of diagnostic.
837
846
  */
@@ -855,11 +864,16 @@ pm_diagnostic_list_append(pm_arena_t *arena, pm_list_t *list, uint32_t start, ui
855
864
  */
856
865
  void
857
866
  pm_diagnostic_list_append_format(pm_arena_t *arena, pm_list_t *list, uint32_t start, uint32_t length, pm_diagnostic_id_t diag_id, ...) {
867
+ /* Format the message into a buffer on the stack, which nearly every message
868
+ * fits in. The result is the length of the whole message whether or not it
869
+ * fit, so a single pass both formats the message and sizes it. */
870
+ char stack[256];
871
+
858
872
  va_list arguments;
859
873
  va_start(arguments, diag_id);
860
874
 
861
875
  const char *format = pm_diagnostic_id_message(diag_id);
862
- int result = vsnprintf(NULL, 0, format, arguments);
876
+ int result = vsnprintf(stack, sizeof(stack), format, arguments);
863
877
  va_end(arguments);
864
878
 
865
879
  if (result < 0) {
@@ -871,9 +885,15 @@ pm_diagnostic_list_append_format(pm_arena_t *arena, pm_list_t *list, uint32_t st
871
885
  size_t message_length = (size_t) (result + 1);
872
886
  char *message = (char *) pm_arena_alloc(arena, message_length, 1);
873
887
 
874
- va_start(arguments, diag_id);
875
- vsnprintf(message, message_length, format, arguments);
876
- va_end(arguments);
888
+ if (message_length <= sizeof(stack)) {
889
+ memcpy(message, stack, message_length);
890
+ } else {
891
+ /* The message was cut off on the stack, so format it again into an
892
+ * allocation of the right size. */
893
+ va_start(arguments, diag_id);
894
+ vsnprintf(message, message_length, format, arguments);
895
+ va_end(arguments);
896
+ }
877
897
 
878
898
  *diagnostic = (pm_diagnostic_t) {
879
899
  .location = { .start = start, .length = length },
data/src/integer.c CHANGED
@@ -3,8 +3,13 @@
3
3
  #include "prism/internal/allocator.h"
4
4
  #include "prism/internal/buffer.h"
5
5
 
6
- #include <assert.h>
6
+ /* Include sys/types.h before inttypes.h to work around an issue with certain
7
+ * versions of GCC and newlib which causes omission of PRIu64 and the other
8
+ * 64-bit macros. */
9
+ #include <sys/types.h>
7
10
  #include <inttypes.h>
11
+
12
+ #include <assert.h>
8
13
  #include <stdbool.h>
9
14
  #include <stddef.h>
10
15
  #include <stdlib.h>