prism 2.0.0-arm64-darwin

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 (233) hide show
  1. checksums.yaml +7 -0
  2. data/BSDmakefile +58 -0
  3. data/CHANGELOG.md +881 -0
  4. data/CODE_OF_CONDUCT.md +76 -0
  5. data/CONTRIBUTING.md +58 -0
  6. data/LICENSE.md +7 -0
  7. data/Makefile +145 -0
  8. data/README.md +81 -0
  9. data/config.yml +4752 -0
  10. data/docs/build_system.md +119 -0
  11. data/docs/configuration.md +69 -0
  12. data/docs/cruby_compilation.md +27 -0
  13. data/docs/design.md +53 -0
  14. data/docs/encoding.md +121 -0
  15. data/docs/fuzzing.md +81 -0
  16. data/docs/heredocs.md +36 -0
  17. data/docs/javascript.md +133 -0
  18. data/docs/local_variable_depth.md +229 -0
  19. data/docs/mapping.md +117 -0
  20. data/docs/parser_translation.md +24 -0
  21. data/docs/parsing_rules.md +22 -0
  22. data/docs/releasing.md +147 -0
  23. data/docs/relocation.md +32 -0
  24. data/docs/ripper_translation.md +63 -0
  25. data/docs/ruby_api.md +45 -0
  26. data/docs/ruby_parser_translation.md +19 -0
  27. data/docs/serialization.md +244 -0
  28. data/docs/testing.md +72 -0
  29. data/ext/prism/api_node.c +7005 -0
  30. data/ext/prism/extconf.rb +126 -0
  31. data/ext/prism/extension.c +1768 -0
  32. data/ext/prism/extension.h +19 -0
  33. data/include/prism/arena.h +45 -0
  34. data/include/prism/arena.hpp +42 -0
  35. data/include/prism/ast.h +10381 -0
  36. data/include/prism/ast.hpp +641 -0
  37. data/include/prism/buffer.h +60 -0
  38. data/include/prism/buffer.hpp +55 -0
  39. data/include/prism/comments.h +72 -0
  40. data/include/prism/compiler/accel.h +26 -0
  41. data/include/prism/compiler/align.h +36 -0
  42. data/include/prism/compiler/assume.h +25 -0
  43. data/include/prism/compiler/branch.h +26 -0
  44. data/include/prism/compiler/bswap.h +46 -0
  45. data/include/prism/compiler/cplusplus.hpp +16 -0
  46. data/include/prism/compiler/endianness.h +34 -0
  47. data/include/prism/compiler/exceptions.hpp +17 -0
  48. data/include/prism/compiler/exported.h +24 -0
  49. data/include/prism/compiler/fallthrough.h +22 -0
  50. data/include/prism/compiler/filesystem.h +32 -0
  51. data/include/prism/compiler/flex_array.h +19 -0
  52. data/include/prism/compiler/force_inline.h +21 -0
  53. data/include/prism/compiler/format.h +27 -0
  54. data/include/prism/compiler/inline.h +17 -0
  55. data/include/prism/compiler/lifetimebound.hpp +25 -0
  56. data/include/prism/compiler/nodiscard.h +22 -0
  57. data/include/prism/compiler/nonnull.h +18 -0
  58. data/include/prism/compiler/unused.h +18 -0
  59. data/include/prism/constant_pool.h +106 -0
  60. data/include/prism/diagnostic.h +111 -0
  61. data/include/prism/errors_format.h +61 -0
  62. data/include/prism/excludes.h +29 -0
  63. data/include/prism/integer.h +41 -0
  64. data/include/prism/internal/allocator.h +68 -0
  65. data/include/prism/internal/allocator_debug.h +88 -0
  66. data/include/prism/internal/arena.h +108 -0
  67. data/include/prism/internal/bit.h +42 -0
  68. data/include/prism/internal/buffer.h +94 -0
  69. data/include/prism/internal/char.h +145 -0
  70. data/include/prism/internal/comments.h +53 -0
  71. data/include/prism/internal/constant_pool.h +152 -0
  72. data/include/prism/internal/diagnostic.h +389 -0
  73. data/include/prism/internal/encoding.h +242 -0
  74. data/include/prism/internal/integer.h +68 -0
  75. data/include/prism/internal/isinf.h +16 -0
  76. data/include/prism/internal/line_offset_list.h +34 -0
  77. data/include/prism/internal/list.h +62 -0
  78. data/include/prism/internal/magic_comments.h +23 -0
  79. data/include/prism/internal/memchr.h +15 -0
  80. data/include/prism/internal/node.h +42 -0
  81. data/include/prism/internal/options.h +232 -0
  82. data/include/prism/internal/parser.h +1004 -0
  83. data/include/prism/internal/regexp.h +26 -0
  84. data/include/prism/internal/serialize.h +39 -0
  85. data/include/prism/internal/source.h +72 -0
  86. data/include/prism/internal/static_literals.h +100 -0
  87. data/include/prism/internal/stringy.h +30 -0
  88. data/include/prism/internal/strncasecmp.h +18 -0
  89. data/include/prism/internal/strpbrk.h +33 -0
  90. data/include/prism/internal/tokens.h +11 -0
  91. data/include/prism/internal/xxh3.h +16 -0
  92. data/include/prism/json.h +40 -0
  93. data/include/prism/line_offset_list.h +69 -0
  94. data/include/prism/magic_comments.h +53 -0
  95. data/include/prism/node.h +139 -0
  96. data/include/prism/node.hpp +272 -0
  97. data/include/prism/node_type.hpp +38 -0
  98. data/include/prism/options.h +363 -0
  99. data/include/prism/options.hpp +43 -0
  100. data/include/prism/parser.h +433 -0
  101. data/include/prism/parser.hpp +274 -0
  102. data/include/prism/prettyprint.h +39 -0
  103. data/include/prism/serialize.h +123 -0
  104. data/include/prism/source.h +163 -0
  105. data/include/prism/stream.h +36 -0
  106. data/include/prism/string_query.h +82 -0
  107. data/include/prism/stringy.h +61 -0
  108. data/include/prism/stringy.hpp +31 -0
  109. data/include/prism/version.h +46 -0
  110. data/include/prism.h +123 -0
  111. data/include/prism.hpp +24 -0
  112. data/lib/prism/3.3/prism.bundle +0 -0
  113. data/lib/prism/3.4/prism.bundle +0 -0
  114. data/lib/prism/4.0/prism.bundle +0 -0
  115. data/lib/prism/compiler.rb +815 -0
  116. data/lib/prism/desugar_compiler.rb +463 -0
  117. data/lib/prism/dispatcher.rb +2383 -0
  118. data/lib/prism/dot_visitor.rb +4848 -0
  119. data/lib/prism/dsl.rb +1369 -0
  120. data/lib/prism/ffi.rb +746 -0
  121. data/lib/prism/inspect_visitor.rb +2425 -0
  122. data/lib/prism/lex_compat.rb +953 -0
  123. data/lib/prism/mutation_compiler.rb +780 -0
  124. data/lib/prism/node.rb +33940 -0
  125. data/lib/prism/node_ext.rb +504 -0
  126. data/lib/prism/node_find.rb +215 -0
  127. data/lib/prism/parse_result/errors.rb +72 -0
  128. data/lib/prism/parse_result/newlines.rb +423 -0
  129. data/lib/prism/parse_result.rb +1236 -0
  130. data/lib/prism/pattern.rb +314 -0
  131. data/lib/prism/polyfill/append_as_bytes.rb +15 -0
  132. data/lib/prism/polyfill/byteindex.rb +13 -0
  133. data/lib/prism/polyfill/scan_byte.rb +14 -0
  134. data/lib/prism/polyfill/unpack1.rb +14 -0
  135. data/lib/prism/polyfill/warn.rb +36 -0
  136. data/lib/prism/reflection.rb +427 -0
  137. data/lib/prism/relocation.rb +567 -0
  138. data/lib/prism/serialize.rb +4589 -0
  139. data/lib/prism/string_query.rb +54 -0
  140. data/lib/prism/translation/parser/builder.rb +70 -0
  141. data/lib/prism/translation/parser/compiler.rb +2226 -0
  142. data/lib/prism/translation/parser/lexer.rb +769 -0
  143. data/lib/prism/translation/parser.rb +376 -0
  144. data/lib/prism/translation/parser_current.rb +26 -0
  145. data/lib/prism/translation/parser_versions.rb +36 -0
  146. data/lib/prism/translation/ripper/filter.rb +53 -0
  147. data/lib/prism/translation/ripper/lexer.rb +133 -0
  148. data/lib/prism/translation/ripper/sexp.rb +118 -0
  149. data/lib/prism/translation/ripper/shim.rb +7 -0
  150. data/lib/prism/translation/ripper.rb +4278 -0
  151. data/lib/prism/translation/ruby_parser.rb +1721 -0
  152. data/lib/prism/translation.rb +20 -0
  153. data/lib/prism/visitor.rb +1440 -0
  154. data/lib/prism.rb +168 -0
  155. data/prism.gemspec +251 -0
  156. data/rbi/generated/prism/compiler.rbi +489 -0
  157. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  158. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  159. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  160. data/rbi/generated/prism/dsl.rbi +751 -0
  161. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  162. data/rbi/generated/prism/lex_compat.rbi +162 -0
  163. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  164. data/rbi/generated/prism/node.rbi +15143 -0
  165. data/rbi/generated/prism/node_ext.rbi +259 -0
  166. data/rbi/generated/prism/node_find.rbi +79 -0
  167. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  168. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  169. data/rbi/generated/prism/parse_result.rbi +776 -0
  170. data/rbi/generated/prism/pattern.rbi +134 -0
  171. data/rbi/generated/prism/reflection.rbi +89 -0
  172. data/rbi/generated/prism/relocation.rbi +329 -0
  173. data/rbi/generated/prism/serialize.rbi +185 -0
  174. data/rbi/generated/prism/string_query.rbi +43 -0
  175. data/rbi/generated/prism/translation.rbi +7 -0
  176. data/rbi/generated/prism/visitor.rbi +650 -0
  177. data/rbi/generated/prism.rbi +86 -0
  178. data/rbi/prism/translation/parser.rbi +11 -0
  179. data/rbi/prism/translation/parser_versions.rbi +28 -0
  180. data/rbi/prism/translation/ripper.rbi +15 -0
  181. data/rbi/rubyvm/node_find.rbi +14 -0
  182. data/sig/generated/prism/compiler.rbs +492 -0
  183. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  184. data/sig/generated/prism/dispatcher.rbs +1292 -0
  185. data/sig/generated/prism/dot_visitor.rbs +635 -0
  186. data/sig/generated/prism/dsl.rbs +928 -0
  187. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  188. data/sig/generated/prism/lex_compat.rbs +166 -0
  189. data/sig/generated/prism/mutation_compiler.rbs +464 -0
  190. data/sig/generated/prism/node.rbs +21632 -0
  191. data/sig/generated/prism/node_ext.rbs +290 -0
  192. data/sig/generated/prism/node_find.rbs +90 -0
  193. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  194. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  195. data/sig/generated/prism/parse_result.rbs +871 -0
  196. data/sig/generated/prism/pattern.rbs +156 -0
  197. data/sig/generated/prism/reflection.rbs +101 -0
  198. data/sig/generated/prism/relocation.rbs +428 -0
  199. data/sig/generated/prism/serialize.rbs +200 -0
  200. data/sig/generated/prism/string_query.rbs +43 -0
  201. data/sig/generated/prism/translation.rbs +8 -0
  202. data/sig/generated/prism/visitor.rbs +1111 -0
  203. data/sig/generated/prism.rbs +84 -0
  204. data/sig/manifest.yaml +2 -0
  205. data/src/arena.c +117 -0
  206. data/src/buffer.c +389 -0
  207. data/src/char.c +289 -0
  208. data/src/comments.c +454 -0
  209. data/src/constant_pool.c +406 -0
  210. data/src/diagnostic.c +894 -0
  211. data/src/encoding.c +5345 -0
  212. data/src/errors_format.c +519 -0
  213. data/src/integer.c +681 -0
  214. data/src/json.c +5733 -0
  215. data/src/line_offset_list.c +100 -0
  216. data/src/list.c +24 -0
  217. data/src/memchr.c +37 -0
  218. data/src/node.c +4363 -0
  219. data/src/options.c +445 -0
  220. data/src/parser.c +371 -0
  221. data/src/prettyprint.c +9024 -0
  222. data/src/prism.c +23901 -0
  223. data/src/regexp.c +1705 -0
  224. data/src/serialize.c +2660 -0
  225. data/src/source.c +524 -0
  226. data/src/static_literals.c +685 -0
  227. data/src/string_query.c +180 -0
  228. data/src/stringy.c +61 -0
  229. data/src/strncasecmp.c +37 -0
  230. data/src/strpbrk.c +447 -0
  231. data/src/tokens.c +724 -0
  232. data/src/xxh3.c +292 -0
  233. metadata +276 -0
@@ -0,0 +1,94 @@
1
+ #ifndef PRISM_INTERNAL_BUFFER_H
2
+ #define PRISM_INTERNAL_BUFFER_H
3
+
4
+ #include "prism/compiler/format.h"
5
+
6
+ #include "prism/buffer.h"
7
+
8
+ #include <stdbool.h>
9
+ #include <stdint.h>
10
+
11
+ /*
12
+ * A simple memory buffer that stores data in a contiguous block of memory.
13
+ */
14
+ struct pm_buffer_t {
15
+ /* The length of the buffer in bytes. */
16
+ size_t length;
17
+
18
+ /* The capacity of the buffer in bytes that has been allocated. */
19
+ size_t capacity;
20
+
21
+ /* A pointer to the start of the buffer. */
22
+ char *value;
23
+ };
24
+
25
+ /* Initialize a pm_buffer_t with the given capacity. */
26
+ void pm_buffer_init(pm_buffer_t *buffer, size_t capacity);
27
+
28
+ /* Free the memory held by the buffer. */
29
+ void pm_buffer_cleanup(pm_buffer_t *buffer);
30
+
31
+ /* Append the given amount of space as zeroes to the buffer. */
32
+ void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
33
+
34
+ /* Append a formatted string to the buffer. */
35
+ void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
36
+
37
+ /* Append a string to the buffer. */
38
+ void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
39
+
40
+ /* Append a list of bytes to the buffer. */
41
+ void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
42
+
43
+ /* Append a single byte to the buffer. */
44
+ void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
45
+
46
+ /* Append a 32-bit unsigned integer to the buffer as a variable-length integer. */
47
+ void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
48
+
49
+ /* Append a 32-bit signed integer to the buffer as a variable-length integer. */
50
+ void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
51
+
52
+ /* Append a double to the buffer. */
53
+ void pm_buffer_append_double(pm_buffer_t *buffer, double value);
54
+
55
+ /* Append a 64-bit unsigned integer to the buffer in native endian order. */
56
+ void pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value);
57
+
58
+ /* Append a unicode codepoint to the buffer. */
59
+ bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
60
+
61
+ /*
62
+ * The different types of escaping that can be performed by the buffer when
63
+ * appending a slice of Ruby source code.
64
+ */
65
+ typedef enum {
66
+ PM_BUFFER_ESCAPING_RUBY,
67
+ PM_BUFFER_ESCAPING_JSON
68
+ } pm_buffer_escaping_t;
69
+
70
+ /* Append a slice of source code to the buffer. */
71
+ void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
72
+
73
+ /* Prepend the given string to the buffer. */
74
+ void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
75
+
76
+ /* Concatenate one buffer onto another. */
77
+ void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
78
+
79
+ /*
80
+ * Clear the buffer by reducing its size to 0. This does not free the allocated
81
+ * memory, but it does allow the buffer to be reused.
82
+ */
83
+ void pm_buffer_clear(pm_buffer_t *buffer);
84
+
85
+ /* Strip the whitespace from the end of the buffer. */
86
+ void pm_buffer_rstrip(pm_buffer_t *buffer);
87
+
88
+ /* Checks if the buffer includes the given value. */
89
+ size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
90
+
91
+ /* Insert the given string into the buffer at the given index. */
92
+ void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
93
+
94
+ #endif
@@ -0,0 +1,145 @@
1
+ #ifndef PRISM_INTERNAL_CHAR_H
2
+ #define PRISM_INTERNAL_CHAR_H
3
+
4
+ #include "prism/compiler/force_inline.h"
5
+
6
+ #include "prism/arena.h"
7
+ #include "prism/line_offset_list.h"
8
+
9
+ #include <stdbool.h>
10
+ #include <stddef.h>
11
+ #include <stdint.h>
12
+
13
+ /* Bit flag for whitespace characters in pm_byte_table. */
14
+ #define PRISM_CHAR_BIT_WHITESPACE (1 << 0)
15
+
16
+ /* Bit flag for inline whitespace characters in pm_byte_table. */
17
+ #define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1)
18
+
19
+ /*
20
+ * A lookup table for classifying bytes. Each entry is a bitfield of
21
+ * PRISM_CHAR_BIT_* flags. Defined in char.c.
22
+ */
23
+ extern const uint8_t pm_byte_table[256];
24
+
25
+ /* Returns true if the given character is a whitespace character. */
26
+ static PRISM_FORCE_INLINE bool
27
+ pm_char_is_whitespace(const uint8_t b) {
28
+ return (pm_byte_table[b] & PRISM_CHAR_BIT_WHITESPACE) != 0;
29
+ }
30
+
31
+ /* Returns true if the given character is an inline whitespace character. */
32
+ static PRISM_FORCE_INLINE bool
33
+ pm_char_is_inline_whitespace(const uint8_t b) {
34
+ return (pm_byte_table[b] & PRISM_CHAR_BIT_INLINE_WHITESPACE) != 0;
35
+ }
36
+
37
+ /*
38
+ * Returns the number of characters at the start of the string that are inline
39
+ * whitespace (space/tab). Scans the byte table directly for use in hot paths.
40
+ */
41
+ static PRISM_FORCE_INLINE size_t
42
+ pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length) {
43
+ if (length <= 0) return 0;
44
+ size_t size = 0;
45
+ size_t maximum = (size_t) length;
46
+ while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_INLINE_WHITESPACE)) size++;
47
+ return size;
48
+ }
49
+
50
+ /*
51
+ * Returns the number of characters at the start of the string that are
52
+ * whitespace. Disallows searching past the given maximum number of characters.
53
+ */
54
+ size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);
55
+
56
+ /*
57
+ * Returns the number of characters at the end of the string that are
58
+ * whitespace. Disallows searching past the given maximum number of characters.
59
+ */
60
+ size_t pm_strrspn_whitespace(const uint8_t *string, ptrdiff_t length);
61
+
62
+ /*
63
+ * Returns the number of characters at the start of the string that are
64
+ * whitespace while also tracking the location of each newline. Disallows
65
+ * searching past the given maximum number of characters.
66
+ */
67
+ size_t pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_arena_t *arena, pm_line_offset_list_t *line_offsets, uint32_t start_offset);
68
+
69
+ /*
70
+ * Returns the number of characters at the start of the string that are decimal
71
+ * digits. Disallows searching past the given maximum number of characters.
72
+ */
73
+ size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length);
74
+
75
+ /*
76
+ * Returns the number of characters at the start of the string that are
77
+ * hexadecimal digits. Disallows searching past the given maximum number of
78
+ * characters.
79
+ */
80
+ size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length);
81
+
82
+ /*
83
+ * Returns the number of characters at the start of the string that are octal
84
+ * digits or underscores. Disallows searching past the given maximum number of
85
+ * characters.
86
+ *
87
+ * If multiple underscores are found in a row or if an underscore is
88
+ * found at the end of the number, then the invalid pointer is set to the index
89
+ * of the first invalid underscore.
90
+ */
91
+ size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
92
+
93
+ /*
94
+ * Returns the number of characters at the start of the string that are decimal
95
+ * digits or underscores. Disallows searching past the given maximum number of
96
+ * characters.
97
+ *
98
+ * If multiple underscores are found in a row or if an underscore is
99
+ * found at the end of the number, then the invalid pointer is set to the index
100
+ * of the first invalid underscore.
101
+ */
102
+ size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
103
+
104
+ /*
105
+ * Returns the number of characters at the start of the string that are
106
+ * hexadecimal digits or underscores. Disallows searching past the given maximum
107
+ * number of characters.
108
+ *
109
+ * If multiple underscores are found in a row or if an underscore is
110
+ * found at the end of the number, then the invalid pointer is set to the index
111
+ * of the first invalid underscore.
112
+ */
113
+ size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
114
+
115
+ /*
116
+ * Returns the number of characters at the start of the string that are regexp
117
+ * options. Disallows searching past the given maximum number of characters.
118
+ */
119
+ size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);
120
+
121
+ /*
122
+ * Returns the number of characters at the start of the string that are binary
123
+ * digits or underscores. Disallows searching past the given maximum number of
124
+ * characters.
125
+ *
126
+ * If multiple underscores are found in a row or if an underscore is
127
+ * found at the end of the number, then the invalid pointer is set to the index
128
+ * of the first invalid underscore.
129
+ */
130
+ size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
131
+
132
+
133
+ /* Returns true if the given character is a binary digit. */
134
+ bool pm_char_is_binary_digit(const uint8_t b);
135
+
136
+ /* Returns true if the given character is an octal digit. */
137
+ bool pm_char_is_octal_digit(const uint8_t b);
138
+
139
+ /* Returns true if the given character is a decimal digit. */
140
+ bool pm_char_is_decimal_digit(const uint8_t b);
141
+
142
+ /* Returns true if the given character is a hexadecimal digit. */
143
+ bool pm_char_is_hexadecimal_digit(const uint8_t b);
144
+
145
+ #endif
@@ -0,0 +1,53 @@
1
+ #ifndef PRISM_INTERNAL_COMMENTS_H
2
+ #define PRISM_INTERNAL_COMMENTS_H
3
+
4
+ #include "prism/comments.h"
5
+ #include "prism/parser.h"
6
+
7
+ #include "prism/internal/list.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ /* A comment found while parsing. */
12
+ struct pm_comment_t {
13
+ /* The embedded base node. */
14
+ pm_list_node_t node;
15
+
16
+ /* The location of the comment in the source. */
17
+ pm_location_t location;
18
+
19
+ /* The type of the comment. */
20
+ pm_comment_type_t type;
21
+ };
22
+
23
+ /*
24
+ * The result of attaching comments to the nodes that own them. All of the
25
+ * arrays are allocated in the parser's metadata arena.
26
+ */
27
+ typedef struct {
28
+ /* The comments found while parsing, sorted by start offset. */
29
+ const pm_comment_t **comments;
30
+
31
+ /*
32
+ * Indices into comments, grouped by owning node in order of node id and
33
+ * sorted by start offset within each group. A node's comments are not
34
+ * necessarily contiguous in comments, because a child can own a comment
35
+ * that falls between two comments owned by its parent.
36
+ */
37
+ uint32_t *attachments;
38
+
39
+ /* The slice of attachments for each node, indexed by node id. */
40
+ pm_comment_range_t *ranges;
41
+
42
+ /* The number of entries in ranges. */
43
+ uint32_t ranges_size;
44
+ } pm_comment_attachments_t;
45
+
46
+ /*
47
+ * Sort the parser's comment list in place by start offset, then assign every
48
+ * comment to the node that owns it. This is called after parsing when the
49
+ * attach_comments option is set.
50
+ */
51
+ void pm_comments_attach(pm_parser_t *parser, const pm_node_t *root);
52
+
53
+ #endif
@@ -0,0 +1,152 @@
1
+ #ifndef PRISM_INTERNAL_CONSTANT_POOL_H
2
+ #define PRISM_INTERNAL_CONSTANT_POOL_H
3
+
4
+ #include "prism/constant_pool.h"
5
+
6
+ #include "prism/arena.h"
7
+ #include "prism/compiler/inline.h"
8
+
9
+ #include <stdbool.h>
10
+
11
+ /* A constant in the pool which effectively stores a string. */
12
+ struct pm_constant_t {
13
+ /* A pointer to the start of the string. */
14
+ const uint8_t *start;
15
+
16
+ /* The length of the string. */
17
+ size_t length;
18
+ };
19
+
20
+ /*
21
+ * The type of bucket in the constant pool hash map. This determines how the
22
+ * bucket should be freed.
23
+ */
24
+ typedef unsigned int pm_constant_pool_bucket_type_t;
25
+
26
+ /* By default, each constant is a slice of the source. */
27
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0;
28
+
29
+ /* An owned constant is one for which memory has been allocated. */
30
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1;
31
+
32
+ /* A constant constant is known at compile time. */
33
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2;
34
+
35
+ /* A bucket in the hash map. */
36
+ typedef struct {
37
+ /* The incremental ID used for indexing back into the pool. */
38
+ unsigned int id: 30;
39
+
40
+ /* The type of the bucket, which determines how to free it. */
41
+ pm_constant_pool_bucket_type_t type: 2;
42
+
43
+ /* The hash of the bucket. */
44
+ uint32_t hash;
45
+
46
+ /*
47
+ * A pointer to the start of the string, stored directly in the bucket to
48
+ * avoid a pointer chase to the constants array during probing.
49
+ */
50
+ const uint8_t *start;
51
+
52
+ /* The length of the string. */
53
+ size_t length;
54
+ } pm_constant_pool_bucket_t;
55
+
56
+ /* The overall constant pool, which stores constants found while parsing. */
57
+ struct pm_constant_pool_t {
58
+ /* The buckets in the hash map. */
59
+ pm_constant_pool_bucket_t *buckets;
60
+
61
+ /* The constants that are stored in the buckets. */
62
+ pm_constant_t *constants;
63
+
64
+ /* The number of buckets in the hash map. */
65
+ uint32_t size;
66
+
67
+ /* The number of buckets that have been allocated in the hash map. */
68
+ uint32_t capacity;
69
+ };
70
+
71
+ /*
72
+ * When we allocate constants into the pool, we reserve 0 to mean that the slot
73
+ * is not yet filled. This constant is reused in other places to indicate the
74
+ * lack of a constant id.
75
+ */
76
+ #define PM_CONSTANT_ID_UNSET 0
77
+
78
+ /* Initialize a list of constant ids with a given capacity. */
79
+ void pm_constant_id_list_init_capacity(pm_arena_t *arena, pm_constant_id_list_t *list, size_t capacity);
80
+
81
+ /* Insert a constant id into a list of constant ids at the specified index. */
82
+ void pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id);
83
+
84
+ /*
85
+ * Mix the bits of a constant id so that ids which are close together, as
86
+ * sequentially assigned ids are, land in unrelated slots of a hash table.
87
+ */
88
+ static PRISM_INLINE uint32_t
89
+ pm_constant_id_hash(pm_constant_id_t id) {
90
+ id = ((id >> 16) ^ id) * 0x45d9f3b;
91
+ id = ((id >> 16) ^ id) * 0x45d9f3b;
92
+ id = (id >> 16) ^ id;
93
+ return id;
94
+ }
95
+
96
+ /*
97
+ * A set of constant ids, for where the parser only needs to know whether it has
98
+ * already seen a name, as with the variables a pattern captures or the named
99
+ * capture groups of a regular expression.
100
+ */
101
+ typedef struct {
102
+ /* The number of ids in the set. */
103
+ size_t size;
104
+
105
+ /* The number of slots in `ids`, either zero or a power of two. */
106
+ size_t capacity;
107
+
108
+ /*
109
+ * An open-addressed table of the ids in the set, where PM_CONSTANT_ID_UNSET
110
+ * marks an empty slot. It is NULL until the first id is added.
111
+ */
112
+ pm_constant_id_t *ids;
113
+ } pm_constant_id_set_t;
114
+
115
+ /*
116
+ * Add a constant id, which must not be PM_CONSTANT_ID_UNSET, to the set.
117
+ * Returns whether the set did not already contain it.
118
+ */
119
+ bool pm_constant_id_set_insert(pm_arena_t *arena, pm_constant_id_set_t *set, pm_constant_id_t id);
120
+
121
+ /* Initialize a new constant pool with a given capacity. */
122
+ void pm_constant_pool_init(pm_arena_t *arena, pm_constant_pool_t *pool, uint32_t capacity);
123
+
124
+ /* Return a pointer to the constant indicated by the given constant id. */
125
+ pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id);
126
+
127
+ /*
128
+ * Find a constant in a constant pool. Returns the id of the constant, or 0 if
129
+ * the constant is not found.
130
+ */
131
+ pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length);
132
+
133
+ /*
134
+ * Insert a constant into a constant pool that is a slice of a source string.
135
+ * Returns the id of the constant, or 0 if any potential calls to resize fail.
136
+ */
137
+ pm_constant_id_t pm_constant_pool_insert_shared(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
138
+
139
+ /*
140
+ * Insert a constant into a constant pool from memory that is now owned by the
141
+ * constant pool. Returns the id of the constant, or 0 if any potential calls to
142
+ * resize fail.
143
+ */
144
+ pm_constant_id_t pm_constant_pool_insert_owned(pm_arena_t *arena, pm_constant_pool_t *pool, uint8_t *start, size_t length);
145
+
146
+ /*
147
+ * Insert a constant into a constant pool from memory that is constant. Returns
148
+ * the id of the constant, or 0 if any potential calls to resize fail.
149
+ */
150
+ pm_constant_id_t pm_constant_pool_insert_constant(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
151
+
152
+ #endif