herb 0.11.1 → 0.11.3

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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +2 -0
  3. data/Rakefile +4 -2
  4. data/config.yml +13 -8
  5. data/ext/herb/error_helpers.c +3 -3
  6. data/ext/herb/extconf.rb +8 -31
  7. data/ext/herb/nodes.c +75 -75
  8. data/ext/herb/parser_options_helpers.c +26 -0
  9. data/lib/herb/analysis/partial_index.rb +1 -1
  10. data/lib/herb/analysis/partial_resolution.rb +27 -0
  11. data/lib/herb/ast/erb_render_node.rb +6 -2
  12. data/lib/herb/ast/helpers.rb +1 -1
  13. data/lib/herb/cli.rb +7 -3
  14. data/lib/herb/configuration.rb +11 -1
  15. data/lib/herb/dev/classifier.rb +1 -1
  16. data/lib/herb/dev/runner.rb +5 -2
  17. data/lib/herb/engine/compiler.rb +44 -12
  18. data/lib/herb/engine/slots/dynamics_compiler.rb +54 -15
  19. data/lib/herb/engine/slots/state_compiler.rb +9 -1
  20. data/lib/herb/engine/slots/state_overrides.rb +17 -3
  21. data/lib/herb/engine/slots/subtree_compiler.rb +1 -1
  22. data/lib/herb/engine/slots/visitor.rb +69 -11
  23. data/lib/herb/engine/visitors/instrumentation_visitor.rb +18 -6
  24. data/lib/herb/engine.rb +11 -1
  25. data/lib/herb/errors.rb +7 -7
  26. data/lib/herb/parser_options.rb +6 -3
  27. data/lib/herb/project.rb +19 -8
  28. data/lib/herb/version.rb +1 -1
  29. data/lib/herb.rb +1 -1
  30. data/sig/herb/analysis/partial_resolution.rbs +8 -0
  31. data/sig/herb/ast/erb_render_node.rbs +2 -0
  32. data/sig/herb/configuration.rbs +3 -0
  33. data/sig/herb/dev/runner.rbs +2 -2
  34. data/sig/herb/engine/compiler.rbs +9 -0
  35. data/sig/herb/engine/slots/dynamics_compiler.rbs +24 -7
  36. data/sig/herb/engine/slots/state_compiler.rbs +3 -0
  37. data/sig/herb/engine/slots/state_overrides.rbs +11 -2
  38. data/sig/herb/engine/slots/visitor.rbs +21 -0
  39. data/sig/herb/engine/visitors/instrumentation_visitor.rbs +5 -1
  40. data/sig/herb/engine.rbs +2 -0
  41. data/sig/herb/errors.rbs +2 -2
  42. data/sig/herb/parser_options.rbs +6 -4
  43. data/sig/herb.rbs +2 -2
  44. data/sig/serialized_ast_errors.rbs +1 -1
  45. data/src/analyze/action_view/attribute_extraction_helpers.c +40 -21
  46. data/src/analyze/action_view/generated_handlers.c +36 -36
  47. data/src/analyze/action_view/image_tag.c +7 -3
  48. data/src/analyze/action_view/javascript_include_tag.c +6 -3
  49. data/src/analyze/action_view/link_to.c +23 -23
  50. data/src/analyze/action_view/registry.c +7 -3
  51. data/src/analyze/action_view/tag_helper_node_builders.c +6 -13
  52. data/src/analyze/action_view/tag_helpers.c +160 -97
  53. data/src/analyze/action_view/turbo_frame_tag.c +5 -4
  54. data/src/analyze/analyze.c +52 -29
  55. data/src/analyze/analyze_helpers.c +161 -188
  56. data/src/analyze/analyzed_ruby.c +8 -7
  57. data/src/analyze/control_type.c +29 -22
  58. data/src/analyze/herb_directives.c +16 -20
  59. data/src/analyze/iteration_nodes.c +69 -24
  60. data/src/analyze/parse_errors.c +32 -22
  61. data/src/analyze/postfix_conditionals.c +23 -13
  62. data/src/analyze/prism_annotate.c +29 -28
  63. data/src/analyze/render_nodes.c +239 -78
  64. data/src/analyze/strict_locals.c +14 -15
  65. data/src/analyze/ternary_conditionals.c +14 -10
  66. data/src/diff/herb_diff_children.c +1 -1
  67. data/src/diff/herb_diff_nodes.c +1 -9
  68. data/src/diff/herb_hash_tree.c +1 -1
  69. data/src/errors.c +17 -22
  70. data/src/extract.c +47 -7
  71. data/src/herb.c +10 -8
  72. data/src/include/analyze/action_view/tag_helper_handler.h +1 -1
  73. data/src/include/analyze/action_view/tag_helper_node_builders.h +1 -0
  74. data/src/include/analyze/action_view/tag_helpers.h +1 -1
  75. data/src/include/analyze/analyze.h +3 -2
  76. data/src/include/analyze/analyzed_ruby.h +4 -2
  77. data/src/include/analyze/helpers.h +6 -6
  78. data/src/include/errors.h +3 -3
  79. data/src/include/herb.h +3 -2
  80. data/src/include/parser/parser_options.h +5 -0
  81. data/src/include/prism/prism_context.h +12 -12
  82. data/src/include/prism/prism_helpers.h +30 -0
  83. data/src/include/prism_version.h +11 -0
  84. data/src/include/version.h +1 -1
  85. data/src/lexer/token.c +2 -2
  86. data/src/parser/dot_notation.c +15 -5
  87. data/src/parser/parser_helpers.c +1 -2
  88. data/src/parser.c +9 -8
  89. data/src/parser_options_defaults.c +1 -0
  90. data/src/prism/prism_helpers.c +84 -50
  91. data/src/prism/ruby_parser.c +20 -20
  92. data/templates/ext/herb/nodes.c.erb +5 -5
  93. data/templates/ext/herb/parser_options_helpers.c.erb +33 -0
  94. data/templates/java/nodes.c.erb +6 -6
  95. data/templates/java/parser_options_helpers.c.erb +41 -2
  96. data/templates/java/parser_options_helpers.h.erb +2 -1
  97. data/templates/javascript/packages/node/extension/nodes.cpp.erb +5 -5
  98. data/templates/javascript/packages/node/extension/parser_options_helpers.cpp.erb +34 -2
  99. data/templates/javascript/packages/node/extension/parser_options_helpers.h.erb +1 -1
  100. data/templates/lib/herb/parser_options.rb.erb +1 -1
  101. data/templates/rust/src/prism/generated_deserialize.rs.erb +90 -44
  102. data/templates/rust/src/prism/generated_prettyprint.rs.erb +18 -27
  103. data/templates/src/analyze/action_view/generated_handlers.c.erb +23 -22
  104. data/templates/src/diff/herb_diff_nodes.c.erb +1 -9
  105. data/templates/src/diff/herb_hash_tree.c.erb +1 -1
  106. data/templates/src/errors.c.erb +2 -2
  107. data/templates/src/include/parser/parser_options.h.erb +10 -0
  108. data/templates/src/include/prism_version.h.erb +17 -0
  109. data/templates/src/parser_options_defaults.c.erb +1 -1
  110. data/templates/template.rb +17 -2
  111. data/templates/wasm/nodes.cpp.erb +5 -5
  112. data/templates/wasm/parser_options_helpers.cpp.erb +24 -0
  113. data/vendor/prism/Rakefile +76 -7
  114. data/vendor/prism/config.yml +174 -161
  115. data/vendor/prism/include/prism/arena.h +45 -0
  116. data/vendor/prism/include/prism/arena.hpp +42 -0
  117. data/vendor/prism/include/prism/ast.h +2738 -611
  118. data/vendor/prism/include/prism/ast.hpp +641 -0
  119. data/vendor/prism/include/prism/buffer.h +60 -0
  120. data/vendor/prism/include/prism/buffer.hpp +55 -0
  121. data/vendor/prism/include/prism/comments.h +72 -0
  122. data/vendor/prism/include/prism/compiler/accel.h +26 -0
  123. data/vendor/prism/include/prism/compiler/align.h +36 -0
  124. data/vendor/prism/include/prism/compiler/assume.h +25 -0
  125. data/vendor/prism/include/prism/compiler/branch.h +26 -0
  126. data/vendor/prism/include/prism/compiler/bswap.h +46 -0
  127. data/vendor/prism/include/prism/compiler/cplusplus.hpp +16 -0
  128. data/vendor/prism/include/prism/compiler/endianness.h +34 -0
  129. data/vendor/prism/include/prism/compiler/exceptions.hpp +17 -0
  130. data/vendor/prism/include/prism/compiler/exported.h +24 -0
  131. data/vendor/prism/include/prism/compiler/fallthrough.h +22 -0
  132. data/vendor/prism/include/prism/compiler/filesystem.h +32 -0
  133. data/vendor/prism/include/prism/compiler/flex_array.h +19 -0
  134. data/vendor/prism/include/prism/compiler/force_inline.h +21 -0
  135. data/vendor/prism/include/prism/compiler/format.h +27 -0
  136. data/vendor/prism/include/prism/compiler/inline.h +17 -0
  137. data/vendor/prism/include/prism/compiler/lifetimebound.hpp +25 -0
  138. data/vendor/prism/include/prism/compiler/nodiscard.h +22 -0
  139. data/vendor/prism/include/prism/compiler/nonnull.h +18 -0
  140. data/vendor/prism/include/prism/compiler/unused.h +18 -0
  141. data/vendor/prism/include/prism/constant_pool.h +106 -0
  142. data/vendor/prism/include/prism/diagnostic.h +55 -402
  143. data/vendor/prism/include/prism/errors_format.h +61 -0
  144. data/vendor/prism/include/prism/excludes.h +29 -0
  145. data/vendor/prism/include/prism/integer.h +41 -0
  146. data/vendor/prism/include/prism/internal/allocator.h +68 -0
  147. data/vendor/prism/include/prism/internal/allocator_debug.h +88 -0
  148. data/vendor/prism/include/prism/internal/arena.h +108 -0
  149. data/vendor/prism/include/prism/internal/bit.h +42 -0
  150. data/vendor/prism/include/prism/internal/buffer.h +94 -0
  151. data/vendor/prism/include/prism/internal/char.h +145 -0
  152. data/vendor/prism/include/prism/internal/comments.h +54 -0
  153. data/vendor/prism/include/prism/internal/constant_pool.h +152 -0
  154. data/vendor/prism/include/prism/internal/diagnostic.h +389 -0
  155. data/vendor/prism/include/prism/{encoding.h → internal/encoding.h} +27 -68
  156. data/vendor/prism/include/prism/internal/integer.h +68 -0
  157. data/vendor/prism/include/prism/internal/isinf.h +16 -0
  158. data/vendor/prism/include/prism/internal/line_offset_list.h +34 -0
  159. data/vendor/prism/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  160. data/vendor/prism/include/prism/internal/magic_comments.h +23 -0
  161. data/vendor/prism/include/prism/internal/memchr.h +15 -0
  162. data/vendor/prism/include/prism/internal/node.h +42 -0
  163. data/vendor/prism/include/prism/internal/options.h +232 -0
  164. data/vendor/prism/include/prism/internal/parser.h +1015 -0
  165. data/vendor/prism/include/prism/internal/regexp.h +26 -0
  166. data/vendor/prism/include/prism/internal/serialize.h +39 -0
  167. data/vendor/prism/include/prism/internal/source.h +72 -0
  168. data/vendor/prism/include/prism/internal/static_literals.h +60 -0
  169. data/vendor/prism/include/prism/internal/stringy.h +30 -0
  170. data/vendor/prism/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  171. data/vendor/prism/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  172. data/vendor/prism/include/prism/internal/tokens.h +11 -0
  173. data/vendor/prism/include/prism/internal/xxh3.h +16 -0
  174. data/vendor/prism/include/prism/json.h +40 -0
  175. data/vendor/prism/include/prism/line_offset_list.h +69 -0
  176. data/vendor/prism/include/prism/magic_comments.h +53 -0
  177. data/vendor/prism/include/prism/node.h +59 -49
  178. data/vendor/prism/include/prism/node.hpp +272 -0
  179. data/vendor/prism/include/prism/node_type.hpp +38 -0
  180. data/vendor/prism/include/prism/options.h +154 -279
  181. data/vendor/prism/include/prism/options.hpp +43 -0
  182. data/vendor/prism/include/prism/parser.h +367 -870
  183. data/vendor/prism/include/prism/parser.hpp +274 -0
  184. data/vendor/prism/include/prism/prettyprint.h +14 -9
  185. data/vendor/prism/include/prism/serialize.h +123 -0
  186. data/vendor/prism/include/prism/source.h +163 -0
  187. data/vendor/prism/include/prism/stream.h +36 -0
  188. data/vendor/prism/include/prism/string_query.h +82 -0
  189. data/vendor/prism/include/prism/stringy.h +61 -0
  190. data/vendor/prism/include/prism/stringy.hpp +31 -0
  191. data/vendor/prism/include/prism/version.h +20 -3
  192. data/vendor/prism/include/prism.h +35 -320
  193. data/vendor/prism/include/prism.hpp +24 -0
  194. data/vendor/prism/src/arena.c +122 -0
  195. data/vendor/prism/src/{util/pm_buffer.c → buffer.c} +53 -21
  196. data/vendor/prism/src/{util/pm_char.c → char.c} +28 -57
  197. data/vendor/prism/src/comments.c +455 -0
  198. data/vendor/prism/src/constant_pool.c +406 -0
  199. data/vendor/prism/src/diagnostic.c +120 -68
  200. data/vendor/prism/src/encoding.c +8 -3
  201. data/vendor/prism/src/errors_format.c +519 -0
  202. data/vendor/prism/src/{util/pm_integer.c → integer.c} +38 -22
  203. data/vendor/prism/src/json.c +5733 -0
  204. data/vendor/prism/src/line_offset_list.c +100 -0
  205. data/vendor/prism/src/list.c +24 -0
  206. data/vendor/prism/src/{util/pm_memchr.c → memchr.c} +8 -6
  207. data/vendor/prism/src/node.c +3465 -7787
  208. data/vendor/prism/src/options.c +161 -54
  209. data/vendor/prism/src/parser.c +371 -0
  210. data/vendor/prism/src/prettyprint.c +421 -354
  211. data/vendor/prism/src/prism.c +8522 -7280
  212. data/vendor/prism/src/regexp.c +960 -45
  213. data/vendor/prism/src/serialize.c +2434 -2048
  214. data/vendor/prism/src/source.c +524 -0
  215. data/vendor/prism/src/static_literals.c +303 -360
  216. data/vendor/prism/src/string_query.c +180 -0
  217. data/vendor/prism/src/stringy.c +61 -0
  218. data/vendor/prism/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  219. data/vendor/prism/src/strpbrk.c +447 -0
  220. data/vendor/prism/src/{token_type.c → tokens.c} +39 -18
  221. data/vendor/prism/src/xxh3.c +292 -0
  222. data/vendor/prism/templates/ext/prism/api_node.c.erb +58 -44
  223. data/vendor/prism/templates/include/prism/ast.h.erb +67 -17
  224. data/vendor/prism/templates/include/prism/ast.hpp.erb +31 -0
  225. data/vendor/prism/templates/include/prism/internal/diagnostic.h.erb +60 -0
  226. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/AbstractNodeVisitor.java.erb +1 -1
  227. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Loader.java.erb +232 -133
  228. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Nodes.java.erb +62 -23
  229. data/vendor/prism/templates/javascript/src/deserialize.js.erb +153 -146
  230. data/vendor/prism/templates/javascript/src/nodes.js.erb +4 -9
  231. data/vendor/prism/templates/lib/prism/compiler.rb.erb +14 -5
  232. data/vendor/prism/templates/lib/prism/dispatcher.rb.erb +20 -12
  233. data/vendor/prism/templates/lib/prism/dot_visitor.rb.erb +37 -11
  234. data/vendor/prism/templates/lib/prism/dsl.rb.erb +43 -4
  235. data/vendor/prism/templates/lib/prism/inspect_visitor.rb.erb +26 -10
  236. data/vendor/prism/templates/lib/prism/mutation_compiler.rb.erb +5 -2
  237. data/vendor/prism/templates/lib/prism/node.rb.erb +283 -105
  238. data/vendor/prism/templates/lib/prism/reflection.rb.erb +11 -2
  239. data/vendor/prism/templates/lib/prism/serialize.rb.erb +291 -134
  240. data/vendor/prism/templates/lib/prism/visitor.rb.erb +18 -0
  241. data/vendor/prism/templates/src/diagnostic.c.erb +114 -63
  242. data/vendor/prism/templates/src/json.c.erb +130 -0
  243. data/vendor/prism/templates/src/node.c.erb +79 -236
  244. data/vendor/prism/templates/src/prettyprint.c.erb +24 -13
  245. data/vendor/prism/templates/src/serialize.c.erb +369 -183
  246. data/vendor/prism/templates/src/{token_type.c.erb → tokens.c.erb} +22 -11
  247. data/vendor/prism/templates/template.rb +193 -91
  248. metadata +100 -42
  249. data/vendor/prism/include/prism/defines.h +0 -260
  250. data/vendor/prism/include/prism/pack.h +0 -163
  251. data/vendor/prism/include/prism/regexp.h +0 -43
  252. data/vendor/prism/include/prism/static_literals.h +0 -121
  253. data/vendor/prism/include/prism/util/pm_buffer.h +0 -236
  254. data/vendor/prism/include/prism/util/pm_char.h +0 -204
  255. data/vendor/prism/include/prism/util/pm_constant_pool.h +0 -218
  256. data/vendor/prism/include/prism/util/pm_integer.h +0 -130
  257. data/vendor/prism/include/prism/util/pm_memchr.h +0 -29
  258. data/vendor/prism/include/prism/util/pm_newline_list.h +0 -113
  259. data/vendor/prism/include/prism/util/pm_string.h +0 -200
  260. data/vendor/prism/src/pack.c +0 -509
  261. data/vendor/prism/src/util/pm_constant_pool.c +0 -342
  262. data/vendor/prism/src/util/pm_list.c +0 -49
  263. data/vendor/prism/src/util/pm_newline_list.c +0 -125
  264. data/vendor/prism/src/util/pm_string.c +0 -381
  265. data/vendor/prism/src/util/pm_strpbrk.c +0 -206
  266. data/vendor/prism/templates/include/prism/diagnostic.h.erb +0 -130
  267. data/vendor/prism/templates/rbi/prism/dsl.rbi.erb +0 -68
  268. data/vendor/prism/templates/rbi/prism/node.rbi.erb +0 -167
  269. data/vendor/prism/templates/rbi/prism/visitor.rbi.erb +0 -18
  270. data/vendor/prism/templates/sig/prism/_private/dot_visitor.rbs.erb +0 -45
  271. data/vendor/prism/templates/sig/prism/dsl.rbs.erb +0 -31
  272. data/vendor/prism/templates/sig/prism/mutation_compiler.rbs.erb +0 -7
  273. data/vendor/prism/templates/sig/prism/node.rbs.erb +0 -136
  274. data/vendor/prism/templates/sig/prism/visitor.rbs.erb +0 -17
  275. data/vendor/prism/templates/sig/prism.rbs.erb +0 -92
@@ -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