prism 1.9.0 → 2.0.0.rc2

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 (244) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +88 -0
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/LICENSE.md +1 -1
  5. data/Makefile +41 -12
  6. data/README.md +35 -97
  7. data/config.yml +174 -161
  8. data/docs/build_system.md +2 -2
  9. data/docs/configuration.md +5 -4
  10. data/docs/encoding.md +2 -2
  11. data/docs/fuzzing.md +4 -11
  12. data/docs/javascript.md +19 -4
  13. data/docs/mapping.md +1 -1
  14. data/docs/releasing.md +77 -5
  15. data/docs/relocation.md +2 -4
  16. data/docs/serialization.md +22 -24
  17. data/docs/testing.md +46 -29
  18. data/ext/prism/api_node.c +1461 -1401
  19. data/ext/prism/extconf.rb +3 -4
  20. data/ext/prism/extension.c +796 -422
  21. data/ext/prism/extension.h +3 -3
  22. data/include/prism/arena.h +37 -0
  23. data/include/prism/ast.h +2727 -610
  24. data/include/prism/buffer.h +52 -0
  25. data/include/prism/comments.h +55 -0
  26. data/include/prism/compiler/accel.h +26 -0
  27. data/include/prism/compiler/align.h +36 -0
  28. data/include/prism/compiler/assume.h +25 -0
  29. data/include/prism/compiler/bswap.h +46 -0
  30. data/include/prism/compiler/endianness.h +34 -0
  31. data/include/prism/compiler/exported.h +24 -0
  32. data/include/prism/compiler/fallthrough.h +22 -0
  33. data/include/prism/compiler/filesystem.h +32 -0
  34. data/include/prism/compiler/flex_array.h +19 -0
  35. data/include/prism/compiler/force_inline.h +21 -0
  36. data/include/prism/compiler/format.h +25 -0
  37. data/include/prism/compiler/inline.h +17 -0
  38. data/include/prism/compiler/nodiscard.h +22 -0
  39. data/include/prism/compiler/nonnull.h +18 -0
  40. data/include/prism/compiler/unused.h +18 -0
  41. data/include/prism/constant_pool.h +81 -0
  42. data/include/prism/diagnostic.h +38 -403
  43. data/include/prism/errors_format.h +53 -0
  44. data/include/prism/excludes.h +29 -0
  45. data/include/prism/integer.h +41 -0
  46. data/include/prism/internal/allocator.h +68 -0
  47. data/include/prism/internal/allocator_debug.h +88 -0
  48. data/include/prism/internal/arena.h +108 -0
  49. data/include/prism/internal/bit.h +42 -0
  50. data/include/prism/internal/buffer.h +94 -0
  51. data/include/prism/internal/char.h +145 -0
  52. data/include/prism/internal/comments.h +53 -0
  53. data/include/prism/internal/constant_pool.h +152 -0
  54. data/include/prism/internal/diagnostic.h +389 -0
  55. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  56. data/include/prism/internal/integer.h +68 -0
  57. data/include/prism/internal/isinf.h +16 -0
  58. data/include/prism/internal/line_offset_list.h +34 -0
  59. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  60. data/include/prism/internal/magic_comments.h +23 -0
  61. data/include/prism/internal/memchr.h +15 -0
  62. data/include/prism/internal/node.h +38 -0
  63. data/include/prism/internal/options.h +232 -0
  64. data/include/prism/internal/parser.h +1004 -0
  65. data/include/prism/internal/regexp.h +26 -0
  66. data/include/prism/internal/serialize.h +39 -0
  67. data/include/prism/internal/source.h +72 -0
  68. data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
  69. data/include/prism/internal/stringy.h +35 -0
  70. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  71. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  72. data/include/prism/internal/tokens.h +11 -0
  73. data/include/prism/internal/xxh3.h +16 -0
  74. data/include/prism/json.h +32 -0
  75. data/include/prism/line_offset_list.h +61 -0
  76. data/include/prism/magic_comments.h +35 -0
  77. data/include/prism/node.h +18 -53
  78. data/include/prism/options.h +146 -280
  79. data/include/prism/parser.h +356 -867
  80. data/include/prism/prettyprint.h +6 -9
  81. data/include/prism/serialize.h +115 -0
  82. data/include/prism/source.h +155 -0
  83. data/include/prism/stream.h +28 -0
  84. data/include/prism/string_query.h +74 -0
  85. data/include/prism/stringy.h +72 -0
  86. data/include/prism/version.h +12 -3
  87. data/include/prism.h +43 -320
  88. data/lib/prism/compiler.rb +319 -305
  89. data/lib/prism/desugar_compiler.rb +105 -34
  90. data/lib/prism/dispatcher.rb +646 -473
  91. data/lib/prism/dot_visitor.rb +592 -511
  92. data/lib/prism/dsl.rb +391 -25
  93. data/lib/prism/ffi.rb +258 -90
  94. data/lib/prism/inspect_visitor.rb +357 -325
  95. data/lib/prism/lex_compat.rb +254 -212
  96. data/lib/prism/mutation_compiler.rb +313 -305
  97. data/lib/prism/node.rb +20966 -6992
  98. data/lib/prism/node_ext.rb +180 -187
  99. data/lib/prism/node_find.rb +215 -0
  100. data/lib/prism/parse_result/errors.rb +7 -1
  101. data/lib/prism/parse_result/newlines.rb +276 -8
  102. data/lib/prism/parse_result.rb +468 -139
  103. data/lib/prism/pattern.rb +65 -20
  104. data/lib/prism/reflection.rb +23 -12
  105. data/lib/prism/relocation.rb +149 -87
  106. data/lib/prism/serialize.rb +3460 -539
  107. data/lib/prism/string_query.rb +24 -1
  108. data/lib/prism/translation/parser/builder.rb +10 -2
  109. data/lib/prism/translation/parser/compiler.rb +44 -52
  110. data/lib/prism/translation/parser/lexer.rb +41 -92
  111. data/lib/prism/translation/parser.rb +1 -1
  112. data/lib/prism/translation/parser_current.rb +1 -1
  113. data/lib/prism/translation/ripper/lexer.rb +8 -10
  114. data/lib/prism/translation/ripper/sexp.rb +2 -10
  115. data/lib/prism/translation/ripper/shim.rb +2 -0
  116. data/lib/prism/translation/ripper.rb +967 -209
  117. data/lib/prism/translation/ruby_parser.rb +73 -316
  118. data/lib/prism/translation.rb +2 -0
  119. data/lib/prism/visitor.rb +632 -5
  120. data/lib/prism.rb +79 -9
  121. data/prism.gemspec +128 -62
  122. data/rbi/generated/prism/compiler.rbi +489 -0
  123. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  124. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  125. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  126. data/rbi/generated/prism/dsl.rbi +751 -0
  127. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  128. data/rbi/generated/prism/lex_compat.rbi +162 -0
  129. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  130. data/rbi/generated/prism/node.rbi +14407 -0
  131. data/rbi/generated/prism/node_ext.rbi +259 -0
  132. data/rbi/generated/prism/node_find.rbi +79 -0
  133. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  134. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  135. data/rbi/generated/prism/parse_result.rbi +776 -0
  136. data/rbi/generated/prism/pattern.rbi +134 -0
  137. data/rbi/generated/prism/reflection.rbi +89 -0
  138. data/rbi/generated/prism/relocation.rbi +329 -0
  139. data/rbi/generated/prism/serialize.rbi +184 -0
  140. data/rbi/generated/prism/string_query.rbi +43 -0
  141. data/rbi/generated/prism/translation.rbi +7 -0
  142. data/rbi/generated/prism/visitor.rbi +650 -0
  143. data/rbi/generated/prism.rbi +86 -0
  144. data/rbi/prism/translation/parser_versions.rbi +5 -0
  145. data/rbi/rubyvm/node_find.rbi +14 -0
  146. data/sig/generated/prism/compiler.rbs +492 -0
  147. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  148. data/sig/generated/prism/dispatcher.rbs +1292 -0
  149. data/sig/generated/prism/dot_visitor.rbs +635 -0
  150. data/sig/generated/prism/dsl.rbs +928 -0
  151. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  152. data/sig/generated/prism/lex_compat.rbs +166 -0
  153. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  154. data/sig/generated/prism/node.rbs +20896 -0
  155. data/sig/generated/prism/node_ext.rbs +290 -0
  156. data/sig/generated/prism/node_find.rbs +90 -0
  157. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  158. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  159. data/sig/generated/prism/parse_result.rbs +871 -0
  160. data/sig/generated/prism/pattern.rbs +156 -0
  161. data/sig/generated/prism/reflection.rbs +101 -0
  162. data/sig/generated/prism/relocation.rbs +428 -0
  163. data/sig/generated/prism/serialize.rbs +198 -0
  164. data/sig/generated/prism/string_query.rbs +43 -0
  165. data/sig/generated/prism/translation.rbs +8 -0
  166. data/sig/generated/prism/visitor.rbs +1111 -0
  167. data/sig/generated/prism.rbs +84 -0
  168. data/sig/manifest.yaml +2 -0
  169. data/src/arena.c +117 -0
  170. data/src/{util/pm_buffer.c → buffer.c} +53 -21
  171. data/src/{util/pm_char.c → char.c} +28 -57
  172. data/src/comments.c +454 -0
  173. data/src/constant_pool.c +406 -0
  174. data/src/diagnostic.c +97 -65
  175. data/src/encoding.c +8 -3
  176. data/src/errors_format.c +519 -0
  177. data/src/{util/pm_integer.c → integer.c} +33 -22
  178. data/src/json.c +5733 -0
  179. data/src/line_offset_list.c +100 -0
  180. data/src/list.c +24 -0
  181. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  182. data/src/node.c +3504 -7674
  183. data/src/options.c +162 -49
  184. data/src/parser.c +367 -0
  185. data/src/prettyprint.c +421 -354
  186. data/src/prism.c +8402 -7161
  187. data/src/regexp.c +961 -45
  188. data/src/serialize.c +460 -424
  189. data/src/source.c +518 -0
  190. data/src/static_literals.c +111 -43
  191. data/src/string_query.c +180 -0
  192. data/src/stringy.c +91 -0
  193. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  194. data/src/strpbrk.c +447 -0
  195. data/src/{token_type.c → tokens.c} +39 -18
  196. data/src/xxh3.c +292 -0
  197. metadata +128 -63
  198. data/ext/prism/api_pack.c +0 -276
  199. data/include/prism/defines.h +0 -260
  200. data/include/prism/pack.h +0 -163
  201. data/include/prism/regexp.h +0 -43
  202. data/include/prism/util/pm_buffer.h +0 -236
  203. data/include/prism/util/pm_char.h +0 -204
  204. data/include/prism/util/pm_constant_pool.h +0 -218
  205. data/include/prism/util/pm_integer.h +0 -130
  206. data/include/prism/util/pm_memchr.h +0 -29
  207. data/include/prism/util/pm_newline_list.h +0 -113
  208. data/include/prism/util/pm_string.h +0 -200
  209. data/lib/prism/pack.rb +0 -230
  210. data/lib/prism/parse_result/comments.rb +0 -188
  211. data/rbi/prism/compiler.rbi +0 -12
  212. data/rbi/prism/dsl.rbi +0 -524
  213. data/rbi/prism/inspect_visitor.rbi +0 -12
  214. data/rbi/prism/node.rbi +0 -8750
  215. data/rbi/prism/node_ext.rbi +0 -107
  216. data/rbi/prism/parse_result.rbi +0 -404
  217. data/rbi/prism/reflection.rbi +0 -58
  218. data/rbi/prism/string_query.rbi +0 -12
  219. data/rbi/prism/visitor.rbi +0 -473
  220. data/rbi/prism.rbi +0 -63
  221. data/sig/prism/compiler.rbs +0 -9
  222. data/sig/prism/dispatcher.rbs +0 -19
  223. data/sig/prism/dot_visitor.rbs +0 -6
  224. data/sig/prism/dsl.rbs +0 -351
  225. data/sig/prism/inspect_visitor.rbs +0 -22
  226. data/sig/prism/lex_compat.rbs +0 -10
  227. data/sig/prism/node.rbs +0 -4042
  228. data/sig/prism/node_ext.rbs +0 -149
  229. data/sig/prism/pack.rbs +0 -43
  230. data/sig/prism/parse_result/comments.rbs +0 -38
  231. data/sig/prism/parse_result.rbs +0 -197
  232. data/sig/prism/pattern.rbs +0 -13
  233. data/sig/prism/reflection.rbs +0 -50
  234. data/sig/prism/relocation.rbs +0 -185
  235. data/sig/prism/serialize.rbs +0 -8
  236. data/sig/prism/string_query.rbs +0 -11
  237. data/sig/prism/visitor.rbs +0 -169
  238. data/sig/prism.rbs +0 -272
  239. data/src/pack.c +0 -509
  240. data/src/util/pm_constant_pool.c +0 -342
  241. data/src/util/pm_list.c +0 -49
  242. data/src/util/pm_newline_list.c +0 -125
  243. data/src/util/pm_string.c +0 -381
  244. data/src/util/pm_strpbrk.c +0 -206
@@ -0,0 +1,26 @@
1
+ #ifndef PRISM_INTERNAL_REGEXP_H
2
+ #define PRISM_INTERNAL_REGEXP_H
3
+
4
+ #include "prism/ast.h"
5
+ #include "prism/parser.h"
6
+
7
+ /*
8
+ * Callback invoked by pm_regexp_parse() for each named capture group found.
9
+ * The data pointer is whatever the caller passed in alongside the callback; the
10
+ * regexp parser passes it through without inspecting it.
11
+ */
12
+ typedef void (*pm_regexp_name_callback_t)(pm_parser_t *parser, const pm_string_t *name, void *data);
13
+
14
+ /*
15
+ * Parse a regular expression, validate its encoding, and optionally extract
16
+ * named capture groups. Returns the encoding flags to set on the node.
17
+ */
18
+ PRISM_EXPORTED_FUNCTION pm_node_flags_t pm_regexp_parse(pm_parser_t *parser, pm_regular_expression_node_t *node, pm_regexp_name_callback_t name_callback, void *name_data);
19
+
20
+ /*
21
+ * Parse an interpolated regular expression for named capture groups only.
22
+ * No encoding validation is performed.
23
+ */
24
+ void pm_regexp_parse_named_captures(pm_parser_t *parser, const uint8_t *source, size_t size, bool extended_mode, pm_regexp_name_callback_t name_callback, void *name_data);
25
+
26
+ #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
@@ -1,33 +1,27 @@
1
- /**
2
- * @file static_literals.h
3
- *
4
- * A set of static literal nodes that can be checked for duplicates.
5
- */
6
- #ifndef PRISM_STATIC_LITERALS_H
7
- #define PRISM_STATIC_LITERALS_H
1
+ #ifndef PRISM_INTERNAL_STATIC_LITERALS_H
2
+ #define PRISM_INTERNAL_STATIC_LITERALS_H
8
3
 
9
- #include "prism/defines.h"
10
- #include "prism/ast.h"
11
- #include "prism/util/pm_newline_list.h"
4
+ #include "prism/internal/encoding.h"
12
5
 
13
- #include <assert.h>
14
- #include <stdbool.h>
6
+ #include "prism/ast.h"
7
+ #include "prism/buffer.h"
8
+ #include "prism/line_offset_list.h"
15
9
 
16
- /**
10
+ /*
17
11
  * An internal hash table for a set of nodes.
18
12
  */
19
13
  typedef struct {
20
- /** The array of nodes in the hash table. */
14
+ /* The array of nodes in the hash table. */
21
15
  pm_node_t **nodes;
22
16
 
23
- /** The size of the hash table. */
17
+ /* The size of the hash table. */
24
18
  uint32_t size;
25
19
 
26
- /** The space that has been allocated in the hash table. */
20
+ /* The space that has been allocated in the hash table. */
27
21
  uint32_t capacity;
28
22
  } pm_node_hash_t;
29
23
 
30
- /**
24
+ /*
31
25
  * Certain sets of nodes (hash keys and when clauses) check for duplicate nodes
32
26
  * to alert the user of potential issues. To do this, we keep a set of the nodes
33
27
  * that have been seen so far, and compare whenever we find a new node.
@@ -36,86 +30,71 @@ typedef struct {
36
30
  * that need to be performed.
37
31
  */
38
32
  typedef struct {
39
- /**
33
+ /*
40
34
  * This is the set of IntegerNode and SourceLineNode instances.
41
35
  */
42
36
  pm_node_hash_t integer_nodes;
43
37
 
44
- /**
38
+ /*
45
39
  * This is the set of FloatNode instances.
46
40
  */
47
41
  pm_node_hash_t float_nodes;
48
42
 
49
- /**
43
+ /*
50
44
  * This is the set of RationalNode and ImaginaryNode instances.
51
45
  */
52
46
  pm_node_hash_t number_nodes;
53
47
 
54
- /**
48
+ /*
55
49
  * This is the set of StringNode and SourceFileNode instances.
56
50
  */
57
51
  pm_node_hash_t string_nodes;
58
52
 
59
- /**
53
+ /*
60
54
  * This is the set of RegularExpressionNode instances.
61
55
  */
62
56
  pm_node_hash_t regexp_nodes;
63
57
 
64
- /**
58
+ /*
65
59
  * This is the set of SymbolNode instances.
66
60
  */
67
61
  pm_node_hash_t symbol_nodes;
68
62
 
69
- /**
63
+ /*
70
64
  * A pointer to the last TrueNode instance that was inserted, or NULL.
71
65
  */
72
66
  pm_node_t *true_node;
73
67
 
74
- /**
68
+ /*
75
69
  * A pointer to the last FalseNode instance that was inserted, or NULL.
76
70
  */
77
71
  pm_node_t *false_node;
78
72
 
79
- /**
73
+ /*
80
74
  * A pointer to the last NilNode instance that was inserted, or NULL.
81
75
  */
82
76
  pm_node_t *nil_node;
83
77
 
84
- /**
78
+ /*
85
79
  * A pointer to the last SourceEncodingNode instance that was inserted, or
86
80
  * NULL.
87
81
  */
88
82
  pm_node_t *source_encoding_node;
89
83
  } pm_static_literals_t;
90
84
 
91
- /**
85
+ /*
92
86
  * Add a node to the set of static literals.
93
- *
94
- * @param newline_list The list of newline offsets to use to calculate lines.
95
- * @param start_line The line number that the parser starts on.
96
- * @param literals The set of static literals to add the node to.
97
- * @param node The node to add to the set.
98
- * @param replace Whether to replace the previous node if one already exists.
99
- * @return A pointer to the node that is being overwritten, if there is one.
100
87
  */
101
- pm_node_t * pm_static_literals_add(const pm_newline_list_t *newline_list, int32_t start_line, pm_static_literals_t *literals, pm_node_t *node, bool replace);
88
+ pm_node_t * pm_static_literals_add(const pm_line_offset_list_t *line_offsets, const uint8_t *start, int32_t start_line, const pm_encoding_t *encoding, pm_static_literals_t *literals, pm_node_t *node, bool replace);
102
89
 
103
- /**
90
+ /*
104
91
  * Free the internal memory associated with the given static literals set.
105
- *
106
- * @param literals The set of static literals to free.
107
92
  */
108
93
  void pm_static_literals_free(pm_static_literals_t *literals);
109
94
 
110
- /**
95
+ /*
111
96
  * Create a string-based representation of the given static literal.
112
- *
113
- * @param buffer The buffer to write the string to.
114
- * @param newline_list The list of newline offsets to use to calculate lines.
115
- * @param start_line The line number that the parser starts on.
116
- * @param encoding_name The name of the encoding of the source being parsed.
117
- * @param node The node to create a string representation of.
118
97
  */
119
- void pm_static_literal_inspect(pm_buffer_t *buffer, const pm_newline_list_t *newline_list, int32_t start_line, const char *encoding_name, const pm_node_t *node);
98
+ void pm_static_literal_inspect(pm_buffer_t *buffer, const pm_line_offset_list_t *line_offsets, const uint8_t *start, int32_t start_line, const pm_encoding_t *encoding, const pm_node_t *node);
120
99
 
121
100
  #endif
@@ -0,0 +1,35 @@
1
+ #ifndef PRISM_INTERNAL_STRINGY_H
2
+ #define PRISM_INTERNAL_STRINGY_H
3
+
4
+ #include "prism/stringy.h"
5
+
6
+ /*
7
+ * Backing storage for an empty string.
8
+ */
9
+ static const uint8_t empty_source[] = "";
10
+
11
+ /*
12
+ * Defines an empty string. This is useful for initializing a string that will
13
+ * be filled in later.
14
+ */
15
+ #define PM_STRING_EMPTY ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = empty_source, .length = 0 })
16
+
17
+ /*
18
+ * Initialize a shared string that is based on initial input.
19
+ */
20
+ void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end);
21
+
22
+ /*
23
+ * Compare the underlying lengths and bytes of two strings. Returns 0 if the
24
+ * strings are equal, a negative number if the left string is less than the
25
+ * right string, and a positive number if the left string is greater than the
26
+ * right string.
27
+ */
28
+ int pm_string_compare(const pm_string_t *left, const pm_string_t *right);
29
+
30
+ /*
31
+ * Free the associated memory of the given string.
32
+ */
33
+ void pm_string_cleanup(pm_string_t *string);
34
+
35
+ #endif
@@ -1,18 +1,10 @@
1
- /**
2
- * @file pm_strncasecmp.h
3
- *
4
- * A custom strncasecmp implementation.
5
- */
6
- #ifndef PRISM_STRNCASECMP_H
7
- #define PRISM_STRNCASECMP_H
1
+ #ifndef PRISM_INTERNAL_STRNCASECMP_H
2
+ #define PRISM_INTERNAL_STRNCASECMP_H
8
3
 
9
- #include "prism/defines.h"
10
-
11
- #include <ctype.h>
12
4
  #include <stddef.h>
13
5
  #include <stdint.h>
14
6
 
15
- /**
7
+ /*
16
8
  * Compare two strings, ignoring case, up to the given length. Returns 0 if the
17
9
  * strings are equal, a negative number if string1 is less than string2, or a
18
10
  * positive number if string1 is greater than string2.
@@ -20,12 +12,6 @@
20
12
  * Note that this is effectively our own implementation of strncasecmp, but it's
21
13
  * not available on all of the platforms we want to support so we're rolling it
22
14
  * here.
23
- *
24
- * @param string1 The first string to compare.
25
- * @param string2 The second string to compare
26
- * @param length The maximum number of characters to compare.
27
- * @return 0 if the strings are equal, a negative number if string1 is less than
28
- * string2, or a positive number if string1 is greater than string2.
29
15
  */
30
16
  int pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length);
31
17
 
@@ -1,19 +1,15 @@
1
- /**
2
- * @file pm_strpbrk.h
3
- *
4
- * A custom strpbrk implementation.
5
- */
6
- #ifndef PRISM_STRPBRK_H
7
- #define PRISM_STRPBRK_H
1
+ #ifndef PRISM_INTERNAL_STRPBRK_H
2
+ #define PRISM_INTERNAL_STRPBRK_H
8
3
 
9
- #include "prism/defines.h"
10
- #include "prism/diagnostic.h"
11
4
  #include "prism/parser.h"
12
5
 
6
+ /* The maximum number of bytes in a strpbrk charset. */
7
+ #define PM_STRPBRK_CACHE_SIZE 16
8
+
13
9
  #include <stddef.h>
14
- #include <string.h>
10
+ #include <stdint.h>
15
11
 
16
- /**
12
+ /*
17
13
  * Here we have rolled our own version of strpbrk. The standard library strpbrk
18
14
  * has undefined behavior when the source string is not null-terminated. We want
19
15
  * to support strings that are not null-terminated because pm_parse does not
@@ -31,15 +27,6 @@
31
27
  * characters that are trailing bytes of multi-byte characters. For example, in
32
28
  * Shift-JIS, the backslash character can be a trailing byte. In that case we
33
29
  * need to take a slower path and iterate one multi-byte character at a time.
34
- *
35
- * @param parser The parser.
36
- * @param source The source to search.
37
- * @param charset The charset to search for.
38
- * @param length The maximum number of bytes to search.
39
- * @param validate Whether to validate that the source string is valid in the
40
- * current encoding of the parser.
41
- * @return A pointer to the first character in the source string that is in the
42
- * charset, or NULL if no such character exists.
43
30
  */
44
31
  const uint8_t * pm_strpbrk(pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, ptrdiff_t length, bool validate);
45
32
 
@@ -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,32 @@
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
+ /**
22
+ * Dump JSON to the given buffer.
23
+ *
24
+ * @param buffer The buffer to serialize to.
25
+ * @param parser The parser that parsed the node.
26
+ * @param node The node to serialize.
27
+ */
28
+ 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);
29
+
30
+ #endif
31
+
32
+ #endif
@@ -0,0 +1,61 @@
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
+ /**
24
+ * A list of offsets of the start of lines in a string. The offsets are assumed
25
+ * to be sorted/inserted in ascending order.
26
+ */
27
+ typedef struct {
28
+ /** The number of offsets in the list. */
29
+ size_t size;
30
+
31
+ /** The capacity of the list that has been allocated. */
32
+ size_t capacity;
33
+
34
+ /** The list of offsets. */
35
+ uint32_t *offsets;
36
+ } pm_line_offset_list_t;
37
+
38
+ /**
39
+ * A line and column in a string.
40
+ */
41
+ typedef struct {
42
+ /** The line number. */
43
+ int32_t line;
44
+
45
+ /** The column in bytes. */
46
+ uint32_t column;
47
+ } pm_line_column_t;
48
+
49
+ /**
50
+ * Returns the line and column of the given offset. If the offset is not in the
51
+ * list, the line and column of the closest offset less than the given offset
52
+ * are returned.
53
+ *
54
+ * @param list The list to search.
55
+ * @param cursor The offset to search for.
56
+ * @param start_line The line to start counting from.
57
+ * @returns The line and column of the given offset.
58
+ */
59
+ 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);
60
+
61
+ #endif
@@ -0,0 +1,35 @@
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
+ /** An opaque pointer to a magic comment found while parsing. */
17
+ typedef struct pm_magic_comment_t pm_magic_comment_t;
18
+
19
+ /**
20
+ * Returns the location of the key associated with the given magic comment.
21
+ *
22
+ * @param magic_comment the magic comment whose key location we want to get
23
+ * @returns the location of the key associated with the given magic comment
24
+ */
25
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_key(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1);
26
+
27
+ /**
28
+ * Returns the location of the value associated with the given magic comment.
29
+ *
30
+ * @param magic_comment the magic comment whose value location we want to get
31
+ * @returns the location of the value associated with the given magic comment
32
+ */
33
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_value(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1);
34
+
35
+ #endif
data/include/prism/node.h CHANGED
@@ -6,9 +6,10 @@
6
6
  #ifndef PRISM_NODE_H
7
7
  #define PRISM_NODE_H
8
8
 
9
- #include "prism/defines.h"
10
- #include "prism/parser.h"
11
- #include "prism/util/pm_buffer.h"
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nonnull.h"
11
+
12
+ #include "prism/ast.h"
12
13
 
13
14
  /**
14
15
  * Loop through each node in the node list, writing each node to the given
@@ -17,52 +18,13 @@
17
18
  #define PM_NODE_LIST_FOREACH(list, index, node) \
18
19
  for (size_t index = 0; index < (list)->size && ((node) = (list)->nodes[index]); index++)
19
20
 
20
- /**
21
- * Append a new node onto the end of the node list.
22
- *
23
- * @param list The list to append to.
24
- * @param node The node to append.
25
- */
26
- void pm_node_list_append(pm_node_list_t *list, pm_node_t *node);
27
-
28
- /**
29
- * Prepend a new node onto the beginning of the node list.
30
- *
31
- * @param list The list to prepend to.
32
- * @param node The node to prepend.
33
- */
34
- void pm_node_list_prepend(pm_node_list_t *list, pm_node_t *node);
35
-
36
- /**
37
- * Concatenate the given node list onto the end of the other node list.
38
- *
39
- * @param list The list to concatenate onto.
40
- * @param other The list to concatenate.
41
- */
42
- void pm_node_list_concat(pm_node_list_t *list, pm_node_list_t *other);
43
-
44
- /**
45
- * Free the internal memory associated with the given node list.
46
- *
47
- * @param list The list to free.
48
- */
49
- void pm_node_list_free(pm_node_list_t *list);
50
-
51
- /**
52
- * Deallocate a node and all of its children.
53
- *
54
- * @param parser The parser that owns the node.
55
- * @param node The node to deallocate.
56
- */
57
- PRISM_EXPORTED_FUNCTION void pm_node_destroy(pm_parser_t *parser, struct pm_node *node);
58
-
59
21
  /**
60
22
  * Returns a string representation of the given node type.
61
23
  *
62
24
  * @param node_type The node type to convert to a string.
63
- * @return A string representation of the given node type.
25
+ * @returns A string representation of the given node type.
64
26
  */
65
- PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str(pm_node_type_t node_type);
27
+ PRISM_EXPORTED_FUNCTION const char * pm_node_type(pm_node_type_t node_type);
66
28
 
67
29
  /**
68
30
  * Visit each of the nodes in this subtree using the given visitor callback. The
@@ -80,7 +42,7 @@ PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str(pm_node_type_t node_typ
80
42
  * bool visit(const pm_node_t *node, void *data) {
81
43
  * size_t *indent = (size_t *) data;
82
44
  * for (size_t i = 0; i < *indent * 2; i++) putc(' ', stdout);
83
- * printf("%s\n", pm_node_type_to_str(node->type));
45
+ * printf("%s\n", pm_node_type(node->type));
84
46
  *
85
47
  * size_t next_indent = *indent + 1;
86
48
  * size_t *next_data = &next_indent;
@@ -93,18 +55,21 @@ PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str(pm_node_type_t node_typ
93
55
  * const char *source = "1 + 2; 3 + 4";
94
56
  * size_t size = strlen(source);
95
57
  *
96
- * pm_parser_t parser;
97
- * pm_options_t options = { 0 };
98
- * pm_parser_init(&parser, (const uint8_t *) source, size, &options);
58
+ * pm_arena_t *arena = pm_arena_new();
59
+ * pm_options_t *options = pm_options_new();
60
+ *
61
+ * pm_parser_t *parser = pm_parser_new(arena, (const uint8_t *) source, size, options);
99
62
  *
100
63
  * size_t indent = 0;
101
- * pm_node_t *node = pm_parse(&parser);
64
+ * pm_node_t *node = pm_parse(parser);
102
65
  *
103
66
  * size_t *data = &indent;
104
67
  * pm_visit_node(node, visit, data);
105
68
  *
106
- * pm_node_destroy(&parser, node);
107
- * pm_parser_free(&parser);
69
+ * pm_parser_free(parser);
70
+ * pm_options_free(options);
71
+ * pm_arena_free(arena);
72
+ *
108
73
  * return EXIT_SUCCESS;
109
74
  * }
110
75
  * ```
@@ -113,7 +78,7 @@ PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str(pm_node_type_t node_typ
113
78
  * @param visitor The callback to call for each node in the subtree.
114
79
  * @param data An opaque pointer that is passed to the visitor callback.
115
80
  */
116
- PRISM_EXPORTED_FUNCTION void pm_visit_node(const pm_node_t *node, bool (*visitor)(const pm_node_t *node, void *data), void *data);
81
+ PRISM_EXPORTED_FUNCTION void pm_visit_node(const pm_node_t *node, bool (*visitor)(const pm_node_t *node, void *data), void *data) PRISM_NONNULL(1);
117
82
 
118
83
  /**
119
84
  * Visit the children of the given node with the given callback. This is the
@@ -124,6 +89,6 @@ PRISM_EXPORTED_FUNCTION void pm_visit_node(const pm_node_t *node, bool (*visitor
124
89
  * @param visitor The callback to call for each child node.
125
90
  * @param data An opaque pointer that is passed to the visitor callback.
126
91
  */
127
- PRISM_EXPORTED_FUNCTION void pm_visit_child_nodes(const pm_node_t *node, bool (*visitor)(const pm_node_t *node, void *data), void *data);
92
+ PRISM_EXPORTED_FUNCTION void pm_visit_child_nodes(const pm_node_t *node, bool (*visitor)(const pm_node_t *node, void *data), void *data) PRISM_NONNULL(1);
128
93
 
129
94
  #endif