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,70 @@
1
+ #ifndef PRISM_INTERNAL_REGEXP_H
2
+ #define PRISM_INTERNAL_REGEXP_H
3
+
4
+ #include "prism/ast.h"
5
+ #include "prism/compiler/inline.h"
6
+ #include "prism/internal/parser.h"
7
+
8
+ /* The name in the preprocessed pattern and its range in the original source. */
9
+ typedef struct {
10
+ const uint8_t *name;
11
+ size_t length;
12
+ pm_location_t location;
13
+ } pm_regexp_capture_t;
14
+
15
+ /* Captures saved until the parser has read the right operand of =~. */
16
+ typedef struct pm_regexp_match {
17
+ const pm_node_t *receiver;
18
+ pm_regexp_capture_t *captures;
19
+ size_t size;
20
+ } pm_regexp_match_t;
21
+
22
+ /*
23
+ * Append a place to a list of places. When the list is full, it moves to a
24
+ * larger array in the scratch arena, which releases the old one when parsing
25
+ * finishes.
26
+ */
27
+ void pm_regexp_offsets_append(pm_parser_t *parser, pm_regexp_offsets_t *offsets, pm_regexp_offset_t offset);
28
+
29
+ /*
30
+ * Record that the given offset in the content is at the given offset in the
31
+ * source, if the distance between them changed since the last place. The base
32
+ * is the offset in the source of the start of the content, which gives the
33
+ * distance before the first place.
34
+ *
35
+ * Text that is dropped, like a line continuation, leaves two places at the
36
+ * same content offset. Only the second one is kept, so the places stay in
37
+ * order with no two at the same offset.
38
+ */
39
+ static PRISM_INLINE void
40
+ pm_regexp_offsets_record(pm_parser_t *parser, pm_regexp_offsets_t *offsets, uint32_t content, uint32_t source, uint32_t base) {
41
+ pm_regexp_offset_t *last = offsets->size == 0 ? NULL : &offsets->values[offsets->size - 1];
42
+
43
+ /* Each distance is a source offset minus a content offset. Both fit in 32
44
+ * bits, so two distances differ by less than 2^32, and their subtractions,
45
+ * which may wrap, are equal exactly when the distances are. */
46
+ uint32_t previous = (last == NULL) ? base : (last->source - last->content);
47
+ if (source - content == previous) return;
48
+
49
+ if (last != NULL && last->content == content) {
50
+ last->source = source;
51
+ } else {
52
+ pm_regexp_offsets_append(parser, offsets, (pm_regexp_offset_t) { .content = content, .source = source });
53
+ }
54
+ }
55
+
56
+ /*
57
+ * Parse a regular expression and validate its encoding. The offsets are where
58
+ * the unescaped content of the node stops lining up with the source, as the
59
+ * lexer recorded them. Returns the encoding flags to set on the node.
60
+ */
61
+ PRISM_EXPORTED_FUNCTION pm_node_flags_t pm_regexp_parse(pm_parser_t *parser, pm_regular_expression_node_t *node, const pm_regexp_offsets_t *offsets);
62
+
63
+ /*
64
+ * Validate a regular expression with interpolation. One that is a static
65
+ * literal is validated as the regular expression that its parts join to, with
66
+ * errors reported at the node.
67
+ */
68
+ void pm_regexp_parse_interpolated(pm_parser_t *parser, const pm_interpolated_regular_expression_node_t *node);
69
+
70
+ #endif
@@ -0,0 +1,39 @@
1
+ #ifndef PRISM_INTERNAL_SERIALIZE_H
2
+ #define PRISM_INTERNAL_SERIALIZE_H
3
+
4
+ #include "prism/internal/encoding.h"
5
+ #include "prism/internal/list.h"
6
+
7
+ #include "prism/ast.h"
8
+ #include "prism/buffer.h"
9
+ #include "prism/excludes.h"
10
+ #include "prism/parser.h"
11
+
12
+ /* We optionally support serializing to a binary string. For systems that do not
13
+ * want or need this functionality, it can be turned off with the
14
+ * PRISM_EXCLUDE_SERIALIZATION define. */
15
+ #ifndef PRISM_EXCLUDE_SERIALIZATION
16
+
17
+ /*
18
+ * Serialize the given list of offsets to the given buffer.
19
+ */
20
+ void pm_serialize_line_offset_list(pm_line_offset_list_t *list, pm_buffer_t *buffer);
21
+
22
+ /*
23
+ * Serialize the given list of comments to the given buffer.
24
+ */
25
+ void pm_serialize_comment_list(pm_list_t *list, pm_buffer_t *buffer);
26
+
27
+ /*
28
+ * Serialize the name of the encoding to the buffer.
29
+ */
30
+ void pm_serialize_encoding(const pm_encoding_t *encoding, pm_buffer_t *buffer);
31
+
32
+ /*
33
+ * Serialize the encoding, metadata, nodes, and constant pool.
34
+ */
35
+ void pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer);
36
+
37
+ #endif
38
+
39
+ #endif
@@ -0,0 +1,72 @@
1
+ #ifndef PRISM_INTERNAL_SOURCE_H
2
+ #define PRISM_INTERNAL_SOURCE_H
3
+
4
+ #include "prism/source.h"
5
+ #include "prism/buffer.h"
6
+
7
+ #include <stdbool.h>
8
+
9
+ /*
10
+ * The type of source, which determines cleanup behavior.
11
+ */
12
+ typedef enum {
13
+ /* Wraps existing constant memory, no cleanup. */
14
+ PM_SOURCE_CONSTANT,
15
+
16
+ /* Wraps existing shared memory (non-owning slice), no cleanup. */
17
+ PM_SOURCE_SHARED,
18
+
19
+ /* Owns a heap-allocated buffer, freed on cleanup. */
20
+ PM_SOURCE_OWNED,
21
+
22
+ /* Memory-mapped file, unmapped on cleanup. */
23
+ PM_SOURCE_MAPPED,
24
+
25
+ /* Stream source backed by a pm_buffer_t. */
26
+ PM_SOURCE_STREAM
27
+ } pm_source_type_t;
28
+
29
+ /*
30
+ * The internal representation of a source.
31
+ */
32
+ struct pm_source_t {
33
+ /* A pointer to the start of the source data. */
34
+ const uint8_t *source;
35
+
36
+ /* The length of the source data in bytes. */
37
+ size_t length;
38
+
39
+ /* The type of the source. */
40
+ pm_source_type_t type;
41
+
42
+ /* Stream-specific data, only used for PM_SOURCE_STREAM sources. */
43
+ struct {
44
+ /* The buffer that holds the accumulated stream data. */
45
+ pm_buffer_t *buffer;
46
+
47
+ /* The stream object to read from. */
48
+ void *stream;
49
+
50
+ /* The function to use to read from the stream. */
51
+ pm_source_stream_fgets_t *fgets;
52
+
53
+ /* The function to use to check if the stream is at EOF. */
54
+ pm_source_stream_feof_t *feof;
55
+
56
+ /* Whether the stream has reached EOF. */
57
+ bool eof;
58
+ } stream;
59
+ };
60
+
61
+ /*
62
+ * Read from a stream into the source's internal buffer. This is used by
63
+ * pm_parse_stream to incrementally read the source.
64
+ */
65
+ bool pm_source_stream_read(pm_source_t *source);
66
+
67
+ /*
68
+ * Returns whether the stream source has reached EOF.
69
+ */
70
+ bool pm_source_stream_eof(const pm_source_t *source);
71
+
72
+ #endif
@@ -0,0 +1,60 @@
1
+ #ifndef PRISM_INTERNAL_STATIC_LITERALS_H
2
+ #define PRISM_INTERNAL_STATIC_LITERALS_H
3
+
4
+ #include "prism/ast.h"
5
+ #include "prism/buffer.h"
6
+ #include "prism/parser.h"
7
+
8
+ /*
9
+ * Certain sets of nodes (hash keys and when clauses) check for duplicate nodes
10
+ * to alert the user of potential issues. To do this, we keep a set of the nodes
11
+ * that have been seen so far, and compare whenever we find a new node.
12
+ */
13
+ typedef struct {
14
+ /* The node stored in this slot, or NULL if the slot is empty. */
15
+ pm_node_t *node;
16
+
17
+ /* The hash of the node, kept so that it is never recomputed. */
18
+ uint64_t hash;
19
+ } pm_static_literals_entry_t;
20
+
21
+ /*
22
+ * The number of nodes a set holds directly before it moves them into a hash
23
+ * table. Most sets are this small, so they are searched linearly without
24
+ * hashing their nodes or allocating any memory.
25
+ */
26
+ #define PM_STATIC_LITERALS_INLINE_CAPACITY 4
27
+
28
+ typedef struct {
29
+ /*
30
+ * The nodes in the set while it has no hash table. Once the set outgrows
31
+ * this array, its nodes move into the hash table and this is unused.
32
+ */
33
+ pm_node_t *nodes[PM_STATIC_LITERALS_INLINE_CAPACITY];
34
+
35
+ /* The array of entries in the hash table, or NULL if there is none. */
36
+ pm_static_literals_entry_t *entries;
37
+
38
+ /* The number of nodes in the set. */
39
+ uint32_t size;
40
+
41
+ /* The number of entries allocated in the hash table, or 0 if there is none. */
42
+ uint32_t capacity;
43
+ } pm_static_literals_t;
44
+
45
+ /*
46
+ * Add a node to the set of static literals.
47
+ */
48
+ pm_node_t * pm_static_literals_add(const pm_parser_t *parser, pm_static_literals_t *literals, pm_node_t *node, bool replace);
49
+
50
+ /*
51
+ * Free the internal memory associated with the given static literals set.
52
+ */
53
+ void pm_static_literals_free(pm_static_literals_t *literals);
54
+
55
+ /*
56
+ * Create a string-based representation of the given static literal.
57
+ */
58
+ void pm_static_literal_inspect(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node);
59
+
60
+ #endif
@@ -0,0 +1,37 @@
1
+ #ifndef PRISM_INTERNAL_STRINGY_H
2
+ #define PRISM_INTERNAL_STRINGY_H
3
+
4
+ #include "prism/stringy.h"
5
+
6
+ #include <stdbool.h>
7
+
8
+ /*
9
+ * Backing storage for an empty string.
10
+ */
11
+ static const uint8_t empty_source[] = "";
12
+
13
+ /*
14
+ * Defines an empty string. This is useful for initializing a string that will
15
+ * be filled in later.
16
+ */
17
+ #define PM_STRING_EMPTY ((pm_string_t) { .source = empty_source, .length = 0 })
18
+
19
+ /*
20
+ * Initialize a shared string that is based on initial input.
21
+ */
22
+ void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end);
23
+
24
+ /*
25
+ * Compare the underlying lengths and bytes of two strings. Returns 0 if the
26
+ * strings are equal, a negative number if the left string is less than the
27
+ * right string, and a positive number if the left string is greater than the
28
+ * right string.
29
+ */
30
+ int pm_string_compare(const pm_string_t *left, const pm_string_t *right);
31
+
32
+ /*
33
+ * Returns true if every byte of the string is ASCII.
34
+ */
35
+ bool pm_string_ascii_only_p(const pm_string_t *string);
36
+
37
+ #endif
@@ -0,0 +1,18 @@
1
+ #ifndef PRISM_INTERNAL_STRNCASECMP_H
2
+ #define PRISM_INTERNAL_STRNCASECMP_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ /*
8
+ * Compare two strings, ignoring case, up to the given length. Returns 0 if the
9
+ * strings are equal, a negative number if string1 is less than string2, or a
10
+ * positive number if string1 is greater than string2.
11
+ *
12
+ * Note that this is effectively our own implementation of strncasecmp, but it's
13
+ * not available on all of the platforms we want to support so we're rolling it
14
+ * here.
15
+ */
16
+ int pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length);
17
+
18
+ #endif
@@ -0,0 +1,61 @@
1
+ #ifndef PRISM_INTERNAL_STRPBRK_H
2
+ #define PRISM_INTERNAL_STRPBRK_H
3
+
4
+ #include "prism/parser.h"
5
+
6
+ /* The size in bytes of a strpbrk charset, including its NUL padding. */
7
+ #define PM_STRPBRK_CACHE_SIZE 16
8
+
9
+ #include <stddef.h>
10
+ #include <stdint.h>
11
+
12
+ /*
13
+ * The kind of content that pm_strpbrk searches, which decides how it reports
14
+ * the bytes it skips over that do not begin a character in the encoding of the
15
+ * source.
16
+ */
17
+ typedef enum {
18
+ /*
19
+ * String content, where each run of such bytes is one error, and each
20
+ * character that is not ASCII sets the explicit encoding of the string.
21
+ */
22
+ PM_STRPBRK_STRING,
23
+
24
+ /*
25
+ * Regular expression content, where each such byte is its own error. The
26
+ * encoding of a regular expression is resolved after it is lexed.
27
+ */
28
+ PM_STRPBRK_REGEXP,
29
+
30
+ /*
31
+ * Content that is kept byte for byte, like the body of a single-quoted
32
+ * heredoc, where no byte is an error and none sets the explicit encoding.
33
+ */
34
+ PM_STRPBRK_RAW
35
+ } pm_strpbrk_mode_t;
36
+
37
+ /*
38
+ * Here we have rolled our own version of strpbrk. The standard library strpbrk
39
+ * has undefined behavior when the source string is not null-terminated. We want
40
+ * to support strings that are not null-terminated because pm_parse does not
41
+ * have the contract that the string is null-terminated. (This is desirable
42
+ * because it means the extension can call pm_parse with the result of a call to
43
+ * mmap).
44
+ *
45
+ * The standard library strpbrk also does not support passing a maximum length
46
+ * to search. We want to support this for the reason mentioned above, but we
47
+ * also don't want it to stop on null bytes. Ruby actually allows null bytes
48
+ * within strings, comments, regular expressions, etc. So we need to be able to
49
+ * skip past them.
50
+ *
51
+ * Finally, we want to support encodings wherein the charset could contain
52
+ * characters that are trailing bytes of multi-byte characters. For example, in
53
+ * Shift-JIS, the backslash character can be a trailing byte. In that case we
54
+ * need to take a slower path and iterate one multi-byte character at a time.
55
+ *
56
+ * The charset must have PM_STRPBRK_CACHE_SIZE bytes, padded with NULs after
57
+ * its ASCII breakpoints, so the scanner can compare the full cache key.
58
+ */
59
+ const uint8_t * pm_strpbrk(pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, ptrdiff_t length, pm_strpbrk_mode_t mode);
60
+
61
+ #endif
@@ -0,0 +1,11 @@
1
+ #ifndef PRISM_INTERNAL_TOKENS_H
2
+ #define PRISM_INTERNAL_TOKENS_H
3
+
4
+ #include "prism/ast.h"
5
+
6
+ /*
7
+ * Returns the human name of the given token type.
8
+ */
9
+ const char * pm_token_str(pm_token_type_t token_type);
10
+
11
+ #endif
@@ -0,0 +1,16 @@
1
+ #ifndef PRISM_INTERNAL_XXH3_H
2
+ #define PRISM_INTERNAL_XXH3_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ /**
8
+ * Computes the XXH3 digest of the given input.
9
+ *
10
+ * @param input The input data to hash.
11
+ * @param length The length of the input data.
12
+ * @return The 64-bit XXH3 hash of the input data.
13
+ */
14
+ uint64_t pm_xxh3(const uint8_t *input, size_t length);
15
+
16
+ #endif
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @file json.h
3
+ */
4
+ #ifndef PRISM_JSON_H
5
+ #define PRISM_JSON_H
6
+
7
+ #include "prism/excludes.h"
8
+
9
+ /* We optionally support dumping to JSON. For systems that don't want or need
10
+ * this functionality, it can be turned off with the PRISM_EXCLUDE_JSON define.
11
+ */
12
+ #ifndef PRISM_EXCLUDE_JSON
13
+
14
+ #include "prism/compiler/exported.h"
15
+ #include "prism/compiler/nonnull.h"
16
+
17
+ #include "prism/ast.h"
18
+ #include "prism/buffer.h"
19
+ #include "prism/parser.h"
20
+
21
+ #ifdef __cplusplus
22
+ extern "C" {
23
+ #endif
24
+
25
+ /**
26
+ * Dump JSON to the given buffer.
27
+ *
28
+ * @param buffer The buffer to serialize to.
29
+ * @param parser The parser that parsed the node.
30
+ * @param node The node to serialize.
31
+ */
32
+ PRISM_EXPORTED_FUNCTION void pm_dump_json(pm_buffer_t *buffer, const pm_parser_t *parser, const pm_node_t *node) PRISM_NONNULL(1, 2, 3);
33
+
34
+ #ifdef __cplusplus
35
+ }
36
+ #endif
37
+
38
+ #endif
39
+
40
+ #endif
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @file line_offset_list.h
3
+ *
4
+ * A list of byte offsets of newlines in a string.
5
+ *
6
+ * When compiling the syntax tree, it's necessary to know the line and column
7
+ * of many nodes. This is necessary to support things like error messages,
8
+ * tracepoints, etc.
9
+ *
10
+ * It's possible that we could store the start line, start column, end line, and
11
+ * end column on every node in addition to the offsets that we already store,
12
+ * but that would be quite a lot of memory overhead.
13
+ */
14
+ #ifndef PRISM_LINE_OFFSET_LIST_H
15
+ #define PRISM_LINE_OFFSET_LIST_H
16
+
17
+ #include "prism/compiler/exported.h"
18
+ #include "prism/compiler/nonnull.h"
19
+
20
+ #include <stddef.h>
21
+ #include <stdint.h>
22
+
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ /**
28
+ * A list of offsets of the start of lines in a string. The offsets are assumed
29
+ * to be sorted/inserted in ascending order.
30
+ */
31
+ typedef struct {
32
+ /** The number of offsets in the list. */
33
+ size_t size;
34
+
35
+ /** The capacity of the list that has been allocated. */
36
+ size_t capacity;
37
+
38
+ /** The list of offsets. */
39
+ uint32_t *offsets;
40
+ } pm_line_offset_list_t;
41
+
42
+ /**
43
+ * A line and column in a string.
44
+ */
45
+ typedef struct {
46
+ /** The line number. */
47
+ int32_t line;
48
+
49
+ /** The column in bytes. */
50
+ uint32_t column;
51
+ } pm_line_column_t;
52
+
53
+ /**
54
+ * Returns the line and column of the given offset. If the offset is not in the
55
+ * list, the line and column of the closest offset less than the given offset
56
+ * are returned.
57
+ *
58
+ * @param list The list to search.
59
+ * @param cursor The offset to search for.
60
+ * @param start_line The line to start counting from.
61
+ * @returns The line and column of the given offset.
62
+ */
63
+ PRISM_EXPORTED_FUNCTION pm_line_column_t pm_line_offset_list_line_column(const pm_line_offset_list_t *list, uint32_t cursor, int32_t start_line) PRISM_NONNULL(1);
64
+
65
+ #ifdef __cplusplus
66
+ }
67
+ #endif
68
+
69
+ #endif
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @file magic_comments.h
3
+ *
4
+ * Types and functions related to magic comments found during parsing.
5
+ */
6
+ #ifndef PRISM_MAGIC_COMMENTS_H
7
+ #define PRISM_MAGIC_COMMENTS_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nonnull.h"
11
+
12
+ #include "prism/ast.h"
13
+
14
+ #include <stddef.h>
15
+
16
+ #ifdef __cplusplus
17
+ extern "C" {
18
+ #endif
19
+
20
+ /** An opaque pointer to a magic comment found while parsing. */
21
+ typedef struct pm_magic_comment_t pm_magic_comment_t;
22
+
23
+ /**
24
+ * Returns the location of the key associated with the given magic comment.
25
+ *
26
+ * @param magic_comment the magic comment whose key location we want to get
27
+ * @returns the location of the key associated with the given magic comment
28
+ */
29
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_key(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1);
30
+
31
+ /**
32
+ * Returns the location of the value associated with the given magic comment.
33
+ *
34
+ * @param magic_comment the magic comment whose value location we want to get
35
+ * @returns the location of the value associated with the given magic comment
36
+ */
37
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_value(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1);
38
+
39
+ /**
40
+ * Returns the magic comment that follows the given magic comment among the
41
+ * magic comments associated with the parser that found it.
42
+ *
43
+ * @param magic_comment the magic comment whose successor we want to get
44
+ * @returns the next magic comment, or NULL if the given magic comment is the
45
+ * last one
46
+ */
47
+ PRISM_EXPORTED_FUNCTION const pm_magic_comment_t * pm_magic_comment_next(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1);
48
+
49
+ #ifdef __cplusplus
50
+ }
51
+ #endif
52
+
53
+ #endif
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @file node.h
3
+ *
4
+ * Functions related to nodes in the AST.
5
+ */
6
+ #ifndef PRISM_NODE_H
7
+ #define PRISM_NODE_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nonnull.h"
11
+
12
+ #include "prism/ast.h"
13
+
14
+ #ifdef __cplusplus
15
+ extern "C" {
16
+ #endif
17
+
18
+ /**
19
+ * Loop through each node in the node list, writing each node to the given
20
+ * pm_node_t pointer.
21
+ */
22
+ #define PM_NODE_LIST_FOREACH(list, index, node) \
23
+ for (size_t index = 0; index < (list)->size && ((node) = (list)->nodes[index]); index++)
24
+
25
+ /**
26
+ * Append a node to the end of a node list. A node list's storage is always
27
+ * exactly its size, so it is regrown to exactly one more node by allocating from
28
+ * the given arena, which must live as long as the list does. For a list in a
29
+ * parsed tree, that is the arena that was passed to pm_parser_new.
30
+ *
31
+ * @param arena The arena to allocate the list's storage from.
32
+ * @param list The list to append to.
33
+ * @param node The node to append.
34
+ */
35
+ PRISM_EXPORTED_FUNCTION void pm_node_list_append(pm_arena_t *arena, pm_node_list_t *list, pm_node_t *node) PRISM_NONNULL(1, 2, 3);
36
+
37
+ /**
38
+ * Returns a string representation of the given node type.
39
+ *
40
+ * @param node_type The node type to convert to a string.
41
+ * @returns A string representation of the given node type.
42
+ */
43
+ PRISM_EXPORTED_FUNCTION const char * pm_node_type(pm_node_type_t node_type);
44
+
45
+ /**
46
+ * The value returned by a visitor callback, which determines where the walk
47
+ * goes next.
48
+ */
49
+ typedef enum {
50
+ /** Continue the walk without visiting this node's children. */
51
+ PM_VISIT_SKIP,
52
+
53
+ /** Continue the walk by visiting this node's children. */
54
+ PM_VISIT_DESCEND,
55
+
56
+ /** End the walk without visiting any more nodes. */
57
+ PM_VISIT_STOP
58
+ } pm_visit_result_t;
59
+
60
+ /**
61
+ * A callback that is called for each node visited by pm_visit_node and
62
+ * pm_visit_child_nodes. The data parameter is the opaque pointer that was
63
+ * passed to the function that started the walk.
64
+ */
65
+ typedef pm_visit_result_t (*pm_visit_callback_t)(const pm_node_t *node, void *data);
66
+
67
+ /**
68
+ * Visit each of the nodes in this subtree using the given visitor callback. The
69
+ * callback function will be called for each node in the subtree, and its
70
+ * return value determines whether the walk visits that node's children, skips
71
+ * them, or stops entirely. The data parameter is treated as an opaque pointer
72
+ * and is passed to the visitor callback for consumers to use as they see fit.
73
+ *
74
+ * As an example:
75
+ *
76
+ * ```c
77
+ * #include "prism.h"
78
+ *
79
+ * pm_visit_result_t visit(const pm_node_t *node, void *data) {
80
+ * size_t *indent = (size_t *) data;
81
+ * for (size_t i = 0; i < *indent * 2; i++) putc(' ', stdout);
82
+ * printf("%s\n", pm_node_type(node->type));
83
+ *
84
+ * size_t next_indent = *indent + 1;
85
+ * size_t *next_data = &next_indent;
86
+ * pm_visit_child_nodes(node, visit, next_data);
87
+ *
88
+ * return PM_VISIT_SKIP;
89
+ * }
90
+ *
91
+ * int main(void) {
92
+ * const char *source = "1 + 2; 3 + 4";
93
+ * size_t size = strlen(source);
94
+ *
95
+ * pm_arena_t *arena = pm_arena_new();
96
+ * pm_options_t *options = pm_options_new();
97
+ *
98
+ * pm_parser_t *parser = pm_parser_new(arena, (const uint8_t *) source, size, options);
99
+ *
100
+ * size_t indent = 0;
101
+ * pm_node_t *node = pm_parse(parser);
102
+ *
103
+ * size_t *data = &indent;
104
+ * pm_visit_node(node, visit, data);
105
+ *
106
+ * pm_parser_free(parser);
107
+ * pm_options_free(options);
108
+ * pm_arena_free(arena);
109
+ *
110
+ * return EXIT_SUCCESS;
111
+ * }
112
+ * ```
113
+ *
114
+ * @param node The root node to start visiting from.
115
+ * @param visitor The callback to call for each node in the subtree.
116
+ * @param data An opaque pointer that is passed to the visitor callback.
117
+ * @returns false if the visitor stopped the walk by returning PM_VISIT_STOP,
118
+ * true otherwise.
119
+ */
120
+ PRISM_EXPORTED_FUNCTION bool pm_visit_node(const pm_node_t *node, pm_visit_callback_t visitor, void *data) PRISM_NONNULL(1);
121
+
122
+ /**
123
+ * Visit the children of the given node with the given callback. This is the
124
+ * default behavior for walking the tree that is called from pm_visit_node if
125
+ * the callback returns PM_VISIT_DESCEND.
126
+ *
127
+ * @param node The node to visit the children of.
128
+ * @param visitor The callback to call for each child node.
129
+ * @param data An opaque pointer that is passed to the visitor callback.
130
+ * @returns false if the visitor stopped the walk by returning PM_VISIT_STOP,
131
+ * true otherwise.
132
+ */
133
+ PRISM_EXPORTED_FUNCTION bool pm_visit_child_nodes(const pm_node_t *node, pm_visit_callback_t visitor, void *data) PRISM_NONNULL(1);
134
+
135
+ #ifdef __cplusplus
136
+ }
137
+ #endif
138
+
139
+ #endif