prism 2.0.0-arm-linux-gnu

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 (233) hide show
  1. checksums.yaml +7 -0
  2. data/BSDmakefile +58 -0
  3. data/CHANGELOG.md +881 -0
  4. data/CODE_OF_CONDUCT.md +76 -0
  5. data/CONTRIBUTING.md +58 -0
  6. data/LICENSE.md +7 -0
  7. data/Makefile +145 -0
  8. data/README.md +81 -0
  9. data/config.yml +4752 -0
  10. data/docs/build_system.md +119 -0
  11. data/docs/configuration.md +69 -0
  12. data/docs/cruby_compilation.md +27 -0
  13. data/docs/design.md +53 -0
  14. data/docs/encoding.md +121 -0
  15. data/docs/fuzzing.md +81 -0
  16. data/docs/heredocs.md +36 -0
  17. data/docs/javascript.md +133 -0
  18. data/docs/local_variable_depth.md +229 -0
  19. data/docs/mapping.md +117 -0
  20. data/docs/parser_translation.md +24 -0
  21. data/docs/parsing_rules.md +22 -0
  22. data/docs/releasing.md +147 -0
  23. data/docs/relocation.md +32 -0
  24. data/docs/ripper_translation.md +63 -0
  25. data/docs/ruby_api.md +45 -0
  26. data/docs/ruby_parser_translation.md +19 -0
  27. data/docs/serialization.md +244 -0
  28. data/docs/testing.md +72 -0
  29. data/ext/prism/api_node.c +7005 -0
  30. data/ext/prism/extconf.rb +126 -0
  31. data/ext/prism/extension.c +1768 -0
  32. data/ext/prism/extension.h +19 -0
  33. data/include/prism/arena.h +45 -0
  34. data/include/prism/arena.hpp +42 -0
  35. data/include/prism/ast.h +10381 -0
  36. data/include/prism/ast.hpp +641 -0
  37. data/include/prism/buffer.h +60 -0
  38. data/include/prism/buffer.hpp +55 -0
  39. data/include/prism/comments.h +72 -0
  40. data/include/prism/compiler/accel.h +26 -0
  41. data/include/prism/compiler/align.h +36 -0
  42. data/include/prism/compiler/assume.h +25 -0
  43. data/include/prism/compiler/branch.h +26 -0
  44. data/include/prism/compiler/bswap.h +46 -0
  45. data/include/prism/compiler/cplusplus.hpp +16 -0
  46. data/include/prism/compiler/endianness.h +34 -0
  47. data/include/prism/compiler/exceptions.hpp +17 -0
  48. data/include/prism/compiler/exported.h +24 -0
  49. data/include/prism/compiler/fallthrough.h +22 -0
  50. data/include/prism/compiler/filesystem.h +32 -0
  51. data/include/prism/compiler/flex_array.h +19 -0
  52. data/include/prism/compiler/force_inline.h +21 -0
  53. data/include/prism/compiler/format.h +27 -0
  54. data/include/prism/compiler/inline.h +17 -0
  55. data/include/prism/compiler/lifetimebound.hpp +25 -0
  56. data/include/prism/compiler/nodiscard.h +22 -0
  57. data/include/prism/compiler/nonnull.h +18 -0
  58. data/include/prism/compiler/unused.h +18 -0
  59. data/include/prism/constant_pool.h +106 -0
  60. data/include/prism/diagnostic.h +111 -0
  61. data/include/prism/errors_format.h +61 -0
  62. data/include/prism/excludes.h +29 -0
  63. data/include/prism/integer.h +41 -0
  64. data/include/prism/internal/allocator.h +68 -0
  65. data/include/prism/internal/allocator_debug.h +88 -0
  66. data/include/prism/internal/arena.h +108 -0
  67. data/include/prism/internal/bit.h +42 -0
  68. data/include/prism/internal/buffer.h +94 -0
  69. data/include/prism/internal/char.h +145 -0
  70. data/include/prism/internal/comments.h +53 -0
  71. data/include/prism/internal/constant_pool.h +152 -0
  72. data/include/prism/internal/diagnostic.h +389 -0
  73. data/include/prism/internal/encoding.h +242 -0
  74. data/include/prism/internal/integer.h +68 -0
  75. data/include/prism/internal/isinf.h +16 -0
  76. data/include/prism/internal/line_offset_list.h +34 -0
  77. data/include/prism/internal/list.h +62 -0
  78. data/include/prism/internal/magic_comments.h +23 -0
  79. data/include/prism/internal/memchr.h +15 -0
  80. data/include/prism/internal/node.h +42 -0
  81. data/include/prism/internal/options.h +232 -0
  82. data/include/prism/internal/parser.h +1004 -0
  83. data/include/prism/internal/regexp.h +26 -0
  84. data/include/prism/internal/serialize.h +39 -0
  85. data/include/prism/internal/source.h +72 -0
  86. data/include/prism/internal/static_literals.h +100 -0
  87. data/include/prism/internal/stringy.h +30 -0
  88. data/include/prism/internal/strncasecmp.h +18 -0
  89. data/include/prism/internal/strpbrk.h +33 -0
  90. data/include/prism/internal/tokens.h +11 -0
  91. data/include/prism/internal/xxh3.h +16 -0
  92. data/include/prism/json.h +40 -0
  93. data/include/prism/line_offset_list.h +69 -0
  94. data/include/prism/magic_comments.h +53 -0
  95. data/include/prism/node.h +139 -0
  96. data/include/prism/node.hpp +272 -0
  97. data/include/prism/node_type.hpp +38 -0
  98. data/include/prism/options.h +363 -0
  99. data/include/prism/options.hpp +43 -0
  100. data/include/prism/parser.h +433 -0
  101. data/include/prism/parser.hpp +274 -0
  102. data/include/prism/prettyprint.h +39 -0
  103. data/include/prism/serialize.h +123 -0
  104. data/include/prism/source.h +163 -0
  105. data/include/prism/stream.h +36 -0
  106. data/include/prism/string_query.h +82 -0
  107. data/include/prism/stringy.h +61 -0
  108. data/include/prism/stringy.hpp +31 -0
  109. data/include/prism/version.h +46 -0
  110. data/include/prism.h +123 -0
  111. data/include/prism.hpp +24 -0
  112. data/lib/prism/3.3/prism.so +0 -0
  113. data/lib/prism/3.4/prism.so +0 -0
  114. data/lib/prism/4.0/prism.so +0 -0
  115. data/lib/prism/compiler.rb +815 -0
  116. data/lib/prism/desugar_compiler.rb +463 -0
  117. data/lib/prism/dispatcher.rb +2383 -0
  118. data/lib/prism/dot_visitor.rb +4848 -0
  119. data/lib/prism/dsl.rb +1369 -0
  120. data/lib/prism/ffi.rb +746 -0
  121. data/lib/prism/inspect_visitor.rb +2425 -0
  122. data/lib/prism/lex_compat.rb +953 -0
  123. data/lib/prism/mutation_compiler.rb +780 -0
  124. data/lib/prism/node.rb +33940 -0
  125. data/lib/prism/node_ext.rb +504 -0
  126. data/lib/prism/node_find.rb +215 -0
  127. data/lib/prism/parse_result/errors.rb +72 -0
  128. data/lib/prism/parse_result/newlines.rb +423 -0
  129. data/lib/prism/parse_result.rb +1236 -0
  130. data/lib/prism/pattern.rb +314 -0
  131. data/lib/prism/polyfill/append_as_bytes.rb +15 -0
  132. data/lib/prism/polyfill/byteindex.rb +13 -0
  133. data/lib/prism/polyfill/scan_byte.rb +14 -0
  134. data/lib/prism/polyfill/unpack1.rb +14 -0
  135. data/lib/prism/polyfill/warn.rb +36 -0
  136. data/lib/prism/reflection.rb +427 -0
  137. data/lib/prism/relocation.rb +567 -0
  138. data/lib/prism/serialize.rb +4589 -0
  139. data/lib/prism/string_query.rb +54 -0
  140. data/lib/prism/translation/parser/builder.rb +70 -0
  141. data/lib/prism/translation/parser/compiler.rb +2226 -0
  142. data/lib/prism/translation/parser/lexer.rb +769 -0
  143. data/lib/prism/translation/parser.rb +376 -0
  144. data/lib/prism/translation/parser_current.rb +26 -0
  145. data/lib/prism/translation/parser_versions.rb +36 -0
  146. data/lib/prism/translation/ripper/filter.rb +53 -0
  147. data/lib/prism/translation/ripper/lexer.rb +133 -0
  148. data/lib/prism/translation/ripper/sexp.rb +118 -0
  149. data/lib/prism/translation/ripper/shim.rb +7 -0
  150. data/lib/prism/translation/ripper.rb +4278 -0
  151. data/lib/prism/translation/ruby_parser.rb +1721 -0
  152. data/lib/prism/translation.rb +20 -0
  153. data/lib/prism/visitor.rb +1440 -0
  154. data/lib/prism.rb +168 -0
  155. data/prism.gemspec +251 -0
  156. data/rbi/generated/prism/compiler.rbi +489 -0
  157. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  158. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  159. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  160. data/rbi/generated/prism/dsl.rbi +751 -0
  161. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  162. data/rbi/generated/prism/lex_compat.rbi +162 -0
  163. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  164. data/rbi/generated/prism/node.rbi +15143 -0
  165. data/rbi/generated/prism/node_ext.rbi +259 -0
  166. data/rbi/generated/prism/node_find.rbi +79 -0
  167. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  168. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  169. data/rbi/generated/prism/parse_result.rbi +776 -0
  170. data/rbi/generated/prism/pattern.rbi +134 -0
  171. data/rbi/generated/prism/reflection.rbi +89 -0
  172. data/rbi/generated/prism/relocation.rbi +329 -0
  173. data/rbi/generated/prism/serialize.rbi +185 -0
  174. data/rbi/generated/prism/string_query.rbi +43 -0
  175. data/rbi/generated/prism/translation.rbi +7 -0
  176. data/rbi/generated/prism/visitor.rbi +650 -0
  177. data/rbi/generated/prism.rbi +86 -0
  178. data/rbi/prism/translation/parser.rbi +11 -0
  179. data/rbi/prism/translation/parser_versions.rbi +28 -0
  180. data/rbi/prism/translation/ripper.rbi +15 -0
  181. data/rbi/rubyvm/node_find.rbi +14 -0
  182. data/sig/generated/prism/compiler.rbs +492 -0
  183. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  184. data/sig/generated/prism/dispatcher.rbs +1292 -0
  185. data/sig/generated/prism/dot_visitor.rbs +635 -0
  186. data/sig/generated/prism/dsl.rbs +928 -0
  187. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  188. data/sig/generated/prism/lex_compat.rbs +166 -0
  189. data/sig/generated/prism/mutation_compiler.rbs +464 -0
  190. data/sig/generated/prism/node.rbs +21632 -0
  191. data/sig/generated/prism/node_ext.rbs +290 -0
  192. data/sig/generated/prism/node_find.rbs +90 -0
  193. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  194. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  195. data/sig/generated/prism/parse_result.rbs +871 -0
  196. data/sig/generated/prism/pattern.rbs +156 -0
  197. data/sig/generated/prism/reflection.rbs +101 -0
  198. data/sig/generated/prism/relocation.rbs +428 -0
  199. data/sig/generated/prism/serialize.rbs +200 -0
  200. data/sig/generated/prism/string_query.rbs +43 -0
  201. data/sig/generated/prism/translation.rbs +8 -0
  202. data/sig/generated/prism/visitor.rbs +1111 -0
  203. data/sig/generated/prism.rbs +84 -0
  204. data/sig/manifest.yaml +2 -0
  205. data/src/arena.c +117 -0
  206. data/src/buffer.c +389 -0
  207. data/src/char.c +289 -0
  208. data/src/comments.c +454 -0
  209. data/src/constant_pool.c +406 -0
  210. data/src/diagnostic.c +894 -0
  211. data/src/encoding.c +5345 -0
  212. data/src/errors_format.c +519 -0
  213. data/src/integer.c +681 -0
  214. data/src/json.c +5733 -0
  215. data/src/line_offset_list.c +100 -0
  216. data/src/list.c +24 -0
  217. data/src/memchr.c +37 -0
  218. data/src/node.c +4363 -0
  219. data/src/options.c +445 -0
  220. data/src/parser.c +371 -0
  221. data/src/prettyprint.c +9024 -0
  222. data/src/prism.c +23901 -0
  223. data/src/regexp.c +1705 -0
  224. data/src/serialize.c +2660 -0
  225. data/src/source.c +524 -0
  226. data/src/static_literals.c +685 -0
  227. data/src/string_query.c +180 -0
  228. data/src/stringy.c +61 -0
  229. data/src/strncasecmp.c +37 -0
  230. data/src/strpbrk.c +447 -0
  231. data/src/tokens.c +724 -0
  232. data/src/xxh3.c +292 -0
  233. metadata +276 -0
@@ -0,0 +1,106 @@
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
+ /** The number of constant ids in the list. */
53
+ size_t size;
54
+
55
+ /** The number of constant ids that have been allocated in the list. */
56
+ size_t capacity;
57
+
58
+ /** The constant ids in the list. */
59
+ pm_constant_id_t *ids;
60
+ } pm_constant_id_list_t;
61
+
62
+ /** A constant in the pool which effectively stores a string. */
63
+ typedef struct pm_constant_t pm_constant_t;
64
+
65
+ /**
66
+ * The overall constant pool, which stores constants found while parsing.
67
+ */
68
+ typedef struct pm_constant_pool_t pm_constant_pool_t;
69
+
70
+ /**
71
+ * Return a raw pointer to the start of a constant.
72
+ *
73
+ * @param constant The constant to get the start of.
74
+ * @returns A raw pointer to the start of the constant.
75
+ */
76
+ PRISM_EXPORTED_FUNCTION const uint8_t * pm_constant_start(const pm_constant_t *constant) PRISM_NONNULL(1);
77
+
78
+ /**
79
+ * Return the length of a constant.
80
+ *
81
+ * @param constant The constant to get the length of.
82
+ * @returns The length of the constant.
83
+ */
84
+ PRISM_EXPORTED_FUNCTION size_t pm_constant_length(const pm_constant_t *constant) PRISM_NONNULL(1);
85
+
86
+ /**
87
+ * Initialize a list of constant ids.
88
+ *
89
+ * @param list The list to initialize.
90
+ */
91
+ PRISM_EXPORTED_FUNCTION void pm_constant_id_list_init(pm_constant_id_list_t *list) PRISM_NONNULL(1);
92
+
93
+ /**
94
+ * Append a constant id to a list of constant ids.
95
+ *
96
+ * @param arena The arena to use for allocations.
97
+ * @param list The list to append to.
98
+ * @param id The constant id to append.
99
+ */
100
+ 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);
101
+
102
+ #ifdef __cplusplus
103
+ }
104
+ #endif
105
+
106
+ #endif
@@ -0,0 +1,111 @@
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
+ } pm_error_level_t;
37
+
38
+ /**
39
+ * The levels of warnings generated during parsing.
40
+ */
41
+ typedef enum {
42
+ /** For warnings which should be emitted if $VERBOSE != nil. */
43
+ PM_WARNING_LEVEL_DEFAULT = 0,
44
+
45
+ /** For warnings which should be emitted if $VERBOSE == true. */
46
+ PM_WARNING_LEVEL_VERBOSE = 1
47
+ } pm_warning_level_t;
48
+
49
+ /**
50
+ * Get the type of the given diagnostic.
51
+ *
52
+ * @param diagnostic The diagnostic to get the type of.
53
+ * @returns The type of the given diagnostic. Note that this is a string
54
+ * representation of an internal ID, and is not meant to be relied upon as a
55
+ * stable identifier for the diagnostic. We do not guarantee that these will
56
+ * not change in the future. This is meant to be used for debugging and
57
+ * error reporting purposes, and not for programmatic checks.
58
+ */
59
+ PRISM_EXPORTED_FUNCTION const char * pm_diagnostic_type(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
60
+
61
+ /**
62
+ * Get the location of the given diagnostic.
63
+ *
64
+ * @param diagnostic The diagnostic to get the location of.
65
+ * @returns The location of the given diagnostic.
66
+ */
67
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_diagnostic_location(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
68
+
69
+ /**
70
+ * Get the message of the given diagnostic.
71
+ *
72
+ * @param diagnostic The diagnostic to get the message of.
73
+ * @returns The message of the given diagnostic.
74
+ */
75
+ PRISM_EXPORTED_FUNCTION const char * pm_diagnostic_message(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
76
+
77
+ /**
78
+ * Get the error level associated with the given diagnostic.
79
+ *
80
+ * @param diagnostic The diagnostic to get the error level of.
81
+ * @returns The error level of the given diagnostic. If the diagnostic was a
82
+ * warning, or is in any way not an error, then the return value is
83
+ * undefined and should not be relied upon.
84
+ */
85
+ PRISM_EXPORTED_FUNCTION pm_error_level_t pm_diagnostic_error_level(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
86
+
87
+ /**
88
+ * Get the warning level associated with the given diagnostic.
89
+ *
90
+ * @param diagnostic The diagnostic to get the warning level of.
91
+ * @returns The warning level of the given diagnostic. If the diagnostic was an
92
+ * error, or is in any way not a warning, then the return value is
93
+ * undefined and should not be relied upon.
94
+ */
95
+ PRISM_EXPORTED_FUNCTION pm_warning_level_t pm_diagnostic_warning_level(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
96
+
97
+ /**
98
+ * Get the diagnostic that follows the given diagnostic among the errors or the
99
+ * warnings of the parser that produced it.
100
+ *
101
+ * @param diagnostic The diagnostic to get the successor of.
102
+ * @returns The next diagnostic of the same kind, or NULL if the given
103
+ * diagnostic is the last one.
104
+ */
105
+ PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_diagnostic_next(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);
106
+
107
+ #ifdef __cplusplus
108
+ }
109
+ #endif
110
+
111
+ #endif
@@ -0,0 +1,61 @@
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 argument or load error
52
+ * level if any were found, otherwise it will be the syntax error level.
53
+ * Callers should raise appropriate error types based on the returned level.
54
+ */
55
+ 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);
56
+
57
+ #ifdef __cplusplus
58
+ }
59
+ #endif
60
+
61
+ #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,41 @@
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 <stdbool.h>
10
+ #include <stddef.h>
11
+ #include <stdint.h>
12
+
13
+ /**
14
+ * A structure represents an arbitrary-sized integer.
15
+ */
16
+ typedef struct {
17
+ /**
18
+ * The number of allocated values. length is set to 0 if the integer fits
19
+ * into uint32_t.
20
+ */
21
+ size_t length;
22
+
23
+ /**
24
+ * List of 32-bit integers. Set to NULL if the integer fits into uint32_t.
25
+ */
26
+ uint32_t *values;
27
+
28
+ /**
29
+ * Embedded value for small integer. This value is set to 0 if the value
30
+ * does not fit into uint32_t.
31
+ */
32
+ uint32_t value;
33
+
34
+ /**
35
+ * Whether or not the integer is negative. It is stored this way so that a
36
+ * zeroed pm_integer_t is always positive zero.
37
+ */
38
+ bool negative;
39
+ } pm_integer_t;
40
+
41
+ #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 %lu but freed with size %lu\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 %lu\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 %lu but realloced with size %lu\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
@@ -0,0 +1,108 @@
1
+ #ifndef PRISM_INTERNAL_ARENA_H
2
+ #define PRISM_INTERNAL_ARENA_H
3
+
4
+ #include "prism/compiler/exported.h"
5
+ #include "prism/compiler/flex_array.h"
6
+ #include "prism/compiler/force_inline.h"
7
+ #include "prism/compiler/inline.h"
8
+
9
+ #include "prism/arena.h"
10
+
11
+ #include <stddef.h>
12
+ #include <string.h>
13
+
14
+ /*
15
+ * A single block of memory in the arena. Blocks are linked via prev pointers so
16
+ * they can be freed by walking the chain.
17
+ */
18
+ typedef struct pm_arena_block {
19
+ /* The previous block in the chain (for freeing). */
20
+ struct pm_arena_block *prev;
21
+
22
+ /* The total usable bytes in data[]. */
23
+ size_t capacity;
24
+
25
+ /* The number of bytes consumed so far. */
26
+ size_t used;
27
+
28
+ /* The block's data. */
29
+ char data[PM_FLEX_ARRAY_LENGTH];
30
+ } pm_arena_block_t;
31
+
32
+ /*
33
+ * A bump allocator. Allocations are made by bumping a pointer within the
34
+ * current block. When a block is full, a new block is allocated and linked to
35
+ * the previous one. All blocks are freed at once by walking the chain.
36
+ */
37
+ struct pm_arena_t {
38
+ /* The active block (allocate from here). */
39
+ pm_arena_block_t *current;
40
+
41
+ /* The number of blocks allocated. */
42
+ size_t block_count;
43
+ };
44
+
45
+ /*
46
+ * Free all blocks in the arena. After this call, all pointers returned by
47
+ * pm_arena_alloc and pm_arena_zalloc are invalid.
48
+ */
49
+ void pm_arena_cleanup(pm_arena_t *arena);
50
+
51
+ /*
52
+ * Ensure the arena has at least `capacity` bytes available in its current
53
+ * block, allocating a new block if necessary. This allows callers to
54
+ * pre-size the arena to avoid repeated small block allocations.
55
+ */
56
+ void pm_arena_reserve(pm_arena_t *arena, size_t capacity);
57
+
58
+ /*
59
+ * Slow path for pm_arena_alloc: allocate a new block and return a pointer to
60
+ * the first `size` bytes. Do not call directly — use pm_arena_alloc instead.
61
+ */
62
+ void * pm_arena_alloc_slow(pm_arena_t *arena, size_t size);
63
+
64
+ /*
65
+ * Allocate memory from the arena. The returned memory is NOT zeroed. This
66
+ * function is infallible — it aborts on allocation failure.
67
+ *
68
+ * The fast path (bump pointer within the current block) is inlined at each
69
+ * call site. The slow path (new block allocation) is out-of-line.
70
+ */
71
+ static PRISM_FORCE_INLINE void *
72
+ pm_arena_alloc(pm_arena_t *arena, size_t size, size_t alignment) {
73
+ if (arena->current != NULL) {
74
+ size_t used_aligned = (arena->current->used + alignment - 1) & ~(alignment - 1);
75
+ size_t needed = used_aligned + size;
76
+
77
+ if (used_aligned >= arena->current->used && needed >= used_aligned && needed <= arena->current->capacity) {
78
+ arena->current->used = needed;
79
+ return arena->current->data + used_aligned;
80
+ }
81
+ }
82
+
83
+ return pm_arena_alloc_slow(arena, size);
84
+ }
85
+
86
+ /*
87
+ * Allocate zero-initialized memory from the arena. This function is infallible
88
+ * — it aborts on allocation failure.
89
+ */
90
+ static PRISM_INLINE void *
91
+ pm_arena_zalloc(pm_arena_t *arena, size_t size, size_t alignment) {
92
+ void *ptr = pm_arena_alloc(arena, size, alignment);
93
+ memset(ptr, 0, size);
94
+ return ptr;
95
+ }
96
+
97
+ /*
98
+ * Allocate memory from the arena and copy the given data into it. This is a
99
+ * convenience wrapper around pm_arena_alloc + memcpy.
100
+ */
101
+ static PRISM_INLINE void *
102
+ pm_arena_memdup(pm_arena_t *arena, const void *src, size_t size, size_t alignment) {
103
+ void *dst = pm_arena_alloc(arena, size, alignment);
104
+ memcpy(dst, src, size);
105
+ return dst;
106
+ }
107
+
108
+ #endif
@@ -0,0 +1,42 @@
1
+ #ifndef PRISM_INTERNAL_BIT_H
2
+ #define PRISM_INTERNAL_BIT_H
3
+
4
+ #include "prism/compiler/inline.h"
5
+
6
+ /*
7
+ * Count trailing zero bits in a 64-bit value. Used by SWAR identifier scanning
8
+ * to find the first non-matching byte in a word.
9
+ *
10
+ * Precondition: v must be nonzero. The result is undefined when v == 0
11
+ * (matching the behavior of __builtin_ctzll and _BitScanForward64).
12
+ */
13
+ #if defined(__GNUC__) || defined(__clang__)
14
+ #define pm_ctzll(v) ((unsigned) __builtin_ctzll(v))
15
+ #elif defined(_MSC_VER) && defined(_WIN64)
16
+ #include <intrin.h>
17
+ #include <stdint.h>
18
+
19
+ static PRISM_INLINE unsigned
20
+ pm_ctzll(uint64_t v) {
21
+ unsigned long index;
22
+ _BitScanForward64(&index, v);
23
+ return (unsigned) index;
24
+ }
25
+ #else
26
+ #include <stdint.h>
27
+
28
+ static PRISM_INLINE unsigned
29
+ pm_ctzll(uint64_t v) {
30
+ unsigned c = 0;
31
+ v &= (uint64_t) (-(int64_t) v);
32
+ if (v & 0x00000000FFFFFFFFULL) c += 0; else c += 32;
33
+ if (v & 0x0000FFFF0000FFFFULL) c += 0; else c += 16;
34
+ if (v & 0x00FF00FF00FF00FFULL) c += 0; else c += 8;
35
+ if (v & 0x0F0F0F0F0F0F0F0FULL) c += 0; else c += 4;
36
+ if (v & 0x3333333333333333ULL) c += 0; else c += 2;
37
+ if (v & 0x5555555555555555ULL) c += 0; else c += 1;
38
+ return c;
39
+ }
40
+ #endif
41
+
42
+ #endif