jruby-prism-parser 0.23.0.pre.SNAPSHOT-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +401 -0
  3. data/CODE_OF_CONDUCT.md +76 -0
  4. data/CONTRIBUTING.md +62 -0
  5. data/LICENSE.md +7 -0
  6. data/Makefile +101 -0
  7. data/README.md +98 -0
  8. data/config.yml +2902 -0
  9. data/docs/build_system.md +91 -0
  10. data/docs/configuration.md +64 -0
  11. data/docs/cruby_compilation.md +27 -0
  12. data/docs/design.md +53 -0
  13. data/docs/encoding.md +121 -0
  14. data/docs/fuzzing.md +88 -0
  15. data/docs/heredocs.md +36 -0
  16. data/docs/javascript.md +118 -0
  17. data/docs/local_variable_depth.md +229 -0
  18. data/docs/mapping.md +117 -0
  19. data/docs/parser_translation.md +34 -0
  20. data/docs/parsing_rules.md +19 -0
  21. data/docs/releasing.md +98 -0
  22. data/docs/ripper.md +36 -0
  23. data/docs/ruby_api.md +43 -0
  24. data/docs/ruby_parser_translation.md +19 -0
  25. data/docs/serialization.md +209 -0
  26. data/docs/testing.md +55 -0
  27. data/ext/prism/api_node.c +5098 -0
  28. data/ext/prism/api_pack.c +267 -0
  29. data/ext/prism/extconf.rb +110 -0
  30. data/ext/prism/extension.c +1155 -0
  31. data/ext/prism/extension.h +18 -0
  32. data/include/prism/ast.h +5807 -0
  33. data/include/prism/defines.h +102 -0
  34. data/include/prism/diagnostic.h +339 -0
  35. data/include/prism/encoding.h +265 -0
  36. data/include/prism/node.h +57 -0
  37. data/include/prism/options.h +230 -0
  38. data/include/prism/pack.h +152 -0
  39. data/include/prism/parser.h +732 -0
  40. data/include/prism/prettyprint.h +26 -0
  41. data/include/prism/regexp.h +33 -0
  42. data/include/prism/util/pm_buffer.h +155 -0
  43. data/include/prism/util/pm_char.h +205 -0
  44. data/include/prism/util/pm_constant_pool.h +209 -0
  45. data/include/prism/util/pm_list.h +97 -0
  46. data/include/prism/util/pm_memchr.h +29 -0
  47. data/include/prism/util/pm_newline_list.h +93 -0
  48. data/include/prism/util/pm_state_stack.h +42 -0
  49. data/include/prism/util/pm_string.h +150 -0
  50. data/include/prism/util/pm_string_list.h +44 -0
  51. data/include/prism/util/pm_strncasecmp.h +32 -0
  52. data/include/prism/util/pm_strpbrk.h +46 -0
  53. data/include/prism/version.h +29 -0
  54. data/include/prism.h +289 -0
  55. data/jruby-prism.jar +0 -0
  56. data/lib/prism/compiler.rb +486 -0
  57. data/lib/prism/debug.rb +206 -0
  58. data/lib/prism/desugar_compiler.rb +207 -0
  59. data/lib/prism/dispatcher.rb +2150 -0
  60. data/lib/prism/dot_visitor.rb +4634 -0
  61. data/lib/prism/dsl.rb +785 -0
  62. data/lib/prism/ffi.rb +346 -0
  63. data/lib/prism/lex_compat.rb +908 -0
  64. data/lib/prism/mutation_compiler.rb +753 -0
  65. data/lib/prism/node.rb +17864 -0
  66. data/lib/prism/node_ext.rb +212 -0
  67. data/lib/prism/node_inspector.rb +68 -0
  68. data/lib/prism/pack.rb +224 -0
  69. data/lib/prism/parse_result/comments.rb +177 -0
  70. data/lib/prism/parse_result/newlines.rb +64 -0
  71. data/lib/prism/parse_result.rb +498 -0
  72. data/lib/prism/pattern.rb +250 -0
  73. data/lib/prism/serialize.rb +1354 -0
  74. data/lib/prism/translation/parser/compiler.rb +1838 -0
  75. data/lib/prism/translation/parser/lexer.rb +335 -0
  76. data/lib/prism/translation/parser/rubocop.rb +37 -0
  77. data/lib/prism/translation/parser.rb +178 -0
  78. data/lib/prism/translation/ripper.rb +577 -0
  79. data/lib/prism/translation/ruby_parser.rb +1521 -0
  80. data/lib/prism/translation.rb +11 -0
  81. data/lib/prism/version.rb +3 -0
  82. data/lib/prism/visitor.rb +495 -0
  83. data/lib/prism.rb +99 -0
  84. data/prism.gemspec +135 -0
  85. data/rbi/prism.rbi +7767 -0
  86. data/rbi/prism_static.rbi +207 -0
  87. data/sig/prism.rbs +4773 -0
  88. data/sig/prism_static.rbs +201 -0
  89. data/src/diagnostic.c +400 -0
  90. data/src/encoding.c +5132 -0
  91. data/src/node.c +2786 -0
  92. data/src/options.c +213 -0
  93. data/src/pack.c +493 -0
  94. data/src/prettyprint.c +8881 -0
  95. data/src/prism.c +18406 -0
  96. data/src/regexp.c +638 -0
  97. data/src/serialize.c +1554 -0
  98. data/src/token_type.c +700 -0
  99. data/src/util/pm_buffer.c +190 -0
  100. data/src/util/pm_char.c +318 -0
  101. data/src/util/pm_constant_pool.c +322 -0
  102. data/src/util/pm_list.c +49 -0
  103. data/src/util/pm_memchr.c +35 -0
  104. data/src/util/pm_newline_list.c +84 -0
  105. data/src/util/pm_state_stack.c +25 -0
  106. data/src/util/pm_string.c +203 -0
  107. data/src/util/pm_string_list.c +28 -0
  108. data/src/util/pm_strncasecmp.c +24 -0
  109. data/src/util/pm_strpbrk.c +180 -0
  110. metadata +156 -0
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @file prettyprint.h
3
+ *
4
+ * An AST node pretty-printer.
5
+ */
6
+ #ifndef PRISM_PRETTYPRINT_H
7
+ #define PRISM_PRETTYPRINT_H
8
+
9
+ #include "prism/defines.h"
10
+
11
+ #include <stdio.h>
12
+
13
+ #include "prism/ast.h"
14
+ #include "prism/parser.h"
15
+ #include "prism/util/pm_buffer.h"
16
+
17
+ /**
18
+ * Pretty-prints the AST represented by the given node to the given buffer.
19
+ *
20
+ * @param output_buffer The buffer to write the pretty-printed AST to.
21
+ * @param parser The parser that parsed the AST.
22
+ * @param node The root node of the AST to pretty-print.
23
+ */
24
+ PRISM_EXPORTED_FUNCTION void pm_prettyprint(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm_node_t *node);
25
+
26
+ #endif
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @file regexp.h
3
+ *
4
+ * A regular expression parser.
5
+ */
6
+ #ifndef PRISM_REGEXP_H
7
+ #define PRISM_REGEXP_H
8
+
9
+ #include "prism/defines.h"
10
+ #include "prism/parser.h"
11
+ #include "prism/encoding.h"
12
+ #include "prism/util/pm_memchr.h"
13
+ #include "prism/util/pm_string_list.h"
14
+ #include "prism/util/pm_string.h"
15
+
16
+ #include <stdbool.h>
17
+ #include <stddef.h>
18
+ #include <string.h>
19
+
20
+ /**
21
+ * Parse a regular expression and extract the names of all of the named capture
22
+ * groups.
23
+ *
24
+ * @param source The source code to parse.
25
+ * @param size The size of the source code.
26
+ * @param named_captures The list to add the names of the named capture groups.
27
+ * @param encoding_changed Whether or not the encoding changed from the default.
28
+ * @param encoding The encoding of the source code.
29
+ * @return Whether or not the parsing was successful.
30
+ */
31
+ PRISM_EXPORTED_FUNCTION bool pm_regexp_named_capture_group_names(const uint8_t *source, size_t size, pm_string_list_t *named_captures, bool encoding_changed, const pm_encoding_t *encoding);
32
+
33
+ #endif
@@ -0,0 +1,155 @@
1
+ /**
2
+ * @file pm_buffer.h
3
+ *
4
+ * A wrapper around a contiguous block of allocated memory.
5
+ */
6
+ #ifndef PRISM_BUFFER_H
7
+ #define PRISM_BUFFER_H
8
+
9
+ #include "prism/defines.h"
10
+
11
+ #include <assert.h>
12
+ #include <stdbool.h>
13
+ #include <stdint.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+
17
+ /**
18
+ * A pm_buffer_t is a simple memory buffer that stores data in a contiguous
19
+ * block of memory.
20
+ */
21
+ typedef struct {
22
+ /** The length of the buffer in bytes. */
23
+ size_t length;
24
+
25
+ /** The capacity of the buffer in bytes that has been allocated. */
26
+ size_t capacity;
27
+
28
+ /** A pointer to the start of the buffer. */
29
+ char *value;
30
+ } pm_buffer_t;
31
+
32
+ /**
33
+ * Return the size of the pm_buffer_t struct.
34
+ *
35
+ * @returns The size of the pm_buffer_t struct.
36
+ */
37
+ PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof(void);
38
+
39
+ /**
40
+ * Initialize a pm_buffer_t with the given capacity.
41
+ *
42
+ * @param buffer The buffer to initialize.
43
+ * @param capacity The capacity of the buffer.
44
+ * @returns True if the buffer was initialized successfully, false otherwise.
45
+ */
46
+ bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity);
47
+
48
+ /**
49
+ * Initialize a pm_buffer_t with its default values.
50
+ *
51
+ * @param buffer The buffer to initialize.
52
+ * @returns True if the buffer was initialized successfully, false otherwise.
53
+ */
54
+ PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer);
55
+
56
+ /**
57
+ * Return the value of the buffer.
58
+ *
59
+ * @param buffer The buffer to get the value of.
60
+ * @returns The value of the buffer.
61
+ */
62
+ PRISM_EXPORTED_FUNCTION char * pm_buffer_value(pm_buffer_t *buffer);
63
+
64
+ /**
65
+ * Return the length of the buffer.
66
+ *
67
+ * @param buffer The buffer to get the length of.
68
+ * @returns The length of the buffer.
69
+ */
70
+ PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(pm_buffer_t *buffer);
71
+
72
+ /**
73
+ * Append the given amount of space as zeroes to the buffer.
74
+ *
75
+ * @param buffer The buffer to append to.
76
+ * @param length The amount of space to append and zero.
77
+ */
78
+ void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
79
+
80
+ /**
81
+ * Append a formatted string to the buffer.
82
+ *
83
+ * @param buffer The buffer to append to.
84
+ * @param format The format string to append.
85
+ * @param ... The arguments to the format string.
86
+ */
87
+ void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
88
+
89
+ /**
90
+ * Append a string to the buffer.
91
+ *
92
+ * @param buffer The buffer to append to.
93
+ * @param value The string to append.
94
+ * @param length The length of the string to append.
95
+ */
96
+ void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
97
+
98
+ /**
99
+ * Append a list of bytes to the buffer.
100
+ *
101
+ * @param buffer The buffer to append to.
102
+ * @param value The bytes to append.
103
+ * @param length The length of the bytes to append.
104
+ */
105
+ void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
106
+
107
+ /**
108
+ * Append a single byte to the buffer.
109
+ *
110
+ * @param buffer The buffer to append to.
111
+ * @param value The byte to append.
112
+ */
113
+ void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
114
+
115
+ /**
116
+ * Append a 32-bit unsigned integer to the buffer as a variable-length integer.
117
+ *
118
+ * @param buffer The buffer to append to.
119
+ * @param value The integer to append.
120
+ */
121
+ void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
122
+
123
+ /**
124
+ * Append a 32-bit signed integer to the buffer as a variable-length integer.
125
+ *
126
+ * @param buffer The buffer to append to.
127
+ * @param value The integer to append.
128
+ */
129
+ void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
130
+
131
+ /**
132
+ * Prepend the given string to the buffer.
133
+ *
134
+ * @param buffer The buffer to prepend to.
135
+ * @param value The string to prepend.
136
+ * @param length The length of the string to prepend.
137
+ */
138
+ void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
139
+
140
+ /**
141
+ * Concatenate one buffer onto another.
142
+ *
143
+ * @param destination The buffer to concatenate onto.
144
+ * @param source The buffer to concatenate.
145
+ */
146
+ void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
147
+
148
+ /**
149
+ * Free the memory associated with the buffer.
150
+ *
151
+ * @param buffer The buffer to free.
152
+ */
153
+ PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer);
154
+
155
+ #endif
@@ -0,0 +1,205 @@
1
+ /**
2
+ * @file pm_char.h
3
+ *
4
+ * Functions for working with characters and strings.
5
+ */
6
+ #ifndef PRISM_CHAR_H
7
+ #define PRISM_CHAR_H
8
+
9
+ #include "prism/defines.h"
10
+ #include "prism/util/pm_newline_list.h"
11
+
12
+ #include <stdbool.h>
13
+ #include <stddef.h>
14
+
15
+ /**
16
+ * Returns the number of characters at the start of the string that are
17
+ * whitespace. Disallows searching past the given maximum number of characters.
18
+ *
19
+ * @param string The string to search.
20
+ * @param length The maximum number of characters to search.
21
+ * @return The number of characters at the start of the string that are
22
+ * whitespace.
23
+ */
24
+ size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);
25
+
26
+ /**
27
+ * Returns the number of characters at the start of the string that are
28
+ * whitespace while also tracking the location of each newline. Disallows
29
+ * searching past the given maximum number of characters.
30
+ *
31
+ * @param string The string to search.
32
+ * @param length The maximum number of characters to search.
33
+ * @param newline_list The list of newlines to populate.
34
+ * @return The number of characters at the start of the string that are
35
+ * whitespace.
36
+ */
37
+ size_t
38
+ pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_newline_list_t *newline_list);
39
+
40
+ /**
41
+ * Returns the number of characters at the start of the string that are inline
42
+ * whitespace. Disallows searching past the given maximum number of characters.
43
+ *
44
+ * @param string The string to search.
45
+ * @param length The maximum number of characters to search.
46
+ * @return The number of characters at the start of the string that are inline
47
+ * whitespace.
48
+ */
49
+ size_t pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length);
50
+
51
+ /**
52
+ * Returns the number of characters at the start of the string that are decimal
53
+ * digits. Disallows searching past the given maximum number of characters.
54
+ *
55
+ * @param string The string to search.
56
+ * @param length The maximum number of characters to search.
57
+ * @return The number of characters at the start of the string that are decimal
58
+ * digits.
59
+ */
60
+ size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length);
61
+
62
+ /**
63
+ * Returns the number of characters at the start of the string that are
64
+ * hexadecimal digits. Disallows searching past the given maximum number of
65
+ * characters.
66
+ *
67
+ * @param string The string to search.
68
+ * @param length The maximum number of characters to search.
69
+ * @return The number of characters at the start of the string that are
70
+ * hexadecimal digits.
71
+ */
72
+ size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length);
73
+
74
+ /**
75
+ * Returns the number of characters at the start of the string that are octal
76
+ * digits or underscores. Disallows searching past the given maximum number of
77
+ * characters.
78
+ *
79
+ * If multiple underscores are found in a row or if an underscore is
80
+ * found at the end of the number, then the invalid pointer is set to the index
81
+ * of the first invalid underscore.
82
+ *
83
+ * @param string The string to search.
84
+ * @param length The maximum number of characters to search.
85
+ * @param invalid The pointer to set to the index of the first invalid
86
+ * underscore.
87
+ * @return The number of characters at the start of the string that are octal
88
+ * digits or underscores.
89
+ */
90
+ size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
91
+
92
+ /**
93
+ * Returns the number of characters at the start of the string that are decimal
94
+ * digits or underscores. Disallows searching past the given maximum number of
95
+ * characters.
96
+ *
97
+ * If multiple underscores are found in a row or if an underscore is
98
+ * found at the end of the number, then the invalid pointer is set to the index
99
+ * of the first invalid underscore.
100
+ *
101
+ * @param string The string to search.
102
+ * @param length The maximum number of characters to search.
103
+ * @param invalid The pointer to set to the index of the first invalid
104
+ * underscore.
105
+ * @return The number of characters at the start of the string that are decimal
106
+ * digits or underscores.
107
+ */
108
+ size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
109
+
110
+ /**
111
+ * Returns the number of characters at the start of the string that are
112
+ * hexadecimal digits or underscores. Disallows searching past the given maximum
113
+ * number of characters.
114
+ *
115
+ * If multiple underscores are found in a row or if an underscore is
116
+ * found at the end of the number, then the invalid pointer is set to the index
117
+ * of the first invalid underscore.
118
+ *
119
+ * @param string The string to search.
120
+ * @param length The maximum number of characters to search.
121
+ * @param invalid The pointer to set to the index of the first invalid
122
+ * underscore.
123
+ * @return The number of characters at the start of the string that are
124
+ * hexadecimal digits or underscores.
125
+ */
126
+ size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
127
+
128
+ /**
129
+ * Returns the number of characters at the start of the string that are regexp
130
+ * options. Disallows searching past the given maximum number of characters.
131
+ *
132
+ * @param string The string to search.
133
+ * @param length The maximum number of characters to search.
134
+ * @return The number of characters at the start of the string that are regexp
135
+ * options.
136
+ */
137
+ size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);
138
+
139
+ /**
140
+ * Returns the number of characters at the start of the string that are binary
141
+ * digits or underscores. Disallows searching past the given maximum number of
142
+ * characters.
143
+ *
144
+ * If multiple underscores are found in a row or if an underscore is
145
+ * found at the end of the number, then the invalid pointer is set to the index
146
+ * of the first invalid underscore.
147
+ *
148
+ * @param string The string to search.
149
+ * @param length The maximum number of characters to search.
150
+ * @param invalid The pointer to set to the index of the first invalid
151
+ * underscore.
152
+ * @return The number of characters at the start of the string that are binary
153
+ * digits or underscores.
154
+ */
155
+ size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
156
+
157
+ /**
158
+ * Returns true if the given character is a whitespace character.
159
+ *
160
+ * @param b The character to check.
161
+ * @return True if the given character is a whitespace character.
162
+ */
163
+ bool pm_char_is_whitespace(const uint8_t b);
164
+
165
+ /**
166
+ * Returns true if the given character is an inline whitespace character.
167
+ *
168
+ * @param b The character to check.
169
+ * @return True if the given character is an inline whitespace character.
170
+ */
171
+ bool pm_char_is_inline_whitespace(const uint8_t b);
172
+
173
+ /**
174
+ * Returns true if the given character is a binary digit.
175
+ *
176
+ * @param b The character to check.
177
+ * @return True if the given character is a binary digit.
178
+ */
179
+ bool pm_char_is_binary_digit(const uint8_t b);
180
+
181
+ /**
182
+ * Returns true if the given character is an octal digit.
183
+ *
184
+ * @param b The character to check.
185
+ * @return True if the given character is an octal digit.
186
+ */
187
+ bool pm_char_is_octal_digit(const uint8_t b);
188
+
189
+ /**
190
+ * Returns true if the given character is a decimal digit.
191
+ *
192
+ * @param b The character to check.
193
+ * @return True if the given character is a decimal digit.
194
+ */
195
+ bool pm_char_is_decimal_digit(const uint8_t b);
196
+
197
+ /**
198
+ * Returns true if the given character is a hexadecimal digit.
199
+ *
200
+ * @param b The character to check.
201
+ * @return True if the given character is a hexadecimal digit.
202
+ */
203
+ bool pm_char_is_hexadecimal_digit(const uint8_t b);
204
+
205
+ #endif
@@ -0,0 +1,209 @@
1
+ /**
2
+ * @file pm_constant_pool.h
3
+ *
4
+ * A data structure that stores a set of strings.
5
+ *
6
+ * Each string is assigned a unique id, which can be used to compare strings for
7
+ * equality. This comparison ends up being much faster than strcmp, since it
8
+ * only requires a single integer comparison.
9
+ */
10
+ #ifndef PRISM_CONSTANT_POOL_H
11
+ #define PRISM_CONSTANT_POOL_H
12
+
13
+ #include "prism/defines.h"
14
+
15
+ #include <assert.h>
16
+ #include <stdbool.h>
17
+ #include <stdint.h>
18
+ #include <stdlib.h>
19
+ #include <string.h>
20
+
21
+ /**
22
+ * When we allocate constants into the pool, we reserve 0 to mean that the slot
23
+ * is not yet filled. This constant is reused in other places to indicate the
24
+ * lack of a constant id.
25
+ */
26
+ #define PM_CONSTANT_ID_UNSET 0
27
+
28
+ /**
29
+ * A constant id is a unique identifier for a constant in the constant pool.
30
+ */
31
+ typedef uint32_t pm_constant_id_t;
32
+
33
+ /**
34
+ * A list of constant IDs. Usually used to represent a set of locals.
35
+ */
36
+ typedef struct {
37
+ /** The number of constant ids in the list. */
38
+ size_t size;
39
+
40
+ /** The number of constant ids that have been allocated in the list. */
41
+ size_t capacity;
42
+
43
+ /** The constant ids in the list. */
44
+ pm_constant_id_t *ids;
45
+ } pm_constant_id_list_t;
46
+
47
+ /**
48
+ * Initialize a list of constant ids.
49
+ *
50
+ * @param list The list to initialize.
51
+ */
52
+ void pm_constant_id_list_init(pm_constant_id_list_t *list);
53
+
54
+ /**
55
+ * Append a constant id to a list of constant ids. Returns false if any
56
+ * potential reallocations fail.
57
+ *
58
+ * @param list The list to append to.
59
+ * @param id The id to append.
60
+ * @return Whether the append succeeded.
61
+ */
62
+ bool pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id);
63
+
64
+ /**
65
+ * Checks if the current constant id list includes the given constant id.
66
+ *
67
+ * @param list The list to check.
68
+ * @param id The id to check for.
69
+ * @return Whether the list includes the given id.
70
+ */
71
+ bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id);
72
+
73
+ /**
74
+ * Get the memory size of a list of constant ids.
75
+ *
76
+ * @param list The list to get the memory size of.
77
+ * @return The memory size of the list.
78
+ */
79
+ size_t pm_constant_id_list_memsize(pm_constant_id_list_t *list);
80
+
81
+ /**
82
+ * Free the memory associated with a list of constant ids.
83
+ *
84
+ * @param list The list to free.
85
+ */
86
+ void pm_constant_id_list_free(pm_constant_id_list_t *list);
87
+
88
+ /**
89
+ * The type of bucket in the constant pool hash map. This determines how the
90
+ * bucket should be freed.
91
+ */
92
+ typedef unsigned int pm_constant_pool_bucket_type_t;
93
+
94
+ /** By default, each constant is a slice of the source. */
95
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0;
96
+
97
+ /** An owned constant is one for which memory has been allocated. */
98
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1;
99
+
100
+ /** A constant constant is known at compile time. */
101
+ static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2;
102
+
103
+ /** A bucket in the hash map. */
104
+ typedef struct {
105
+ /** The incremental ID used for indexing back into the pool. */
106
+ unsigned int id: 30;
107
+
108
+ /** The type of the bucket, which determines how to free it. */
109
+ pm_constant_pool_bucket_type_t type: 2;
110
+
111
+ /** The hash of the bucket. */
112
+ uint32_t hash;
113
+ } pm_constant_pool_bucket_t;
114
+
115
+ /** A constant in the pool which effectively stores a string. */
116
+ typedef struct {
117
+ /** A pointer to the start of the string. */
118
+ const uint8_t *start;
119
+
120
+ /** The length of the string. */
121
+ size_t length;
122
+ } pm_constant_t;
123
+
124
+ /** The overall constant pool, which stores constants found while parsing. */
125
+ typedef struct {
126
+ /** The buckets in the hash map. */
127
+ pm_constant_pool_bucket_t *buckets;
128
+
129
+ /** The constants that are stored in the buckets. */
130
+ pm_constant_t *constants;
131
+
132
+ /** The number of buckets in the hash map. */
133
+ uint32_t size;
134
+
135
+ /** The number of buckets that have been allocated in the hash map. */
136
+ uint32_t capacity;
137
+ } pm_constant_pool_t;
138
+
139
+ /**
140
+ * Initialize a new constant pool with a given capacity.
141
+ *
142
+ * @param pool The pool to initialize.
143
+ * @param capacity The initial capacity of the pool.
144
+ * @return Whether the initialization succeeded.
145
+ */
146
+ bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity);
147
+
148
+ /**
149
+ * Return a pointer to the constant indicated by the given constant id.
150
+ *
151
+ * @param pool The pool to get the constant from.
152
+ * @param constant_id The id of the constant to get.
153
+ * @return A pointer to the constant.
154
+ */
155
+ pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id);
156
+
157
+ /**
158
+ * Find a constant in a constant pool. Returns the id of the constant, or 0 if
159
+ * the constant is not found.
160
+ *
161
+ * @param pool The pool to find the constant in.
162
+ * @param start A pointer to the start of the constant.
163
+ * @param length The length of the constant.
164
+ * @return The id of the constant.
165
+ */
166
+ pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length);
167
+
168
+ /**
169
+ * Insert a constant into a constant pool that is a slice of a source string.
170
+ * Returns the id of the constant, or 0 if any potential calls to resize fail.
171
+ *
172
+ * @param pool The pool to insert the constant into.
173
+ * @param start A pointer to the start of the constant.
174
+ * @param length The length of the constant.
175
+ * @return The id of the constant.
176
+ */
177
+ pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
178
+
179
+ /**
180
+ * Insert a constant into a constant pool from memory that is now owned by the
181
+ * constant pool. Returns the id of the constant, or 0 if any potential calls to
182
+ * resize fail.
183
+ *
184
+ * @param pool The pool to insert the constant into.
185
+ * @param start A pointer to the start of the constant.
186
+ * @param length The length of the constant.
187
+ * @return The id of the constant.
188
+ */
189
+ pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
190
+
191
+ /**
192
+ * Insert a constant into a constant pool from memory that is constant. Returns
193
+ * the id of the constant, or 0 if any potential calls to resize fail.
194
+ *
195
+ * @param pool The pool to insert the constant into.
196
+ * @param start A pointer to the start of the constant.
197
+ * @param length The length of the constant.
198
+ * @return The id of the constant.
199
+ */
200
+ pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
201
+
202
+ /**
203
+ * Free the memory associated with a constant pool.
204
+ *
205
+ * @param pool The pool to free.
206
+ */
207
+ void pm_constant_pool_free(pm_constant_pool_t *pool);
208
+
209
+ #endif