prism 2.0.0.rc1 → 2.0.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 (79) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -2
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/LICENSE.md +1 -1
  5. data/docs/serialization.md +20 -6
  6. data/ext/prism/api_node.c +81 -81
  7. data/ext/prism/extension.c +54 -69
  8. data/ext/prism/extension.h +1 -1
  9. data/include/prism/arena.h +8 -0
  10. data/include/prism/arena.hpp +42 -0
  11. data/include/prism/ast.h +11 -1
  12. data/include/prism/ast.hpp +641 -0
  13. data/include/prism/buffer.h +8 -0
  14. data/include/prism/buffer.hpp +55 -0
  15. data/include/prism/comments.h +17 -0
  16. data/include/prism/compiler/accel.h +3 -1
  17. data/include/prism/compiler/branch.h +26 -0
  18. data/include/prism/compiler/bswap.h +46 -0
  19. data/include/prism/compiler/cplusplus.hpp +16 -0
  20. data/include/prism/compiler/endianness.h +34 -0
  21. data/include/prism/compiler/exceptions.hpp +17 -0
  22. data/include/prism/compiler/format.h +2 -0
  23. data/include/prism/compiler/lifetimebound.hpp +25 -0
  24. data/include/prism/constant_pool.h +25 -0
  25. data/include/prism/diagnostic.h +18 -0
  26. data/include/prism/errors_format.h +8 -0
  27. data/include/prism/internal/buffer.h +3 -0
  28. data/include/prism/internal/node.h +8 -4
  29. data/include/prism/internal/parser.h +20 -0
  30. data/include/prism/internal/stringy.h +1 -6
  31. data/include/prism/internal/xxh3.h +16 -0
  32. data/include/prism/json.h +8 -0
  33. data/include/prism/line_offset_list.h +8 -0
  34. data/include/prism/magic_comments.h +18 -0
  35. data/include/prism/node.h +55 -10
  36. data/include/prism/node.hpp +272 -0
  37. data/include/prism/node_type.hpp +38 -0
  38. data/include/prism/options.h +10 -1
  39. data/include/prism/options.hpp +43 -0
  40. data/include/prism/parser.h +93 -64
  41. data/include/prism/parser.hpp +274 -0
  42. data/include/prism/prettyprint.h +8 -0
  43. data/include/prism/serialize.h +8 -0
  44. data/include/prism/source.h +8 -0
  45. data/include/prism/stream.h +8 -0
  46. data/include/prism/string_query.h +19 -0
  47. data/include/prism/stringy.h +16 -27
  48. data/include/prism/stringy.hpp +31 -0
  49. data/include/prism/version.h +9 -1
  50. data/include/prism.h +0 -8
  51. data/include/prism.hpp +24 -0
  52. data/lib/prism/ffi.rb +6 -0
  53. data/lib/prism/node.rb +736 -0
  54. data/lib/prism/parse_result.rb +23 -8
  55. data/lib/prism/serialize.rb +2567 -3286
  56. data/lib/prism/string_query.rb +8 -0
  57. data/prism.gemspec +21 -4
  58. data/rbi/generated/prism/node.rbi +2430 -968
  59. data/rbi/generated/prism/parse_result.rbi +16 -4
  60. data/rbi/generated/prism/serialize.rbi +8 -4
  61. data/rbi/generated/prism/string_query.rbi +7 -0
  62. data/sig/generated/prism/node.rbs +2439 -967
  63. data/sig/generated/prism/parse_result.rbs +16 -4
  64. data/sig/generated/prism/serialize.rbs +9 -4
  65. data/sig/generated/prism/string_query.rbs +7 -0
  66. data/src/buffer.c +9 -0
  67. data/src/comments.c +2 -2
  68. data/src/diagnostic.c +8 -0
  69. data/src/node.c +216 -368
  70. data/src/options.c +7 -13
  71. data/src/parser.c +70 -48
  72. data/src/prism.c +581 -419
  73. data/src/regexp.c +0 -1
  74. data/src/serialize.c +2301 -1950
  75. data/src/source.c +8 -2
  76. data/src/string_query.c +14 -0
  77. data/src/stringy.c +3 -33
  78. data/src/xxh3.c +292 -0
  79. metadata +20 -3
@@ -30,8 +30,11 @@ module Prism
30
30
  # empty when the source is created by the parser and filled in afterward,
31
31
  # because the comments themselves refer to the source.
32
32
  #
33
- # : (String source, Integer start_line, Array[Integer] offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges) -> Source
34
- def self.for: (String source, Integer start_line, Array[Integer] offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges) -> Source
33
+ # The checksum parameter is the value described by the attribute of the same
34
+ # name. It is nil when the source is not created by the parser.
35
+ #
36
+ # : (String source, Integer start_line, Array[Integer] offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges, ?Integer? checksum) -> Source
37
+ def self.for: (String source, Integer start_line, Array[Integer] offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges, ?Integer? checksum) -> Source
35
38
 
36
39
  # The source code that this source object represents.
37
40
  attr_reader source: String
@@ -39,6 +42,10 @@ module Prism
39
42
  # The line number where this source starts.
40
43
  attr_reader start_line: Integer
41
44
 
45
+ # The checksum of the source code which can be used to quickly determine if
46
+ # the source has changed.
47
+ attr_reader checksum: Integer?
48
+
42
49
  # The list of comments that were encountered during parsing. When the source
43
50
  # is parsed with the attach_comments option, these are sorted by start
44
51
  # offset.
@@ -66,14 +73,19 @@ module Prism
66
73
  # parameter can be either an Array of Integer byte offsets or a packed
67
74
  # binary string of uint32_t values (from the C extension).
68
75
  #
69
- # : (String source, Integer start_line, Array[Integer] | String offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges) -> void
70
- def initialize: (String source, Integer start_line, Array[Integer] | String offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges) -> void
76
+ # : (String source, Integer start_line, Array[Integer] | String offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges, ?Integer? checksum) -> void
77
+ def initialize: (String source, Integer start_line, Array[Integer] | String offsets, ?Array[Comment] comments, ?Array[Comment] comment_attachments, ?Array[Integer?] comment_ranges, ?Integer? checksum) -> void
71
78
 
72
79
  # Replace the value of start_line with the given value.
73
80
  #
74
81
  # : (Integer start_line) -> void
75
82
  def replace_start_line: (Integer start_line) -> void
76
83
 
84
+ # Replace the value of checksum with the given value.
85
+ #
86
+ # : (Integer checksum) -> void
87
+ def replace_checksum: (Integer checksum) -> void
88
+
77
89
  # Replace the value of offsets with the given value.
78
90
  #
79
91
  # : (Array[Integer] offsets) -> void
@@ -149,8 +149,8 @@ module Prism
149
149
  # : () -> Integer
150
150
  def load_uint32: () -> Integer
151
151
 
152
- # : (ConstantPool constant_pool, Encoding encoding, bool freeze) -> node?
153
- def load_optional_node: (ConstantPool constant_pool, Encoding encoding, bool freeze) -> node?
152
+ # : () -> Integer
153
+ def load_uint64: () -> Integer
154
154
 
155
155
  # : (Encoding encoding) -> String
156
156
  def load_string: (Encoding encoding) -> String
@@ -181,8 +181,13 @@ module Prism
181
181
  # : (ConstantPool constant_pool, Encoding encoding) -> Symbol?
182
182
  def load_optional_constant: (ConstantPool constant_pool, Encoding encoding) -> Symbol?
183
183
 
184
- # : (ConstantPool constant_pool, Encoding encoding, bool freeze) -> node
185
- 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
186
191
 
187
192
  @load_node_lambdas: Array[Proc]
188
193
 
@@ -10,6 +10,8 @@ module Prism
10
10
 
11
11
  def self.method_name?: (String string) -> bool
12
12
 
13
+ def self.keyword?: (String string) -> bool
14
+
13
15
  # The string that this query is wrapping.
14
16
  attr_reader string: String
15
17
 
@@ -32,5 +34,10 @@ module Prism
32
34
  #
33
35
  # : () -> bool
34
36
  def method_name?: () -> bool
37
+
38
+ # Whether or not this string is a valid keyword.
39
+ #
40
+ # : () -> bool
41
+ def keyword?: () -> bool
35
42
  end
36
43
  end
data/src/buffer.c CHANGED
@@ -186,6 +186,15 @@ pm_buffer_append_double(pm_buffer_t *buffer, double value) {
186
186
  pm_buffer_append(buffer, source, sizeof(double));
187
187
  }
188
188
 
189
+ /**
190
+ * Append a 64-bit unsigned integer to the buffer in native endian order.
191
+ */
192
+ void
193
+ pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value) {
194
+ const void *source = &value;
195
+ pm_buffer_append(buffer, source, sizeof(uint64_t));
196
+ }
197
+
189
198
  /**
190
199
  * Append a unicode codepoint to the buffer.
191
200
  */
data/src/comments.c CHANGED
@@ -223,7 +223,7 @@ pm_comments_blank_after(const pm_comments_walk_t *walk, uint32_t offset, uint32_
223
223
  * descends itself, so that it can visit children in source order and handle
224
224
  * each node after all of its descendants.
225
225
  */
226
- static bool
226
+ static pm_visit_result_t
227
227
  pm_comments_push(const pm_node_t *node, void *data) {
228
228
  pm_comments_walk_t *walk = (pm_comments_walk_t *) data;
229
229
 
@@ -237,7 +237,7 @@ pm_comments_push(const pm_node_t *node, void *data) {
237
237
  }
238
238
 
239
239
  walk->stack.nodes[walk->stack.size++] = node;
240
- return false;
240
+ return PM_VISIT_SKIP;
241
241
  }
242
242
 
243
243
  /*
data/src/diagnostic.c CHANGED
@@ -832,6 +832,14 @@ pm_diagnostic_warning_level(const pm_diagnostic_t *diagnostic) {
832
832
  return (pm_warning_level_t) pm_diagnostic_id_level(diagnostic->diag_id);
833
833
  }
834
834
 
835
+ /**
836
+ * Get the diagnostic that follows the given diagnostic.
837
+ */
838
+ const pm_diagnostic_t *
839
+ pm_diagnostic_next(const pm_diagnostic_t *diagnostic) {
840
+ return (const pm_diagnostic_t *) diagnostic->node.next;
841
+ }
842
+
835
843
  /**
836
844
  * Append an error to the given list of diagnostic.
837
845
  */