backburn 0.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 (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/Makefile +50 -0
  4. data/README.md +520 -0
  5. data/docs/cli-and-outputs.md +226 -0
  6. data/docs/technical-spec.md +656 -0
  7. data/exe/backburn +8 -0
  8. data/ext/backburn/extconf.rb +31 -0
  9. data/ext/backburn/prism/COMMIT +1 -0
  10. data/ext/backburn/prism/LICENSE.md +7 -0
  11. data/ext/backburn/prism/Makefile +213 -0
  12. data/ext/backburn/prism/include/prism/arena.h +45 -0
  13. data/ext/backburn/prism/include/prism/arena.hpp +42 -0
  14. data/ext/backburn/prism/include/prism/ast.h +10395 -0
  15. data/ext/backburn/prism/include/prism/ast.hpp +641 -0
  16. data/ext/backburn/prism/include/prism/buffer.h +60 -0
  17. data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
  18. data/ext/backburn/prism/include/prism/comments.h +72 -0
  19. data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
  20. data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
  21. data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
  22. data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
  23. data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
  24. data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
  25. data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
  26. data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
  27. data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
  28. data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
  29. data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
  30. data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
  31. data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
  32. data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
  33. data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
  34. data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
  35. data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
  36. data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
  37. data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
  38. data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
  39. data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
  40. data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
  41. data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
  42. data/ext/backburn/prism/include/prism/errors_format.h +63 -0
  43. data/ext/backburn/prism/include/prism/excludes.h +29 -0
  44. data/ext/backburn/prism/include/prism/integer.h +79 -0
  45. data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
  46. data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
  47. data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
  48. data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
  49. data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
  50. data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
  51. data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
  52. data/ext/backburn/prism/include/prism/internal/char.h +145 -0
  53. data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
  54. data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
  55. data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
  56. data/ext/backburn/prism/include/prism/internal/double.h +19 -0
  57. data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
  58. data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
  59. data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
  60. data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
  61. data/ext/backburn/prism/include/prism/internal/list.h +62 -0
  62. data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
  63. data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
  64. data/ext/backburn/prism/include/prism/internal/node.h +197 -0
  65. data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
  66. data/ext/backburn/prism/include/prism/internal/options.h +248 -0
  67. data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
  68. data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
  69. data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
  70. data/ext/backburn/prism/include/prism/internal/source.h +72 -0
  71. data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
  72. data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
  73. data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
  74. data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
  75. data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
  76. data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
  77. data/ext/backburn/prism/include/prism/json.h +40 -0
  78. data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
  79. data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
  80. data/ext/backburn/prism/include/prism/node.h +139 -0
  81. data/ext/backburn/prism/include/prism/node.hpp +272 -0
  82. data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
  83. data/ext/backburn/prism/include/prism/options.h +393 -0
  84. data/ext/backburn/prism/include/prism/options.hpp +43 -0
  85. data/ext/backburn/prism/include/prism/parser.h +433 -0
  86. data/ext/backburn/prism/include/prism/parser.hpp +274 -0
  87. data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
  88. data/ext/backburn/prism/include/prism/serialize.h +123 -0
  89. data/ext/backburn/prism/include/prism/source.h +163 -0
  90. data/ext/backburn/prism/include/prism/stream.h +36 -0
  91. data/ext/backburn/prism/include/prism/string_query.h +97 -0
  92. data/ext/backburn/prism/include/prism/stringy.h +61 -0
  93. data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
  94. data/ext/backburn/prism/include/prism/version.h +46 -0
  95. data/ext/backburn/prism/include/prism.h +123 -0
  96. data/ext/backburn/prism/include/prism.hpp +24 -0
  97. data/ext/backburn/prism/src/arena.c +200 -0
  98. data/ext/backburn/prism/src/bignum.c +399 -0
  99. data/ext/backburn/prism/src/buffer.c +405 -0
  100. data/ext/backburn/prism/src/char.c +289 -0
  101. data/ext/backburn/prism/src/comments.c +455 -0
  102. data/ext/backburn/prism/src/constant_pool.c +410 -0
  103. data/ext/backburn/prism/src/diagnostic.c +918 -0
  104. data/ext/backburn/prism/src/double.c +394 -0
  105. data/ext/backburn/prism/src/encoding.c +1701 -0
  106. data/ext/backburn/prism/src/errors_format.c +650 -0
  107. data/ext/backburn/prism/src/integer.c +675 -0
  108. data/ext/backburn/prism/src/json.c +5745 -0
  109. data/ext/backburn/prism/src/line_offset_list.c +99 -0
  110. data/ext/backburn/prism/src/list.c +24 -0
  111. data/ext/backburn/prism/src/memchr.c +37 -0
  112. data/ext/backburn/prism/src/node.c +4464 -0
  113. data/ext/backburn/prism/src/options.c +475 -0
  114. data/ext/backburn/prism/src/parser.c +371 -0
  115. data/ext/backburn/prism/src/prettyprint.c +9038 -0
  116. data/ext/backburn/prism/src/prism.c +23615 -0
  117. data/ext/backburn/prism/src/regexp.c +3833 -0
  118. data/ext/backburn/prism/src/serialize.c +2656 -0
  119. data/ext/backburn/prism/src/source.c +525 -0
  120. data/ext/backburn/prism/src/static_literals.c +562 -0
  121. data/ext/backburn/prism/src/string_query.c +195 -0
  122. data/ext/backburn/prism/src/stringy.c +77 -0
  123. data/ext/backburn/prism/src/strncasecmp.c +37 -0
  124. data/ext/backburn/prism/src/strpbrk.c +465 -0
  125. data/ext/backburn/prism/src/tokens.c +724 -0
  126. data/ext/backburn/prism/src/xxh3.c +292 -0
  127. data/lib/backburn/version.rb +7 -0
  128. data/presets/devise.roots +11 -0
  129. data/presets/fast-mcp.roots +5 -0
  130. data/presets/graphql.roots +6 -0
  131. data/presets/phlex.roots +7 -0
  132. data/presets/pundit.roots +3 -0
  133. data/presets/rails-framework.roots +10 -0
  134. data/presets/rails.roots +83 -0
  135. data/presets/sidekiq.roots +4 -0
  136. data/presets/thor.roots +3 -0
  137. data/rails.roots +99 -0
  138. data/scripts/embed-presets.sh +13 -0
  139. data/scripts/version-h.sh +7 -0
  140. data/src/analysis/analysis.c +95 -0
  141. data/src/analysis/analysis.h +24 -0
  142. data/src/analysis/batches.c +127 -0
  143. data/src/analysis/batches.h +17 -0
  144. data/src/analysis/graph.c +238 -0
  145. data/src/analysis/graph.h +34 -0
  146. data/src/analysis/mark.c +300 -0
  147. data/src/analysis/mark.h +21 -0
  148. data/src/analysis/roots.c +229 -0
  149. data/src/analysis/roots.h +29 -0
  150. data/src/cli/cli.c +237 -0
  151. data/src/cli/cli.h +9 -0
  152. data/src/cli/config.c +112 -0
  153. data/src/cli/config.h +14 -0
  154. data/src/cli/options.c +17 -0
  155. data/src/cli/options.h +35 -0
  156. data/src/commands/commands.h +11 -0
  157. data/src/commands/explain.c +255 -0
  158. data/src/commands/gemfile.c +126 -0
  159. data/src/commands/gemfile.h +27 -0
  160. data/src/commands/gems.c +408 -0
  161. data/src/commands/init.c +92 -0
  162. data/src/commands/roots.c +82 -0
  163. data/src/commands/scan.c +91 -0
  164. data/src/core/intern.c +94 -0
  165. data/src/core/intern.h +40 -0
  166. data/src/core/json.c +17 -0
  167. data/src/core/json.h +9 -0
  168. data/src/core/model.c +25 -0
  169. data/src/core/model.h +85 -0
  170. data/src/core/util.c +116 -0
  171. data/src/core/util.h +48 -0
  172. data/src/index/erb.c +40 -0
  173. data/src/index/erb.h +9 -0
  174. data/src/index/haml.c +334 -0
  175. data/src/index/haml.h +10 -0
  176. data/src/index/indexer.c +702 -0
  177. data/src/index/indexer.h +19 -0
  178. data/src/index/walk.c +144 -0
  179. data/src/index/walk.h +14 -0
  180. data/src/main.c +81 -0
  181. data/src/output/baseline.c +101 -0
  182. data/src/output/baseline.h +15 -0
  183. data/src/output/report.c +440 -0
  184. data/src/output/report.h +28 -0
  185. metadata +231 -0
@@ -0,0 +1,97 @@
1
+ /**
2
+ * @file string_query.h
3
+ *
4
+ * Functions for querying properties of strings, such as whether they are valid
5
+ * local variable names, constant names, or method names.
6
+ */
7
+ #ifndef PRISM_STRING_QUERY_H
8
+ #define PRISM_STRING_QUERY_H
9
+
10
+ #include "prism/compiler/exported.h"
11
+ #include "prism/compiler/nonnull.h"
12
+
13
+ #include <stddef.h>
14
+ #include <stdint.h>
15
+
16
+ #ifdef __cplusplus
17
+ extern "C" {
18
+ #endif
19
+
20
+ /**
21
+ * Represents the results of a slice query.
22
+ */
23
+ typedef enum {
24
+ /** Returned if the version given to a slice query was invalid. */
25
+ PM_STRING_QUERY_ERROR_VERSION = -2,
26
+
27
+ /** Returned if the encoding given to a slice query was invalid. */
28
+ PM_STRING_QUERY_ERROR = -1,
29
+
30
+ /** Returned if the result of the slice query is false. */
31
+ PM_STRING_QUERY_FALSE,
32
+
33
+ /** Returned if the result of the slice query is true. */
34
+ PM_STRING_QUERY_TRUE
35
+ } pm_string_query_t;
36
+
37
+ /**
38
+ * Check that the slice is a valid local variable name.
39
+ *
40
+ * @param source The source to check.
41
+ * @param length The length of the source.
42
+ * @param encoding_name The name of the encoding of the source.
43
+ * @param version The version of Ruby to answer for, as pm_options_version_set
44
+ * reads it, such as "3.4" or "latest". NULL is the latest version.
45
+ * @returns PM_STRING_QUERY_TRUE if the query is true, PM_STRING_QUERY_FALSE if
46
+ * the query is false, PM_STRING_QUERY_ERROR if the encoding was invalid, and
47
+ * PM_STRING_QUERY_ERROR_VERSION if the version was invalid.
48
+ */
49
+ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name, const char *version) PRISM_NONNULL(1, 3);
50
+
51
+ /**
52
+ * Check that the slice is a valid constant name.
53
+ *
54
+ * @param source The source to check.
55
+ * @param length The length of the source.
56
+ * @param encoding_name The name of the encoding of the source.
57
+ * @param version The version of Ruby to answer for, as pm_options_version_set
58
+ * reads it, such as "3.4" or "latest". NULL is the latest version.
59
+ * @returns PM_STRING_QUERY_TRUE if the query is true, PM_STRING_QUERY_FALSE if
60
+ * the query is false, PM_STRING_QUERY_ERROR if the encoding was invalid, and
61
+ * PM_STRING_QUERY_ERROR_VERSION if the version was invalid.
62
+ */
63
+ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name, const char *version) PRISM_NONNULL(1, 3);
64
+
65
+ /**
66
+ * Check that the slice is a valid method name.
67
+ *
68
+ * @param source The source to check.
69
+ * @param length The length of the source.
70
+ * @param encoding_name The name of the encoding of the source.
71
+ * @param version The version of Ruby to answer for, as pm_options_version_set
72
+ * reads it, such as "3.4" or "latest". NULL is the latest version.
73
+ * @returns PM_STRING_QUERY_TRUE if the query is true, PM_STRING_QUERY_FALSE if
74
+ * the query is false, PM_STRING_QUERY_ERROR if the encoding was invalid, and
75
+ * PM_STRING_QUERY_ERROR_VERSION if the version was invalid.
76
+ */
77
+ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name, const char *version) PRISM_NONNULL(1, 3);
78
+
79
+ /**
80
+ * Check that the slice is a keyword.
81
+ *
82
+ * @param source The source to check.
83
+ * @param length The length of the source.
84
+ * @param encoding_name The name of the encoding of the source.
85
+ * @param version The version of Ruby to answer for, as pm_options_version_set
86
+ * reads it, such as "3.4" or "latest". NULL is the latest version.
87
+ * @returns PM_STRING_QUERY_TRUE if the query is true, PM_STRING_QUERY_FALSE if
88
+ * the query is false, PM_STRING_QUERY_ERROR if the encoding was invalid, and
89
+ * PM_STRING_QUERY_ERROR_VERSION if the version was invalid.
90
+ */
91
+ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_keyword(const uint8_t *source, size_t length, const char *encoding_name, const char *version) PRISM_NONNULL(1, 3);
92
+
93
+ #ifdef __cplusplus
94
+ }
95
+ #endif
96
+
97
+ #endif
@@ -0,0 +1,61 @@
1
+ /**
2
+ * @file stringy.h
3
+ *
4
+ * A view of a sequence of bytes owned by someone else.
5
+ */
6
+ #ifndef PRISM_STRINGY_H
7
+ #define PRISM_STRINGY_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nonnull.h"
11
+
12
+ #include <stddef.h>
13
+ #include <stdint.h>
14
+
15
+ #ifdef __cplusplus
16
+ extern "C" {
17
+ #endif
18
+
19
+ /**
20
+ * A view of a sequence of bytes. The string does not own the bytes, so they
21
+ * must remain valid for as long as the string is in use.
22
+ */
23
+ typedef struct {
24
+ /** A pointer to the start of the string. */
25
+ const uint8_t *source;
26
+
27
+ /** The length of the string in bytes of memory. */
28
+ size_t length;
29
+ } pm_string_t;
30
+
31
+ /**
32
+ * Initialize a string that views the given bytes. The bytes are not copied, so
33
+ * they must remain valid for as long as the string is in use.
34
+ *
35
+ * @param string The string to initialize.
36
+ * @param source The start of the bytes.
37
+ * @param length The number of bytes.
38
+ */
39
+ PRISM_EXPORTED_FUNCTION void pm_string_init(pm_string_t *string, const uint8_t *source, size_t length) PRISM_NONNULL(1);
40
+
41
+ /**
42
+ * Returns the length associated with the string.
43
+ *
44
+ * @param string The string to get the length of.
45
+ * @returns The length of the string.
46
+ */
47
+ PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string) PRISM_NONNULL(1);
48
+
49
+ /**
50
+ * Returns the start pointer associated with the string.
51
+ *
52
+ * @param string The string to get the start pointer of.
53
+ * @returns The start pointer of the string.
54
+ */
55
+ PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string) PRISM_NONNULL(1);
56
+
57
+ #ifdef __cplusplus
58
+ }
59
+ #endif
60
+
61
+ #endif
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @file stringy.hpp
3
+ *
4
+ * Access to the contents of strings from C++.
5
+ */
6
+ #ifndef PRISM_STRINGY_HPP
7
+ #define PRISM_STRINGY_HPP
8
+
9
+ #include "prism/compiler/cplusplus.hpp"
10
+ #include "prism/compiler/lifetimebound.hpp"
11
+
12
+ #include "prism/stringy.h"
13
+
14
+ #include <string_view>
15
+
16
+ namespace prism {
17
+
18
+ /**
19
+ * The bytes of the given string.
20
+ *
21
+ * @param string The string to view.
22
+ * @return A view of the bytes of the string.
23
+ */
24
+ [[nodiscard]] inline std::string_view
25
+ view(const pm_string_t &string PRISM_LIFETIMEBOUND) noexcept {
26
+ return std::string_view(reinterpret_cast<const char *>(pm_string_source(&string)), pm_string_length(&string));
27
+ }
28
+
29
+ }
30
+
31
+ #endif
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @file version.h
3
+ *
4
+ * The version of the Prism library.
5
+ */
6
+ #ifndef PRISM_VERSION_H
7
+ #define PRISM_VERSION_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ /**
16
+ * The major version of the Prism library as an int.
17
+ */
18
+ #define PRISM_VERSION_MAJOR 2
19
+
20
+ /**
21
+ * The minor version of the Prism library as an int.
22
+ */
23
+ #define PRISM_VERSION_MINOR 1
24
+
25
+ /**
26
+ * The patch version of the Prism library as an int.
27
+ */
28
+ #define PRISM_VERSION_PATCH 0
29
+
30
+ /**
31
+ * The version of the Prism library as a constant string.
32
+ */
33
+ #define PRISM_VERSION "2.1.0"
34
+
35
+ /**
36
+ * The prism version and the serialization format.
37
+ *
38
+ * @returns The prism version as a constant string.
39
+ */
40
+ PRISM_EXPORTED_FUNCTION const char * pm_version(void);
41
+
42
+ #ifdef __cplusplus
43
+ }
44
+ #endif
45
+
46
+ #endif
@@ -0,0 +1,123 @@
1
+ /**
2
+ * @file prism.h
3
+ *
4
+ * The main header file for the prism parser.
5
+ */
6
+ #ifndef PRISM_H
7
+ #define PRISM_H
8
+
9
+ #include "prism/arena.h"
10
+ #include "prism/ast.h"
11
+ #include "prism/buffer.h"
12
+ #include "prism/diagnostic.h"
13
+ #include "prism/errors_format.h"
14
+ #include "prism/json.h"
15
+ #include "prism/node.h"
16
+ #include "prism/options.h"
17
+ #include "prism/parser.h"
18
+ #include "prism/prettyprint.h"
19
+ #include "prism/serialize.h"
20
+ #include "prism/source.h"
21
+ #include "prism/stream.h"
22
+ #include "prism/string_query.h"
23
+ #include "prism/version.h"
24
+
25
+ /**
26
+ * @mainpage
27
+ *
28
+ * Prism is a parser for the Ruby programming language. It is designed to be
29
+ * portable, error tolerant, and maintainable. It is written in C99 and has no
30
+ * dependencies. It is currently being integrated into
31
+ * [CRuby](https://github.com/ruby/ruby),
32
+ * [JRuby](https://github.com/jruby/jruby),
33
+ * [TruffleRuby](https://github.com/truffleruby/truffleruby),
34
+ * [Sorbet](https://github.com/sorbet/sorbet), and
35
+ * [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree).
36
+ *
37
+ * @section getting-started Getting started
38
+ *
39
+ * If you're vendoring this project and compiling it statically then as long as
40
+ * you have a C99 compiler you will be fine. If you're linking against it as
41
+ * shared library, then you should compile with `-fvisibility=hidden` and
42
+ * `-DPRISM_EXPORT_SYMBOLS` to tell prism to make only its public interface
43
+ * visible.
44
+ *
45
+ * @section parsing Parsing
46
+ *
47
+ * In order to parse Ruby code, the functions that you are going to want to use
48
+ * and be aware of are:
49
+ *
50
+ * * `pm_arena_new()` - create a new arena to hold all AST-lifetime allocations
51
+ * * `pm_parser_new()` - allocate and initialize a new parser
52
+ * * `pm_parse()` - parse and return the root node
53
+ * * `pm_parser_free()` - free the parser and its internal memory
54
+ * * `pm_arena_free()` - free all AST-lifetime memory
55
+ *
56
+ * Putting all of this together would look something like:
57
+ *
58
+ * ```c
59
+ * void parse(const uint8_t *source, size_t length) {
60
+ * pm_arena_t *arena = pm_arena_new();
61
+ * pm_parser_t *parser = pm_parser_new(arena, source, length, NULL);
62
+ *
63
+ * pm_node_t *root = pm_parse(parser);
64
+ * printf("PARSED!\n");
65
+ *
66
+ * pm_parser_free(parser);
67
+ * pm_arena_free(arena);
68
+ * }
69
+ * ```
70
+ *
71
+ * All of the nodes "inherit" from `pm_node_t` by embedding those structures
72
+ * as their first member. This means you can downcast and upcast any node in the
73
+ * tree to a `pm_node_t`.
74
+ *
75
+ * @section serializing Serializing
76
+ *
77
+ * Prism provides the ability to serialize the AST and its related metadata into
78
+ * a binary format. This format is designed to be portable to different
79
+ * languages and runtimes so that you only need to make one FFI call in order to
80
+ * parse Ruby code. The functions that you are going to want to use and be
81
+ * aware of are:
82
+ *
83
+ * * `pm_buffer_new()` - create a new buffer
84
+ * * `pm_buffer_free()` - free the buffer and its internal memory
85
+ * * `pm_serialize_parse()` - parse and serialize the AST into a buffer
86
+ *
87
+ * Putting all of this together would look something like:
88
+ *
89
+ * ```c
90
+ * void serialize(const uint8_t *source, size_t length) {
91
+ * pm_buffer_t *buffer = pm_buffer_new();
92
+ *
93
+ * pm_serialize_parse(buffer, source, length, NULL);
94
+ * printf("SERIALIZED!\n");
95
+ *
96
+ * pm_buffer_free(buffer);
97
+ * }
98
+ * ```
99
+ *
100
+ * @section inspecting Inspecting
101
+ *
102
+ * Prism provides the ability to inspect the AST by pretty-printing nodes. You
103
+ * can do this with the `pm_prettyprint()` function, which you would use like:
104
+ *
105
+ * ```c
106
+ * void prettyprint(const uint8_t *source, size_t length) {
107
+ * pm_arena_t *arena = pm_arena_new();
108
+ * pm_parser_t *parser = pm_parser_new(arena, source, length, NULL);
109
+ *
110
+ * pm_node_t *root = pm_parse(parser);
111
+ * pm_buffer_t *buffer = pm_buffer_new();
112
+ *
113
+ * pm_prettyprint(buffer, parser, root);
114
+ * printf("%*.s\n", (int) pm_buffer_length(buffer), pm_buffer_value(buffer));
115
+ *
116
+ * pm_buffer_free(buffer);
117
+ * pm_parser_free(parser);
118
+ * pm_arena_free(arena);
119
+ * }
120
+ * ```
121
+ */
122
+
123
+ #endif
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @file prism.hpp
3
+ *
4
+ * The main header file for the C++ interface to the prism parser. It adds
5
+ * ownership, checked casts between node types, and callable-based iteration on
6
+ * top of the C API. It never wraps the C structs: every function in it takes
7
+ * and returns the same pm_*_t types that prism.h declares, so the two can be
8
+ * mixed freely. It requires C++17 or later.
9
+ */
10
+ #ifndef PRISM_HPP
11
+ #define PRISM_HPP
12
+
13
+ #include "prism.h"
14
+
15
+ #include "prism/arena.hpp"
16
+ #include "prism/ast.hpp"
17
+ #include "prism/buffer.hpp"
18
+ #include "prism/node.hpp"
19
+ #include "prism/node_type.hpp"
20
+ #include "prism/options.hpp"
21
+ #include "prism/parser.hpp"
22
+ #include "prism/stringy.hpp"
23
+
24
+ #endif
@@ -0,0 +1,200 @@
1
+ #include "prism/internal/arena.h"
2
+
3
+ #include "prism/internal/allocator.h"
4
+ #include "prism/internal/assert.h"
5
+ #include "prism/internal/oom.h"
6
+
7
+ #include <stdint.h>
8
+ #include <stdlib.h>
9
+
10
+ /**
11
+ * Compute the block allocation size using offsetof so it is correct regardless
12
+ * of PM_FLEX_ARRAY_LENGTH.
13
+ */
14
+ #define PM_ARENA_BLOCK_SIZE(data_size) (offsetof(pm_arena_block_t, data) + (data_size))
15
+
16
+ /**
17
+ * Initial block data size: 8 KB. An embedder on a small heap can define a
18
+ * smaller one; a parse of a short program then takes blocks of that size.
19
+ */
20
+ #ifndef PM_ARENA_INITIAL_SIZE
21
+ #define PM_ARENA_INITIAL_SIZE 8192
22
+ #endif
23
+
24
+ /** Double the block size every this many blocks. */
25
+ #define PM_ARENA_GROWTH_INTERVAL 8
26
+
27
+ /** Maximum block data size: 1 MB. */
28
+ #define PM_ARENA_MAX_SIZE (1024 * 1024)
29
+
30
+ /**
31
+ * An allocation that does not fit in the current block and is larger than this
32
+ * fraction of the next block gets a block of its own. The current block keeps
33
+ * its free space for the allocations after it, so a block is only given up
34
+ * when a small allocation does not fit, with less than that much left unused.
35
+ */
36
+ #define PM_ARENA_LARGE_DIVISOR 2
37
+
38
+ /**
39
+ * Compute the data size for the next block.
40
+ */
41
+ static size_t
42
+ pm_arena_next_block_size(const pm_arena_t *arena) {
43
+ size_t size = PM_ARENA_INITIAL_SIZE;
44
+
45
+ for (size_t exp = PM_ARENA_GROWTH_INTERVAL; exp <= arena->block_count; exp += PM_ARENA_GROWTH_INTERVAL) {
46
+ if (size < PM_ARENA_MAX_SIZE) size *= 2;
47
+ }
48
+
49
+ return size;
50
+ }
51
+
52
+ /**
53
+ * Allocate a block with the given data capacity and initial usage, linked to
54
+ * the given previous block. Aborts on allocation failure.
55
+ */
56
+ static pm_arena_block_t *
57
+ pm_arena_block_alloc(pm_arena_block_t *prev, size_t data_size, size_t initial_used) {
58
+ PRISM_ASSERT(initial_used <= data_size);
59
+ if (PRISM_UNLIKELY(data_size > SIZE_MAX - PM_ARENA_BLOCK_SIZE(0))) {
60
+ pm_arena_alloc_overflow();
61
+ }
62
+
63
+ pm_arena_block_t *block = (pm_arena_block_t *) xmalloc(PM_ARENA_BLOCK_SIZE(data_size));
64
+ if (block == NULL) PRISM_OOM();
65
+
66
+ block->capacity = data_size;
67
+ block->used = initial_used;
68
+ block->prev = prev;
69
+
70
+ return block;
71
+ }
72
+
73
+ /**
74
+ * Allocate a new block with the given data capacity and initial usage, make it
75
+ * the current block, and return it.
76
+ */
77
+ static pm_arena_block_t *
78
+ pm_arena_block_new(pm_arena_t *arena, size_t data_size, size_t initial_used) {
79
+ pm_arena_block_t *block = pm_arena_block_alloc(arena->current, data_size, initial_used);
80
+ arena->current = block;
81
+ arena->block_count++;
82
+ return block;
83
+ }
84
+
85
+ /**
86
+ * Free every block in a chain linked through prev.
87
+ */
88
+ static void
89
+ pm_arena_blocks_free(pm_arena_block_t *block) {
90
+ while (block != NULL) {
91
+ pm_arena_block_t *prev = block->prev;
92
+ xfree_sized(block, PM_ARENA_BLOCK_SIZE(block->capacity));
93
+ block = prev;
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Ensure the arena has at least `capacity` bytes available in its current
99
+ * block, allocating a new block if necessary. This allows callers to
100
+ * pre-size the arena to avoid repeated small block allocations.
101
+ */
102
+ void
103
+ pm_arena_reserve(pm_arena_t *arena, size_t capacity) {
104
+ if (capacity <= PM_ARENA_INITIAL_SIZE) return;
105
+ if (arena->current != NULL && (arena->current->capacity - arena->current->used) >= capacity) return;
106
+ pm_arena_block_new(arena, capacity, 0);
107
+ }
108
+
109
+ /**
110
+ * Slow path for pm_arena_alloc: allocate a new block and return a pointer to
111
+ * the first `size` bytes. Called when the current block has insufficient space.
112
+ * A large allocation goes in a block of exactly its size on the large list,
113
+ * which leaves the current block in place.
114
+ */
115
+ void *
116
+ pm_arena_alloc_slow(pm_arena_t *arena, size_t size) {
117
+ size_t block_data_size = pm_arena_next_block_size(arena);
118
+
119
+ if (size > block_data_size / PM_ARENA_LARGE_DIVISOR) {
120
+ arena->large = pm_arena_block_alloc(arena->large, size, size);
121
+ return arena->large->data;
122
+ }
123
+
124
+ pm_arena_block_t *block = pm_arena_block_new(arena, block_data_size, size);
125
+ return block->data;
126
+ }
127
+
128
+ /**
129
+ * Allocate memory whose address is aligned to the given power of two. In the
130
+ * current block the address is known ahead of time, so this pads by exactly
131
+ * what it needs. A new block's address is only known once it exists, so that
132
+ * case asks for the worst case and gives the unused tail back to the block.
133
+ */
134
+ void *
135
+ pm_arena_alloc_aligned(pm_arena_t *arena, size_t size, size_t alignment) {
136
+ pm_arena_block_t *block = arena->current;
137
+
138
+ if (block != NULL) {
139
+ uintptr_t next = (uintptr_t) (block->data + block->used);
140
+ size_t padding = (size_t) (-next & (alignment - 1));
141
+ size_t available = block->capacity - block->used;
142
+
143
+ if (padding <= available && size <= available - padding) {
144
+ block->used += padding + size;
145
+ return (void *) (next + padding);
146
+ }
147
+ }
148
+
149
+ size_t mask = alignment - 1;
150
+ if (PRISM_UNLIKELY(size > SIZE_MAX - mask)) pm_arena_alloc_overflow();
151
+ uintptr_t start = (uintptr_t) pm_arena_alloc_slow(arena, size + mask);
152
+ uintptr_t aligned = (start + mask) & ~(uintptr_t) mask;
153
+
154
+ block = arena->current;
155
+ if (block != NULL && (uintptr_t) block->data == start) {
156
+ block->used = (size_t) (aligned - start) + size;
157
+ }
158
+
159
+ return (void *) aligned;
160
+ }
161
+
162
+ /**
163
+ * Report an allocation whose total size does not fit in a size_t. A
164
+ * consumer's PRISM_OOM is not required to stop execution, so this aborts after
165
+ * it to keep the promise that it never returns.
166
+ */
167
+ void
168
+ pm_arena_alloc_overflow(void) {
169
+ PRISM_OOM();
170
+ abort();
171
+ }
172
+
173
+ /**
174
+ * Returns a newly allocated and initialized arena.
175
+ */
176
+ pm_arena_t *
177
+ pm_arena_new(void) {
178
+ pm_arena_t *arena = (pm_arena_t *) xcalloc(1, sizeof(pm_arena_t));
179
+ if (arena == NULL) PRISM_OOM();
180
+ return arena;
181
+ }
182
+
183
+ /**
184
+ * Free all blocks in the arena.
185
+ */
186
+ void
187
+ pm_arena_cleanup(pm_arena_t *arena) {
188
+ pm_arena_blocks_free(arena->current);
189
+ pm_arena_blocks_free(arena->large);
190
+ *arena = (pm_arena_t) { 0 };
191
+ }
192
+
193
+ /**
194
+ * Frees both the held memory and the arena itself.
195
+ */
196
+ void
197
+ pm_arena_free(pm_arena_t *arena) {
198
+ pm_arena_cleanup(arena);
199
+ xfree_sized(arena, sizeof(pm_arena_t));
200
+ }