prism 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -1
  3. data/Makefile +6 -0
  4. data/README.md +1 -1
  5. data/config.yml +50 -35
  6. data/docs/fuzzing.md +1 -1
  7. data/docs/serialization.md +28 -29
  8. data/ext/prism/api_node.c +802 -770
  9. data/ext/prism/api_pack.c +20 -9
  10. data/ext/prism/extension.c +464 -162
  11. data/ext/prism/extension.h +1 -1
  12. data/include/prism/ast.h +3173 -763
  13. data/include/prism/defines.h +32 -9
  14. data/include/prism/diagnostic.h +36 -3
  15. data/include/prism/enc/pm_encoding.h +118 -28
  16. data/include/prism/node.h +38 -13
  17. data/include/prism/options.h +204 -0
  18. data/include/prism/pack.h +44 -33
  19. data/include/prism/parser.h +445 -200
  20. data/include/prism/prettyprint.h +12 -1
  21. data/include/prism/regexp.h +16 -2
  22. data/include/prism/util/pm_buffer.h +94 -16
  23. data/include/prism/util/pm_char.h +162 -48
  24. data/include/prism/util/pm_constant_pool.h +126 -32
  25. data/include/prism/util/pm_list.h +68 -38
  26. data/include/prism/util/pm_memchr.h +18 -3
  27. data/include/prism/util/pm_newline_list.h +70 -27
  28. data/include/prism/util/pm_state_stack.h +25 -7
  29. data/include/prism/util/pm_string.h +115 -27
  30. data/include/prism/util/pm_string_list.h +25 -6
  31. data/include/prism/util/pm_strncasecmp.h +32 -0
  32. data/include/prism/util/pm_strpbrk.h +31 -17
  33. data/include/prism/version.h +27 -2
  34. data/include/prism.h +224 -31
  35. data/lib/prism/compiler.rb +6 -3
  36. data/lib/prism/debug.rb +23 -7
  37. data/lib/prism/dispatcher.rb +33 -18
  38. data/lib/prism/dsl.rb +10 -5
  39. data/lib/prism/ffi.rb +132 -80
  40. data/lib/prism/lex_compat.rb +25 -15
  41. data/lib/prism/mutation_compiler.rb +10 -5
  42. data/lib/prism/node.rb +370 -135
  43. data/lib/prism/node_ext.rb +1 -1
  44. data/lib/prism/node_inspector.rb +1 -1
  45. data/lib/prism/pack.rb +79 -40
  46. data/lib/prism/parse_result/comments.rb +7 -2
  47. data/lib/prism/parse_result/newlines.rb +4 -0
  48. data/lib/prism/parse_result.rb +150 -30
  49. data/lib/prism/pattern.rb +11 -0
  50. data/lib/prism/ripper_compat.rb +28 -10
  51. data/lib/prism/serialize.rb +86 -54
  52. data/lib/prism/visitor.rb +10 -3
  53. data/lib/prism.rb +20 -2
  54. data/prism.gemspec +4 -2
  55. data/rbi/prism.rbi +104 -60
  56. data/rbi/prism_static.rbi +16 -2
  57. data/sig/prism.rbs +72 -43
  58. data/sig/prism_static.rbs +14 -1
  59. data/src/diagnostic.c +56 -53
  60. data/src/enc/pm_big5.c +1 -0
  61. data/src/enc/pm_euc_jp.c +1 -0
  62. data/src/enc/pm_gbk.c +1 -0
  63. data/src/enc/pm_shift_jis.c +1 -0
  64. data/src/enc/pm_tables.c +316 -80
  65. data/src/enc/pm_unicode.c +53 -8
  66. data/src/enc/pm_windows_31j.c +1 -0
  67. data/src/node.c +334 -321
  68. data/src/options.c +170 -0
  69. data/src/prettyprint.c +74 -47
  70. data/src/prism.c +1642 -856
  71. data/src/regexp.c +151 -95
  72. data/src/serialize.c +44 -20
  73. data/src/token_type.c +3 -1
  74. data/src/util/pm_buffer.c +45 -15
  75. data/src/util/pm_char.c +103 -57
  76. data/src/util/pm_constant_pool.c +51 -21
  77. data/src/util/pm_list.c +12 -4
  78. data/src/util/pm_memchr.c +5 -3
  79. data/src/util/pm_newline_list.c +20 -12
  80. data/src/util/pm_state_stack.c +9 -3
  81. data/src/util/pm_string.c +95 -85
  82. data/src/util/pm_string_list.c +14 -15
  83. data/src/util/pm_strncasecmp.c +10 -3
  84. data/src/util/pm_strpbrk.c +25 -19
  85. metadata +5 -3
  86. data/docs/prism.png +0 -0
@@ -1,8 +1,14 @@
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
+ */
1
9
  #ifndef PRISM_DEFINES_H
2
10
  #define PRISM_DEFINES_H
3
11
 
4
- // This file should be included first by any *.h or *.c in prism
5
-
6
12
  #include <ctype.h>
7
13
  #include <stdarg.h>
8
14
  #include <stddef.h>
@@ -10,7 +16,11 @@
10
16
  #include <stdio.h>
11
17
  #include <string.h>
12
18
 
13
- // PRISM_EXPORTED_FUNCTION
19
+ /**
20
+ * By default, we compile with -fvisibility=hidden. When this is enabled, we
21
+ * need to mark certain functions as being publically-visible. This macro does
22
+ * that in a compiler-agnostic way.
23
+ */
14
24
  #ifndef PRISM_EXPORTED_FUNCTION
15
25
  # ifdef PRISM_EXPORT_SYMBOLS
16
26
  # ifdef _WIN32
@@ -23,7 +33,12 @@
23
33
  # endif
24
34
  #endif
25
35
 
26
- // PRISM_ATTRIBUTE_FORMAT
36
+ /**
37
+ * Certain compilers support specifying that a function accepts variadic
38
+ * parameters that look like printf format strings to provide a better developer
39
+ * experience when someone is using the function. This macro does that in a
40
+ * compiler-agnostic way.
41
+ */
27
42
  #if defined(__GNUC__)
28
43
  # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index)))
29
44
  #elif defined(__clang__)
@@ -32,23 +47,31 @@
32
47
  # define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index)
33
48
  #endif
34
49
 
35
- // PRISM_ATTRIBUTE_UNUSED
50
+ /**
51
+ * GCC will warn if you specify a function or parameter that is unused at
52
+ * runtime. This macro allows you to mark a function or parameter as unused in a
53
+ * compiler-agnostic way.
54
+ */
36
55
  #if defined(__GNUC__)
37
56
  # define PRISM_ATTRIBUTE_UNUSED __attribute__((unused))
38
57
  #else
39
58
  # define PRISM_ATTRIBUTE_UNUSED
40
59
  #endif
41
60
 
42
- // inline
61
+ /**
62
+ * Old Visual Studio versions do not support the inline keyword, so we need to
63
+ * define it to be __inline.
64
+ */
43
65
  #if defined(_MSC_VER) && !defined(inline)
44
66
  # define inline __inline
45
67
  #endif
46
68
 
47
- // Windows versions before 2015 use _snprintf
69
+ /**
70
+ * Old Visual Studio versions before 2015 do not implement sprintf, but instead
71
+ * implement _snprintf. We standard that here.
72
+ */
48
73
  #if !defined(snprintf) && defined(_MSC_VER) && (_MSC_VER < 1900)
49
74
  # define snprintf _snprintf
50
75
  #endif
51
76
 
52
- int pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length);
53
-
54
77
  #endif
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @file diagnostic.h
3
+ *
4
+ * A list of diagnostics generated during parsing.
5
+ */
1
6
  #ifndef PRISM_DIAGNOSTIC_H
2
7
  #define PRISM_DIAGNOSTIC_H
3
8
 
@@ -8,14 +13,29 @@
8
13
  #include <stdlib.h>
9
14
  #include <assert.h>
10
15
 
11
- // This struct represents a diagnostic found during parsing.
16
+ /**
17
+ * This struct represents a diagnostic generated during parsing.
18
+ *
19
+ * @extends pm_list_node_t
20
+ */
12
21
  typedef struct {
22
+ /** The embedded base node. */
13
23
  pm_list_node_t node;
24
+
25
+ /** A pointer to the start of the source that generated the diagnostic. */
14
26
  const uint8_t *start;
27
+
28
+ /** A pointer to the end of the source that generated the diagnostic. */
15
29
  const uint8_t *end;
30
+
31
+ /** The message associated with the diagnostic. */
16
32
  const char *message;
17
33
  } pm_diagnostic_t;
18
34
 
35
+ /**
36
+ * The diagnostic IDs of all of the diagnostics, used to communicate the types
37
+ * of errors between the parser and the user.
38
+ */
19
39
  typedef enum {
20
40
  PM_ERR_ALIAS_ARGUMENT,
21
41
  PM_ERR_AMPAMPEQ_MULTI_ASSIGN,
@@ -219,14 +239,27 @@ typedef enum {
219
239
  PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_PLUS,
220
240
  PM_WARN_AMBIGUOUS_PREFIX_STAR,
221
241
  PM_WARN_AMBIGUOUS_SLASH,
242
+
222
243
  /* This must be the last member. */
223
244
  PM_DIAGNOSTIC_ID_LEN,
224
245
  } pm_diagnostic_id_t;
225
246
 
226
- // Append a diagnostic to the given list of diagnostics.
247
+ /**
248
+ * Append a diagnostic to the given list of diagnostics.
249
+ *
250
+ * @param list The list to append to.
251
+ * @param start The start of the diagnostic.
252
+ * @param end The end of the diagnostic.
253
+ * @param diag_id The diagnostic ID.
254
+ * @return Whether the diagnostic was successfully appended.
255
+ */
227
256
  bool pm_diagnostic_list_append(pm_list_t *list, const uint8_t *start, const uint8_t *end, pm_diagnostic_id_t diag_id);
228
257
 
229
- // Deallocate the internal state of the given diagnostic list.
258
+ /**
259
+ * Deallocate the internal state of the given diagnostic list.
260
+ *
261
+ * @param list The list to deallocate.
262
+ */
230
263
  void pm_diagnostic_list_free(pm_list_t *list);
231
264
 
232
265
  #endif
@@ -1,3 +1,8 @@
1
+ /**
2
+ * @file pm_encoding.h
3
+ *
4
+ * The encoding interface and implementations used by the parser.
5
+ */
1
6
  #ifndef PRISM_ENCODING_H
2
7
  #define PRISM_ENCODING_H
3
8
 
@@ -8,63 +13,148 @@
8
13
  #include <stddef.h>
9
14
  #include <stdint.h>
10
15
 
11
- // This struct defines the functions necessary to implement the encoding
12
- // interface so we can determine how many bytes the subsequent character takes.
13
- // Each callback should return the number of bytes, or 0 if the next bytes are
14
- // invalid for the encoding and type.
16
+ /**
17
+ * This struct defines the functions necessary to implement the encoding
18
+ * interface so we can determine how many bytes the subsequent character takes.
19
+ * Each callback should return the number of bytes, or 0 if the next bytes are
20
+ * invalid for the encoding and type.
21
+ */
15
22
  typedef struct {
16
- // Return the number of bytes that the next character takes if it is valid
17
- // in the encoding. Does not read more than n bytes. It is assumed that n is
18
- // at least 1.
23
+ /**
24
+ * Return the number of bytes that the next character takes if it is valid
25
+ * in the encoding. Does not read more than n bytes. It is assumed that n is
26
+ * at least 1.
27
+ */
19
28
  size_t (*char_width)(const uint8_t *b, ptrdiff_t n);
20
29
 
21
- // Return the number of bytes that the next character takes if it is valid
22
- // in the encoding and is alphabetical. Does not read more than n bytes. It
23
- // is assumed that n is at least 1.
30
+ /**
31
+ * Return the number of bytes that the next character takes if it is valid
32
+ * in the encoding and is alphabetical. Does not read more than n bytes. It
33
+ * is assumed that n is at least 1.
34
+ */
24
35
  size_t (*alpha_char)(const uint8_t *b, ptrdiff_t n);
25
36
 
26
- // Return the number of bytes that the next character takes if it is valid
27
- // in the encoding and is alphanumeric. Does not read more than n bytes. It
28
- // is assumed that n is at least 1.
37
+ /**
38
+ * Return the number of bytes that the next character takes if it is valid
39
+ * in the encoding and is alphanumeric. Does not read more than n bytes. It
40
+ * is assumed that n is at least 1.
41
+ */
29
42
  size_t (*alnum_char)(const uint8_t *b, ptrdiff_t n);
30
43
 
31
- // Return true if the next character is valid in the encoding and is an
32
- // uppercase character. Does not read more than n bytes. It is assumed that
33
- // n is at least 1.
44
+ /**
45
+ * Return true if the next character is valid in the encoding and is an
46
+ * uppercase character. Does not read more than n bytes. It is assumed that
47
+ * n is at least 1.
48
+ */
34
49
  bool (*isupper_char)(const uint8_t *b, ptrdiff_t n);
35
50
 
36
- // The name of the encoding. This should correspond to a value that can be
37
- // passed to Encoding.find in Ruby.
51
+ /**
52
+ * The name of the encoding. This should correspond to a value that can be
53
+ * passed to Encoding.find in Ruby.
54
+ */
38
55
  const char *name;
39
56
 
40
- // Return true if the encoding is a multibyte encoding.
57
+ /**
58
+ * Return true if the encoding is a multibyte encoding.
59
+ */
41
60
  bool multibyte;
42
61
  } pm_encoding_t;
43
62
 
44
- // These bits define the location of each bit of metadata within the various
45
- // lookup tables that are used to determine the properties of a character.
63
+ /**
64
+ * All of the lookup tables use the first bit of each embedded byte to indicate
65
+ * whether the codepoint is alphabetical.
66
+ */
46
67
  #define PRISM_ENCODING_ALPHABETIC_BIT 1 << 0
68
+
69
+ /**
70
+ * All of the lookup tables use the second bit of each embedded byte to indicate
71
+ * whether the codepoint is alphanumeric.
72
+ */
47
73
  #define PRISM_ENCODING_ALPHANUMERIC_BIT 1 << 1
74
+
75
+ /**
76
+ * All of the lookup tables use the third bit of each embedded byte to indicate
77
+ * whether the codepoint is uppercase.
78
+ */
48
79
  #define PRISM_ENCODING_UPPERCASE_BIT 1 << 2
49
80
 
50
- // These functions are reused by some other encodings, so they are defined here
51
- // so they can be shared.
81
+ /**
82
+ * Return the size of the next character in the ASCII encoding if it is an
83
+ * alphabetical character.
84
+ *
85
+ * @param b The bytes to read.
86
+ * @param n The number of bytes that can be read.
87
+ * @returns The number of bytes that the next character takes if it is valid in
88
+ * the encoding, or 0 if it is not.
89
+ */
52
90
  size_t pm_encoding_ascii_alpha_char(const uint8_t *b, PRISM_ATTRIBUTE_UNUSED ptrdiff_t n);
91
+
92
+ /**
93
+ * Return the size of the next character in the ASCII encoding if it is an
94
+ * alphanumeric character.
95
+ *
96
+ * @param b The bytes to read.
97
+ * @param n The number of bytes that can be read.
98
+ * @returns The number of bytes that the next character takes if it is valid in
99
+ * the encoding, or 0 if it is not.
100
+ */
53
101
  size_t pm_encoding_ascii_alnum_char(const uint8_t *b, PRISM_ATTRIBUTE_UNUSED ptrdiff_t n);
102
+
103
+ /**
104
+ * Return true if the next character in the ASCII encoding if it is an uppercase
105
+ * character.
106
+ *
107
+ * @param b The bytes to read.
108
+ * @param n The number of bytes that can be read.
109
+ * @returns True if the next character is valid in the encoding and is an
110
+ * uppercase character, or false if it is not.
111
+ */
54
112
  bool pm_encoding_ascii_isupper_char(const uint8_t *b, PRISM_ATTRIBUTE_UNUSED ptrdiff_t n);
55
113
 
56
- // These functions are shared between the actual encoding and the fast path in
57
- // the parser so they need to be internally visible.
114
+ /**
115
+ * Return the size of the next character in the UTF-8 encoding if it is an
116
+ * alphabetical character.
117
+ *
118
+ * @param b The bytes to read.
119
+ * @param n The number of bytes that can be read.
120
+ * @returns The number of bytes that the next character takes if it is valid in
121
+ * the encoding, or 0 if it is not.
122
+ */
58
123
  size_t pm_encoding_utf_8_alpha_char(const uint8_t *b, ptrdiff_t n);
124
+
125
+ /**
126
+ * Return the size of the next character in the UTF-8 encoding if it is an
127
+ * alphanumeric character.
128
+ *
129
+ * @param b The bytes to read.
130
+ * @param n The number of bytes that can be read.
131
+ * @returns The number of bytes that the next character takes if it is valid in
132
+ * the encoding, or 0 if it is not.
133
+ */
59
134
  size_t pm_encoding_utf_8_alnum_char(const uint8_t *b, ptrdiff_t n);
135
+
136
+ /**
137
+ * Return true if the next character in the UTF-8 encoding if it is an uppercase
138
+ * character.
139
+ *
140
+ * @param b The bytes to read.
141
+ * @param n The number of bytes that can be read.
142
+ * @returns True if the next character is valid in the encoding and is an
143
+ * uppercase character, or false if it is not.
144
+ */
60
145
  bool pm_encoding_utf_8_isupper_char(const uint8_t *b, ptrdiff_t n);
61
146
 
62
- // This lookup table is referenced in both the UTF-8 encoding file and the
63
- // parser directly in order to speed up the default encoding processing.
147
+ /**
148
+ * This lookup table is referenced in both the UTF-8 encoding file and the
149
+ * parser directly in order to speed up the default encoding processing. It is
150
+ * used to indicate whether a character is alphabetical, alphanumeric, or
151
+ * uppercase in unicode mappings.
152
+ */
64
153
  extern const uint8_t pm_encoding_unicode_table[256];
65
154
 
66
- // These are the encodings that are supported by the parser. They are defined in
155
+ // Below are the encodings that are supported by the parser. They are defined in
67
156
  // their own files in the src/enc directory.
157
+
68
158
  extern pm_encoding_t pm_encoding_ascii;
69
159
  extern pm_encoding_t pm_encoding_ascii_8bit;
70
160
  extern pm_encoding_t pm_encoding_big5;
data/include/prism/node.h CHANGED
@@ -1,32 +1,57 @@
1
+ /**
2
+ * @file node.h
3
+ *
4
+ * Functions related to nodes in the AST.
5
+ */
1
6
  #ifndef PRISM_NODE_H
2
7
  #define PRISM_NODE_H
3
8
 
4
9
  #include "prism/defines.h"
5
10
  #include "prism/parser.h"
6
11
 
7
- // Append a new node onto the end of the node list.
12
+ /**
13
+ * Append a new node onto the end of the node list.
14
+ *
15
+ * @param list The list to append to.
16
+ * @param node The node to append.
17
+ */
8
18
  void pm_node_list_append(pm_node_list_t *list, pm_node_t *node);
9
19
 
10
- // Clear the node but preserves the location.
11
- void pm_node_clear(pm_node_t *node);
12
-
13
- // Deallocate a node and all of its children.
20
+ /**
21
+ * Deallocate a node and all of its children.
22
+ *
23
+ * @param parser The parser that owns the node.
24
+ * @param node The node to deallocate.
25
+ */
14
26
  PRISM_EXPORTED_FUNCTION void pm_node_destroy(pm_parser_t *parser, struct pm_node *node);
15
27
 
16
- // This struct stores the information gathered by the pm_node_memsize function.
17
- // It contains both the memory footprint and additionally metadata about the
18
- // shape of the tree.
28
+ /**
29
+ * This struct stores the information gathered by the pm_node_memsize function.
30
+ * It contains both the memory footprint and additionally metadata about the
31
+ * shape of the tree.
32
+ */
19
33
  typedef struct {
34
+ /** The total memory footprint of the node and all of its children. */
20
35
  size_t memsize;
36
+
37
+ /** The number of children the node has. */
21
38
  size_t node_count;
22
39
  } pm_memsize_t;
23
40
 
24
- // Calculates the memory footprint of a given node.
41
+ /**
42
+ * Calculates the memory footprint of a given node.
43
+ *
44
+ * @param node The node to calculate the memory footprint of.
45
+ * @param memsize The memory footprint of the node and all of its children.
46
+ */
25
47
  PRISM_EXPORTED_FUNCTION void pm_node_memsize(pm_node_t *node, pm_memsize_t *memsize);
26
48
 
27
- // Returns a string representation of the given node type.
49
+ /**
50
+ * Returns a string representation of the given node type.
51
+ *
52
+ * @param node_type The node type to convert to a string.
53
+ * @return A string representation of the given node type.
54
+ */
28
55
  PRISM_EXPORTED_FUNCTION const char * pm_node_type_to_str(pm_node_type_t node_type);
29
56
 
30
- #define PM_EMPTY_NODE_LIST ((pm_node_list_t) { .nodes = NULL, .size = 0, .capacity = 0 })
31
-
32
- #endif // PRISM_NODE_H
57
+ #endif
@@ -0,0 +1,204 @@
1
+ /**
2
+ * @file options.h
3
+ *
4
+ * The options that can be passed to parsing.
5
+ */
6
+ #ifndef PRISM_OPTIONS_H
7
+ #define PRISM_OPTIONS_H
8
+
9
+ #include "prism/defines.h"
10
+ #include "prism/util/pm_string.h"
11
+
12
+ #include <stdbool.h>
13
+ #include <stddef.h>
14
+ #include <stdint.h>
15
+
16
+ /**
17
+ * A scope of locals surrounding the code that is being parsed.
18
+ */
19
+ typedef struct pm_options_scope {
20
+ /** The number of locals in the scope. */
21
+ size_t locals_count;
22
+
23
+ /** The names of the locals in the scope. */
24
+ pm_string_t *locals;
25
+ } pm_options_scope_t;
26
+
27
+ /**
28
+ * The options that can be passed to the parser.
29
+ */
30
+ typedef struct {
31
+ /** The name of the file that is currently being parsed. */
32
+ pm_string_t filepath;
33
+
34
+ /**
35
+ * The line within the file that the parse starts on. This value is
36
+ * 0-indexed.
37
+ */
38
+ uint32_t line;
39
+
40
+ /**
41
+ * The name of the encoding that the source file is in. Note that this must
42
+ * correspond to a name that can be found with Encoding.find in Ruby.
43
+ */
44
+ pm_string_t encoding;
45
+
46
+ /**
47
+ * The number of scopes surrounding the code that is being parsed.
48
+ */
49
+ size_t scopes_count;
50
+
51
+ /**
52
+ * The scopes surrounding the code that is being parsed. For most parses
53
+ * this will be NULL, but for evals it will be the locals that are in scope
54
+ * surrounding the eval.
55
+ */
56
+ pm_options_scope_t *scopes;
57
+
58
+ /** Whether or not the frozen string literal option has been set. */
59
+ bool frozen_string_literal;
60
+
61
+ /**
62
+ * Whether or not we should suppress warnings. This is purposefully negated
63
+ * so that the default is to not suppress warnings, which allows us to still
64
+ * create an options struct with zeroed memory.
65
+ */
66
+ bool suppress_warnings;
67
+ } pm_options_t;
68
+
69
+ /**
70
+ * Set the filepath option on the given options struct.
71
+ *
72
+ * @param options The options struct to set the filepath on.
73
+ * @param filepath The filepath to set.
74
+ */
75
+ PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath);
76
+
77
+ /**
78
+ * Set the line option on the given options struct.
79
+ *
80
+ * @param options The options struct to set the line on.
81
+ * @param line The line to set.
82
+ */
83
+ PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, uint32_t line);
84
+
85
+ /**
86
+ * Set the encoding option on the given options struct.
87
+ *
88
+ * @param options The options struct to set the encoding on.
89
+ * @param encoding The encoding to set.
90
+ */
91
+ PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding);
92
+
93
+ /**
94
+ * Set the frozen string literal option on the given options struct.
95
+ *
96
+ * @param options The options struct to set the frozen string literal value on.
97
+ * @param frozen_string_literal The frozen string literal value to set.
98
+ */
99
+ PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal);
100
+
101
+ /**
102
+ * Set the suppress warnings option on the given options struct.
103
+ *
104
+ * @param options The options struct to set the suppress warnings value on.
105
+ * @param suppress_warnings The suppress warnings value to set.
106
+ */
107
+ PRISM_EXPORTED_FUNCTION void pm_options_suppress_warnings_set(pm_options_t *options, bool suppress_warnings);
108
+
109
+ /**
110
+ * Allocate and zero out the scopes array on the given options struct.
111
+ *
112
+ * @param options The options struct to initialize the scopes array on.
113
+ * @param scopes_count The number of scopes to allocate.
114
+ */
115
+ PRISM_EXPORTED_FUNCTION void pm_options_scopes_init(pm_options_t *options, size_t scopes_count);
116
+
117
+ /**
118
+ * Return a pointer to the scope at the given index within the given options.
119
+ *
120
+ * @param options The options struct to get the scope from.
121
+ * @param index The index of the scope to get.
122
+ * @return A pointer to the scope at the given index.
123
+ */
124
+ PRISM_EXPORTED_FUNCTION const pm_options_scope_t * pm_options_scope_get(const pm_options_t *options, size_t index);
125
+
126
+ /**
127
+ * Create a new options scope struct. This will hold a set of locals that are in
128
+ * scope surrounding the code that is being parsed.
129
+ *
130
+ * @param scope The scope struct to initialize.
131
+ * @param locals_count The number of locals to allocate.
132
+ */
133
+ PRISM_EXPORTED_FUNCTION void pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count);
134
+
135
+ /**
136
+ * Return a pointer to the local at the given index within the given scope.
137
+ *
138
+ * @param scope The scope struct to get the local from.
139
+ * @param index The index of the local to get.
140
+ * @return A pointer to the local at the given index.
141
+ */
142
+ PRISM_EXPORTED_FUNCTION const pm_string_t * pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index);
143
+
144
+ /**
145
+ * Free the internal memory associated with the options.
146
+ *
147
+ * @param options The options struct whose internal memory should be freed.
148
+ */
149
+ PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
150
+
151
+ /**
152
+ * Deserialize an options struct from the given binary string. This is used to
153
+ * pass options to the parser from an FFI call so that consumers of the library
154
+ * from an FFI perspective don't have to worry about the structure of our
155
+ * options structs. Since the source of these calls will be from Ruby
156
+ * implementation internals we assume it is from a trusted source.
157
+ *
158
+ * `data` is assumed to be a valid pointer pointing to well-formed data. The
159
+ * layout of this data should be the same every time, and is described below:
160
+ *
161
+ * | # bytes | field |
162
+ * | ------- | -------------------------- |
163
+ * | `4` | the length of the filepath |
164
+ * | ... | the filepath bytes |
165
+ * | `4` | the line number |
166
+ * | `4` | the length the encoding |
167
+ * | ... | the encoding bytes |
168
+ * | `1` | frozen string literal |
169
+ * | `1` | suppress warnings |
170
+ * | `4` | the number of scopes |
171
+ * | ... | the scopes |
172
+ *
173
+ * Each scope is layed out as follows:
174
+ *
175
+ * | # bytes | field |
176
+ * | ------- | -------------------------- |
177
+ * | `4` | the number of locals |
178
+ * | ... | the locals |
179
+ *
180
+ * Each local is layed out as follows:
181
+ *
182
+ * | # bytes | field |
183
+ * | ------- | -------------------------- |
184
+ * | `4` | the length of the local |
185
+ * | ... | the local bytes |
186
+ *
187
+ * Some additional things to note about this layout:
188
+ *
189
+ * * The filepath can have a length of 0, in which case we'll consider it an
190
+ * empty string.
191
+ * * The line number should be 0-indexed.
192
+ * * The encoding can have a length of 0, in which case we'll use the default
193
+ * encoding (UTF-8). If it's not 0, it should correspond to a name of an
194
+ * encoding that can be passed to `Encoding.find` in Ruby.
195
+ * * The frozen string literal and suppress warnings fields are booleans, so
196
+ * their values should be either 0 or 1.
197
+ * * The number of scopes can be 0.
198
+ *
199
+ * @param options The options struct to deserialize into.
200
+ * @param data The binary string to deserialize from.
201
+ */
202
+ void pm_options_read(pm_options_t *options, const char *data);
203
+
204
+ #endif