prism 1.9.0 → 2.1.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 (258) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +144 -40
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/LICENSE.md +1 -1
  5. data/Makefile +49 -20
  6. data/README.md +38 -98
  7. data/config.yml +174 -161
  8. data/docs/build_system.md +2 -2
  9. data/docs/configuration.md +5 -4
  10. data/docs/encoding.md +2 -2
  11. data/docs/fuzzing.md +9 -16
  12. data/docs/javascript.md +19 -4
  13. data/docs/mapping.md +1 -1
  14. data/docs/releasing.md +77 -5
  15. data/docs/relocation.md +2 -4
  16. data/docs/serialization.md +39 -28
  17. data/docs/testing.md +46 -29
  18. data/ext/prism/api_node.c +1527 -1467
  19. data/ext/prism/extconf.rb +3 -4
  20. data/ext/prism/extension.c +759 -418
  21. data/ext/prism/extension.h +3 -3
  22. data/include/prism/arena.h +45 -0
  23. data/include/prism/arena.hpp +42 -0
  24. data/include/prism/ast.h +2738 -611
  25. data/include/prism/ast.hpp +641 -0
  26. data/include/prism/buffer.h +60 -0
  27. data/include/prism/buffer.hpp +55 -0
  28. data/include/prism/comments.h +72 -0
  29. data/include/prism/compiler/accel.h +26 -0
  30. data/include/prism/compiler/align.h +36 -0
  31. data/include/prism/compiler/assume.h +25 -0
  32. data/include/prism/compiler/branch.h +26 -0
  33. data/include/prism/compiler/bswap.h +46 -0
  34. data/include/prism/compiler/cplusplus.hpp +16 -0
  35. data/include/prism/compiler/endianness.h +34 -0
  36. data/include/prism/compiler/exceptions.hpp +17 -0
  37. data/include/prism/compiler/exported.h +24 -0
  38. data/include/prism/compiler/fallthrough.h +22 -0
  39. data/include/prism/compiler/filesystem.h +32 -0
  40. data/include/prism/compiler/flex_array.h +19 -0
  41. data/include/prism/compiler/force_inline.h +21 -0
  42. data/include/prism/compiler/format.h +27 -0
  43. data/include/prism/compiler/inline.h +17 -0
  44. data/include/prism/compiler/lifetimebound.hpp +25 -0
  45. data/include/prism/compiler/nodiscard.h +22 -0
  46. data/include/prism/compiler/nonnull.h +18 -0
  47. data/include/prism/compiler/unused.h +18 -0
  48. data/include/prism/constant_pool.h +106 -0
  49. data/include/prism/diagnostic.h +55 -402
  50. data/include/prism/errors_format.h +61 -0
  51. data/include/prism/excludes.h +29 -0
  52. data/include/prism/integer.h +41 -0
  53. data/include/prism/internal/allocator.h +68 -0
  54. data/include/prism/internal/allocator_debug.h +88 -0
  55. data/include/prism/internal/arena.h +108 -0
  56. data/include/prism/internal/bit.h +42 -0
  57. data/include/prism/internal/buffer.h +94 -0
  58. data/include/prism/internal/char.h +145 -0
  59. data/include/prism/internal/comments.h +54 -0
  60. data/include/prism/internal/constant_pool.h +152 -0
  61. data/include/prism/internal/diagnostic.h +389 -0
  62. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  63. data/include/prism/internal/integer.h +68 -0
  64. data/include/prism/internal/isinf.h +16 -0
  65. data/include/prism/internal/line_offset_list.h +34 -0
  66. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  67. data/include/prism/internal/magic_comments.h +23 -0
  68. data/include/prism/internal/memchr.h +15 -0
  69. data/include/prism/internal/node.h +42 -0
  70. data/include/prism/internal/options.h +232 -0
  71. data/include/prism/internal/parser.h +1015 -0
  72. data/include/prism/internal/regexp.h +26 -0
  73. data/include/prism/internal/serialize.h +39 -0
  74. data/include/prism/internal/source.h +72 -0
  75. data/include/prism/internal/static_literals.h +60 -0
  76. data/include/prism/internal/stringy.h +30 -0
  77. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  78. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  79. data/include/prism/internal/tokens.h +11 -0
  80. data/include/prism/internal/xxh3.h +16 -0
  81. data/include/prism/json.h +40 -0
  82. data/include/prism/line_offset_list.h +69 -0
  83. data/include/prism/magic_comments.h +53 -0
  84. data/include/prism/node.h +59 -49
  85. data/include/prism/node.hpp +272 -0
  86. data/include/prism/node_type.hpp +38 -0
  87. data/include/prism/options.h +154 -279
  88. data/include/prism/options.hpp +43 -0
  89. data/include/prism/parser.h +367 -870
  90. data/include/prism/parser.hpp +274 -0
  91. data/include/prism/prettyprint.h +14 -9
  92. data/include/prism/serialize.h +123 -0
  93. data/include/prism/source.h +163 -0
  94. data/include/prism/stream.h +36 -0
  95. data/include/prism/string_query.h +82 -0
  96. data/include/prism/stringy.h +61 -0
  97. data/include/prism/stringy.hpp +31 -0
  98. data/include/prism/version.h +20 -3
  99. data/include/prism.h +35 -320
  100. data/include/prism.hpp +24 -0
  101. data/lib/prism/compiler.rb +319 -305
  102. data/lib/prism/desugar_compiler.rb +105 -34
  103. data/lib/prism/dispatcher.rb +646 -473
  104. data/lib/prism/dot_visitor.rb +592 -511
  105. data/lib/prism/dsl.rb +391 -25
  106. data/lib/prism/ffi.rb +258 -90
  107. data/lib/prism/inspect_visitor.rb +357 -325
  108. data/lib/prism/lex_compat.rb +254 -212
  109. data/lib/prism/mutation_compiler.rb +313 -305
  110. data/lib/prism/node.rb +20966 -6992
  111. data/lib/prism/node_ext.rb +180 -187
  112. data/lib/prism/node_find.rb +215 -0
  113. data/lib/prism/parse_result/errors.rb +7 -1
  114. data/lib/prism/parse_result/newlines.rb +276 -8
  115. data/lib/prism/parse_result.rb +468 -139
  116. data/lib/prism/pattern.rb +65 -20
  117. data/lib/prism/reflection.rb +23 -12
  118. data/lib/prism/relocation.rb +149 -87
  119. data/lib/prism/serialize.rb +2889 -700
  120. data/lib/prism/string_query.rb +24 -1
  121. data/lib/prism/translation/parser/builder.rb +10 -2
  122. data/lib/prism/translation/parser/compiler.rb +44 -52
  123. data/lib/prism/translation/parser/lexer.rb +41 -92
  124. data/lib/prism/translation/parser.rb +1 -1
  125. data/lib/prism/translation/parser_current.rb +1 -1
  126. data/lib/prism/translation/ripper/lexer.rb +8 -10
  127. data/lib/prism/translation/ripper/sexp.rb +2 -10
  128. data/lib/prism/translation/ripper/shim.rb +2 -0
  129. data/lib/prism/translation/ripper.rb +967 -209
  130. data/lib/prism/translation/ruby_parser.rb +73 -316
  131. data/lib/prism/translation.rb +2 -0
  132. data/lib/prism/visitor.rb +632 -5
  133. data/lib/prism.rb +79 -9
  134. data/prism.gemspec +141 -62
  135. data/rbi/generated/prism/compiler.rbi +489 -0
  136. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  137. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  138. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  139. data/rbi/generated/prism/dsl.rbi +751 -0
  140. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  141. data/rbi/generated/prism/lex_compat.rbi +162 -0
  142. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  143. data/rbi/generated/prism/node.rbi +15143 -0
  144. data/rbi/generated/prism/node_ext.rbi +259 -0
  145. data/rbi/generated/prism/node_find.rbi +79 -0
  146. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  147. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  148. data/rbi/generated/prism/parse_result.rbi +776 -0
  149. data/rbi/generated/prism/pattern.rbi +134 -0
  150. data/rbi/generated/prism/reflection.rbi +89 -0
  151. data/rbi/generated/prism/relocation.rbi +329 -0
  152. data/rbi/generated/prism/serialize.rbi +185 -0
  153. data/rbi/generated/prism/string_query.rbi +43 -0
  154. data/rbi/generated/prism/translation.rbi +7 -0
  155. data/rbi/generated/prism/visitor.rbi +650 -0
  156. data/rbi/generated/prism.rbi +86 -0
  157. data/rbi/prism/translation/parser_versions.rbi +5 -0
  158. data/rbi/rubyvm/node_find.rbi +14 -0
  159. data/sig/generated/prism/compiler.rbs +492 -0
  160. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  161. data/sig/generated/prism/dispatcher.rbs +1292 -0
  162. data/sig/generated/prism/dot_visitor.rbs +635 -0
  163. data/sig/generated/prism/dsl.rbs +928 -0
  164. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  165. data/sig/generated/prism/lex_compat.rbs +166 -0
  166. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  167. data/sig/generated/prism/node.rbs +21632 -0
  168. data/sig/generated/prism/node_ext.rbs +290 -0
  169. data/sig/generated/prism/node_find.rbs +90 -0
  170. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  171. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  172. data/sig/generated/prism/parse_result.rbs +871 -0
  173. data/sig/generated/prism/pattern.rbs +156 -0
  174. data/sig/generated/prism/reflection.rbs +101 -0
  175. data/sig/generated/prism/relocation.rbs +428 -0
  176. data/sig/generated/prism/serialize.rbs +200 -0
  177. data/sig/generated/prism/string_query.rbs +43 -0
  178. data/sig/generated/prism/translation.rbs +8 -0
  179. data/sig/generated/prism/visitor.rbs +1111 -0
  180. data/sig/generated/prism.rbs +84 -0
  181. data/sig/manifest.yaml +2 -0
  182. data/src/arena.c +122 -0
  183. data/src/{util/pm_buffer.c → buffer.c} +53 -21
  184. data/src/{util/pm_char.c → char.c} +28 -57
  185. data/src/comments.c +455 -0
  186. data/src/constant_pool.c +406 -0
  187. data/src/diagnostic.c +120 -68
  188. data/src/encoding.c +8 -3
  189. data/src/errors_format.c +519 -0
  190. data/src/{util/pm_integer.c → integer.c} +38 -22
  191. data/src/json.c +5733 -0
  192. data/src/line_offset_list.c +100 -0
  193. data/src/list.c +24 -0
  194. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  195. data/src/node.c +3465 -7787
  196. data/src/options.c +161 -54
  197. data/src/parser.c +371 -0
  198. data/src/prettyprint.c +421 -354
  199. data/src/prism.c +8503 -7280
  200. data/src/regexp.c +960 -45
  201. data/src/serialize.c +2434 -2048
  202. data/src/source.c +524 -0
  203. data/src/static_literals.c +303 -360
  204. data/src/string_query.c +180 -0
  205. data/src/stringy.c +61 -0
  206. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  207. data/src/strpbrk.c +447 -0
  208. data/src/{token_type.c → tokens.c} +39 -18
  209. data/src/xxh3.c +292 -0
  210. metadata +141 -63
  211. data/ext/prism/api_pack.c +0 -276
  212. data/include/prism/defines.h +0 -260
  213. data/include/prism/pack.h +0 -163
  214. data/include/prism/regexp.h +0 -43
  215. data/include/prism/static_literals.h +0 -121
  216. data/include/prism/util/pm_buffer.h +0 -236
  217. data/include/prism/util/pm_char.h +0 -204
  218. data/include/prism/util/pm_constant_pool.h +0 -218
  219. data/include/prism/util/pm_integer.h +0 -130
  220. data/include/prism/util/pm_memchr.h +0 -29
  221. data/include/prism/util/pm_newline_list.h +0 -113
  222. data/include/prism/util/pm_string.h +0 -200
  223. data/lib/prism/pack.rb +0 -230
  224. data/lib/prism/parse_result/comments.rb +0 -188
  225. data/rbi/prism/compiler.rbi +0 -12
  226. data/rbi/prism/dsl.rbi +0 -524
  227. data/rbi/prism/inspect_visitor.rbi +0 -12
  228. data/rbi/prism/node.rbi +0 -8750
  229. data/rbi/prism/node_ext.rbi +0 -107
  230. data/rbi/prism/parse_result.rbi +0 -404
  231. data/rbi/prism/reflection.rbi +0 -58
  232. data/rbi/prism/string_query.rbi +0 -12
  233. data/rbi/prism/visitor.rbi +0 -473
  234. data/rbi/prism.rbi +0 -63
  235. data/sig/prism/compiler.rbs +0 -9
  236. data/sig/prism/dispatcher.rbs +0 -19
  237. data/sig/prism/dot_visitor.rbs +0 -6
  238. data/sig/prism/dsl.rbs +0 -351
  239. data/sig/prism/inspect_visitor.rbs +0 -22
  240. data/sig/prism/lex_compat.rbs +0 -10
  241. data/sig/prism/node.rbs +0 -4042
  242. data/sig/prism/node_ext.rbs +0 -149
  243. data/sig/prism/pack.rbs +0 -43
  244. data/sig/prism/parse_result/comments.rbs +0 -38
  245. data/sig/prism/parse_result.rbs +0 -197
  246. data/sig/prism/pattern.rbs +0 -13
  247. data/sig/prism/reflection.rbs +0 -50
  248. data/sig/prism/relocation.rbs +0 -185
  249. data/sig/prism/serialize.rbs +0 -8
  250. data/sig/prism/string_query.rbs +0 -11
  251. data/sig/prism/visitor.rbs +0 -169
  252. data/sig/prism.rbs +0 -272
  253. data/src/pack.c +0 -509
  254. data/src/util/pm_constant_pool.c +0 -342
  255. data/src/util/pm_list.c +0 -49
  256. data/src/util/pm_newline_list.c +0 -125
  257. data/src/util/pm_string.c +0 -381
  258. data/src/util/pm_strpbrk.c +0 -206
@@ -0,0 +1,272 @@
1
+ /**
2
+ * @file node.hpp
3
+ *
4
+ * Checked casts between node structs, iteration over lists of nodes, and
5
+ * walking the tree from C++.
6
+ */
7
+ #ifndef PRISM_NODE_HPP
8
+ #define PRISM_NODE_HPP
9
+
10
+ #include "prism/compiler/cplusplus.hpp"
11
+ #include "prism/compiler/exceptions.hpp"
12
+ #include "prism/compiler/lifetimebound.hpp"
13
+
14
+ #include "prism/ast.hpp"
15
+ #include "prism/node.h"
16
+ #include "prism/node_type.hpp"
17
+
18
+ #include <cassert>
19
+ #include <cstddef>
20
+ #include <exception>
21
+ #include <functional>
22
+ #include <memory>
23
+ #include <type_traits>
24
+
25
+ namespace prism {
26
+
27
+ /**
28
+ * Whether the given node, which must not be null, is of the given struct
29
+ *
30
+ * @tparam T The node struct type to check against.
31
+ * @param node The node to check.
32
+ * @return True if the node is of the given struct, false otherwise.
33
+ */
34
+ template <typename T> [[nodiscard]] bool
35
+ isa(const pm_node_t *node) noexcept {
36
+ static_assert(is_node_v<T>, "isa<T> requires a node struct declared in ast.h");
37
+ assert(node != nullptr);
38
+ return node->type == node_type_v<T>;
39
+ }
40
+
41
+ /**
42
+ * Cast the given node, which must not be null and must be of the given struct,
43
+ * to that struct. The type is checked in debug builds.
44
+ *
45
+ * @tparam T The node struct type to cast to.
46
+ * @param node The node to cast.
47
+ * @return The node cast to the given struct.
48
+ */
49
+ template <typename T> [[nodiscard]] T *
50
+ cast(pm_node_t *node) noexcept {
51
+ assert(isa<T>(node));
52
+ return reinterpret_cast<T *>(node);
53
+ }
54
+
55
+ /**
56
+ * Cast the given node, which must not be null and must be of the given struct,
57
+ * to that struct. The type is checked in debug builds.
58
+ *
59
+ * @tparam T The node struct type to cast to.
60
+ * @param node The node to cast.
61
+ * @return The node cast to the given struct.
62
+ */
63
+ template <typename T> [[nodiscard]] const T *
64
+ cast(const pm_node_t *node) noexcept {
65
+ assert(isa<T>(node));
66
+ return reinterpret_cast<const T *>(node);
67
+ }
68
+
69
+ /**
70
+ * Cast the given node to the given struct if it is of that struct, and return
71
+ * null otherwise. The node may be null, in which case this returns null.
72
+ *
73
+ * @tparam T The node struct type to cast to.
74
+ * @param node The node to cast.
75
+ * @return The node cast to the given struct if it is of that struct, or null
76
+ * otherwise.
77
+ */
78
+ template <typename T> [[nodiscard]] T *
79
+ dyn_cast(pm_node_t *node) noexcept {
80
+ return (node != nullptr && isa<T>(node)) ? reinterpret_cast<T *>(node) : nullptr;
81
+ }
82
+
83
+ /**
84
+ * Cast the given node to the given struct if it is of that struct, and return
85
+ * null otherwise. The node may be null, in which case this returns null.
86
+ *
87
+ * @tparam T The node struct type to cast to.
88
+ * @param node The node to cast.
89
+ * @return The node cast to the given struct if it is of that struct, or null
90
+ * otherwise.
91
+ */
92
+ template <typename T> [[nodiscard]] const T *
93
+ dyn_cast(const pm_node_t *node) noexcept {
94
+ return (node != nullptr && isa<T>(node)) ? reinterpret_cast<const T *>(node) : nullptr;
95
+ }
96
+
97
+ /**
98
+ * Return the pm_node_t embedded at the start of the given node struct.
99
+ *
100
+ * @tparam T The node struct type.
101
+ * @param node The node struct to upcast.
102
+ * @return The pm_node_t embedded at the start of the node struct.
103
+ */
104
+ template <typename T> [[nodiscard]] pm_node_t *
105
+ upcast(T *node) noexcept {
106
+ static_assert(is_node_v<T>, "upcast requires a node struct declared in ast.h");
107
+ return &node->base;
108
+ }
109
+
110
+ /**
111
+ * Return the pm_node_t embedded at the start of the given node struct.
112
+ *
113
+ * @tparam T The node struct type.
114
+ * @param node The node struct to upcast.
115
+ * @return The pm_node_t embedded at the start of the node struct.
116
+ */
117
+ template <typename T> [[nodiscard]] const pm_node_t *
118
+ upcast(const T *node) noexcept {
119
+ static_assert(is_node_v<T>, "upcast requires a node struct declared in ast.h");
120
+ return &node->base;
121
+ }
122
+
123
+ /**
124
+ * The nodes in a list, usable in a range-based for loop. It refers to the list
125
+ * rather than copying it, so the list must outlive it.
126
+ */
127
+ class node_range {
128
+ public:
129
+ /**
130
+ * Create a range over the nodes in the given list.
131
+ *
132
+ * @param list The list of nodes to create a range for.
133
+ */
134
+ explicit node_range(const pm_node_list_t &list PRISM_LIFETIMEBOUND) noexcept : list_(&list) {}
135
+
136
+ /**
137
+ * A pointer to the first node.
138
+ *
139
+ * @return A pointer to the first node, or null if the list is empty.
140
+ */
141
+ pm_node_t *const *begin() const noexcept { return list_->nodes; }
142
+
143
+ /**
144
+ * A pointer one past the last node.
145
+ *
146
+ * @return A pointer one past the last node, or null if the list is empty.
147
+ */
148
+ pm_node_t *const *end() const noexcept { return list_->nodes + list_->size; }
149
+
150
+ /**
151
+ * The number of nodes.
152
+ *
153
+ * @return The number of nodes.
154
+ */
155
+ std::size_t size() const noexcept { return list_->size; }
156
+
157
+ /**
158
+ * Whether there are no nodes.
159
+ *
160
+ * @return True if there are no nodes, false otherwise.
161
+ */
162
+ [[nodiscard]] bool empty() const noexcept { return list_->size == 0; }
163
+
164
+ private:
165
+ const pm_node_list_t *list_;
166
+ };
167
+
168
+ namespace detail {
169
+
170
+ /**
171
+ * The state shared between a walk and its C callback, passed through the C
172
+ * function's void * data parameter.
173
+ */
174
+ template <typename F> struct visit_context {
175
+ /**
176
+ * The context for a visit, holding the visitor and any exception thrown.
177
+ *
178
+ * @param visitor The visitor callable to be used for each node.
179
+ */
180
+ explicit visit_context(F *visitor) noexcept : callable(visitor) {}
181
+
182
+ /** The visitor that the walk calls for each node. */
183
+ F *callable;
184
+
185
+ #ifdef PRISM_HAS_EXCEPTIONS
186
+ /** The exception thrown by the visitor, if any. */
187
+ std::exception_ptr exception;
188
+ #endif
189
+ };
190
+
191
+ /**
192
+ * Forwards the C visitor callback to a C++ callable. An exception cannot unwind
193
+ * through the C frames that call this function, so one thrown by the callable
194
+ * is caught here and the walk is stopped. The function that started the walk
195
+ * rethrows it once the C walk has returned. Without exceptions, the callable
196
+ * cannot throw, so it is called directly.
197
+ *
198
+ * @tparam F The type of the visitor callable.
199
+ * @param node The current node being visited.
200
+ * @param data The visit context containing the visitor and exception state.
201
+ * @return The result of the visit, indicating whether to descend, skip, or stop.
202
+ */
203
+ template <typename F> pm_visit_result_t
204
+ visit_callback(const pm_node_t *node, void *data) noexcept {
205
+ visit_context<F> *context = static_cast<visit_context<F> *>(data);
206
+
207
+ #ifdef PRISM_HAS_EXCEPTIONS
208
+ try {
209
+ return std::invoke(*context->callable, node);
210
+ } catch (...) {
211
+ context->exception = std::current_exception();
212
+ return PM_VISIT_STOP;
213
+ }
214
+ #else
215
+ return std::invoke(*context->callable, node);
216
+ #endif
217
+ }
218
+
219
+ }
220
+
221
+ /**
222
+ * Walk the tree rooted at the given node in pre-order, calling the visitor with
223
+ * each node. The visitor returns PM_VISIT_DESCEND to continue into that node's
224
+ * children, PM_VISIT_SKIP to continue past them, or PM_VISIT_STOP to end the
225
+ * walk. Returns false if the walk was stopped. If the visitor throws, the walk
226
+ * stops and the exception propagates from this function.
227
+ *
228
+ * @tparam F The type of the visitor callable.
229
+ * @param node The root node to start the walk from.
230
+ * @param visitor The visitor callable to invoke for each node.
231
+ * @return True if the walk completed, false if it was stopped.
232
+ */
233
+ template <typename F> bool
234
+ visit_node(const pm_node_t *node, F &&visitor) {
235
+ static_assert(std::is_invocable_r_v<pm_visit_result_t, F &, const pm_node_t *>, "visit_node requires a callable taking const pm_node_t * and returning pm_visit_result_t");
236
+ detail::visit_context<std::remove_reference_t<F>> context(std::addressof(visitor));
237
+ bool completed = pm_visit_node(node, detail::visit_callback<std::remove_reference_t<F>>, &context);
238
+
239
+ #ifdef PRISM_HAS_EXCEPTIONS
240
+ if (context.exception) std::rethrow_exception(context.exception);
241
+ #endif
242
+
243
+ return completed;
244
+ }
245
+
246
+ /**
247
+ * Walk the children of the given node, as visit_node does for every node whose
248
+ * visitor returns PM_VISIT_DESCEND. Returns false if the walk was stopped. If
249
+ * the visitor throws, the walk stops and the exception propagates from this
250
+ * function.
251
+ *
252
+ * @tparam F The type of the visitor callable.
253
+ * @param node The node whose children to walk.
254
+ * @param visitor The visitor callable to invoke for each child node.
255
+ * @return True if the walk completed, false if it was stopped.
256
+ */
257
+ template <typename F> bool
258
+ visit_child_nodes(const pm_node_t *node, F &&visitor) {
259
+ static_assert(std::is_invocable_r_v<pm_visit_result_t, F &, const pm_node_t *>, "visit_child_nodes requires a callable taking const pm_node_t * and returning pm_visit_result_t");
260
+ detail::visit_context<std::remove_reference_t<F>> context(std::addressof(visitor));
261
+ bool completed = pm_visit_child_nodes(node, detail::visit_callback<std::remove_reference_t<F>>, &context);
262
+
263
+ #ifdef PRISM_HAS_EXCEPTIONS
264
+ if (context.exception) std::rethrow_exception(context.exception);
265
+ #endif
266
+
267
+ return completed;
268
+ }
269
+
270
+ }
271
+
272
+ #endif
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @file node_type.hpp
3
+ *
4
+ * The traits that map node structs to node types. The specialization for each
5
+ * node struct is generated into ast.hpp.
6
+ */
7
+ #ifndef PRISM_NODE_TYPE_HPP
8
+ #define PRISM_NODE_TYPE_HPP
9
+
10
+ #include "prism/compiler/cplusplus.hpp"
11
+
12
+ #include "prism/ast.h"
13
+
14
+ #include <type_traits>
15
+
16
+ namespace prism {
17
+
18
+ /**
19
+ * Maps a node struct to its node type, as in node_type<pm_call_node_t>::value
20
+ * being PM_CALL_NODE. Only the node structs declared in ast.h have a value.
21
+ */
22
+ template <typename T> struct node_type {};
23
+
24
+ /** The node type of the given node struct. */
25
+ template <typename T> inline constexpr pm_node_type_t node_type_v = node_type<T>::value;
26
+
27
+ /** Whether the given type is one of the node structs declared in ast.h. */
28
+ template <typename T, typename = void> struct is_node : std::false_type {};
29
+
30
+ /** Whether the given type is one of the node structs declared in ast.h. */
31
+ template <typename T> struct is_node<T, std::void_t<decltype(node_type<T>::value)>> : std::true_type {};
32
+
33
+ /** Whether the given type is one of the node structs declared in ast.h. */
34
+ template <typename T> inline constexpr bool is_node_v = is_node<T>::value;
35
+
36
+ }
37
+
38
+ #endif