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
@@ -1,130 +0,0 @@
1
- /**
2
- * @file pm_integer.h
3
- *
4
- * This module provides functions for working with arbitrary-sized integers.
5
- */
6
- #ifndef PRISM_NUMBER_H
7
- #define PRISM_NUMBER_H
8
-
9
- #include "prism/defines.h"
10
- #include "prism/util/pm_buffer.h"
11
-
12
- #include <assert.h>
13
- #include <stdbool.h>
14
- #include <stdint.h>
15
- #include <stdlib.h>
16
-
17
- /**
18
- * A structure represents an arbitrary-sized integer.
19
- */
20
- typedef struct {
21
- /**
22
- * The number of allocated values. length is set to 0 if the integer fits
23
- * into uint32_t.
24
- */
25
- size_t length;
26
-
27
- /**
28
- * List of 32-bit integers. Set to NULL if the integer fits into uint32_t.
29
- */
30
- uint32_t *values;
31
-
32
- /**
33
- * Embedded value for small integer. This value is set to 0 if the value
34
- * does not fit into uint32_t.
35
- */
36
- uint32_t value;
37
-
38
- /**
39
- * Whether or not the integer is negative. It is stored this way so that a
40
- * zeroed pm_integer_t is always positive zero.
41
- */
42
- bool negative;
43
- } pm_integer_t;
44
-
45
- /**
46
- * An enum controlling the base of an integer. It is expected that the base is
47
- * already known before parsing the integer, even though it could be derived
48
- * from the string itself.
49
- */
50
- typedef enum {
51
- /** The default decimal base, with no prefix. Leading 0s will be ignored. */
52
- PM_INTEGER_BASE_DEFAULT,
53
-
54
- /** The binary base, indicated by a 0b or 0B prefix. */
55
- PM_INTEGER_BASE_BINARY,
56
-
57
- /** The octal base, indicated by a 0, 0o, or 0O prefix. */
58
- PM_INTEGER_BASE_OCTAL,
59
-
60
- /** The decimal base, indicated by a 0d, 0D, or empty prefix. */
61
- PM_INTEGER_BASE_DECIMAL,
62
-
63
- /** The hexadecimal base, indicated by a 0x or 0X prefix. */
64
- PM_INTEGER_BASE_HEXADECIMAL,
65
-
66
- /**
67
- * An unknown base, in which case pm_integer_parse will derive it based on
68
- * the content of the string. This is less efficient and does more
69
- * comparisons, so if callers know the base ahead of time, they should use
70
- * that instead.
71
- */
72
- PM_INTEGER_BASE_UNKNOWN
73
- } pm_integer_base_t;
74
-
75
- /**
76
- * Parse an integer from a string. This assumes that the format of the integer
77
- * has already been validated, as internal validation checks are not performed
78
- * here.
79
- *
80
- * @param integer The integer to parse into.
81
- * @param base The base of the integer.
82
- * @param start The start of the string.
83
- * @param end The end of the string.
84
- */
85
- void pm_integer_parse(pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end);
86
-
87
- /**
88
- * Compare two integers. This function returns -1 if the left integer is less
89
- * than the right integer, 0 if they are equal, and 1 if the left integer is
90
- * greater than the right integer.
91
- *
92
- * @param left The left integer to compare.
93
- * @param right The right integer to compare.
94
- * @return The result of the comparison.
95
- */
96
- int pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right);
97
-
98
- /**
99
- * Reduce a ratio of integers to its simplest form.
100
- *
101
- * If either the numerator or denominator do not fit into a 32-bit integer, then
102
- * this function is a no-op. In the future, we may consider reducing even the
103
- * larger numbers, but for now we're going to keep it simple.
104
- *
105
- * @param numerator The numerator of the ratio.
106
- * @param denominator The denominator of the ratio.
107
- */
108
- void pm_integers_reduce(pm_integer_t *numerator, pm_integer_t *denominator);
109
-
110
- /**
111
- * Convert an integer to a decimal string.
112
- *
113
- * @param buffer The buffer to append the string to.
114
- * @param integer The integer to convert to a string.
115
- *
116
- * \public \memberof pm_integer_t
117
- */
118
- PRISM_EXPORTED_FUNCTION void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer);
119
-
120
- /**
121
- * Free the internal memory of an integer. This memory will only be allocated if
122
- * the integer exceeds the size of a single node in the linked list.
123
- *
124
- * @param integer The integer to free.
125
- *
126
- * \public \memberof pm_integer_t
127
- */
128
- PRISM_EXPORTED_FUNCTION void pm_integer_free(pm_integer_t *integer);
129
-
130
- #endif
@@ -1,29 +0,0 @@
1
- /**
2
- * @file pm_memchr.h
3
- *
4
- * A custom memchr implementation.
5
- */
6
- #ifndef PRISM_MEMCHR_H
7
- #define PRISM_MEMCHR_H
8
-
9
- #include "prism/defines.h"
10
- #include "prism/encoding.h"
11
-
12
- #include <stddef.h>
13
-
14
- /**
15
- * We need to roll our own memchr to handle cases where the encoding changes and
16
- * we need to search for a character in a buffer that could be the trailing byte
17
- * of a multibyte character.
18
- *
19
- * @param source The source string.
20
- * @param character The character to search for.
21
- * @param number The maximum number of bytes to search.
22
- * @param encoding_changed Whether the encoding changed.
23
- * @param encoding A pointer to the encoding.
24
- * @return A pointer to the first occurrence of the character in the source
25
- * string, or NULL if no such character exists.
26
- */
27
- void * pm_memchr(const void *source, int character, size_t number, bool encoding_changed, const pm_encoding_t *encoding);
28
-
29
- #endif
@@ -1,113 +0,0 @@
1
- /**
2
- * @file pm_newline_list.h
3
- *
4
- * A list of byte offsets of newlines in a string.
5
- *
6
- * When compiling the syntax tree, it's necessary to know the line and column
7
- * of many nodes. This is necessary to support things like error messages,
8
- * tracepoints, etc.
9
- *
10
- * It's possible that we could store the start line, start column, end line, and
11
- * end column on every node in addition to the offsets that we already store,
12
- * but that would be quite a lot of memory overhead.
13
- */
14
- #ifndef PRISM_NEWLINE_LIST_H
15
- #define PRISM_NEWLINE_LIST_H
16
-
17
- #include "prism/defines.h"
18
-
19
- #include <assert.h>
20
- #include <stdbool.h>
21
- #include <stddef.h>
22
- #include <stdlib.h>
23
-
24
- /**
25
- * A list of offsets of newlines in a string. The offsets are assumed to be
26
- * sorted/inserted in ascending order.
27
- */
28
- typedef struct {
29
- /** A pointer to the start of the source string. */
30
- const uint8_t *start;
31
-
32
- /** The number of offsets in the list. */
33
- size_t size;
34
-
35
- /** The capacity of the list that has been allocated. */
36
- size_t capacity;
37
-
38
- /** The list of offsets. */
39
- size_t *offsets;
40
- } pm_newline_list_t;
41
-
42
- /**
43
- * A line and column in a string.
44
- */
45
- typedef struct {
46
- /** The line number. */
47
- int32_t line;
48
-
49
- /** The column number. */
50
- uint32_t column;
51
- } pm_line_column_t;
52
-
53
- /**
54
- * Initialize a new newline list with the given capacity. Returns true if the
55
- * allocation of the offsets succeeds, otherwise returns false.
56
- *
57
- * @param list The list to initialize.
58
- * @param start A pointer to the start of the source string.
59
- * @param capacity The initial capacity of the list.
60
- * @return True if the allocation of the offsets succeeds, otherwise false.
61
- */
62
- bool pm_newline_list_init(pm_newline_list_t *list, const uint8_t *start, size_t capacity);
63
-
64
- /**
65
- * Clear out the newlines that have been appended to the list.
66
- *
67
- * @param list The list to clear.
68
- */
69
- void
70
- pm_newline_list_clear(pm_newline_list_t *list);
71
-
72
- /**
73
- * Append a new offset to the newline list. Returns true if the reallocation of
74
- * the offsets succeeds (if one was necessary), otherwise returns false.
75
- *
76
- * @param list The list to append to.
77
- * @param cursor A pointer to the offset to append.
78
- * @return True if the reallocation of the offsets succeeds (if one was
79
- * necessary), otherwise false.
80
- */
81
- bool pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor);
82
-
83
- /**
84
- * Returns the line of the given offset. If the offset is not in the list, the
85
- * line of the closest offset less than the given offset is returned.
86
- *
87
- * @param list The list to search.
88
- * @param cursor A pointer to the offset to search for.
89
- * @param start_line The line to start counting from.
90
- * @return The line of the given offset.
91
- */
92
- int32_t pm_newline_list_line(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line);
93
-
94
- /**
95
- * Returns the line and column of the given offset. If the offset is not in the
96
- * list, the line and column of the closest offset less than the given offset
97
- * are returned.
98
- *
99
- * @param list The list to search.
100
- * @param cursor A pointer to the offset to search for.
101
- * @param start_line The line to start counting from.
102
- * @return The line and column of the given offset.
103
- */
104
- pm_line_column_t pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line);
105
-
106
- /**
107
- * Free the internal memory allocated for the newline list.
108
- *
109
- * @param list The list to free.
110
- */
111
- void pm_newline_list_free(pm_newline_list_t *list);
112
-
113
- #endif
@@ -1,200 +0,0 @@
1
- /**
2
- * @file pm_string.h
3
- *
4
- * A generic string type that can have various ownership semantics.
5
- */
6
- #ifndef PRISM_STRING_H
7
- #define PRISM_STRING_H
8
-
9
- #include "prism/defines.h"
10
-
11
- #include <assert.h>
12
- #include <errno.h>
13
- #include <stdbool.h>
14
- #include <stddef.h>
15
- #include <stdlib.h>
16
- #include <string.h>
17
-
18
- // The following headers are necessary to read files using demand paging.
19
- #ifdef _WIN32
20
- #include <windows.h>
21
- #elif defined(_POSIX_MAPPED_FILES)
22
- #include <fcntl.h>
23
- #include <sys/mman.h>
24
- #include <sys/stat.h>
25
- #elif defined(PRISM_HAS_FILESYSTEM)
26
- #include <fcntl.h>
27
- #include <sys/stat.h>
28
- #endif
29
-
30
- /**
31
- * A generic string type that can have various ownership semantics.
32
- */
33
- typedef struct {
34
- /** A pointer to the start of the string. */
35
- const uint8_t *source;
36
-
37
- /** The length of the string in bytes of memory. */
38
- size_t length;
39
-
40
- /** The type of the string. This field determines how the string should be freed. */
41
- enum {
42
- /** This string is a constant string, and should not be freed. */
43
- PM_STRING_CONSTANT,
44
-
45
- /** This is a slice of another string, and should not be freed. */
46
- PM_STRING_SHARED,
47
-
48
- /** This string owns its memory, and should be freed using `pm_string_free()`. */
49
- PM_STRING_OWNED,
50
-
51
- #ifdef PRISM_HAS_MMAP
52
- /** This string is a memory-mapped file, and should be freed using `pm_string_free()`. */
53
- PM_STRING_MAPPED
54
- #endif
55
- } type;
56
- } pm_string_t;
57
-
58
- /**
59
- * Returns the size of the pm_string_t struct. This is necessary to allocate the
60
- * correct amount of memory in the FFI backend.
61
- *
62
- * @return The size of the pm_string_t struct.
63
- */
64
- PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void);
65
-
66
- /**
67
- * Defines an empty string. This is useful for initializing a string that will
68
- * be filled in later.
69
- */
70
- #define PM_STRING_EMPTY ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 })
71
-
72
- /**
73
- * Initialize a shared string that is based on initial input.
74
- *
75
- * @param string The string to initialize.
76
- * @param start The start of the string.
77
- * @param end The end of the string.
78
- */
79
- void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end);
80
-
81
- /**
82
- * Initialize an owned string that is responsible for freeing allocated memory.
83
- *
84
- * @param string The string to initialize.
85
- * @param source The source of the string.
86
- * @param length The length of the string.
87
- */
88
- void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length);
89
-
90
- /**
91
- * Initialize a constant string that doesn't own its memory source.
92
- *
93
- * @param string The string to initialize.
94
- * @param source The source of the string.
95
- * @param length The length of the string.
96
- */
97
- void pm_string_constant_init(pm_string_t *string, const char *source, size_t length);
98
-
99
- /**
100
- * Represents the result of calling pm_string_mapped_init or
101
- * pm_string_file_init. We need this additional information because there is
102
- * not a platform-agnostic way to indicate that the file that was attempted to
103
- * be opened was a directory.
104
- */
105
- typedef enum {
106
- /** Indicates that the string was successfully initialized. */
107
- PM_STRING_INIT_SUCCESS = 0,
108
- /**
109
- * Indicates a generic error from a string_*_init function, where the type
110
- * of error should be read from `errno` or `GetLastError()`.
111
- */
112
- PM_STRING_INIT_ERROR_GENERIC = 1,
113
- /**
114
- * Indicates that the file that was attempted to be opened was a directory.
115
- */
116
- PM_STRING_INIT_ERROR_DIRECTORY = 2
117
- } pm_string_init_result_t;
118
-
119
- /**
120
- * Read the file indicated by the filepath parameter into source and load its
121
- * contents and size into the given `pm_string_t`. The given `pm_string_t`
122
- * should be freed using `pm_string_free` when it is no longer used.
123
- *
124
- * We want to use demand paging as much as possible in order to avoid having to
125
- * read the entire file into memory (which could be detrimental to performance
126
- * for large files). This means that if we're on windows we'll use
127
- * `MapViewOfFile`, on POSIX systems that have access to `mmap` we'll use
128
- * `mmap`, and on other POSIX systems we'll use `read`.
129
- *
130
- * @param string The string to initialize.
131
- * @param filepath The filepath to read.
132
- * @return The success of the read, indicated by the value of the enum.
133
- *
134
- * \public \memberof pm_string_t
135
- */
136
- PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init(pm_string_t *string, const char *filepath);
137
-
138
- /**
139
- * Read the file indicated by the filepath parameter into source and load its
140
- * contents and size into the given `pm_string_t`. The given `pm_string_t`
141
- * should be freed using `pm_string_free` when it is no longer used.
142
- *
143
- * @param string The string to initialize.
144
- * @param filepath The filepath to read.
145
- * @return The success of the read, indicated by the value of the enum.
146
- *
147
- * \public \memberof pm_string_t
148
- */
149
- PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath);
150
-
151
- /**
152
- * Ensure the string is owned. If it is not, then reinitialize it as owned and
153
- * copy over the previous source.
154
- *
155
- * @param string The string to ensure is owned.
156
- */
157
- void pm_string_ensure_owned(pm_string_t *string);
158
-
159
- /**
160
- * Compare the underlying lengths and bytes of two strings. Returns 0 if the
161
- * strings are equal, a negative number if the left string is less than the
162
- * right string, and a positive number if the left string is greater than the
163
- * right string.
164
- *
165
- * @param left The left string to compare.
166
- * @param right The right string to compare.
167
- * @return The comparison result.
168
- */
169
- int pm_string_compare(const pm_string_t *left, const pm_string_t *right);
170
-
171
- /**
172
- * Returns the length associated with the string.
173
- *
174
- * @param string The string to get the length of.
175
- * @return The length of the string.
176
- *
177
- * \public \memberof pm_string_t
178
- */
179
- PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string);
180
-
181
- /**
182
- * Returns the start pointer associated with the string.
183
- *
184
- * @param string The string to get the start pointer of.
185
- * @return The start pointer of the string.
186
- *
187
- * \public \memberof pm_string_t
188
- */
189
- PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string);
190
-
191
- /**
192
- * Free the associated memory of the given string.
193
- *
194
- * @param string The string to free.
195
- *
196
- * \public \memberof pm_string_t
197
- */
198
- PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string);
199
-
200
- #endif
data/lib/prism/pack.rb DELETED
@@ -1,230 +0,0 @@
1
- # frozen_string_literal: true
2
- # :markup: markdown
3
- # typed: ignore
4
-
5
- #
6
- module Prism
7
- # A parser for the pack template language.
8
- module Pack
9
- %i[
10
- SPACE
11
- COMMENT
12
- INTEGER
13
- UTF8
14
- BER
15
- FLOAT
16
- STRING_SPACE_PADDED
17
- STRING_NULL_PADDED
18
- STRING_NULL_TERMINATED
19
- STRING_MSB
20
- STRING_LSB
21
- STRING_HEX_HIGH
22
- STRING_HEX_LOW
23
- STRING_UU
24
- STRING_MIME
25
- STRING_BASE64
26
- STRING_FIXED
27
- STRING_POINTER
28
- MOVE
29
- BACK
30
- NULL
31
-
32
- UNSIGNED
33
- SIGNED
34
- SIGNED_NA
35
-
36
- AGNOSTIC_ENDIAN
37
- LITTLE_ENDIAN
38
- BIG_ENDIAN
39
- NATIVE_ENDIAN
40
- ENDIAN_NA
41
-
42
- SIZE_SHORT
43
- SIZE_INT
44
- SIZE_LONG
45
- SIZE_LONG_LONG
46
- SIZE_8
47
- SIZE_16
48
- SIZE_32
49
- SIZE_64
50
- SIZE_P
51
- SIZE_NA
52
-
53
- LENGTH_FIXED
54
- LENGTH_MAX
55
- LENGTH_RELATIVE
56
- LENGTH_NA
57
- ].each do |const|
58
- const_set(const, const)
59
- end
60
-
61
- # A directive in the pack template language.
62
- class Directive
63
- # A symbol representing the version of Ruby.
64
- attr_reader :version
65
-
66
- # A symbol representing whether or not we are packing or unpacking.
67
- attr_reader :variant
68
-
69
- # A byteslice of the source string that this directive represents.
70
- attr_reader :source
71
-
72
- # The type of the directive.
73
- attr_reader :type
74
-
75
- # The type of signedness of the directive.
76
- attr_reader :signed
77
-
78
- # The type of endianness of the directive.
79
- attr_reader :endian
80
-
81
- # The size of the directive.
82
- attr_reader :size
83
-
84
- # The length type of this directive (used for integers).
85
- attr_reader :length_type
86
-
87
- # The length of this directive (used for integers).
88
- attr_reader :length
89
-
90
- # Initialize a new directive with the given values.
91
- def initialize(version, variant, source, type, signed, endian, size, length_type, length)
92
- @version = version
93
- @variant = variant
94
- @source = source
95
- @type = type
96
- @signed = signed
97
- @endian = endian
98
- @size = size
99
- @length_type = length_type
100
- @length = length
101
- end
102
-
103
- # The descriptions of the various types of endianness.
104
- ENDIAN_DESCRIPTIONS = {
105
- AGNOSTIC_ENDIAN: "agnostic",
106
- LITTLE_ENDIAN: "little-endian (VAX)",
107
- BIG_ENDIAN: "big-endian (network)",
108
- NATIVE_ENDIAN: "native-endian",
109
- ENDIAN_NA: "n/a"
110
- }
111
-
112
- # The descriptions of the various types of signedness.
113
- SIGNED_DESCRIPTIONS = {
114
- UNSIGNED: "unsigned",
115
- SIGNED: "signed",
116
- SIGNED_NA: "n/a"
117
- }
118
-
119
- # The descriptions of the various types of sizes.
120
- SIZE_DESCRIPTIONS = {
121
- SIZE_SHORT: "short",
122
- SIZE_INT: "int-width",
123
- SIZE_LONG: "long",
124
- SIZE_LONG_LONG: "long long",
125
- SIZE_8: "8-bit",
126
- SIZE_16: "16-bit",
127
- SIZE_32: "32-bit",
128
- SIZE_64: "64-bit",
129
- SIZE_P: "pointer-width"
130
- }
131
-
132
- # Provide a human-readable description of the directive.
133
- def describe
134
- case type
135
- when SPACE
136
- "whitespace"
137
- when COMMENT
138
- "comment"
139
- when INTEGER
140
- if size == SIZE_8
141
- base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} integer"
142
- else
143
- base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} integer"
144
- end
145
- case length_type
146
- when LENGTH_FIXED
147
- if length > 1
148
- base + ", x#{length}"
149
- else
150
- base
151
- end
152
- when LENGTH_MAX
153
- base + ", as many as possible"
154
- else
155
- raise
156
- end
157
- when UTF8
158
- "UTF-8 character"
159
- when BER
160
- "BER-compressed integer"
161
- when FLOAT
162
- "#{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} float"
163
- when STRING_SPACE_PADDED
164
- "arbitrary binary string (space padded)"
165
- when STRING_NULL_PADDED
166
- "arbitrary binary string (null padded, count is width)"
167
- when STRING_NULL_TERMINATED
168
- "arbitrary binary string (null padded, count is width), except that null is added with *"
169
- when STRING_MSB
170
- "bit string (MSB first)"
171
- when STRING_LSB
172
- "bit string (LSB first)"
173
- when STRING_HEX_HIGH
174
- "hex string (high nibble first)"
175
- when STRING_HEX_LOW
176
- "hex string (low nibble first)"
177
- when STRING_UU
178
- "UU-encoded string"
179
- when STRING_MIME
180
- "quoted printable, MIME encoding"
181
- when STRING_BASE64
182
- "base64 encoded string"
183
- when STRING_FIXED
184
- "pointer to a structure (fixed-length string)"
185
- when STRING_POINTER
186
- "pointer to a null-terminated string"
187
- when MOVE
188
- "move to absolute position"
189
- when BACK
190
- "back up a byte"
191
- when NULL
192
- "null byte"
193
- else
194
- raise
195
- end
196
- end
197
- end
198
-
199
- # The result of parsing a pack template.
200
- class Format
201
- # A list of the directives in the template.
202
- attr_reader :directives
203
-
204
- # The encoding of the template.
205
- attr_reader :encoding
206
-
207
- # Create a new Format with the given directives and encoding.
208
- def initialize(directives, encoding)
209
- @directives = directives
210
- @encoding = encoding
211
- end
212
-
213
- # Provide a human-readable description of the format.
214
- def describe
215
- source_width = directives.map { |d| d.source.inspect.length }.max
216
- directive_lines = directives.map do |directive|
217
- if directive.type == SPACE
218
- source = directive.source.inspect
219
- else
220
- source = directive.source
221
- end
222
- # @type var source_width: Integer
223
- " #{source.ljust(source_width)} #{directive.describe}"
224
- end
225
-
226
- (["Directives:"] + directive_lines + ["Encoding:", " #{encoding}"]).join("\n")
227
- end
228
- end
229
- end
230
- end