prism 2.0.0-arm-linux-musl

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,274 @@
1
+ /**
2
+ * @file parser.hpp
3
+ *
4
+ * Ownership of parsers from C++, and lookups through a parser into the source,
5
+ * the constant pool, comments, and diagnostics.
6
+ */
7
+ #ifndef PRISM_PARSER_HPP
8
+ #define PRISM_PARSER_HPP
9
+
10
+ #include "prism/compiler/cplusplus.hpp"
11
+ #include "prism/compiler/lifetimebound.hpp"
12
+
13
+ #include "prism/arena.h"
14
+ #include "prism/constant_pool.h"
15
+ #include "prism/line_offset_list.h"
16
+ #include "prism/parser.h"
17
+
18
+ #include <cassert>
19
+ #include <cstddef>
20
+ #include <cstdint>
21
+ #include <iterator>
22
+ #include <memory>
23
+ #include <string_view>
24
+
25
+ namespace prism {
26
+
27
+ /** Frees a parser with pm_parser_free. */
28
+ struct parser_deleter {
29
+ /**
30
+ * Free the given parser.
31
+ *
32
+ * @param parser The parser to free.
33
+ */
34
+ void operator()(pm_parser_t *parser) const noexcept { pm_parser_free(parser); }
35
+ };
36
+
37
+ /** An owned parser. */
38
+ using unique_parser = std::unique_ptr<pm_parser_t, parser_deleter>;
39
+
40
+ /**
41
+ * Allocate a new parser over the given source. The source, the arena, and the
42
+ * options (which may be null) are not copied, so all three must outlive the
43
+ * parser. Allocation failure aborts, so this never throws.
44
+ *
45
+ * @param arena The arena to use for allocations.
46
+ * @param source The source code to parse.
47
+ * @param options The parse options, or null for defaults.
48
+ * @return A unique_parser owning the newly allocated parser.
49
+ */
50
+ [[nodiscard]] inline unique_parser
51
+ make_parser(pm_arena_t *arena PRISM_LIFETIMEBOUND, std::string_view source PRISM_LIFETIMEBOUND, const pm_options_t *options PRISM_LIFETIMEBOUND = nullptr) noexcept {
52
+ return unique_parser(pm_parser_new(arena, reinterpret_cast<const std::uint8_t *>(source.data()), source.size(), options));
53
+ }
54
+
55
+ /**
56
+ * The name of the constant with the given id, which must not be zero. Fields
57
+ * holding an optional constant use zero for none, so check those first.
58
+ *
59
+ * @param parser The parser containing the constant.
60
+ * @param constant_id The id of the constant.
61
+ * @return The value of the constant.
62
+ */
63
+ [[nodiscard]] inline std::string_view
64
+ constant(const pm_parser_t *parser PRISM_LIFETIMEBOUND, pm_constant_id_t constant_id) noexcept {
65
+ assert(constant_id != 0);
66
+ const pm_constant_t *entry = pm_parser_constant(parser, constant_id);
67
+ return std::string_view(reinterpret_cast<const char *>(pm_constant_start(entry)), pm_constant_length(entry));
68
+ }
69
+
70
+ /**
71
+ * The bytes of source that the given location covers.
72
+ *
73
+ * @param parser The parser containing the source.
74
+ * @param location The location in the source.
75
+ * @return The bytes of source covered by the location.
76
+ */
77
+ [[nodiscard]] inline std::string_view
78
+ slice(const pm_parser_t *parser PRISM_LIFETIMEBOUND, pm_location_t location) noexcept {
79
+ return std::string_view(reinterpret_cast<const char *>(pm_parser_start(parser) + location.start), location.length);
80
+ }
81
+
82
+ /**
83
+ * The line and column of the given byte offset into the source. Lines start at
84
+ * the parser's start line, and columns are byte offsets from the line start.
85
+ *
86
+ * @param parser The parser containing the source.
87
+ * @param offset The byte offset into the source.
88
+ * @return The line and column of the byte offset.
89
+ */
90
+ [[nodiscard]] inline pm_line_column_t
91
+ line_column(const pm_parser_t *parser, std::uint32_t offset) noexcept {
92
+ return pm_line_offset_list_line_column(pm_parser_line_offsets(parser), offset, pm_parser_start_line(parser));
93
+ }
94
+
95
+ namespace detail {
96
+
97
+ /**
98
+ * The diagnostics, comments, or magic comments found while parsing, usable in a
99
+ * range-based for loop and with the standard algorithms. Each element is a
100
+ * pointer to the C struct, which the C accessors read, and Next is the C
101
+ * function that returns the item after a given one. It refers to the parser
102
+ * rather than copying from it, so the parser must outlive it. It is used
103
+ * through diagnostic_range, comment_range, and magic_comment_range.
104
+ */
105
+ template <typename T, const T *(*Next)(const T *)> class next_range {
106
+ public:
107
+ /** Steps through the items one after another. */
108
+ class iterator {
109
+ public:
110
+ /** The iterator category, for the standard algorithms. */
111
+ using iterator_category = std::forward_iterator_tag;
112
+
113
+ /** The type of each element. */
114
+ using value_type = const T *;
115
+
116
+ /** The type of the distance between two iterators. */
117
+ using difference_type = std::ptrdiff_t;
118
+
119
+ /** The type of a pointer to an element. */
120
+ using pointer = const value_type *;
121
+
122
+ /** The type of a reference to an element. */
123
+ using reference = const value_type &;
124
+
125
+ /** Create the iterator that marks the end of the items. */
126
+ iterator() noexcept = default;
127
+
128
+ /**
129
+ * Create an iterator at the given item.
130
+ *
131
+ * @param current The current item.
132
+ */
133
+ explicit iterator(const T *current) noexcept : current_(current) {}
134
+
135
+ /**
136
+ * The item at this position, which must not be the end.
137
+ *
138
+ * @return The current item.
139
+ */
140
+ reference operator*() const noexcept { return current_; }
141
+
142
+ /**
143
+ * Advance to the next item.
144
+ *
145
+ * @return The iterator after advancing.
146
+ */
147
+ iterator &
148
+ operator++() noexcept {
149
+ assert(current_ != nullptr);
150
+ current_ = Next(current_);
151
+ return *this;
152
+ }
153
+
154
+ /**
155
+ * Advance to the next item, returning the iterator from before.
156
+ *
157
+ * @return The iterator from before advancing.
158
+ */
159
+ iterator
160
+ operator++(int) noexcept {
161
+ iterator previous = *this;
162
+ ++*this;
163
+ return previous;
164
+ }
165
+
166
+ /** Whether the two iterators are at the same item. */
167
+ friend bool operator==(iterator left, iterator right) noexcept { return left.current_ == right.current_; }
168
+
169
+ /** Whether the two iterators are at different items. */
170
+ friend bool operator!=(iterator left, iterator right) noexcept { return left.current_ != right.current_; }
171
+
172
+ private:
173
+ const T *current_ = nullptr;
174
+ };
175
+
176
+ /**
177
+ * Create a range of the given number of items starting at first.
178
+ *
179
+ * @param first The first item.
180
+ * @param size The number of items.
181
+ */
182
+ next_range(const T *first, std::size_t size) noexcept : first_(first), size_(size) {}
183
+
184
+ /**
185
+ * An iterator at the first item.
186
+ *
187
+ * @return An iterator at the first item.
188
+ */
189
+ iterator begin() const noexcept { return iterator(first_); }
190
+
191
+ /**
192
+ * An iterator past the last item.
193
+ *
194
+ * @return An iterator past the last item.
195
+ */
196
+ iterator end() const noexcept { return iterator(); }
197
+
198
+ /**
199
+ * The number of items.
200
+ *
201
+ * @return The number of items.
202
+ */
203
+ std::size_t size() const noexcept { return size_; }
204
+
205
+ /**
206
+ * Whether there are no items.
207
+ *
208
+ * @return Whether there are no items.
209
+ */
210
+ [[nodiscard]] bool empty() const noexcept { return size_ == 0; }
211
+
212
+ private:
213
+ const T *first_;
214
+ std::size_t size_;
215
+ };
216
+
217
+ }
218
+
219
+ /** The errors or warnings found while parsing. */
220
+ using diagnostic_range = detail::next_range<pm_diagnostic_t, pm_diagnostic_next>;
221
+
222
+ /** The comments found while parsing. */
223
+ using comment_range = detail::next_range<pm_comment_t, pm_comment_next>;
224
+
225
+ /** The magic comments found while parsing. */
226
+ using magic_comment_range = detail::next_range<pm_magic_comment_t, pm_magic_comment_next>;
227
+
228
+ /**
229
+ * The errors found while parsing.
230
+ *
231
+ * @param parser The parser containing the source.
232
+ * @return The range of errors found while parsing.
233
+ */
234
+ [[nodiscard]] inline diagnostic_range
235
+ errors(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
236
+ return diagnostic_range(pm_parser_errors_first(parser), pm_parser_errors_size(parser));
237
+ }
238
+
239
+ /**
240
+ * The warnings found while parsing.
241
+ *
242
+ * @param parser The parser containing the source.
243
+ * @return The range of warnings found while parsing.
244
+ */
245
+ [[nodiscard]] inline diagnostic_range
246
+ warnings(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
247
+ return diagnostic_range(pm_parser_warnings_first(parser), pm_parser_warnings_size(parser));
248
+ }
249
+
250
+ /**
251
+ * The comments found while parsing, in source order.
252
+ *
253
+ * @param parser The parser containing the source.
254
+ * @return The range of comments found while parsing.
255
+ */
256
+ [[nodiscard]] inline comment_range
257
+ comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
258
+ return comment_range(pm_parser_comments_first(parser), pm_parser_comments_size(parser));
259
+ }
260
+
261
+ /**
262
+ * The magic comments found while parsing.
263
+ *
264
+ * @param parser The parser containing the source.
265
+ * @return The range of magic comments found while parsing.
266
+ */
267
+ [[nodiscard]] inline magic_comment_range
268
+ magic_comments(const pm_parser_t *parser PRISM_LIFETIMEBOUND) noexcept {
269
+ return magic_comment_range(pm_parser_magic_comments_first(parser), pm_parser_magic_comments_size(parser));
270
+ }
271
+
272
+ }
273
+
274
+ #endif
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @file prettyprint.h
3
+ *
4
+ * An AST node pretty-printer.
5
+ */
6
+ #ifndef PRISM_PRETTYPRINT_H
7
+ #define PRISM_PRETTYPRINT_H
8
+
9
+ #include "prism/excludes.h"
10
+
11
+ #ifndef PRISM_EXCLUDE_PRETTYPRINT
12
+
13
+ #include "prism/compiler/exported.h"
14
+ #include "prism/compiler/nonnull.h"
15
+
16
+ #include "prism/ast.h"
17
+ #include "prism/buffer.h"
18
+ #include "prism/parser.h"
19
+
20
+ #ifdef __cplusplus
21
+ extern "C" {
22
+ #endif
23
+
24
+ /**
25
+ * Pretty-prints the AST represented by the given node to the given buffer.
26
+ *
27
+ * @param output_buffer The buffer to write the pretty-printed AST to.
28
+ * @param parser The parser that parsed the AST.
29
+ * @param node The root node of the AST to pretty-print.
30
+ */
31
+ PRISM_EXPORTED_FUNCTION void pm_prettyprint(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm_node_t *node) PRISM_NONNULL(1, 2, 3);
32
+
33
+ #ifdef __cplusplus
34
+ }
35
+ #endif
36
+
37
+ #endif
38
+
39
+ #endif
@@ -0,0 +1,123 @@
1
+ /**
2
+ * @file serialize.h
3
+ *
4
+ * The functions related to serializing the AST to a binary format.
5
+ */
6
+ #ifndef PRISM_SERIALIZE_H
7
+ #define PRISM_SERIALIZE_H
8
+
9
+ #include "prism/excludes.h"
10
+
11
+ /* We optionally support serializing to a binary string. For systems that do not
12
+ * want or need this functionality, it can be turned off with the
13
+ * PRISM_EXCLUDE_SERIALIZATION define. */
14
+ #ifndef PRISM_EXCLUDE_SERIALIZATION
15
+
16
+ #include "prism/compiler/exported.h"
17
+ #include "prism/compiler/nonnull.h"
18
+
19
+ #include "prism/buffer.h"
20
+ #include "prism/errors_format.h"
21
+ #include "prism/parser.h"
22
+ #include "prism/source.h"
23
+ #include "prism/stream.h"
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ /**
30
+ * Serialize the AST represented by the given node to the given buffer.
31
+ *
32
+ * @param parser The parser to serialize.
33
+ * @param node The node to serialize.
34
+ * @param buffer The buffer to serialize to.
35
+ */
36
+ PRISM_EXPORTED_FUNCTION void pm_serialize(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) PRISM_NONNULL(1, 2, 3);
37
+
38
+ /**
39
+ * Parse the given source to the AST and dump the AST to the given buffer.
40
+ *
41
+ * @param buffer The buffer to serialize to.
42
+ * @param source The source to parse.
43
+ * @param size The size of the source.
44
+ * @param data The optional data to pass to the parser.
45
+ */
46
+ PRISM_EXPORTED_FUNCTION void pm_serialize_parse(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data) PRISM_NONNULL(1, 2);
47
+
48
+ /**
49
+ * Parse and serialize the AST represented by the given source into the given
50
+ * buffer.
51
+ *
52
+ * @param buffer The buffer to serialize to.
53
+ * @param source The source to parse.
54
+ * @param data The optional data to pass to the parser.
55
+ */
56
+ PRISM_EXPORTED_FUNCTION void pm_serialize_parse_stream(pm_buffer_t *buffer, pm_source_t *source, const char *data) PRISM_NONNULL(1, 2);
57
+
58
+ /**
59
+ * Parse and serialize the comments in the given source to the given buffer.
60
+ *
61
+ * @param buffer The buffer to serialize to.
62
+ * @param source The source to parse.
63
+ * @param size The size of the source.
64
+ * @param data The optional data to pass to the parser.
65
+ */
66
+ PRISM_EXPORTED_FUNCTION void pm_serialize_parse_comments(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data) PRISM_NONNULL(1, 2);
67
+
68
+ /**
69
+ * Lex the given source and serialize to the given buffer.
70
+ *
71
+ * @param source The source to lex.
72
+ * @param size The size of the source.
73
+ * @param buffer The buffer to serialize to.
74
+ * @param data The optional data to pass to the lexer.
75
+ */
76
+ PRISM_EXPORTED_FUNCTION void pm_serialize_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data) PRISM_NONNULL(1, 2);
77
+
78
+ /**
79
+ * Parse and serialize both the AST and the tokens represented by the given
80
+ * source to the given buffer.
81
+ *
82
+ * @param buffer The buffer to serialize to.
83
+ * @param source The source to parse.
84
+ * @param size The size of the source.
85
+ * @param data The optional data to pass to the parser.
86
+ */
87
+ PRISM_EXPORTED_FUNCTION void pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data) PRISM_NONNULL(1, 2);
88
+
89
+ /**
90
+ * Parse the source and return true if it parses without errors or warnings.
91
+ *
92
+ * @param source The source to parse.
93
+ * @param size The size of the source.
94
+ * @param data The optional data to pass to the parser.
95
+ * @returns True if the source parses without errors or warnings.
96
+ */
97
+ PRISM_EXPORTED_FUNCTION bool pm_serialize_parse_success_p(const uint8_t *source, size_t size, const char *data) PRISM_NONNULL(1);
98
+
99
+ /**
100
+ * Parse the given source and format any errors that are encountered into the
101
+ * given buffer using the given format type. If the source parses without any
102
+ * errors, then -1 is returned and the buffer is left empty. Otherwise, the
103
+ * name of the encoding of the source is written to the buffer, followed by a
104
+ * null byte, followed by the formatted errors, and the error level of the
105
+ * error with the highest precedence is returned.
106
+ *
107
+ * @param buffer The buffer to write the encoding name and formatted errors to.
108
+ * @param source The source to parse.
109
+ * @param size The size of the source.
110
+ * @param data The optional data to pass to the parser.
111
+ * @param format_type The type of formatting to use when formatting the errors.
112
+ * @returns The error level of the error with the highest precedence, or -1 if
113
+ * the source parsed without errors.
114
+ */
115
+ PRISM_EXPORTED_FUNCTION int8_t pm_serialize_parse_errors_format(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data, pm_errors_format_type_t format_type) PRISM_NONNULL(1, 2);
116
+
117
+ #ifdef __cplusplus
118
+ }
119
+ #endif
120
+
121
+ #endif
122
+
123
+ #endif
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @file source.h
3
+ *
4
+ * An opaque type representing the source code being parsed, regardless of
5
+ * origin (constant memory, file, memory-mapped file, or stream).
6
+ */
7
+ #ifndef PRISM_SOURCE_H
8
+ #define PRISM_SOURCE_H
9
+
10
+ #include "prism/compiler/exported.h"
11
+ #include "prism/compiler/filesystem.h"
12
+ #include "prism/compiler/nodiscard.h"
13
+ #include "prism/compiler/nonnull.h"
14
+
15
+ #include <stddef.h>
16
+ #include <stdint.h>
17
+
18
+ #ifdef __cplusplus
19
+ extern "C" {
20
+ #endif
21
+
22
+ /**
23
+ * An opaque type representing source code being parsed.
24
+ */
25
+ typedef struct pm_source_t pm_source_t;
26
+
27
+ /**
28
+ * This function is used to retrieve a line of input from a stream. It closely
29
+ * mirrors that of fgets so that fgets can be used as the default
30
+ * implementation.
31
+ *
32
+ * As with fgets, implementations MUST write at most `size` bytes into
33
+ * `string`, including the terminating NUL byte (i.e. at most `size - 1` bytes
34
+ * of data followed by a '\0'). The caller relies on this bound for memory
35
+ * safety, so an implementation that reads from a source which may return more
36
+ * data than requested (for example a multi-byte-aware `gets`) is responsible
37
+ * for clamping the amount it copies. Returns `string`, or NULL on EOF.
38
+ */
39
+ typedef char * (pm_source_stream_fgets_t)(char *string, int size, void *stream);
40
+
41
+ /**
42
+ * This function is used to check whether a stream is at EOF. It closely mirrors
43
+ * that of feof so that feof can be used as the default implementation.
44
+ */
45
+ typedef int (pm_source_stream_feof_t)(void *stream);
46
+
47
+ /**
48
+ * Represents the result of initializing a source from a file.
49
+ */
50
+ typedef enum {
51
+ /** Indicates that the source was successfully initialized. */
52
+ PM_SOURCE_INIT_SUCCESS = 0,
53
+
54
+ /**
55
+ * Indicates a generic error from a source init function, where the type
56
+ * of error should be read from `errno` or `GetLastError()`.
57
+ */
58
+ PM_SOURCE_INIT_ERROR_GENERIC = 1,
59
+
60
+ /**
61
+ * Indicates that the file that was attempted to be opened was a directory.
62
+ */
63
+ PM_SOURCE_INIT_ERROR_DIRECTORY = 2,
64
+
65
+ /**
66
+ * Indicates that the file is not a regular file (e.g. a pipe or character
67
+ * device) and the caller should handle reading it.
68
+ */
69
+ PM_SOURCE_INIT_ERROR_NON_REGULAR = 3
70
+ } pm_source_init_result_t;
71
+
72
+ /**
73
+ * Create a new source that wraps existing constant memory. The memory is not
74
+ * owned and will not be freed.
75
+ *
76
+ * @param data The pointer to the source data.
77
+ * @param length The length of the source data in bytes.
78
+ * @returns A new source. Aborts on allocation failure.
79
+ */
80
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_source_t * pm_source_constant_new(const uint8_t *data, size_t length);
81
+
82
+ /**
83
+ * Create a new source that wraps existing shared memory. The memory is not
84
+ * owned and will not be freed. Semantically a "slice" of another source.
85
+ *
86
+ * @param data The pointer to the source data.
87
+ * @param length The length of the source data in bytes.
88
+ * @returns A new source. Aborts on allocation failure.
89
+ */
90
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_source_t * pm_source_shared_new(const uint8_t *data, size_t length);
91
+
92
+ /**
93
+ * Create a new source that owns its memory. The memory will be freed with
94
+ * xfree when the source is freed.
95
+ *
96
+ * @param data The pointer to the heap-allocated source data.
97
+ * @param length The length of the source data in bytes.
98
+ * @returns A new source. Aborts on allocation failure.
99
+ */
100
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_source_t * pm_source_owned_new(uint8_t *data, size_t length);
101
+
102
+ /**
103
+ * Create a new source by reading a file into a heap-allocated buffer.
104
+ *
105
+ * @param filepath The path to the file to read.
106
+ * @param result Out parameter for the result of the initialization.
107
+ * @returns A new source, or NULL on error (with result written to out param).
108
+ */
109
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_source_t * pm_source_file_new(const char *filepath, pm_source_init_result_t *result) PRISM_NONNULL(1, 2);
110
+
111
+ /**
112
+ * Create a new source by memory-mapping a file. Falls back to file reading on
113
+ * platforms without mmap support.
114
+ *
115
+ * If the file is a non-regular file (e.g. a pipe or character device),
116
+ * PM_SOURCE_INIT_ERROR_NON_REGULAR is returned, allowing the caller to handle
117
+ * it appropriately (e.g. by reading it through their own I/O layer).
118
+ *
119
+ * @param filepath The path to the file to read.
120
+ * @param open_flags Additional flags to pass to open(2) (e.g. O_NONBLOCK).
121
+ * @param result Out parameter for the result of the initialization.
122
+ * @returns A new source, or NULL on error (with result written to out param).
123
+ */
124
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_source_t * pm_source_mapped_new(const char *filepath, int open_flags, pm_source_init_result_t *result) PRISM_NONNULL(1, 3);
125
+
126
+ /**
127
+ * Create a new source by reading from a stream using the provided callbacks.
128
+ *
129
+ * @param stream The stream to read from.
130
+ * @param fgets The function to use to read from the stream.
131
+ * @param feof The function to use to check if the stream is at EOF.
132
+ * @returns A new source. Aborts on allocation failure.
133
+ */
134
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_source_t * pm_source_stream_new(void *stream, pm_source_stream_fgets_t *fgets, pm_source_stream_feof_t *feof);
135
+
136
+ /**
137
+ * Free the given source and any memory it owns.
138
+ *
139
+ * @param source The source to free.
140
+ */
141
+ PRISM_EXPORTED_FUNCTION void pm_source_free(pm_source_t *source) PRISM_NONNULL(1);
142
+
143
+ /**
144
+ * Returns the length of the source data in bytes.
145
+ *
146
+ * @param source The source to get the length of.
147
+ * @returns The length of the source data.
148
+ */
149
+ PRISM_EXPORTED_FUNCTION size_t pm_source_length(const pm_source_t *source) PRISM_NONNULL(1);
150
+
151
+ /**
152
+ * Returns a pointer to the source data.
153
+ *
154
+ * @param source The source to get the data of.
155
+ * @returns A pointer to the source data.
156
+ */
157
+ PRISM_EXPORTED_FUNCTION const uint8_t * pm_source_source(const pm_source_t *source) PRISM_NONNULL(1);
158
+
159
+ #ifdef __cplusplus
160
+ }
161
+ #endif
162
+
163
+ #endif
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @file stream.h
3
+ *
4
+ * Functions for parsing streams.
5
+ */
6
+ #ifndef PRISM_STREAM_H
7
+ #define PRISM_STREAM_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nonnull.h"
11
+
12
+ #include "prism/arena.h"
13
+ #include "prism/options.h"
14
+ #include "prism/parser.h"
15
+ #include "prism/source.h"
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ /**
22
+ * Parse a stream of Ruby source and return the tree.
23
+ *
24
+ * @param parser The out parameter to write the parser to.
25
+ * @param arena The arena to use for all AST-lifetime allocations.
26
+ * @param source The source to use, created via pm_source_stream_new.
27
+ * @param options The optional options to use when parsing.
28
+ * @returns The AST representing the source.
29
+ */
30
+ PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t **parser, pm_arena_t *arena, pm_source_t *source, const pm_options_t *options) PRISM_NONNULL(1, 2, 3);
31
+
32
+ #ifdef __cplusplus
33
+ }
34
+ #endif
35
+
36
+ #endif