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,24 @@
1
+ /**
2
+ * @file compiler/noreturn.h
3
+ */
4
+ #ifndef PRISM_COMPILER_NORETURN_H
5
+ #define PRISM_COMPILER_NORETURN_H
6
+
7
+ /**
8
+ * Mark a function as never returning to its caller, so that the compiler can
9
+ * treat the code after a call to it as unreachable. Every form of this goes
10
+ * before the declaration.
11
+ */
12
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
13
+ # define PRISM_NORETURN [[noreturn]]
14
+ #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
15
+ # define PRISM_NORETURN _Noreturn
16
+ #elif defined(__GNUC__) || defined(__clang__)
17
+ # define PRISM_NORETURN __attribute__((__noreturn__))
18
+ #elif defined(_MSC_VER)
19
+ # define PRISM_NORETURN __declspec(noreturn)
20
+ #else
21
+ # define PRISM_NORETURN
22
+ #endif
23
+
24
+ #endif
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @file compiler/stack.h
3
+ */
4
+ #ifndef PRISM_COMPILER_STACK_H
5
+ #define PRISM_COMPILER_STACK_H
6
+
7
+ #include "prism/compiler/inline.h"
8
+
9
+ #include <stdint.h>
10
+
11
+ #if defined(__FILC__)
12
+ #include <stdfil.h>
13
+ #endif
14
+
15
+ #if defined(__has_builtin)
16
+ #if __has_builtin(__builtin_stack_address)
17
+ #define PRISM_HAS_BUILTIN_STACK_ADDRESS
18
+ #endif
19
+ #endif
20
+
21
+ #if defined(_MSC_VER)
22
+ #include <intrin.h>
23
+ #endif
24
+
25
+ /**
26
+ * Returns the current position of the C stack, for measuring how much of it has
27
+ * been consumed since an earlier call. The stack pointer is read directly where
28
+ * the compiler or architecture allows it. Unlike __builtin_frame_address, that
29
+ * does not force the calling function to set up a frame pointer, which costs a
30
+ * register on targets that omit frame pointers by default (x86-64 at -O2), and
31
+ * unlike the address of a local it does not add a slot to the frame.
32
+ */
33
+ static PRISM_INLINE uintptr_t
34
+ pm_stack_address(void) {
35
+ #if defined(__FILC__)
36
+ return (uintptr_t) zstack_pointer();
37
+ #elif defined(PRISM_HAS_BUILTIN_STACK_ADDRESS)
38
+ return (uintptr_t) __builtin_stack_address();
39
+ #elif defined(_MSC_VER) && defined(_M_ARM64)
40
+ return (uintptr_t) __getReg(31);
41
+ #elif defined(_MSC_VER)
42
+ return (uintptr_t) _AddressOfReturnAddress();
43
+ #elif defined(__GNUC__) && defined(__x86_64__)
44
+ uintptr_t sp;
45
+ __asm__ ("{movq %%rsp, %0|mov %0, rsp}" : "=r" (sp));
46
+ return sp;
47
+ #elif defined(__GNUC__) && defined(__aarch64__)
48
+ /* The register is 64 bits even where pointers are 32 (arm64_32). */
49
+ uint64_t sp;
50
+ __asm__ ("mov %0, sp" : "=r" (sp));
51
+ return (uintptr_t) sp;
52
+ #elif defined(__GNUC__)
53
+ return (uintptr_t) __builtin_frame_address(0);
54
+ #else
55
+ volatile char marker = 0;
56
+ return (uintptr_t) &marker;
57
+ #endif
58
+ }
59
+
60
+ #endif
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @file compiler/unused.h
3
+ */
4
+ #ifndef PRISM_COMPILER_UNUSED_H
5
+ #define PRISM_COMPILER_UNUSED_H
6
+
7
+ /**
8
+ * GCC will warn if you specify a function or parameter that is unused at
9
+ * runtime. This macro allows you to mark a function or parameter as unused in a
10
+ * compiler-agnostic way.
11
+ */
12
+ #if defined(__GNUC__)
13
+ # define PRISM_UNUSED __attribute__((unused))
14
+ #else
15
+ # define PRISM_UNUSED
16
+ #endif
17
+
18
+ #endif
@@ -0,0 +1,112 @@
1
+ /**
2
+ * @file constant_pool.h
3
+ *
4
+ * A data structure that stores a set of strings.
5
+ *
6
+ * Each string is assigned a unique id, which can be used to compare strings for
7
+ * equality. This comparison ends up being much faster than strcmp, since it
8
+ * only requires a single integer comparison.
9
+ */
10
+ #ifndef PRISM_CONSTANT_POOL_H
11
+ #define PRISM_CONSTANT_POOL_H
12
+
13
+ #include "prism/compiler/exported.h"
14
+ #include "prism/compiler/inline.h"
15
+ #include "prism/compiler/nodiscard.h"
16
+ #include "prism/compiler/nonnull.h"
17
+
18
+ #include "prism/arena.h"
19
+
20
+ #include <stddef.h>
21
+ #include <stdint.h>
22
+
23
+ #ifdef __cplusplus
24
+ extern "C" {
25
+ #endif
26
+
27
+ /**
28
+ * A constant id is a unique identifier for a constant in the constant pool.
29
+ */
30
+ typedef uint32_t pm_constant_id_t;
31
+
32
+ /**
33
+ * Returns the index of the constant with the given id among the constants of
34
+ * the parser that produced it. The indices run from 0 to one less than
35
+ * pm_parser_constants_size, so they can index a cache with one entry per
36
+ * constant that was filled using pm_parser_constant_at.
37
+ *
38
+ * @param constant_id the id of the constant. Optional constant fields on nodes
39
+ * hold 0 when they are absent, which is not a valid id, so check for it
40
+ * before calling this function.
41
+ * @returns the index of the constant
42
+ */
43
+ static PRISM_INLINE size_t
44
+ pm_constant_id_index(pm_constant_id_t constant_id) {
45
+ return (size_t) constant_id - 1;
46
+ }
47
+
48
+ /**
49
+ * A list of constant IDs. Usually used to represent a set of locals.
50
+ */
51
+ typedef struct {
52
+ /**
53
+ * The number of constant ids in the list. Source offsets are 32 bits, so
54
+ * the source is less than 4 GiB, and a list built from it stays far below
55
+ * what this can count.
56
+ */
57
+ uint32_t size;
58
+
59
+ /** The constant ids in the list. */
60
+ pm_constant_id_t *ids;
61
+ } pm_constant_id_list_t;
62
+
63
+ /** A constant in the pool which effectively stores a string. */
64
+ typedef struct pm_constant_t pm_constant_t;
65
+
66
+ /**
67
+ * The overall constant pool, which stores constants found while parsing.
68
+ */
69
+ typedef struct pm_constant_pool_t pm_constant_pool_t;
70
+
71
+ /**
72
+ * Return a raw pointer to the start of a constant.
73
+ *
74
+ * @param constant The constant to get the start of.
75
+ * @returns A raw pointer to the start of the constant.
76
+ */
77
+ PRISM_EXPORTED_FUNCTION const uint8_t * pm_constant_start(const pm_constant_t *constant) PRISM_NONNULL(1);
78
+
79
+ /**
80
+ * Return the length of a constant.
81
+ *
82
+ * @param constant The constant to get the length of.
83
+ * @returns The length of the constant.
84
+ */
85
+ PRISM_EXPORTED_FUNCTION size_t pm_constant_length(const pm_constant_t *constant) PRISM_NONNULL(1);
86
+
87
+ /**
88
+ * Initialize a list of constant ids.
89
+ *
90
+ * @param arena The arena to use for allocations.
91
+ * @param list The list to initialize.
92
+ * @param size The size of the list.
93
+ */
94
+ PRISM_EXPORTED_FUNCTION void pm_constant_id_list_init(pm_arena_t *arena, pm_constant_id_list_t *list, uint32_t size) PRISM_NONNULL(1, 2);
95
+
96
+ /**
97
+ * Append a constant id to a list of constant ids. A list's storage is always
98
+ * exactly its size, so it is regrown to exactly one more id by allocating from
99
+ * the given arena, which must live as long as the list does. For a list in a
100
+ * parsed tree, that is the arena that was passed to pm_parser_new.
101
+ *
102
+ * @param arena The arena to use for allocations.
103
+ * @param list The list to append to.
104
+ * @param id The constant id to append.
105
+ */
106
+ PRISM_EXPORTED_FUNCTION void pm_constant_id_list_append(pm_arena_t *arena, pm_constant_id_list_t *list, pm_constant_id_t id) PRISM_NONNULL(1, 2);
107
+
108
+ #ifdef __cplusplus
109
+ }
110
+ #endif
111
+
112
+ #endif
@@ -0,0 +1,121 @@
1
+ /**
2
+ * @file diagnostic.h
3
+ *
4
+ * A list of diagnostics generated during parsing.
5
+ */
6
+ #ifndef PRISM_DIAGNOSTIC_H
7
+ #define PRISM_DIAGNOSTIC_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nodiscard.h"
11
+ #include "prism/compiler/nonnull.h"
12
+
13
+ #include "prism/ast.h"
14
+
15
+ #ifdef __cplusplus
16
+ extern "C" {
17
+ #endif
18
+
19
+ /**
20
+ * An opaque pointer to a diagnostic generated during parsing.
21
+ */
22
+ typedef struct pm_diagnostic_t pm_diagnostic_t;
23
+
24
+ /**
25
+ * The levels of errors generated during parsing.
26
+ */
27
+ typedef enum {
28
+ /** For errors that should raise a syntax error. */
29
+ PM_ERROR_LEVEL_SYNTAX = 0,
30
+
31
+ /** For errors that should raise an argument error. */
32
+ PM_ERROR_LEVEL_ARGUMENT = 1,
33
+
34
+ /** For errors that should raise a load error. */
35
+ PM_ERROR_LEVEL_LOAD = 2,
36
+
37
+ /**
38
+ * For errors that should raise a system stack error. These mean that the
39
+ * parser ran out of the stack it was given, not that the source is
40
+ * invalid, so the same source may parse with more stack.
41
+ */
42
+ PM_ERROR_LEVEL_STACK = 3,
43
+
44
+ /** For errors that should raise an encoding compatibility error. */
45
+ PM_ERROR_LEVEL_ENCODING_COMPATIBILITY = 4
46
+ } pm_error_level_t;
47
+
48
+ /**
49
+ * The levels of warnings generated during parsing.
50
+ */
51
+ typedef enum {
52
+ /** For warnings which should be emitted if $VERBOSE != nil. */
53
+ PM_WARNING_LEVEL_DEFAULT = 0,
54
+
55
+ /** For warnings which should be emitted if $VERBOSE == true. */
56
+ PM_WARNING_LEVEL_VERBOSE = 1
57
+ } pm_warning_level_t;
58
+
59
+ /**
60
+ * Get the type of the given diagnostic.
61
+ *
62
+ * @param diagnostic The diagnostic to get the type of.
63
+ * @returns The type of the given diagnostic. Note that this is a string
64
+ * representation of an internal ID, and is not meant to be relied upon as a
65
+ * stable identifier for the diagnostic. We do not guarantee that these will
66
+ * not change in the future. This is meant to be used for debugging and
67
+ * error reporting purposes, and not for programmatic checks.
68
+ */
69
+ PRISM_EXPORTED_FUNCTION const char * pm_diagnostic_type(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
70
+
71
+ /**
72
+ * Get the location of the given diagnostic.
73
+ *
74
+ * @param diagnostic The diagnostic to get the location of.
75
+ * @returns The location of the given diagnostic.
76
+ */
77
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_diagnostic_location(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
78
+
79
+ /**
80
+ * Get the message of the given diagnostic.
81
+ *
82
+ * @param diagnostic The diagnostic to get the message of.
83
+ * @returns The message of the given diagnostic.
84
+ */
85
+ PRISM_EXPORTED_FUNCTION const char * pm_diagnostic_message(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
86
+
87
+ /**
88
+ * Get the error level associated with the given diagnostic.
89
+ *
90
+ * @param diagnostic The diagnostic to get the error level of.
91
+ * @returns The error level of the given diagnostic. If the diagnostic was a
92
+ * warning, or is in any way not an error, then the return value is
93
+ * undefined and should not be relied upon.
94
+ */
95
+ PRISM_EXPORTED_FUNCTION pm_error_level_t pm_diagnostic_error_level(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
96
+
97
+ /**
98
+ * Get the warning level associated with the given diagnostic.
99
+ *
100
+ * @param diagnostic The diagnostic to get the warning level of.
101
+ * @returns The warning level of the given diagnostic. If the diagnostic was an
102
+ * error, or is in any way not a warning, then the return value is
103
+ * undefined and should not be relied upon.
104
+ */
105
+ PRISM_EXPORTED_FUNCTION pm_warning_level_t pm_diagnostic_warning_level(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
106
+
107
+ /**
108
+ * Get the diagnostic that follows the given diagnostic among the errors or the
109
+ * warnings of the parser that produced it.
110
+ *
111
+ * @param diagnostic The diagnostic to get the successor of.
112
+ * @returns The next diagnostic of the same kind, or NULL if the given
113
+ * diagnostic is the last one.
114
+ */
115
+ PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_diagnostic_next(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
116
+
117
+ #ifdef __cplusplus
118
+ }
119
+ #endif
120
+
121
+ #endif
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @file errors_format.h
3
+ *
4
+ * A function for formatting the errors in a parser into a buffer.
5
+ */
6
+ #ifndef PRISM_ERRORS_FORMAT_H
7
+ #define PRISM_ERRORS_FORMAT_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nodiscard.h"
11
+ #include "prism/compiler/nonnull.h"
12
+
13
+ #include "prism/buffer.h"
14
+ #include "prism/diagnostic.h"
15
+ #include "prism/parser.h"
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ /** The type of formatting to use when formatting errors. */
22
+ typedef enum {
23
+ /** Format errors in a plain format with no colors or styles. Typically
24
+ * used when outputting to a file or when the output is not a terminal.
25
+ */
26
+ PM_ERRORS_FORMAT_PLAIN = 1,
27
+
28
+ /** Format errors in a style format with bold only. Typically used when
29
+ * outputting to a terminal where you do not want colors.
30
+ */
31
+ PM_ERRORS_FORMAT_STYLE = 2,
32
+
33
+ /** Format errors in a color format with bold and colors. The default case
34
+ * when outputting on the terminal.
35
+ */
36
+ PM_ERRORS_FORMAT_COLOR = 3
37
+ } pm_errors_format_type_t;
38
+
39
+ /**
40
+ * Format the errors in a parser into a buffer. After return, the buffer will
41
+ * contain the formatted errors and any relevant source snippets if available
42
+ * and possible.
43
+ *
44
+ * @param parser The parser containing the errors to format. Note that the
45
+ * parser does not need to be fully initialized, but it will need to have
46
+ * start, end, start_line, encoding, line_offsets, filepath, and error_list
47
+ * initialized.
48
+ * @param buffer The buffer to format the errors into.
49
+ * @param format_type The type of formatting to use when formatting the errors.
50
+ * @returns The error level of the error with the highest precedence that was
51
+ * formatted. In practice this means it will be the level of the first
52
+ * argument, load, or stack error if any were found, then the syntax error
53
+ * level if there are syntax errors, and finally the encoding compatibility
54
+ * level. Callers should raise appropriate error types based on the returned
55
+ * level.
56
+ */
57
+ PRISM_EXPORTED_FUNCTION pm_error_level_t pm_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, pm_errors_format_type_t format_type) PRISM_NONNULL(1, 2);
58
+
59
+ #ifdef __cplusplus
60
+ }
61
+ #endif
62
+
63
+ #endif
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @file excludes.h
3
+ *
4
+ * A header file that defines macros to exclude certain features of the prism
5
+ * library. This is useful for reducing the size of the library when certain
6
+ * features are not needed.
7
+ */
8
+ #ifndef PRISM_EXCLUDES_H
9
+ #define PRISM_EXCLUDES_H
10
+
11
+ /**
12
+ * If PRISM_BUILD_MINIMAL is defined, then we're going to define every possible
13
+ * switch that will turn off certain features of prism.
14
+ */
15
+ #ifdef PRISM_BUILD_MINIMAL
16
+ /** Exclude the serialization API. */
17
+ #define PRISM_EXCLUDE_SERIALIZATION
18
+
19
+ /** Exclude the JSON serialization API. */
20
+ #define PRISM_EXCLUDE_JSON
21
+
22
+ /** Exclude the prettyprint API. */
23
+ #define PRISM_EXCLUDE_PRETTYPRINT
24
+
25
+ /** Exclude the full set of encodings, using the minimal only. */
26
+ #define PRISM_ENCODING_EXCLUDE_FULL
27
+ #endif
28
+
29
+ #endif
@@ -0,0 +1,79 @@
1
+ /**
2
+ * @file integer.h
3
+ *
4
+ * This module provides functions for working with arbitrary-sized integers.
5
+ */
6
+ #ifndef PRISM_INTEGER_H
7
+ #define PRISM_INTEGER_H
8
+
9
+ #include "prism/compiler/inline.h"
10
+
11
+ #include <stdbool.h>
12
+ #include <stddef.h>
13
+ #include <stdint.h>
14
+
15
+ /**
16
+ * A structure represents an arbitrary-sized integer. The magnitude is stored as
17
+ * 32-bit words, least significant first.
18
+ */
19
+ typedef struct {
20
+ /**
21
+ * The number of words in the magnitude, with no leading zero words. Zero
22
+ * has no words, so a zeroed pm_integer_t is always positive zero.
23
+ */
24
+ uint32_t length;
25
+
26
+ /**
27
+ * Whether or not the integer is negative.
28
+ */
29
+ bool negative;
30
+
31
+ /**
32
+ * The words of the magnitude. Read them through pm_integer_words or
33
+ * pm_integer_uint64_value rather than directly.
34
+ */
35
+ union {
36
+ /**
37
+ * The words of an integer whose length is at most 2. Words past the
38
+ * length are zero.
39
+ */
40
+ uint32_t value[2];
41
+
42
+ /**
43
+ * The words of an integer whose length is greater than 2.
44
+ */
45
+ uint32_t *values;
46
+ } words;
47
+ } pm_integer_t;
48
+
49
+ /**
50
+ * Return the words of an integer, least significant first.
51
+ *
52
+ * @param integer The integer to read.
53
+ * @param length Set to the number of words returned, which is 0 for zero.
54
+ * @returns A pointer to the words, valid for as long as the integer is.
55
+ */
56
+ static PRISM_INLINE const uint32_t *
57
+ pm_integer_words(const pm_integer_t *integer, uint32_t *length) {
58
+ *length = integer->length;
59
+
60
+ if (integer->length <= 2) {
61
+ return integer->words.value;
62
+ } else {
63
+ return integer->words.values;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Return the magnitude of an integer whose length is at most 2, which always
69
+ * fits in a uint64_t.
70
+ *
71
+ * @param integer The integer to read, whose length must be at most 2.
72
+ * @returns The magnitude of the integer.
73
+ */
74
+ static PRISM_INLINE uint64_t
75
+ pm_integer_uint64_value(const pm_integer_t *integer) {
76
+ return ((uint64_t) integer->words.value[1] << 32) | integer->words.value[0];
77
+ }
78
+
79
+ #endif
@@ -0,0 +1,68 @@
1
+ #ifndef PRISM_INTERNAL_ALLOCATOR_H
2
+ #define PRISM_INTERNAL_ALLOCATOR_H
3
+
4
+ /* If you build Prism with a custom allocator, configure it with
5
+ * "-D PRISM_XALLOCATOR" to use your own allocator that defines xmalloc,
6
+ * xrealloc, xcalloc, and xfree.
7
+ *
8
+ * For example, your `prism_xallocator.h` file could look like this:
9
+ *
10
+ * ```
11
+ * #ifndef PRISM_XALLOCATOR_H
12
+ * #define PRISM_XALLOCATOR_H
13
+ * #define xmalloc my_malloc
14
+ * #define xrealloc my_realloc
15
+ * #define xcalloc my_calloc
16
+ * #define xfree my_free
17
+ * #define xrealloc_sized my_realloc_sized // (optional)
18
+ * #define xfree_sized my_free_sized // (optional)
19
+ * #endif
20
+ * ```
21
+ */
22
+ #ifdef PRISM_XALLOCATOR
23
+ #include "prism_xallocator.h"
24
+ #else
25
+ #ifndef xmalloc
26
+ /* The malloc function that should be used. This can be overridden with
27
+ * the PRISM_XALLOCATOR define. */
28
+ #define xmalloc malloc
29
+ #endif
30
+
31
+ #ifndef xrealloc
32
+ /* The realloc function that should be used. This can be overridden with
33
+ * the PRISM_XALLOCATOR define. */
34
+ #define xrealloc realloc
35
+ #endif
36
+
37
+ #ifndef xcalloc
38
+ /* The calloc function that should be used. This can be overridden with
39
+ * the PRISM_XALLOCATOR define. */
40
+ #define xcalloc calloc
41
+ #endif
42
+
43
+ #ifndef xfree
44
+ /* The free function that should be used. This can be overridden with
45
+ * the PRISM_XALLOCATOR define. */
46
+ #define xfree free
47
+ #endif
48
+ #endif
49
+
50
+ #ifndef xfree_sized
51
+ /* The free_sized function that should be used. This can be overridden with
52
+ * the PRISM_XALLOCATOR define. If not defined, defaults to calling xfree.
53
+ */
54
+ #define xfree_sized(p, s) xfree(((void)(s), (p)))
55
+ #endif
56
+
57
+ #ifndef xrealloc_sized
58
+ /* The xrealloc_sized function that should be used. This can be overridden
59
+ * with the PRISM_XALLOCATOR define. If not defined, defaults to calling
60
+ * xrealloc. */
61
+ #define xrealloc_sized(p, ns, os) xrealloc((p), ((void)(os), (ns)))
62
+ #endif
63
+
64
+ #ifdef PRISM_BUILD_DEBUG
65
+ #include "prism/internal/allocator_debug.h"
66
+ #endif
67
+
68
+ #endif
@@ -0,0 +1,88 @@
1
+ #ifndef PRISM_INTERNAL_ALLOCATOR_DEBUG_H
2
+ #define PRISM_INTERNAL_ALLOCATOR_DEBUG_H
3
+
4
+ #include <string.h>
5
+ #include <stdio.h>
6
+ #include <stdlib.h>
7
+
8
+ static inline void *
9
+ pm_allocator_debug_malloc(size_t size) {
10
+ size_t *memory = xmalloc(size + sizeof(size_t));
11
+ memory[0] = size;
12
+ return memory + 1;
13
+ }
14
+
15
+ static inline void *
16
+ pm_allocator_debug_calloc(size_t nmemb, size_t size) {
17
+ size_t total_size = nmemb * size;
18
+ void *ptr = pm_allocator_debug_malloc(total_size);
19
+ memset(ptr, 0, total_size);
20
+ return ptr;
21
+ }
22
+
23
+ static inline void *
24
+ pm_allocator_debug_realloc(void *ptr, size_t size) {
25
+ if (ptr == NULL) {
26
+ return pm_allocator_debug_malloc(size);
27
+ }
28
+
29
+ size_t *memory = (size_t *)ptr;
30
+ void *raw_memory = memory - 1;
31
+ memory = (size_t *)xrealloc(raw_memory, size + sizeof(size_t));
32
+ memory[0] = size;
33
+ return memory + 1;
34
+ }
35
+
36
+ static inline void
37
+ pm_allocator_debug_free(void *ptr) {
38
+ if (ptr != NULL) {
39
+ size_t *memory = (size_t *)ptr;
40
+ xfree(memory - 1);
41
+ }
42
+ }
43
+
44
+ static inline void
45
+ pm_allocator_debug_free_sized(void *ptr, size_t old_size) {
46
+ if (ptr != NULL) {
47
+ size_t *memory = (size_t *)ptr;
48
+ if (old_size != memory[-1]) {
49
+ fprintf(stderr, "[BUG] buffer %p was allocated with size %zu but freed with size %zu\n", ptr, memory[-1], old_size);
50
+ abort();
51
+ }
52
+ xfree_sized(memory - 1, old_size + sizeof(size_t));
53
+ }
54
+ }
55
+
56
+ static inline void *
57
+ pm_allocator_debug_realloc_sized(void *ptr, size_t size, size_t old_size) {
58
+ if (ptr == NULL) {
59
+ if (old_size != 0) {
60
+ fprintf(stderr, "[BUG] realloc_sized called with NULL pointer and old size %zu\n", old_size);
61
+ abort();
62
+ }
63
+ return pm_allocator_debug_malloc(size);
64
+ }
65
+
66
+ size_t *memory = (size_t *)ptr;
67
+ if (old_size != memory[-1]) {
68
+ fprintf(stderr, "[BUG] buffer %p was allocated with size %zu but realloced with size %zu\n", ptr, memory[-1], old_size);
69
+ abort();
70
+ }
71
+ return pm_allocator_debug_realloc(ptr, size);
72
+ }
73
+
74
+ #undef xmalloc
75
+ #undef xrealloc
76
+ #undef xcalloc
77
+ #undef xfree
78
+ #undef xrealloc_sized
79
+ #undef xfree_sized
80
+
81
+ #define xmalloc pm_allocator_debug_malloc
82
+ #define xrealloc pm_allocator_debug_realloc
83
+ #define xcalloc pm_allocator_debug_calloc
84
+ #define xfree pm_allocator_debug_free
85
+ #define xrealloc_sized pm_allocator_debug_realloc_sized
86
+ #define xfree_sized pm_allocator_debug_free_sized
87
+
88
+ #endif