prism 1.9.0 → 2.0.0.rc1

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 (238) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +84 -0
  3. data/Makefile +41 -12
  4. data/README.md +35 -97
  5. data/config.yml +174 -161
  6. data/docs/build_system.md +2 -2
  7. data/docs/configuration.md +5 -4
  8. data/docs/encoding.md +2 -2
  9. data/docs/fuzzing.md +4 -11
  10. data/docs/javascript.md +19 -4
  11. data/docs/mapping.md +1 -1
  12. data/docs/releasing.md +77 -5
  13. data/docs/relocation.md +2 -4
  14. data/docs/serialization.md +21 -24
  15. data/docs/testing.md +46 -29
  16. data/ext/prism/api_node.c +1461 -1401
  17. data/ext/prism/extconf.rb +3 -4
  18. data/ext/prism/extension.c +778 -422
  19. data/ext/prism/extension.h +3 -3
  20. data/include/prism/arena.h +37 -0
  21. data/include/prism/ast.h +2727 -610
  22. data/include/prism/buffer.h +52 -0
  23. data/include/prism/comments.h +55 -0
  24. data/include/prism/compiler/accel.h +24 -0
  25. data/include/prism/compiler/align.h +36 -0
  26. data/include/prism/compiler/assume.h +25 -0
  27. data/include/prism/compiler/exported.h +24 -0
  28. data/include/prism/compiler/fallthrough.h +22 -0
  29. data/include/prism/compiler/filesystem.h +32 -0
  30. data/include/prism/compiler/flex_array.h +19 -0
  31. data/include/prism/compiler/force_inline.h +21 -0
  32. data/include/prism/compiler/format.h +25 -0
  33. data/include/prism/compiler/inline.h +17 -0
  34. data/include/prism/compiler/nodiscard.h +22 -0
  35. data/include/prism/compiler/nonnull.h +18 -0
  36. data/include/prism/compiler/unused.h +18 -0
  37. data/include/prism/constant_pool.h +81 -0
  38. data/include/prism/diagnostic.h +38 -403
  39. data/include/prism/errors_format.h +53 -0
  40. data/include/prism/excludes.h +29 -0
  41. data/include/prism/integer.h +41 -0
  42. data/include/prism/internal/allocator.h +68 -0
  43. data/include/prism/internal/allocator_debug.h +88 -0
  44. data/include/prism/internal/arena.h +108 -0
  45. data/include/prism/internal/bit.h +42 -0
  46. data/include/prism/internal/buffer.h +91 -0
  47. data/include/prism/internal/char.h +145 -0
  48. data/include/prism/internal/comments.h +53 -0
  49. data/include/prism/internal/constant_pool.h +152 -0
  50. data/include/prism/internal/diagnostic.h +389 -0
  51. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  52. data/include/prism/internal/integer.h +68 -0
  53. data/include/prism/internal/isinf.h +16 -0
  54. data/include/prism/internal/line_offset_list.h +34 -0
  55. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  56. data/include/prism/internal/magic_comments.h +23 -0
  57. data/include/prism/internal/memchr.h +15 -0
  58. data/include/prism/internal/node.h +38 -0
  59. data/include/prism/internal/options.h +232 -0
  60. data/include/prism/internal/parser.h +984 -0
  61. data/include/prism/internal/regexp.h +26 -0
  62. data/include/prism/internal/serialize.h +39 -0
  63. data/include/prism/internal/source.h +72 -0
  64. data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
  65. data/include/prism/internal/stringy.h +35 -0
  66. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  67. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  68. data/include/prism/internal/tokens.h +11 -0
  69. data/include/prism/json.h +32 -0
  70. data/include/prism/line_offset_list.h +61 -0
  71. data/include/prism/magic_comments.h +35 -0
  72. data/include/prism/node.h +18 -53
  73. data/include/prism/options.h +146 -280
  74. data/include/prism/parser.h +336 -868
  75. data/include/prism/prettyprint.h +6 -9
  76. data/include/prism/serialize.h +115 -0
  77. data/include/prism/source.h +155 -0
  78. data/include/prism/stream.h +28 -0
  79. data/include/prism/string_query.h +63 -0
  80. data/include/prism/stringy.h +72 -0
  81. data/include/prism/version.h +12 -3
  82. data/include/prism.h +43 -320
  83. data/lib/prism/compiler.rb +319 -305
  84. data/lib/prism/desugar_compiler.rb +105 -34
  85. data/lib/prism/dispatcher.rb +646 -473
  86. data/lib/prism/dot_visitor.rb +592 -511
  87. data/lib/prism/dsl.rb +391 -25
  88. data/lib/prism/ffi.rb +252 -90
  89. data/lib/prism/inspect_visitor.rb +357 -325
  90. data/lib/prism/lex_compat.rb +254 -212
  91. data/lib/prism/mutation_compiler.rb +313 -305
  92. data/lib/prism/node.rb +20268 -7030
  93. data/lib/prism/node_ext.rb +180 -187
  94. data/lib/prism/node_find.rb +215 -0
  95. data/lib/prism/parse_result/errors.rb +7 -1
  96. data/lib/prism/parse_result/newlines.rb +276 -8
  97. data/lib/prism/parse_result.rb +453 -139
  98. data/lib/prism/pattern.rb +65 -20
  99. data/lib/prism/reflection.rb +23 -12
  100. data/lib/prism/relocation.rb +149 -87
  101. data/lib/prism/serialize.rb +3443 -535
  102. data/lib/prism/string_query.rb +16 -1
  103. data/lib/prism/translation/parser/builder.rb +10 -2
  104. data/lib/prism/translation/parser/compiler.rb +44 -52
  105. data/lib/prism/translation/parser/lexer.rb +41 -92
  106. data/lib/prism/translation/parser.rb +1 -1
  107. data/lib/prism/translation/parser_current.rb +1 -1
  108. data/lib/prism/translation/ripper/lexer.rb +8 -10
  109. data/lib/prism/translation/ripper/sexp.rb +2 -10
  110. data/lib/prism/translation/ripper/shim.rb +2 -0
  111. data/lib/prism/translation/ripper.rb +967 -209
  112. data/lib/prism/translation/ruby_parser.rb +73 -316
  113. data/lib/prism/translation.rb +2 -0
  114. data/lib/prism/visitor.rb +632 -5
  115. data/lib/prism.rb +79 -9
  116. data/prism.gemspec +122 -60
  117. data/rbi/generated/prism/compiler.rbi +489 -0
  118. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  119. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  120. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  121. data/rbi/generated/prism/dsl.rbi +751 -0
  122. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  123. data/rbi/generated/prism/lex_compat.rbi +162 -0
  124. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  125. data/rbi/generated/prism/node.rbi +13681 -0
  126. data/rbi/generated/prism/node_ext.rbi +259 -0
  127. data/rbi/generated/prism/node_find.rbi +79 -0
  128. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  129. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  130. data/rbi/generated/prism/parse_result.rbi +764 -0
  131. data/rbi/generated/prism/pattern.rbi +134 -0
  132. data/rbi/generated/prism/reflection.rbi +89 -0
  133. data/rbi/generated/prism/relocation.rbi +329 -0
  134. data/rbi/generated/prism/serialize.rbi +181 -0
  135. data/rbi/generated/prism/string_query.rbi +36 -0
  136. data/rbi/generated/prism/translation.rbi +7 -0
  137. data/rbi/generated/prism/visitor.rbi +650 -0
  138. data/rbi/generated/prism.rbi +86 -0
  139. data/rbi/prism/translation/parser_versions.rbi +5 -0
  140. data/rbi/rubyvm/node_find.rbi +14 -0
  141. data/sig/generated/prism/compiler.rbs +492 -0
  142. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  143. data/sig/generated/prism/dispatcher.rbs +1292 -0
  144. data/sig/generated/prism/dot_visitor.rbs +635 -0
  145. data/sig/generated/prism/dsl.rbs +928 -0
  146. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  147. data/sig/generated/prism/lex_compat.rbs +166 -0
  148. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  149. data/sig/generated/prism/node.rbs +20160 -0
  150. data/sig/generated/prism/node_ext.rbs +290 -0
  151. data/sig/generated/prism/node_find.rbs +90 -0
  152. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  153. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  154. data/sig/generated/prism/parse_result.rbs +859 -0
  155. data/sig/generated/prism/pattern.rbs +156 -0
  156. data/sig/generated/prism/reflection.rbs +101 -0
  157. data/sig/generated/prism/relocation.rbs +428 -0
  158. data/sig/generated/prism/serialize.rbs +195 -0
  159. data/sig/generated/prism/string_query.rbs +36 -0
  160. data/sig/generated/prism/translation.rbs +8 -0
  161. data/sig/generated/prism/visitor.rbs +1111 -0
  162. data/sig/generated/prism.rbs +84 -0
  163. data/sig/manifest.yaml +2 -0
  164. data/src/arena.c +117 -0
  165. data/src/{util/pm_buffer.c → buffer.c} +44 -21
  166. data/src/{util/pm_char.c → char.c} +28 -57
  167. data/src/comments.c +454 -0
  168. data/src/constant_pool.c +406 -0
  169. data/src/diagnostic.c +97 -65
  170. data/src/encoding.c +8 -3
  171. data/src/errors_format.c +519 -0
  172. data/src/{util/pm_integer.c → integer.c} +33 -22
  173. data/src/json.c +5733 -0
  174. data/src/line_offset_list.c +100 -0
  175. data/src/list.c +24 -0
  176. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  177. data/src/node.c +3504 -7674
  178. data/src/options.c +162 -49
  179. data/src/parser.c +349 -0
  180. data/src/prettyprint.c +421 -354
  181. data/src/prism.c +8030 -6970
  182. data/src/regexp.c +961 -45
  183. data/src/serialize.c +459 -424
  184. data/src/source.c +518 -0
  185. data/src/static_literals.c +111 -43
  186. data/src/string_query.c +166 -0
  187. data/src/stringy.c +91 -0
  188. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  189. data/src/strpbrk.c +447 -0
  190. data/src/{token_type.c → tokens.c} +39 -18
  191. metadata +122 -61
  192. data/ext/prism/api_pack.c +0 -276
  193. data/include/prism/defines.h +0 -260
  194. data/include/prism/pack.h +0 -163
  195. data/include/prism/regexp.h +0 -43
  196. data/include/prism/util/pm_buffer.h +0 -236
  197. data/include/prism/util/pm_char.h +0 -204
  198. data/include/prism/util/pm_constant_pool.h +0 -218
  199. data/include/prism/util/pm_integer.h +0 -130
  200. data/include/prism/util/pm_memchr.h +0 -29
  201. data/include/prism/util/pm_newline_list.h +0 -113
  202. data/include/prism/util/pm_string.h +0 -200
  203. data/lib/prism/pack.rb +0 -230
  204. data/lib/prism/parse_result/comments.rb +0 -188
  205. data/rbi/prism/compiler.rbi +0 -12
  206. data/rbi/prism/dsl.rbi +0 -524
  207. data/rbi/prism/inspect_visitor.rbi +0 -12
  208. data/rbi/prism/node.rbi +0 -8750
  209. data/rbi/prism/node_ext.rbi +0 -107
  210. data/rbi/prism/parse_result.rbi +0 -404
  211. data/rbi/prism/reflection.rbi +0 -58
  212. data/rbi/prism/string_query.rbi +0 -12
  213. data/rbi/prism/visitor.rbi +0 -473
  214. data/rbi/prism.rbi +0 -63
  215. data/sig/prism/compiler.rbs +0 -9
  216. data/sig/prism/dispatcher.rbs +0 -19
  217. data/sig/prism/dot_visitor.rbs +0 -6
  218. data/sig/prism/dsl.rbs +0 -351
  219. data/sig/prism/inspect_visitor.rbs +0 -22
  220. data/sig/prism/lex_compat.rbs +0 -10
  221. data/sig/prism/node.rbs +0 -4042
  222. data/sig/prism/node_ext.rbs +0 -149
  223. data/sig/prism/pack.rbs +0 -43
  224. data/sig/prism/parse_result/comments.rbs +0 -38
  225. data/sig/prism/parse_result.rbs +0 -197
  226. data/sig/prism/pattern.rbs +0 -13
  227. data/sig/prism/reflection.rbs +0 -50
  228. data/sig/prism/relocation.rbs +0 -185
  229. data/sig/prism/serialize.rbs +0 -8
  230. data/sig/prism/string_query.rbs +0 -11
  231. data/sig/prism/visitor.rbs +0 -169
  232. data/sig/prism.rbs +0 -272
  233. data/src/pack.c +0 -509
  234. data/src/util/pm_constant_pool.c +0 -342
  235. data/src/util/pm_list.c +0 -49
  236. data/src/util/pm_newline_list.c +0 -125
  237. data/src/util/pm_string.c +0 -381
  238. data/src/util/pm_strpbrk.c +0 -206
@@ -1,260 +0,0 @@
1
- /**
2
- * @file defines.h
3
- *
4
- * Macro definitions used throughout the prism library.
5
- *
6
- * This file should be included first by any *.h or *.c in prism for consistency
7
- * and to ensure that the macros are defined before they are used.
8
- */
9
- #ifndef PRISM_DEFINES_H
10
- #define PRISM_DEFINES_H
11
-
12
- #include <ctype.h>
13
- #include <limits.h>
14
- #include <math.h>
15
- #include <stdarg.h>
16
- #include <stddef.h>
17
- #include <stdint.h>
18
- #include <stdio.h>
19
- #include <string.h>
20
-
21
- /**
22
- * We want to be able to use the PRI* macros for printing out integers, but on
23
- * some platforms they aren't included unless this is already defined.
24
- */
25
- #define __STDC_FORMAT_MACROS
26
- // Include sys/types.h before inttypes.h to work around issue with
27
- // certain versions of GCC and newlib which causes omission of PRIx64
28
- #include <sys/types.h>
29
- #include <inttypes.h>
30
-
31
- /**
32
- * When we are parsing using recursive descent, we want to protect against
33
- * malicious payloads that could attempt to crash our parser. We do this by
34
- * specifying a maximum depth to which we are allowed to recurse.
35
- */
36
- #ifndef PRISM_DEPTH_MAXIMUM
37
- #define PRISM_DEPTH_MAXIMUM 10000
38
- #endif
39
-
40
- /**
41
- * By default, we compile with -fvisibility=hidden. When this is enabled, we
42
- * need to mark certain functions as being publically-visible. This macro does
43
- * that in a compiler-agnostic way.
44
- */
45
- #ifndef PRISM_EXPORTED_FUNCTION
46
- # ifdef PRISM_EXPORT_SYMBOLS
47
- # ifdef _WIN32
48
- # define PRISM_EXPORTED_FUNCTION __declspec(dllexport) extern
49
- # else
50
- # define PRISM_EXPORTED_FUNCTION __attribute__((__visibility__("default"))) extern
51
- # endif
52
- # else
53
- # define PRISM_EXPORTED_FUNCTION
54
- # endif
55
- #endif
56
-
57
- /**
58
- * Certain compilers support specifying that a function accepts variadic
59
- * parameters that look like printf format strings to provide a better developer
60
- * experience when someone is using the function. This macro does that in a
61
- * compiler-agnostic way.
62
- */
63
- #if defined(__GNUC__)
64
- # if defined(__MINGW_PRINTF_FORMAT)
65
- # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index)))
66
- # else
67
- # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index)))
68
- # endif
69
- #elif defined(__clang__)
70
- # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index)))
71
- #else
72
- # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index)
73
- #endif
74
-
75
- /**
76
- * GCC will warn if you specify a function or parameter that is unused at
77
- * runtime. This macro allows you to mark a function or parameter as unused in a
78
- * compiler-agnostic way.
79
- */
80
- #if defined(__GNUC__)
81
- # define PRISM_ATTRIBUTE_UNUSED __attribute__((unused))
82
- #else
83
- # define PRISM_ATTRIBUTE_UNUSED
84
- #endif
85
-
86
- /**
87
- * Old Visual Studio versions do not support the inline keyword, so we need to
88
- * define it to be __inline.
89
- */
90
- #if defined(_MSC_VER) && !defined(inline)
91
- # define inline __inline
92
- #endif
93
-
94
- /**
95
- * Old Visual Studio versions before 2015 do not implement sprintf, but instead
96
- * implement _snprintf. We standard that here.
97
- */
98
- #if !defined(snprintf) && defined(_MSC_VER) && (_MSC_VER < 1900)
99
- # define snprintf _snprintf
100
- #endif
101
-
102
- /**
103
- * A simple utility macro to concatenate two tokens together, necessary when one
104
- * of the tokens is itself a macro.
105
- */
106
- #define PM_CONCATENATE(left, right) left ## right
107
-
108
- /**
109
- * We want to be able to use static assertions, but they weren't standardized
110
- * until C11. As such, we polyfill it here by making a hacky typedef that will
111
- * fail to compile due to a negative array size if the condition is false.
112
- */
113
- #if defined(_Static_assert)
114
- # define PM_STATIC_ASSERT(line, condition, message) _Static_assert(condition, message)
115
- #else
116
- # define PM_STATIC_ASSERT(line, condition, message) typedef char PM_CONCATENATE(static_assert_, line)[(condition) ? 1 : -1]
117
- #endif
118
-
119
- /**
120
- * In general, libc for embedded systems does not support memory-mapped files.
121
- * If the target platform is POSIX or Windows, we can map a file in memory and
122
- * read it in a more efficient manner.
123
- */
124
- #ifdef _WIN32
125
- # define PRISM_HAS_MMAP
126
- #else
127
- # include <unistd.h>
128
- # ifdef _POSIX_MAPPED_FILES
129
- # define PRISM_HAS_MMAP
130
- # endif
131
- #endif
132
-
133
- /**
134
- * If PRISM_HAS_NO_FILESYSTEM is defined, then we want to exclude all filesystem
135
- * related code from the library. All filesystem related code should be guarded
136
- * by PRISM_HAS_FILESYSTEM.
137
- */
138
- #ifndef PRISM_HAS_NO_FILESYSTEM
139
- # define PRISM_HAS_FILESYSTEM
140
- #endif
141
-
142
- /**
143
- * isinf on POSIX systems it accepts a float, a double, or a long double.
144
- * But mingw didn't provide an isinf macro, only an isinf function that only
145
- * accepts floats, so we need to use _finite instead.
146
- */
147
- #ifdef __MINGW64__
148
- #include <float.h>
149
- #define PRISM_ISINF(x) (!_finite(x))
150
- #else
151
- #define PRISM_ISINF(x) isinf(x)
152
- #endif
153
-
154
- /**
155
- * If you build prism with a custom allocator, configure it with
156
- * "-D PRISM_XALLOCATOR" to use your own allocator that defines xmalloc,
157
- * xrealloc, xcalloc, and xfree.
158
- *
159
- * For example, your `prism_xallocator.h` file could look like this:
160
- *
161
- * ```
162
- * #ifndef PRISM_XALLOCATOR_H
163
- * #define PRISM_XALLOCATOR_H
164
- * #define xmalloc my_malloc
165
- * #define xrealloc my_realloc
166
- * #define xcalloc my_calloc
167
- * #define xfree my_free
168
- * #endif
169
- * ```
170
- */
171
- #ifdef PRISM_XALLOCATOR
172
- #include "prism_xallocator.h"
173
- #else
174
- #ifndef xmalloc
175
- /**
176
- * The malloc function that should be used. This can be overridden with
177
- * the PRISM_XALLOCATOR define.
178
- */
179
- #define xmalloc malloc
180
- #endif
181
-
182
- #ifndef xrealloc
183
- /**
184
- * The realloc function that should be used. This can be overridden with
185
- * the PRISM_XALLOCATOR define.
186
- */
187
- #define xrealloc realloc
188
- #endif
189
-
190
- #ifndef xcalloc
191
- /**
192
- * The calloc function that should be used. This can be overridden with
193
- * the PRISM_XALLOCATOR define.
194
- */
195
- #define xcalloc calloc
196
- #endif
197
-
198
- #ifndef xfree
199
- /**
200
- * The free function that should be used. This can be overridden with the
201
- * PRISM_XALLOCATOR define.
202
- */
203
- #define xfree free
204
- #endif
205
- #endif
206
-
207
- /**
208
- * If PRISM_BUILD_MINIMAL is defined, then we're going to define every possible
209
- * switch that will turn off certain features of prism.
210
- */
211
- #ifdef PRISM_BUILD_MINIMAL
212
- /** Exclude the serialization API. */
213
- #define PRISM_EXCLUDE_SERIALIZATION
214
-
215
- /** Exclude the JSON serialization API. */
216
- #define PRISM_EXCLUDE_JSON
217
-
218
- /** Exclude the Array#pack parser API. */
219
- #define PRISM_EXCLUDE_PACK
220
-
221
- /** Exclude the prettyprint API. */
222
- #define PRISM_EXCLUDE_PRETTYPRINT
223
-
224
- /** Exclude the full set of encodings, using the minimal only. */
225
- #define PRISM_ENCODING_EXCLUDE_FULL
226
- #endif
227
-
228
- /**
229
- * Support PRISM_LIKELY and PRISM_UNLIKELY to help the compiler optimize its
230
- * branch predication.
231
- */
232
- #if defined(__GNUC__) || defined(__clang__)
233
- /** The compiler should predicate that this branch will be taken. */
234
- #define PRISM_LIKELY(x) __builtin_expect(!!(x), 1)
235
-
236
- /** The compiler should predicate that this branch will not be taken. */
237
- #define PRISM_UNLIKELY(x) __builtin_expect(!!(x), 0)
238
- #else
239
- /** Void because this platform does not support branch prediction hints. */
240
- #define PRISM_LIKELY(x) (x)
241
-
242
- /** Void because this platform does not support branch prediction hints. */
243
- #define PRISM_UNLIKELY(x) (x)
244
- #endif
245
-
246
- /**
247
- * We use -Wimplicit-fallthrough to guard potentially unintended fall-through between cases of a switch.
248
- * Use PRISM_FALLTHROUGH to explicitly annotate cases where the fallthrough is intentional.
249
- */
250
- #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L // C23 or later
251
- #define PRISM_FALLTHROUGH [[fallthrough]];
252
- #elif defined(__GNUC__) || defined(__clang__)
253
- #define PRISM_FALLTHROUGH __attribute__((fallthrough));
254
- #elif defined(_MSC_VER)
255
- #define PRISM_FALLTHROUGH __fallthrough;
256
- #else
257
- #define PRISM_FALLTHROUGH
258
- #endif
259
-
260
- #endif
data/include/prism/pack.h DELETED
@@ -1,163 +0,0 @@
1
- /**
2
- * @file pack.h
3
- *
4
- * A pack template string parser.
5
- */
6
- #ifndef PRISM_PACK_H
7
- #define PRISM_PACK_H
8
-
9
- #include "prism/defines.h"
10
-
11
- // We optionally support parsing String#pack templates. For systems that don't
12
- // want or need this functionality, it can be turned off with the
13
- // PRISM_EXCLUDE_PACK define.
14
- #ifdef PRISM_EXCLUDE_PACK
15
-
16
- void pm_pack_parse(void);
17
-
18
- #else
19
-
20
- #include <stdint.h>
21
- #include <stdlib.h>
22
-
23
- /** The version of the pack template language that we are parsing. */
24
- typedef enum pm_pack_version {
25
- PM_PACK_VERSION_3_2_0
26
- } pm_pack_version;
27
-
28
- /** The type of pack template we are parsing. */
29
- typedef enum pm_pack_variant {
30
- PM_PACK_VARIANT_PACK,
31
- PM_PACK_VARIANT_UNPACK
32
- } pm_pack_variant;
33
-
34
- /** A directive within the pack template. */
35
- typedef enum pm_pack_type {
36
- PM_PACK_SPACE,
37
- PM_PACK_COMMENT,
38
- PM_PACK_INTEGER,
39
- PM_PACK_UTF8,
40
- PM_PACK_BER,
41
- PM_PACK_FLOAT,
42
- PM_PACK_STRING_SPACE_PADDED,
43
- PM_PACK_STRING_NULL_PADDED,
44
- PM_PACK_STRING_NULL_TERMINATED,
45
- PM_PACK_STRING_MSB,
46
- PM_PACK_STRING_LSB,
47
- PM_PACK_STRING_HEX_HIGH,
48
- PM_PACK_STRING_HEX_LOW,
49
- PM_PACK_STRING_UU,
50
- PM_PACK_STRING_MIME,
51
- PM_PACK_STRING_BASE64,
52
- PM_PACK_STRING_FIXED,
53
- PM_PACK_STRING_POINTER,
54
- PM_PACK_MOVE,
55
- PM_PACK_BACK,
56
- PM_PACK_NULL,
57
- PM_PACK_END
58
- } pm_pack_type;
59
-
60
- /** The signness of a pack directive. */
61
- typedef enum pm_pack_signed {
62
- PM_PACK_UNSIGNED,
63
- PM_PACK_SIGNED,
64
- PM_PACK_SIGNED_NA
65
- } pm_pack_signed;
66
-
67
- /** The endianness of a pack directive. */
68
- typedef enum pm_pack_endian {
69
- PM_PACK_AGNOSTIC_ENDIAN,
70
- PM_PACK_LITTLE_ENDIAN, // aka 'VAX', or 'V'
71
- PM_PACK_BIG_ENDIAN, // aka 'network', or 'N'
72
- PM_PACK_NATIVE_ENDIAN,
73
- PM_PACK_ENDIAN_NA
74
- } pm_pack_endian;
75
-
76
- /** The size of an integer pack directive. */
77
- typedef enum pm_pack_size {
78
- PM_PACK_SIZE_SHORT,
79
- PM_PACK_SIZE_INT,
80
- PM_PACK_SIZE_LONG,
81
- PM_PACK_SIZE_LONG_LONG,
82
- PM_PACK_SIZE_8,
83
- PM_PACK_SIZE_16,
84
- PM_PACK_SIZE_32,
85
- PM_PACK_SIZE_64,
86
- PM_PACK_SIZE_P,
87
- PM_PACK_SIZE_NA
88
- } pm_pack_size;
89
-
90
- /** The type of length of a pack directive. */
91
- typedef enum pm_pack_length_type {
92
- PM_PACK_LENGTH_FIXED,
93
- PM_PACK_LENGTH_MAX,
94
- PM_PACK_LENGTH_RELATIVE, // special case for unpack @*
95
- PM_PACK_LENGTH_NA
96
- } pm_pack_length_type;
97
-
98
- /** The type of encoding for a pack template string. */
99
- typedef enum pm_pack_encoding {
100
- PM_PACK_ENCODING_START,
101
- PM_PACK_ENCODING_ASCII_8BIT,
102
- PM_PACK_ENCODING_US_ASCII,
103
- PM_PACK_ENCODING_UTF_8
104
- } pm_pack_encoding;
105
-
106
- /** The result of parsing a pack template. */
107
- typedef enum pm_pack_result {
108
- PM_PACK_OK,
109
- PM_PACK_ERROR_UNSUPPORTED_DIRECTIVE,
110
- PM_PACK_ERROR_UNKNOWN_DIRECTIVE,
111
- PM_PACK_ERROR_LENGTH_TOO_BIG,
112
- PM_PACK_ERROR_BANG_NOT_ALLOWED,
113
- PM_PACK_ERROR_DOUBLE_ENDIAN
114
- } pm_pack_result;
115
-
116
- /**
117
- * Parse a single directive from a pack or unpack format string.
118
- *
119
- * @param variant (in) pack or unpack
120
- * @param format (in, out) the start of the next directive to parse on calling,
121
- * and advanced beyond the parsed directive on return, or as much of it as
122
- * was consumed until an error was encountered
123
- * @param format_end (in) the end of the format string
124
- * @param type (out) the type of the directive
125
- * @param signed_type (out) whether the value is signed
126
- * @param endian (out) the endianness of the value
127
- * @param size (out) the size of the value
128
- * @param length_type (out) what kind of length is specified
129
- * @param length (out) the length of the directive
130
- * @param encoding (in, out) takes the current encoding of the string which
131
- * would result from parsing the whole format string, and returns a possibly
132
- * changed directive - the encoding should be `PM_PACK_ENCODING_START` when
133
- * pm_pack_parse is called for the first directive in a format string
134
- *
135
- * @return `PM_PACK_OK` on success or `PM_PACK_ERROR_*` on error
136
- * @note Consult Ruby documentation for the meaning of directives.
137
- */
138
- PRISM_EXPORTED_FUNCTION pm_pack_result
139
- pm_pack_parse(
140
- pm_pack_variant variant,
141
- const char **format,
142
- const char *format_end,
143
- pm_pack_type *type,
144
- pm_pack_signed *signed_type,
145
- pm_pack_endian *endian,
146
- pm_pack_size *size,
147
- pm_pack_length_type *length_type,
148
- uint64_t *length,
149
- pm_pack_encoding *encoding
150
- );
151
-
152
- /**
153
- * Prism abstracts sizes away from the native system - this converts an abstract
154
- * size to a native size.
155
- *
156
- * @param size The abstract size to convert.
157
- * @return The native size.
158
- */
159
- PRISM_EXPORTED_FUNCTION size_t pm_size_to_native(pm_pack_size size);
160
-
161
- #endif
162
-
163
- #endif
@@ -1,43 +0,0 @@
1
- /**
2
- * @file regexp.h
3
- *
4
- * A regular expression parser.
5
- */
6
- #ifndef PRISM_REGEXP_H
7
- #define PRISM_REGEXP_H
8
-
9
- #include "prism/defines.h"
10
- #include "prism/parser.h"
11
- #include "prism/encoding.h"
12
- #include "prism/util/pm_memchr.h"
13
- #include "prism/util/pm_string.h"
14
-
15
- #include <stdbool.h>
16
- #include <stddef.h>
17
- #include <string.h>
18
-
19
- /**
20
- * This callback is called by pm_regexp_parse() when a named capture group is found.
21
- */
22
- typedef void (*pm_regexp_name_callback_t)(const pm_string_t *name, void *data);
23
-
24
- /**
25
- * This callback is called by pm_regexp_parse() when a parse error is found.
26
- */
27
- typedef void (*pm_regexp_error_callback_t)(const uint8_t *start, const uint8_t *end, const char *message, void *data);
28
-
29
- /**
30
- * Parse a regular expression.
31
- *
32
- * @param parser The parser that is currently being used.
33
- * @param source The source code to parse.
34
- * @param size The size of the source code.
35
- * @param extended_mode Whether to parse the regular expression in extended mode.
36
- * @param name_callback The optional callback to call when a named capture group is found.
37
- * @param name_data The optional data to pass to the name callback.
38
- * @param error_callback The callback to call when a parse error is found.
39
- * @param error_data The data to pass to the error callback.
40
- */
41
- PRISM_EXPORTED_FUNCTION void pm_regexp_parse(pm_parser_t *parser, const uint8_t *source, size_t size, bool extended_mode, pm_regexp_name_callback_t name_callback, void *name_data, pm_regexp_error_callback_t error_callback, void *error_data);
42
-
43
- #endif
@@ -1,236 +0,0 @@
1
- /**
2
- * @file pm_buffer.h
3
- *
4
- * A wrapper around a contiguous block of allocated memory.
5
- */
6
- #ifndef PRISM_BUFFER_H
7
- #define PRISM_BUFFER_H
8
-
9
- #include "prism/defines.h"
10
- #include "prism/util/pm_char.h"
11
-
12
- #include <assert.h>
13
- #include <stdbool.h>
14
- #include <stdint.h>
15
- #include <stdlib.h>
16
- #include <string.h>
17
-
18
- /**
19
- * A pm_buffer_t is a simple memory buffer that stores data in a contiguous
20
- * block of memory.
21
- */
22
- typedef struct {
23
- /** The length of the buffer in bytes. */
24
- size_t length;
25
-
26
- /** The capacity of the buffer in bytes that has been allocated. */
27
- size_t capacity;
28
-
29
- /** A pointer to the start of the buffer. */
30
- char *value;
31
- } pm_buffer_t;
32
-
33
- /**
34
- * Return the size of the pm_buffer_t struct.
35
- *
36
- * @returns The size of the pm_buffer_t struct.
37
- */
38
- PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof(void);
39
-
40
- /**
41
- * Initialize a pm_buffer_t with the given capacity.
42
- *
43
- * @param buffer The buffer to initialize.
44
- * @param capacity The capacity of the buffer.
45
- * @returns True if the buffer was initialized successfully, false otherwise.
46
- */
47
- bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity);
48
-
49
- /**
50
- * Initialize a pm_buffer_t with its default values.
51
- *
52
- * @param buffer The buffer to initialize.
53
- * @returns True if the buffer was initialized successfully, false otherwise.
54
- *
55
- * \public \memberof pm_buffer_t
56
- */
57
- PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer);
58
-
59
- /**
60
- * Return the value of the buffer.
61
- *
62
- * @param buffer The buffer to get the value of.
63
- * @returns The value of the buffer.
64
- *
65
- * \public \memberof pm_buffer_t
66
- */
67
- PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer);
68
-
69
- /**
70
- * Return the length of the buffer.
71
- *
72
- * @param buffer The buffer to get the length of.
73
- * @returns The length of the buffer.
74
- *
75
- * \public \memberof pm_buffer_t
76
- */
77
- PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer);
78
-
79
- /**
80
- * Append the given amount of space as zeroes to the buffer.
81
- *
82
- * @param buffer The buffer to append to.
83
- * @param length The amount of space to append and zero.
84
- */
85
- void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
86
-
87
- /**
88
- * Append a formatted string to the buffer.
89
- *
90
- * @param buffer The buffer to append to.
91
- * @param format The format string to append.
92
- * @param ... The arguments to the format string.
93
- */
94
- void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
95
-
96
- /**
97
- * Append a string to the buffer.
98
- *
99
- * @param buffer The buffer to append to.
100
- * @param value The string to append.
101
- * @param length The length of the string to append.
102
- */
103
- void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
104
-
105
- /**
106
- * Append a list of bytes to the buffer.
107
- *
108
- * @param buffer The buffer to append to.
109
- * @param value The bytes to append.
110
- * @param length The length of the bytes to append.
111
- */
112
- void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
113
-
114
- /**
115
- * Append a single byte to the buffer.
116
- *
117
- * @param buffer The buffer to append to.
118
- * @param value The byte to append.
119
- */
120
- void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
121
-
122
- /**
123
- * Append a 32-bit unsigned integer to the buffer as a variable-length integer.
124
- *
125
- * @param buffer The buffer to append to.
126
- * @param value The integer to append.
127
- */
128
- void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
129
-
130
- /**
131
- * Append a 32-bit signed integer to the buffer as a variable-length integer.
132
- *
133
- * @param buffer The buffer to append to.
134
- * @param value The integer to append.
135
- */
136
- void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
137
-
138
- /**
139
- * Append a double to the buffer.
140
- *
141
- * @param buffer The buffer to append to.
142
- * @param value The double to append.
143
- */
144
- void pm_buffer_append_double(pm_buffer_t *buffer, double value);
145
-
146
- /**
147
- * Append a unicode codepoint to the buffer.
148
- *
149
- * @param buffer The buffer to append to.
150
- * @param value The character to append.
151
- * @returns True if the codepoint was valid and appended successfully, false
152
- * otherwise.
153
- */
154
- bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
155
-
156
- /**
157
- * The different types of escaping that can be performed by the buffer when
158
- * appending a slice of Ruby source code.
159
- */
160
- typedef enum {
161
- PM_BUFFER_ESCAPING_RUBY,
162
- PM_BUFFER_ESCAPING_JSON
163
- } pm_buffer_escaping_t;
164
-
165
- /**
166
- * Append a slice of source code to the buffer.
167
- *
168
- * @param buffer The buffer to append to.
169
- * @param source The source code to append.
170
- * @param length The length of the source code to append.
171
- * @param escaping The type of escaping to perform.
172
- */
173
- void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
174
-
175
- /**
176
- * Prepend the given string to the buffer.
177
- *
178
- * @param buffer The buffer to prepend to.
179
- * @param value The string to prepend.
180
- * @param length The length of the string to prepend.
181
- */
182
- void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
183
-
184
- /**
185
- * Concatenate one buffer onto another.
186
- *
187
- * @param destination The buffer to concatenate onto.
188
- * @param source The buffer to concatenate.
189
- */
190
- void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
191
-
192
- /**
193
- * Clear the buffer by reducing its size to 0. This does not free the allocated
194
- * memory, but it does allow the buffer to be reused.
195
- *
196
- * @param buffer The buffer to clear.
197
- */
198
- void pm_buffer_clear(pm_buffer_t *buffer);
199
-
200
- /**
201
- * Strip the whitespace from the end of the buffer.
202
- *
203
- * @param buffer The buffer to strip.
204
- */
205
- void pm_buffer_rstrip(pm_buffer_t *buffer);
206
-
207
- /**
208
- * Checks if the buffer includes the given value.
209
- *
210
- * @param buffer The buffer to check.
211
- * @param value The value to check for.
212
- * @returns The index of the first occurrence of the value in the buffer, or
213
- * SIZE_MAX if the value is not found.
214
- */
215
- size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
216
-
217
- /**
218
- * Insert the given string into the buffer at the given index.
219
- *
220
- * @param buffer The buffer to insert into.
221
- * @param index The index to insert at.
222
- * @param value The string to insert.
223
- * @param length The length of the string to insert.
224
- */
225
- void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
226
-
227
- /**
228
- * Free the memory associated with the buffer.
229
- *
230
- * @param buffer The buffer to free.
231
- *
232
- * \public \memberof pm_buffer_t
233
- */
234
- PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer);
235
-
236
- #endif