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
data/ext/prism/api_pack.c DELETED
@@ -1,276 +0,0 @@
1
- #include "prism/extension.h"
2
-
3
- #ifdef PRISM_EXCLUDE_PACK
4
-
5
- void
6
- Init_prism_pack(void) {}
7
-
8
- #else
9
-
10
- static VALUE rb_cPrism;
11
- static VALUE rb_cPrismPack;
12
- static VALUE rb_cPrismPackDirective;
13
- static VALUE rb_cPrismPackFormat;
14
-
15
- static VALUE v3_2_0_symbol;
16
- static VALUE pack_symbol;
17
- static VALUE unpack_symbol;
18
-
19
- #if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
20
- # define UINT64T2NUM(x) ULL2NUM(x)
21
- # define NUM2UINT64T(x) (uint64_t)NUM2ULL(x)
22
- #elif SIZEOF_UINT64_T == SIZEOF_LONG
23
- # define UINT64T2NUM(x) ULONG2NUM(x)
24
- # define NUM2UINT64T(x) (uint64_t)NUM2ULONG(x)
25
- #else
26
- // error No uint64_t conversion
27
- #endif
28
-
29
- static VALUE
30
- pack_type_to_symbol(pm_pack_type type) {
31
- switch (type) {
32
- case PM_PACK_SPACE:
33
- return ID2SYM(rb_intern("SPACE"));
34
- case PM_PACK_COMMENT:
35
- return ID2SYM(rb_intern("COMMENT"));
36
- case PM_PACK_INTEGER:
37
- return ID2SYM(rb_intern("INTEGER"));
38
- case PM_PACK_UTF8:
39
- return ID2SYM(rb_intern("UTF8"));
40
- case PM_PACK_BER:
41
- return ID2SYM(rb_intern("BER"));
42
- case PM_PACK_FLOAT:
43
- return ID2SYM(rb_intern("FLOAT"));
44
- case PM_PACK_STRING_SPACE_PADDED:
45
- return ID2SYM(rb_intern("STRING_SPACE_PADDED"));
46
- case PM_PACK_STRING_NULL_PADDED:
47
- return ID2SYM(rb_intern("STRING_NULL_PADDED"));
48
- case PM_PACK_STRING_NULL_TERMINATED:
49
- return ID2SYM(rb_intern("STRING_NULL_TERMINATED"));
50
- case PM_PACK_STRING_MSB:
51
- return ID2SYM(rb_intern("STRING_MSB"));
52
- case PM_PACK_STRING_LSB:
53
- return ID2SYM(rb_intern("STRING_LSB"));
54
- case PM_PACK_STRING_HEX_HIGH:
55
- return ID2SYM(rb_intern("STRING_HEX_HIGH"));
56
- case PM_PACK_STRING_HEX_LOW:
57
- return ID2SYM(rb_intern("STRING_HEX_LOW"));
58
- case PM_PACK_STRING_UU:
59
- return ID2SYM(rb_intern("STRING_UU"));
60
- case PM_PACK_STRING_MIME:
61
- return ID2SYM(rb_intern("STRING_MIME"));
62
- case PM_PACK_STRING_BASE64:
63
- return ID2SYM(rb_intern("STRING_BASE64"));
64
- case PM_PACK_STRING_FIXED:
65
- return ID2SYM(rb_intern("STRING_FIXED"));
66
- case PM_PACK_STRING_POINTER:
67
- return ID2SYM(rb_intern("STRING_POINTER"));
68
- case PM_PACK_MOVE:
69
- return ID2SYM(rb_intern("MOVE"));
70
- case PM_PACK_BACK:
71
- return ID2SYM(rb_intern("BACK"));
72
- case PM_PACK_NULL:
73
- return ID2SYM(rb_intern("NULL"));
74
- default:
75
- return Qnil;
76
- }
77
- }
78
-
79
- static VALUE
80
- pack_signed_to_symbol(pm_pack_signed signed_type) {
81
- switch (signed_type) {
82
- case PM_PACK_UNSIGNED:
83
- return ID2SYM(rb_intern("UNSIGNED"));
84
- case PM_PACK_SIGNED:
85
- return ID2SYM(rb_intern("SIGNED"));
86
- case PM_PACK_SIGNED_NA:
87
- return ID2SYM(rb_intern("SIGNED_NA"));
88
- default:
89
- return Qnil;
90
- }
91
- }
92
-
93
- static VALUE
94
- pack_endian_to_symbol(pm_pack_endian endian) {
95
- switch (endian) {
96
- case PM_PACK_AGNOSTIC_ENDIAN:
97
- return ID2SYM(rb_intern("AGNOSTIC_ENDIAN"));
98
- case PM_PACK_LITTLE_ENDIAN:
99
- return ID2SYM(rb_intern("LITTLE_ENDIAN"));
100
- case PM_PACK_BIG_ENDIAN:
101
- return ID2SYM(rb_intern("BIG_ENDIAN"));
102
- case PM_PACK_NATIVE_ENDIAN:
103
- return ID2SYM(rb_intern("NATIVE_ENDIAN"));
104
- case PM_PACK_ENDIAN_NA:
105
- return ID2SYM(rb_intern("ENDIAN_NA"));
106
- default:
107
- return Qnil;
108
- }
109
- }
110
-
111
- static VALUE
112
- pack_size_to_symbol(pm_pack_size size) {
113
- switch (size) {
114
- case PM_PACK_SIZE_SHORT:
115
- return ID2SYM(rb_intern("SIZE_SHORT"));
116
- case PM_PACK_SIZE_INT:
117
- return ID2SYM(rb_intern("SIZE_INT"));
118
- case PM_PACK_SIZE_LONG:
119
- return ID2SYM(rb_intern("SIZE_LONG"));
120
- case PM_PACK_SIZE_LONG_LONG:
121
- return ID2SYM(rb_intern("SIZE_LONG_LONG"));
122
- case PM_PACK_SIZE_8:
123
- return ID2SYM(rb_intern("SIZE_8"));
124
- case PM_PACK_SIZE_16:
125
- return ID2SYM(rb_intern("SIZE_16"));
126
- case PM_PACK_SIZE_32:
127
- return ID2SYM(rb_intern("SIZE_32"));
128
- case PM_PACK_SIZE_64:
129
- return ID2SYM(rb_intern("SIZE_64"));
130
- case PM_PACK_SIZE_P:
131
- return ID2SYM(rb_intern("SIZE_P"));
132
- case PM_PACK_SIZE_NA:
133
- return ID2SYM(rb_intern("SIZE_NA"));
134
- default:
135
- return Qnil;
136
- }
137
- }
138
-
139
- static VALUE
140
- pack_length_type_to_symbol(pm_pack_length_type length_type) {
141
- switch (length_type) {
142
- case PM_PACK_LENGTH_FIXED:
143
- return ID2SYM(rb_intern("LENGTH_FIXED"));
144
- case PM_PACK_LENGTH_MAX:
145
- return ID2SYM(rb_intern("LENGTH_MAX"));
146
- case PM_PACK_LENGTH_RELATIVE:
147
- return ID2SYM(rb_intern("LENGTH_RELATIVE"));
148
- case PM_PACK_LENGTH_NA:
149
- return ID2SYM(rb_intern("LENGTH_NA"));
150
- default:
151
- return Qnil;
152
- }
153
- }
154
-
155
- static VALUE
156
- pack_encoding_to_ruby(pm_pack_encoding encoding) {
157
- int index;
158
- switch (encoding) {
159
- case PM_PACK_ENCODING_ASCII_8BIT:
160
- index = rb_ascii8bit_encindex();
161
- break;
162
- case PM_PACK_ENCODING_US_ASCII:
163
- index = rb_usascii_encindex();
164
- break;
165
- case PM_PACK_ENCODING_UTF_8:
166
- index = rb_utf8_encindex();
167
- break;
168
- default:
169
- return Qnil;
170
- }
171
- return rb_enc_from_encoding(rb_enc_from_index(index));
172
- }
173
-
174
- /**
175
- * call-seq:
176
- * Pack::parse(version, variant, source) -> Format
177
- *
178
- * Parse the given source and return a format object.
179
- */
180
- static VALUE
181
- pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_string) {
182
- if (version_symbol != v3_2_0_symbol) {
183
- rb_raise(rb_eArgError, "invalid version");
184
- }
185
-
186
- pm_pack_variant variant;
187
- if (variant_symbol == pack_symbol) {
188
- variant = PM_PACK_VARIANT_PACK;
189
- } else if (variant_symbol == unpack_symbol) {
190
- variant = PM_PACK_VARIANT_UNPACK;
191
- } else {
192
- rb_raise(rb_eArgError, "invalid variant");
193
- }
194
-
195
- StringValue(format_string);
196
-
197
- const char *format = RSTRING_PTR(format_string);
198
- const char *format_end = format + RSTRING_LEN(format_string);
199
- pm_pack_encoding encoding = PM_PACK_ENCODING_START;
200
-
201
- VALUE directives_array = rb_ary_new();
202
-
203
- while (format < format_end) {
204
- pm_pack_type type;
205
- pm_pack_signed signed_type;
206
- pm_pack_endian endian;
207
- pm_pack_size size;
208
- pm_pack_length_type length_type;
209
- uint64_t length;
210
-
211
- const char *directive_start = format;
212
-
213
- pm_pack_result parse_result = pm_pack_parse(variant, &format, format_end, &type, &signed_type, &endian,
214
- &size, &length_type, &length, &encoding);
215
-
216
- const char *directive_end = format;
217
-
218
- switch (parse_result) {
219
- case PM_PACK_OK:
220
- break;
221
- case PM_PACK_ERROR_UNSUPPORTED_DIRECTIVE:
222
- rb_raise(rb_eArgError, "unsupported directive");
223
- case PM_PACK_ERROR_UNKNOWN_DIRECTIVE:
224
- rb_raise(rb_eArgError, "unsupported directive");
225
- case PM_PACK_ERROR_LENGTH_TOO_BIG:
226
- rb_raise(rb_eRangeError, "pack length too big");
227
- case PM_PACK_ERROR_BANG_NOT_ALLOWED:
228
- rb_raise(rb_eRangeError, "bang not allowed");
229
- case PM_PACK_ERROR_DOUBLE_ENDIAN:
230
- rb_raise(rb_eRangeError, "double endian");
231
- default:
232
- rb_bug("parse result");
233
- }
234
-
235
- if (type == PM_PACK_END) {
236
- break;
237
- }
238
-
239
- VALUE directive_args[9] = {
240
- version_symbol,
241
- variant_symbol,
242
- rb_usascii_str_new(directive_start, directive_end - directive_start),
243
- pack_type_to_symbol(type),
244
- pack_signed_to_symbol(signed_type),
245
- pack_endian_to_symbol(endian),
246
- pack_size_to_symbol(size),
247
- pack_length_type_to_symbol(length_type),
248
- UINT64T2NUM(length)
249
- };
250
-
251
- rb_ary_push(directives_array, rb_class_new_instance(9, directive_args, rb_cPrismPackDirective));
252
- }
253
-
254
- VALUE format_args[2];
255
- format_args[0] = directives_array;
256
- format_args[1] = pack_encoding_to_ruby(encoding);
257
- return rb_class_new_instance(2, format_args, rb_cPrismPackFormat);
258
- }
259
-
260
- /**
261
- * The function that gets called when Ruby initializes the prism extension.
262
- */
263
- void
264
- Init_prism_pack(void) {
265
- rb_cPrism = rb_define_module("Prism");
266
- rb_cPrismPack = rb_define_module_under(rb_cPrism, "Pack");
267
- rb_cPrismPackDirective = rb_define_class_under(rb_cPrismPack, "Directive", rb_cObject);
268
- rb_cPrismPackFormat = rb_define_class_under(rb_cPrismPack, "Format", rb_cObject);
269
- rb_define_singleton_method(rb_cPrismPack, "parse", pack_parse, 3);
270
-
271
- v3_2_0_symbol = ID2SYM(rb_intern("v3_2_0"));
272
- pack_symbol = ID2SYM(rb_intern("pack"));
273
- unpack_symbol = ID2SYM(rb_intern("unpack"));
274
- }
275
-
276
- #endif
@@ -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