backburn 0.1.0
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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/Makefile +50 -0
- data/README.md +520 -0
- data/docs/cli-and-outputs.md +226 -0
- data/docs/technical-spec.md +656 -0
- data/exe/backburn +8 -0
- data/ext/backburn/extconf.rb +31 -0
- data/ext/backburn/prism/COMMIT +1 -0
- data/ext/backburn/prism/LICENSE.md +7 -0
- data/ext/backburn/prism/Makefile +213 -0
- data/ext/backburn/prism/include/prism/arena.h +45 -0
- data/ext/backburn/prism/include/prism/arena.hpp +42 -0
- data/ext/backburn/prism/include/prism/ast.h +10395 -0
- data/ext/backburn/prism/include/prism/ast.hpp +641 -0
- data/ext/backburn/prism/include/prism/buffer.h +60 -0
- data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
- data/ext/backburn/prism/include/prism/comments.h +72 -0
- data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
- data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
- data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
- data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
- data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
- data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
- data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
- data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
- data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
- data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
- data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
- data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
- data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
- data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
- data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
- data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
- data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
- data/ext/backburn/prism/include/prism/errors_format.h +63 -0
- data/ext/backburn/prism/include/prism/excludes.h +29 -0
- data/ext/backburn/prism/include/prism/integer.h +79 -0
- data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
- data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
- data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
- data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
- data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
- data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
- data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
- data/ext/backburn/prism/include/prism/internal/char.h +145 -0
- data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
- data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
- data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
- data/ext/backburn/prism/include/prism/internal/double.h +19 -0
- data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
- data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
- data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
- data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
- data/ext/backburn/prism/include/prism/internal/list.h +62 -0
- data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
- data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
- data/ext/backburn/prism/include/prism/internal/node.h +197 -0
- data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
- data/ext/backburn/prism/include/prism/internal/options.h +248 -0
- data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
- data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
- data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
- data/ext/backburn/prism/include/prism/internal/source.h +72 -0
- data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
- data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
- data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
- data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
- data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
- data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
- data/ext/backburn/prism/include/prism/json.h +40 -0
- data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
- data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
- data/ext/backburn/prism/include/prism/node.h +139 -0
- data/ext/backburn/prism/include/prism/node.hpp +272 -0
- data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
- data/ext/backburn/prism/include/prism/options.h +393 -0
- data/ext/backburn/prism/include/prism/options.hpp +43 -0
- data/ext/backburn/prism/include/prism/parser.h +433 -0
- data/ext/backburn/prism/include/prism/parser.hpp +274 -0
- data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
- data/ext/backburn/prism/include/prism/serialize.h +123 -0
- data/ext/backburn/prism/include/prism/source.h +163 -0
- data/ext/backburn/prism/include/prism/stream.h +36 -0
- data/ext/backburn/prism/include/prism/string_query.h +97 -0
- data/ext/backburn/prism/include/prism/stringy.h +61 -0
- data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
- data/ext/backburn/prism/include/prism/version.h +46 -0
- data/ext/backburn/prism/include/prism.h +123 -0
- data/ext/backburn/prism/include/prism.hpp +24 -0
- data/ext/backburn/prism/src/arena.c +200 -0
- data/ext/backburn/prism/src/bignum.c +399 -0
- data/ext/backburn/prism/src/buffer.c +405 -0
- data/ext/backburn/prism/src/char.c +289 -0
- data/ext/backburn/prism/src/comments.c +455 -0
- data/ext/backburn/prism/src/constant_pool.c +410 -0
- data/ext/backburn/prism/src/diagnostic.c +918 -0
- data/ext/backburn/prism/src/double.c +394 -0
- data/ext/backburn/prism/src/encoding.c +1701 -0
- data/ext/backburn/prism/src/errors_format.c +650 -0
- data/ext/backburn/prism/src/integer.c +675 -0
- data/ext/backburn/prism/src/json.c +5745 -0
- data/ext/backburn/prism/src/line_offset_list.c +99 -0
- data/ext/backburn/prism/src/list.c +24 -0
- data/ext/backburn/prism/src/memchr.c +37 -0
- data/ext/backburn/prism/src/node.c +4464 -0
- data/ext/backburn/prism/src/options.c +475 -0
- data/ext/backburn/prism/src/parser.c +371 -0
- data/ext/backburn/prism/src/prettyprint.c +9038 -0
- data/ext/backburn/prism/src/prism.c +23615 -0
- data/ext/backburn/prism/src/regexp.c +3833 -0
- data/ext/backburn/prism/src/serialize.c +2656 -0
- data/ext/backburn/prism/src/source.c +525 -0
- data/ext/backburn/prism/src/static_literals.c +562 -0
- data/ext/backburn/prism/src/string_query.c +195 -0
- data/ext/backburn/prism/src/stringy.c +77 -0
- data/ext/backburn/prism/src/strncasecmp.c +37 -0
- data/ext/backburn/prism/src/strpbrk.c +465 -0
- data/ext/backburn/prism/src/tokens.c +724 -0
- data/ext/backburn/prism/src/xxh3.c +292 -0
- data/lib/backburn/version.rb +7 -0
- data/presets/devise.roots +11 -0
- data/presets/fast-mcp.roots +5 -0
- data/presets/graphql.roots +6 -0
- data/presets/phlex.roots +7 -0
- data/presets/pundit.roots +3 -0
- data/presets/rails-framework.roots +10 -0
- data/presets/rails.roots +83 -0
- data/presets/sidekiq.roots +4 -0
- data/presets/thor.roots +3 -0
- data/rails.roots +99 -0
- data/scripts/embed-presets.sh +13 -0
- data/scripts/version-h.sh +7 -0
- data/src/analysis/analysis.c +95 -0
- data/src/analysis/analysis.h +24 -0
- data/src/analysis/batches.c +127 -0
- data/src/analysis/batches.h +17 -0
- data/src/analysis/graph.c +238 -0
- data/src/analysis/graph.h +34 -0
- data/src/analysis/mark.c +300 -0
- data/src/analysis/mark.h +21 -0
- data/src/analysis/roots.c +229 -0
- data/src/analysis/roots.h +29 -0
- data/src/cli/cli.c +237 -0
- data/src/cli/cli.h +9 -0
- data/src/cli/config.c +112 -0
- data/src/cli/config.h +14 -0
- data/src/cli/options.c +17 -0
- data/src/cli/options.h +35 -0
- data/src/commands/commands.h +11 -0
- data/src/commands/explain.c +255 -0
- data/src/commands/gemfile.c +126 -0
- data/src/commands/gemfile.h +27 -0
- data/src/commands/gems.c +408 -0
- data/src/commands/init.c +92 -0
- data/src/commands/roots.c +82 -0
- data/src/commands/scan.c +91 -0
- data/src/core/intern.c +94 -0
- data/src/core/intern.h +40 -0
- data/src/core/json.c +17 -0
- data/src/core/json.h +9 -0
- data/src/core/model.c +25 -0
- data/src/core/model.h +85 -0
- data/src/core/util.c +116 -0
- data/src/core/util.h +48 -0
- data/src/index/erb.c +40 -0
- data/src/index/erb.h +9 -0
- data/src/index/haml.c +334 -0
- data/src/index/haml.h +10 -0
- data/src/index/indexer.c +702 -0
- data/src/index/indexer.h +19 -0
- data/src/index/walk.c +144 -0
- data/src/index/walk.h +14 -0
- data/src/main.c +81 -0
- data/src/output/baseline.c +101 -0
- data/src/output/baseline.h +15 -0
- data/src/output/report.c +440 -0
- data/src/output/report.h +28 -0
- metadata +231 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
#ifndef PRISM_INTERNAL_CHAR_H
|
|
2
|
+
#define PRISM_INTERNAL_CHAR_H
|
|
3
|
+
|
|
4
|
+
#include "prism/compiler/force_inline.h"
|
|
5
|
+
|
|
6
|
+
#include "prism/arena.h"
|
|
7
|
+
#include "prism/line_offset_list.h"
|
|
8
|
+
|
|
9
|
+
#include <stdbool.h>
|
|
10
|
+
#include <stddef.h>
|
|
11
|
+
#include <stdint.h>
|
|
12
|
+
|
|
13
|
+
/* Bit flag for whitespace characters in pm_byte_table. */
|
|
14
|
+
#define PRISM_CHAR_BIT_WHITESPACE (1 << 0)
|
|
15
|
+
|
|
16
|
+
/* Bit flag for inline whitespace characters in pm_byte_table. */
|
|
17
|
+
#define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1)
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
* A lookup table for classifying bytes. Each entry is a bitfield of
|
|
21
|
+
* PRISM_CHAR_BIT_* flags. Defined in char.c.
|
|
22
|
+
*/
|
|
23
|
+
extern const uint8_t pm_byte_table[256];
|
|
24
|
+
|
|
25
|
+
/* Returns true if the given character is a whitespace character. */
|
|
26
|
+
static PRISM_FORCE_INLINE bool
|
|
27
|
+
pm_char_is_whitespace(const uint8_t b) {
|
|
28
|
+
return (pm_byte_table[b] & PRISM_CHAR_BIT_WHITESPACE) != 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* Returns true if the given character is an inline whitespace character. */
|
|
32
|
+
static PRISM_FORCE_INLINE bool
|
|
33
|
+
pm_char_is_inline_whitespace(const uint8_t b) {
|
|
34
|
+
return (pm_byte_table[b] & PRISM_CHAR_BIT_INLINE_WHITESPACE) != 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* Returns the number of characters at the start of the string that are inline
|
|
39
|
+
* whitespace (space/tab). Scans the byte table directly for use in hot paths.
|
|
40
|
+
*/
|
|
41
|
+
static PRISM_FORCE_INLINE size_t
|
|
42
|
+
pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length) {
|
|
43
|
+
if (length <= 0) return 0;
|
|
44
|
+
size_t size = 0;
|
|
45
|
+
size_t maximum = (size_t) length;
|
|
46
|
+
while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_INLINE_WHITESPACE)) size++;
|
|
47
|
+
return size;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
* Returns the number of characters at the start of the string that are
|
|
52
|
+
* whitespace. Disallows searching past the given maximum number of characters.
|
|
53
|
+
*/
|
|
54
|
+
size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* Returns the number of characters at the end of the string that are
|
|
58
|
+
* whitespace. Disallows searching past the given maximum number of characters.
|
|
59
|
+
*/
|
|
60
|
+
size_t pm_strrspn_whitespace(const uint8_t *string, ptrdiff_t length);
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* Returns the number of characters at the start of the string that are
|
|
64
|
+
* whitespace while also tracking the location of each newline. Disallows
|
|
65
|
+
* searching past the given maximum number of characters.
|
|
66
|
+
*/
|
|
67
|
+
size_t pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_arena_t *arena, pm_line_offset_list_t *line_offsets, uint32_t start_offset);
|
|
68
|
+
|
|
69
|
+
/*
|
|
70
|
+
* Returns the number of characters at the start of the string that are decimal
|
|
71
|
+
* digits. Disallows searching past the given maximum number of characters.
|
|
72
|
+
*/
|
|
73
|
+
size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length);
|
|
74
|
+
|
|
75
|
+
/*
|
|
76
|
+
* Returns the number of characters at the start of the string that are
|
|
77
|
+
* hexadecimal digits. Disallows searching past the given maximum number of
|
|
78
|
+
* characters.
|
|
79
|
+
*/
|
|
80
|
+
size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length);
|
|
81
|
+
|
|
82
|
+
/*
|
|
83
|
+
* Returns the number of characters at the start of the string that are octal
|
|
84
|
+
* digits or underscores. Disallows searching past the given maximum number of
|
|
85
|
+
* characters.
|
|
86
|
+
*
|
|
87
|
+
* If multiple underscores are found in a row or if an underscore is
|
|
88
|
+
* found at the end of the number, then the invalid pointer is set to the index
|
|
89
|
+
* of the first invalid underscore.
|
|
90
|
+
*/
|
|
91
|
+
size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* Returns the number of characters at the start of the string that are decimal
|
|
95
|
+
* digits or underscores. Disallows searching past the given maximum number of
|
|
96
|
+
* characters.
|
|
97
|
+
*
|
|
98
|
+
* If multiple underscores are found in a row or if an underscore is
|
|
99
|
+
* found at the end of the number, then the invalid pointer is set to the index
|
|
100
|
+
* of the first invalid underscore.
|
|
101
|
+
*/
|
|
102
|
+
size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
|
|
103
|
+
|
|
104
|
+
/*
|
|
105
|
+
* Returns the number of characters at the start of the string that are
|
|
106
|
+
* hexadecimal digits or underscores. Disallows searching past the given maximum
|
|
107
|
+
* number of characters.
|
|
108
|
+
*
|
|
109
|
+
* If multiple underscores are found in a row or if an underscore is
|
|
110
|
+
* found at the end of the number, then the invalid pointer is set to the index
|
|
111
|
+
* of the first invalid underscore.
|
|
112
|
+
*/
|
|
113
|
+
size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
|
|
114
|
+
|
|
115
|
+
/*
|
|
116
|
+
* Returns the number of characters at the start of the string that are regexp
|
|
117
|
+
* options. Disallows searching past the given maximum number of characters.
|
|
118
|
+
*/
|
|
119
|
+
size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);
|
|
120
|
+
|
|
121
|
+
/*
|
|
122
|
+
* Returns the number of characters at the start of the string that are binary
|
|
123
|
+
* digits or underscores. Disallows searching past the given maximum number of
|
|
124
|
+
* characters.
|
|
125
|
+
*
|
|
126
|
+
* If multiple underscores are found in a row or if an underscore is
|
|
127
|
+
* found at the end of the number, then the invalid pointer is set to the index
|
|
128
|
+
* of the first invalid underscore.
|
|
129
|
+
*/
|
|
130
|
+
size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
/* Returns true if the given character is a binary digit. */
|
|
134
|
+
bool pm_char_is_binary_digit(const uint8_t b);
|
|
135
|
+
|
|
136
|
+
/* Returns true if the given character is an octal digit. */
|
|
137
|
+
bool pm_char_is_octal_digit(const uint8_t b);
|
|
138
|
+
|
|
139
|
+
/* Returns true if the given character is a decimal digit. */
|
|
140
|
+
bool pm_char_is_decimal_digit(const uint8_t b);
|
|
141
|
+
|
|
142
|
+
/* Returns true if the given character is a hexadecimal digit. */
|
|
143
|
+
bool pm_char_is_hexadecimal_digit(const uint8_t b);
|
|
144
|
+
|
|
145
|
+
#endif
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#ifndef PRISM_INTERNAL_COMMENTS_H
|
|
2
|
+
#define PRISM_INTERNAL_COMMENTS_H
|
|
3
|
+
|
|
4
|
+
#include "prism/comments.h"
|
|
5
|
+
#include "prism/parser.h"
|
|
6
|
+
|
|
7
|
+
#include "prism/internal/list.h"
|
|
8
|
+
|
|
9
|
+
#include <stdint.h>
|
|
10
|
+
|
|
11
|
+
/* A comment found while parsing. */
|
|
12
|
+
struct pm_comment_t {
|
|
13
|
+
/* The embedded base node. */
|
|
14
|
+
pm_list_node_t node;
|
|
15
|
+
|
|
16
|
+
/* The location of the comment in the source. */
|
|
17
|
+
pm_location_t location;
|
|
18
|
+
|
|
19
|
+
/* The type of the comment. */
|
|
20
|
+
pm_comment_type_t type;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
* The result of attaching comments to the nodes that own them. All of the
|
|
25
|
+
* arrays are allocated in the parser's metadata arena.
|
|
26
|
+
*/
|
|
27
|
+
typedef struct {
|
|
28
|
+
/* The comments found while parsing, sorted by start offset. */
|
|
29
|
+
const pm_comment_t **comments;
|
|
30
|
+
|
|
31
|
+
/*
|
|
32
|
+
* Indices into comments, grouped by owning node in order of node id and
|
|
33
|
+
* sorted by start offset within each group. A node's comments are not
|
|
34
|
+
* necessarily contiguous in comments, because a child can own a comment
|
|
35
|
+
* that falls between two comments owned by its parent.
|
|
36
|
+
*/
|
|
37
|
+
uint32_t *attachments;
|
|
38
|
+
|
|
39
|
+
/* The slice of attachments for each node, indexed by node id. */
|
|
40
|
+
pm_comment_range_t *ranges;
|
|
41
|
+
|
|
42
|
+
/* The number of entries in ranges. */
|
|
43
|
+
uint32_t ranges_size;
|
|
44
|
+
} pm_comment_attachments_t;
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* Sort the parser's comment list in place by start offset, then assign every
|
|
48
|
+
* comment to the node that owns it. This is called after parsing when the
|
|
49
|
+
* attach_comments option is set, before the parser's scratch arena is released,
|
|
50
|
+
* because the walk allocates from it.
|
|
51
|
+
*/
|
|
52
|
+
void pm_comments_attach(pm_parser_t *parser, const pm_node_t *root);
|
|
53
|
+
|
|
54
|
+
#endif
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
#ifndef PRISM_INTERNAL_CONSTANT_POOL_H
|
|
2
|
+
#define PRISM_INTERNAL_CONSTANT_POOL_H
|
|
3
|
+
|
|
4
|
+
#include "prism/constant_pool.h"
|
|
5
|
+
|
|
6
|
+
#include "prism/arena.h"
|
|
7
|
+
#include "prism/compiler/inline.h"
|
|
8
|
+
|
|
9
|
+
#include <stdbool.h>
|
|
10
|
+
|
|
11
|
+
/* A constant in the pool which effectively stores a string. */
|
|
12
|
+
struct pm_constant_t {
|
|
13
|
+
/* A pointer to the start of the string. */
|
|
14
|
+
const uint8_t *start;
|
|
15
|
+
|
|
16
|
+
/* The length of the string. */
|
|
17
|
+
size_t length;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* The type of bucket in the constant pool hash map. This determines how the
|
|
22
|
+
* bucket should be freed.
|
|
23
|
+
*/
|
|
24
|
+
typedef unsigned int pm_constant_pool_bucket_type_t;
|
|
25
|
+
|
|
26
|
+
/* By default, each constant is a slice of the source. */
|
|
27
|
+
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0;
|
|
28
|
+
|
|
29
|
+
/* An owned constant is one for which memory has been allocated. */
|
|
30
|
+
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1;
|
|
31
|
+
|
|
32
|
+
/* A constant constant is known at compile time. */
|
|
33
|
+
static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2;
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* A bucket in the hash map. It holds no string, so that it stays eight bytes
|
|
37
|
+
* wide for every slot of a mostly empty table. A probe compares hashes first,
|
|
38
|
+
* and reads the string through the constants array only when they match, which
|
|
39
|
+
* is almost always a real hit.
|
|
40
|
+
*/
|
|
41
|
+
typedef struct {
|
|
42
|
+
/* The incremental ID used for indexing back into the pool. */
|
|
43
|
+
unsigned int id: 30;
|
|
44
|
+
|
|
45
|
+
/* The type of the bucket, which determines how to free it. */
|
|
46
|
+
pm_constant_pool_bucket_type_t type: 2;
|
|
47
|
+
|
|
48
|
+
/* The hash of the bucket. */
|
|
49
|
+
uint32_t hash;
|
|
50
|
+
} pm_constant_pool_bucket_t;
|
|
51
|
+
|
|
52
|
+
/* The overall constant pool, which stores constants found while parsing. */
|
|
53
|
+
struct pm_constant_pool_t {
|
|
54
|
+
/* The buckets in the hash map. */
|
|
55
|
+
pm_constant_pool_bucket_t *buckets;
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
* The constants that are stored in the buckets, indexed by id minus one.
|
|
59
|
+
* The pool grows before it is three quarters full, so this has room for
|
|
60
|
+
* three quarters as many constants as there are buckets, rounded up.
|
|
61
|
+
*/
|
|
62
|
+
pm_constant_t *constants;
|
|
63
|
+
|
|
64
|
+
/* The number of buckets in the hash map. */
|
|
65
|
+
uint32_t size;
|
|
66
|
+
|
|
67
|
+
/* The number of buckets that have been allocated in the hash map. */
|
|
68
|
+
uint32_t capacity;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/*
|
|
72
|
+
* When we allocate constants into the pool, we reserve 0 to mean that the slot
|
|
73
|
+
* is not yet filled. This constant is reused in other places to indicate the
|
|
74
|
+
* lack of a constant id.
|
|
75
|
+
*/
|
|
76
|
+
#define PM_CONSTANT_ID_UNSET 0
|
|
77
|
+
|
|
78
|
+
/* Set the unset constant id at the specified index of a list of constant ids. */
|
|
79
|
+
void pm_constant_id_list_insert(pm_constant_id_list_t *list, uint32_t index, pm_constant_id_t id);
|
|
80
|
+
|
|
81
|
+
/*
|
|
82
|
+
* Mix the bits of a constant id so that ids which are close together, as
|
|
83
|
+
* sequentially assigned ids are, land in unrelated slots of a hash table.
|
|
84
|
+
*/
|
|
85
|
+
static PRISM_INLINE uint32_t
|
|
86
|
+
pm_constant_id_hash(pm_constant_id_t id) {
|
|
87
|
+
id = ((id >> 16) ^ id) * 0x45d9f3b;
|
|
88
|
+
id = ((id >> 16) ^ id) * 0x45d9f3b;
|
|
89
|
+
id = (id >> 16) ^ id;
|
|
90
|
+
return id;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* A set of constant ids, for where the parser only needs to know whether it has
|
|
95
|
+
* already seen a name, as with the variables a pattern captures or the named
|
|
96
|
+
* capture groups of a regular expression.
|
|
97
|
+
*/
|
|
98
|
+
typedef struct {
|
|
99
|
+
/* The number of ids in the set. */
|
|
100
|
+
size_t size;
|
|
101
|
+
|
|
102
|
+
/* The number of slots in `ids`, either zero or a power of two. */
|
|
103
|
+
size_t capacity;
|
|
104
|
+
|
|
105
|
+
/*
|
|
106
|
+
* An open-addressed table of the ids in the set, where PM_CONSTANT_ID_UNSET
|
|
107
|
+
* marks an empty slot. It is NULL until the first id is added.
|
|
108
|
+
*/
|
|
109
|
+
pm_constant_id_t *ids;
|
|
110
|
+
} pm_constant_id_set_t;
|
|
111
|
+
|
|
112
|
+
/*
|
|
113
|
+
* Add a constant id, which must not be PM_CONSTANT_ID_UNSET, to the set.
|
|
114
|
+
* Returns whether the set did not already contain it.
|
|
115
|
+
*/
|
|
116
|
+
bool pm_constant_id_set_insert(pm_arena_t *arena, pm_constant_id_set_t *set, pm_constant_id_t id);
|
|
117
|
+
|
|
118
|
+
/* Initialize a new constant pool with a given capacity. */
|
|
119
|
+
void pm_constant_pool_init(pm_arena_t *arena, pm_constant_pool_t *pool, uint32_t capacity);
|
|
120
|
+
|
|
121
|
+
/*
|
|
122
|
+
* Return the number of bytes that pm_constant_pool_init allocates for the given
|
|
123
|
+
* capacity.
|
|
124
|
+
*/
|
|
125
|
+
size_t pm_constant_pool_init_size(uint32_t capacity);
|
|
126
|
+
|
|
127
|
+
/* Return a pointer to the constant indicated by the given constant id. */
|
|
128
|
+
pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id);
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* Find a constant in a constant pool. Returns the id of the constant, or 0 if
|
|
132
|
+
* the constant is not found.
|
|
133
|
+
*/
|
|
134
|
+
pm_constant_id_t pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length);
|
|
135
|
+
|
|
136
|
+
/*
|
|
137
|
+
* Insert a constant into a constant pool that is a slice of a source string.
|
|
138
|
+
* Returns the id of the constant, or 0 if any potential calls to resize fail.
|
|
139
|
+
*/
|
|
140
|
+
pm_constant_id_t pm_constant_pool_insert_shared(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
|
|
141
|
+
|
|
142
|
+
/*
|
|
143
|
+
* Insert a constant into a constant pool from memory that is now owned by the
|
|
144
|
+
* constant pool. Returns the id of the constant, or 0 if any potential calls to
|
|
145
|
+
* resize fail.
|
|
146
|
+
*/
|
|
147
|
+
pm_constant_id_t pm_constant_pool_insert_owned(pm_arena_t *arena, pm_constant_pool_t *pool, uint8_t *start, size_t length);
|
|
148
|
+
|
|
149
|
+
/*
|
|
150
|
+
* Insert a constant into a constant pool from memory that is constant. Returns
|
|
151
|
+
* the id of the constant, or 0 if any potential calls to resize fail.
|
|
152
|
+
*/
|
|
153
|
+
pm_constant_id_t pm_constant_pool_insert_constant(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length);
|
|
154
|
+
|
|
155
|
+
#endif
|