rbs 3.9.2 → 4.0.0.dev.1

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 (115) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.github/workflows/windows.yml +1 -1
  4. data/CHANGELOG.md +0 -13
  5. data/Rakefile +28 -21
  6. data/Steepfile +1 -0
  7. data/config.yml +232 -62
  8. data/ext/rbs_extension/ast_translation.c +1149 -0
  9. data/ext/rbs_extension/ast_translation.h +30 -0
  10. data/{src/constants.c → ext/rbs_extension/class_constants.c} +15 -1
  11. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +10 -1
  12. data/ext/rbs_extension/extconf.rb +3 -1
  13. data/ext/rbs_extension/{location.c → legacy_location.c} +25 -34
  14. data/ext/rbs_extension/legacy_location.h +40 -0
  15. data/ext/rbs_extension/main.c +402 -8
  16. data/ext/rbs_extension/rbs_extension.h +3 -21
  17. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  18. data/ext/rbs_extension/rbs_string_bridging.h +20 -0
  19. data/include/rbs/ast.h +748 -0
  20. data/include/rbs/defines.h +60 -0
  21. data/{ext/rbs_extension → include/rbs}/lexer.h +40 -32
  22. data/include/rbs/location.h +59 -0
  23. data/include/rbs/parser.h +151 -0
  24. data/include/rbs/string.h +49 -0
  25. data/include/rbs/util/rbs_allocator.h +38 -0
  26. data/include/rbs/util/rbs_assert.h +9 -0
  27. data/include/rbs/util/rbs_buffer.h +83 -0
  28. data/include/rbs/util/rbs_constant_pool.h +3 -64
  29. data/include/rbs/util/rbs_encoding.h +280 -0
  30. data/include/rbs/util/rbs_unescape.h +23 -0
  31. data/include/rbs.h +1 -2
  32. data/lib/rbs/annotate/formatter.rb +3 -13
  33. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  34. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  35. data/lib/rbs/ast/ruby/annotations.rb +119 -0
  36. data/lib/rbs/ast/ruby/comment_block.rb +221 -0
  37. data/lib/rbs/ast/ruby/declarations.rb +86 -0
  38. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +24 -0
  39. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  40. data/lib/rbs/ast/ruby/members.rb +213 -0
  41. data/lib/rbs/buffer.rb +104 -24
  42. data/lib/rbs/cli/validate.rb +39 -34
  43. data/lib/rbs/cli.rb +4 -5
  44. data/lib/rbs/definition.rb +6 -1
  45. data/lib/rbs/definition_builder/ancestor_builder.rb +63 -60
  46. data/lib/rbs/definition_builder/method_builder.rb +45 -30
  47. data/lib/rbs/definition_builder.rb +44 -9
  48. data/lib/rbs/environment/class_entry.rb +69 -0
  49. data/lib/rbs/environment/module_entry.rb +66 -0
  50. data/lib/rbs/environment.rb +185 -154
  51. data/lib/rbs/environment_loader.rb +2 -2
  52. data/lib/rbs/errors.rb +4 -3
  53. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  54. data/lib/rbs/inline_parser.rb +206 -0
  55. data/lib/rbs/location_aux.rb +35 -3
  56. data/lib/rbs/parser_aux.rb +11 -1
  57. data/lib/rbs/prototype/runtime.rb +2 -2
  58. data/lib/rbs/source.rb +99 -0
  59. data/lib/rbs/subtractor.rb +4 -3
  60. data/lib/rbs/version.rb +1 -1
  61. data/lib/rbs.rb +12 -0
  62. data/lib/rdoc/discover.rb +1 -1
  63. data/lib/rdoc_plugin/parser.rb +2 -2
  64. data/rbs.gemspec +1 -0
  65. data/sig/ancestor_builder.rbs +1 -1
  66. data/sig/annotate/formatter.rbs +2 -2
  67. data/sig/annotate/rdoc_annotater.rbs +1 -1
  68. data/sig/ast/ruby/annotations.rbs +110 -0
  69. data/sig/ast/ruby/comment_block.rbs +119 -0
  70. data/sig/ast/ruby/declarations.rbs +60 -0
  71. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  72. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  73. data/sig/ast/ruby/members.rbs +72 -0
  74. data/sig/buffer.rbs +63 -5
  75. data/sig/definition.rbs +1 -0
  76. data/sig/definition_builder.rbs +1 -1
  77. data/sig/environment/class_entry.rbs +50 -0
  78. data/sig/environment/module_entry.rbs +50 -0
  79. data/sig/environment.rbs +22 -76
  80. data/sig/errors.rbs +13 -6
  81. data/sig/inline_parser/comment_association.rbs +71 -0
  82. data/sig/inline_parser.rbs +87 -0
  83. data/sig/location.rbs +32 -7
  84. data/sig/method_builder.rbs +7 -4
  85. data/sig/parser.rbs +16 -0
  86. data/sig/source.rbs +48 -0
  87. data/src/ast.c +1345 -0
  88. data/src/lexer.c +2867 -0
  89. data/src/lexer.re +151 -0
  90. data/{ext/rbs_extension → src}/lexstate.c +58 -42
  91. data/src/location.c +71 -0
  92. data/src/parser.c +3739 -0
  93. data/src/string.c +89 -0
  94. data/src/util/rbs_allocator.c +149 -0
  95. data/src/util/rbs_assert.c +19 -0
  96. data/src/util/rbs_buffer.c +54 -0
  97. data/src/util/rbs_constant_pool.c +13 -81
  98. data/src/util/rbs_encoding.c +5273 -0
  99. data/src/util/rbs_unescape.c +130 -0
  100. data/stdlib/rdoc/0/code_object.rbs +2 -2
  101. data/stdlib/rdoc/0/comment.rbs +2 -0
  102. data/stdlib/rdoc/0/options.rbs +76 -0
  103. data/stdlib/rdoc/0/rdoc.rbs +6 -4
  104. data/stdlib/rdoc/0/store.rbs +1 -1
  105. metadata +70 -17
  106. data/ext/rbs_extension/lexer.c +0 -2728
  107. data/ext/rbs_extension/lexer.re +0 -147
  108. data/ext/rbs_extension/location.h +0 -85
  109. data/ext/rbs_extension/parser.c +0 -2982
  110. data/ext/rbs_extension/parser.h +0 -18
  111. data/ext/rbs_extension/parserstate.c +0 -411
  112. data/ext/rbs_extension/parserstate.h +0 -163
  113. data/ext/rbs_extension/unescape.c +0 -32
  114. data/include/rbs/ruby_objs.h +0 -72
  115. data/src/ruby_objs.c +0 -799
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @file defines.h
3
+ *
4
+ * Macro definitions used throughout the rbs library.
5
+ *
6
+ * This file should be included first by any *.h or *.c in rbs for consistency
7
+ * and to ensure that the macros are defined before they are used.
8
+ */
9
+
10
+ #ifndef RBS_DEFINES_H
11
+ #define RBS_DEFINES_H
12
+
13
+
14
+ /***********************************************************************************************************************
15
+ * Copied+modified subset of Prism's `include/prism/defines.h` *
16
+ **********************************************************************************************************************/
17
+
18
+ /**
19
+ * Certain compilers support specifying that a function accepts variadic
20
+ * parameters that look like printf format strings to provide a better developer
21
+ * experience when someone is using the function. This macro does that in a
22
+ * compiler-agnostic way.
23
+ */
24
+ #if defined(__GNUC__)
25
+ # if defined(__MINGW_PRINTF_FORMAT)
26
+ # define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index)))
27
+ # else
28
+ # define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index)))
29
+ # endif
30
+ #elif defined(__clang__)
31
+ # define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index)))
32
+ #else
33
+ # define RBS_ATTRIBUTE_FORMAT(string_index, argument_index)
34
+ #endif
35
+
36
+ /**
37
+ * We use -Wimplicit-fallthrough to guard potentially unintended fall-through between cases of a switch.
38
+ * Use RBS_FALLTHROUGH to explicitly annotate cases where the fallthrough is intentional.
39
+ */
40
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L // C23 or later
41
+ #define RBS_FALLTHROUGH [[fallthrough]];
42
+ #elif defined(__GNUC__) || defined(__clang__)
43
+ #define RBS_FALLTHROUGH __attribute__((fallthrough));
44
+ #elif defined(_MSC_VER)
45
+ #define RBS_FALLTHROUGH __fallthrough;
46
+ #else
47
+ #define RBS_FALLTHROUGH
48
+ #endif
49
+
50
+ /***********************************************************************************************************************
51
+ * Custom defines for RBS *
52
+ **********************************************************************************************************************/
53
+
54
+ #if defined(_MSC_VER)
55
+ #define NODISCARD _Check_return_
56
+ #else
57
+ #define NODISCARD __attribute__((warn_unused_result))
58
+ #endif
59
+
60
+ #endif
@@ -1,7 +1,10 @@
1
1
  #ifndef RBS__LEXER_H
2
2
  #define RBS__LEXER_H
3
3
 
4
- enum TokenType {
4
+ #include "string.h"
5
+ #include "util/rbs_encoding.h"
6
+
7
+ enum RBSTokenType {
5
8
  NullType, /* (Nothing) */
6
9
  pEOF, /* EOF */
7
10
  ErrorToken, /* Error */
@@ -61,6 +64,9 @@ enum TokenType {
61
64
  kUSE, /* use */
62
65
  kAS, /* as */
63
66
  k__TODO__, /* __todo__ */
67
+ kATRBS, /* @rbs */
68
+ kSKIP, /* skip */
69
+ kRETURN, /* return */
64
70
 
65
71
  tLIDENT, /* Identifiers starting with lower case */
66
72
  tUIDENT, /* Identifiers starting with upper case */
@@ -77,6 +83,7 @@ enum TokenType {
77
83
 
78
84
  tCOMMENT, /* Comment */
79
85
  tLINECOMMENT, /* Comment of all line */
86
+ tINLINECOMMENT, /* Comment in inline decl starting with -- */
80
87
 
81
88
  tTRIVIA, /* Trivia tokens -- space and new line */
82
89
 
@@ -100,17 +107,17 @@ typedef struct {
100
107
  int char_pos;
101
108
  int line;
102
109
  int column;
103
- } position;
110
+ } rbs_position_t;
104
111
 
105
112
  typedef struct {
106
- position start;
107
- position end;
108
- } range;
113
+ rbs_position_t start;
114
+ rbs_position_t end;
115
+ } rbs_range_t;
109
116
 
110
117
  typedef struct {
111
- enum TokenType type;
112
- range range;
113
- } token;
118
+ enum RBSTokenType type;
119
+ rbs_range_t range;
120
+ } rbs_token_t;
114
121
 
115
122
  /**
116
123
  * The lexer state is the curren token.
@@ -123,57 +130,58 @@ typedef struct {
123
130
  * ```
124
131
  * */
125
132
  typedef struct {
126
- VALUE string;
133
+ rbs_string_t string;
127
134
  int start_pos; /* The character position that defines the start of the input */
128
135
  int end_pos; /* The character position that defines the end of the input */
129
- position current; /* The current position */
130
- position start; /* The start position of the current token */
136
+ rbs_position_t current; /* The current position */
137
+ rbs_position_t start; /* The start position of the current token */
131
138
  bool first_token_of_line; /* This flag is used for tLINECOMMENT */
132
139
  unsigned int last_char; /* Last peeked character */
133
- } lexstate;
140
+ const rbs_encoding_t *encoding;
141
+ } rbs_lexer_t;
134
142
 
135
- extern token NullToken;
136
- extern position NullPosition;
137
- extern range NULL_RANGE;
143
+ extern rbs_token_t NullToken;
144
+ extern rbs_position_t NullPosition;
145
+ extern rbs_range_t NULL_RANGE;
138
146
 
139
- char *peek_token(lexstate *state, token tok);
140
- int token_chars(token tok);
141
- int token_bytes(token tok);
147
+ char *rbs_peek_token(rbs_lexer_t *lexer, rbs_token_t tok);
148
+ int rbs_token_chars(rbs_token_t tok);
149
+ int rbs_token_bytes(rbs_token_t tok);
142
150
 
143
- #define null_position_p(pos) (pos.byte_pos == -1)
144
- #define null_range_p(range) (range.start.byte_pos == -1)
145
- #define nonnull_pos_or(pos1, pos2) (null_position_p(pos1) ? pos2 : pos1)
146
- #define RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos)
151
+ #define rbs_null_position_p(pos) (pos.byte_pos == -1)
152
+ #define rbs_null_range_p(range) (range.start.byte_pos == -1)
153
+ #define rbs_nonnull_pos_or(pos1, pos2) (rbs_null_position_p(pos1) ? pos2 : pos1)
154
+ #define RBS_RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos)
147
155
 
148
- const char *token_type_str(enum TokenType type);
156
+ const char *rbs_token_type_str(enum RBSTokenType type);
149
157
 
150
158
  /**
151
159
  * Read next character.
152
160
  * */
153
- unsigned int peek(lexstate *state);
161
+ unsigned int rbs_peek(rbs_lexer_t *lexer);
154
162
 
155
163
  /**
156
164
  * Skip one character.
157
165
  * */
158
- void rbs_skip(lexstate *state);
166
+ void rbs_skip(rbs_lexer_t *lexer);
159
167
 
160
168
  /**
161
169
  * Skip n characters.
162
170
  * */
163
- void skipn(lexstate *state, size_t size);
171
+ void rbs_skipn(rbs_lexer_t *lexer, size_t size);
164
172
 
165
173
  /**
166
- * Return new token with given type.
174
+ * Return new rbs_token_t with given type.
167
175
  * */
168
- token next_token(lexstate *state, enum TokenType type);
176
+ rbs_token_t rbs_next_token(rbs_lexer_t *lexer, enum RBSTokenType type);
169
177
 
170
178
  /**
171
- * Return new token with EOF type.
179
+ * Return new rbs_token_t with EOF type.
172
180
  * */
173
- token next_eof_token(lexstate *state);
181
+ rbs_token_t rbs_next_eof_token(rbs_lexer_t *lexer);
174
182
 
175
- token rbsparser_next_token(lexstate *state);
183
+ rbs_token_t rbs_lexer_next_token(rbs_lexer_t *lexer);
176
184
 
177
- void print_token(token tok);
185
+ void rbs_print_token(rbs_token_t tok);
178
186
 
179
187
  #endif
@@ -0,0 +1,59 @@
1
+ #ifndef RBS__RBS_LOCATION_H
2
+ #define RBS__RBS_LOCATION_H
3
+
4
+ #include "lexer.h"
5
+
6
+ #include "rbs/util/rbs_constant_pool.h"
7
+ #include "rbs/util/rbs_allocator.h"
8
+
9
+ typedef struct {
10
+ int start;
11
+ int end;
12
+ } rbs_loc_range;
13
+
14
+ typedef struct {
15
+ rbs_constant_id_t name;
16
+ rbs_loc_range rg;
17
+ } rbs_loc_entry;
18
+
19
+ typedef unsigned int rbs_loc_entry_bitmap;
20
+
21
+ // The flexible array always allocates, but it's okay.
22
+ // This struct is not allocated when the `rbs_loc` doesn't have children.
23
+ typedef struct {
24
+ unsigned short len;
25
+ unsigned short cap;
26
+ rbs_loc_entry_bitmap required_p;
27
+ rbs_loc_entry entries[1];
28
+ } rbs_loc_children;
29
+
30
+ typedef struct rbs_location {
31
+ rbs_range_t rg;
32
+ rbs_loc_children *children;
33
+ } rbs_location_t;
34
+
35
+ typedef struct rbs_location_list_node {
36
+ rbs_location_t *loc;
37
+ struct rbs_location_list_node *next;
38
+ } rbs_location_list_node_t;
39
+
40
+ typedef struct rbs_location_list {
41
+ rbs_allocator_t *allocator;
42
+ rbs_location_list_node_t *head;
43
+ rbs_location_list_node_t *tail;
44
+ size_t length;
45
+ } rbs_location_list_t;
46
+
47
+ void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, size_t capacity);
48
+ void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r);
49
+ void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r);
50
+
51
+ /**
52
+ * Allocate new rbs_location_t object through the given allocator.
53
+ * */
54
+ rbs_location_t *rbs_location_new(rbs_allocator_t *, rbs_range_t rg);
55
+
56
+ rbs_location_list_t *rbs_location_list_new(rbs_allocator_t *allocator);
57
+ void rbs_location_list_append(rbs_location_list_t *list, rbs_location_t *loc);
58
+
59
+ #endif
@@ -0,0 +1,151 @@
1
+ #ifndef RBS__PARSER_H
2
+ #define RBS__PARSER_H
3
+
4
+ #include "rbs/defines.h"
5
+ #include "rbs/util/rbs_allocator.h"
6
+ #include "rbs/util/rbs_constant_pool.h"
7
+ #include "rbs/lexer.h"
8
+ #include "rbs/ast.h"
9
+
10
+ #include <stdbool.h>
11
+ #include <stddef.h>
12
+
13
+ /**
14
+ * comment represents a sequence of comment lines.
15
+ *
16
+ * # Comment for the method.
17
+ * #
18
+ * # ```rb
19
+ * # object.foo() # Do something
20
+ * # ```
21
+ * #
22
+ * def foo: () -> void
23
+ *
24
+ * A comment object represents the six lines of comments.
25
+ * */
26
+ typedef struct rbs_comment_t {
27
+ rbs_position_t start;
28
+ rbs_position_t end;
29
+
30
+ size_t line_size;
31
+ size_t line_count;
32
+ rbs_token_t *tokens;
33
+
34
+ struct rbs_comment_t *next_comment;
35
+ } rbs_comment_t;
36
+
37
+ typedef struct rbs_error_t {
38
+ char *message;
39
+ rbs_token_t token;
40
+ bool syntax_error;
41
+ } rbs_error_t;
42
+
43
+ /**
44
+ * An RBS parser is a LL(3) parser.
45
+ * */
46
+ typedef struct {
47
+ rbs_lexer_t *rbs_lexer_t;
48
+
49
+ rbs_token_t current_token;
50
+ rbs_token_t next_token; /* The first lookahead token */
51
+ rbs_token_t next_token2; /* The second lookahead token */
52
+ rbs_token_t next_token3; /* The third lookahead token */
53
+
54
+ struct id_table *vars; /* Known type variables */
55
+ rbs_comment_t *last_comment; /* Last read comment */
56
+
57
+ rbs_constant_pool_t constant_pool;
58
+ rbs_allocator_t *allocator;
59
+ rbs_error_t *error;
60
+ } rbs_parser_t;
61
+
62
+ /**
63
+ * Insert new table entry.
64
+ * Setting `reset` inserts a _reset_ entry, which stops searching.
65
+ *
66
+ * ```
67
+ * class Foo[A]
68
+ * ^^^ <= push new table with reset
69
+ * def foo: [B] () -> [A, B]
70
+ * ^^^ <= push new table without reset
71
+ *
72
+ * class Baz[C]
73
+ * ^^^ <= push new table with reset
74
+ * end
75
+ * end
76
+ * ```
77
+ * */
78
+ void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset);
79
+
80
+ /**
81
+ * Insert new type variable into the latest table.
82
+ * */
83
+ NODISCARD bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id);
84
+
85
+ /**
86
+ * Allocate new rbs_lexer_t object.
87
+ *
88
+ * ```
89
+ * VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
90
+ * rbs_lexer_new(string, 0, 31) // New rbs_lexer_t with buffer content
91
+ * ```
92
+ * */
93
+ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos);
94
+
95
+ /**
96
+ * Allocate new rbs_parser_t object.
97
+ *
98
+ * ```
99
+ * rbs_parser_new(buffer, string, encoding, 0, 1);
100
+ * ```
101
+ * */
102
+ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos);
103
+ void rbs_parser_free(rbs_parser_t *parser);
104
+
105
+ /**
106
+ * Advance one token.
107
+ * */
108
+ void rbs_parser_advance(rbs_parser_t *parser);
109
+
110
+ void rbs_parser_print(rbs_parser_t *parser);
111
+
112
+ /**
113
+ * Returns a RBS::Comment object associated with an subject at `subject_line`.
114
+ *
115
+ * ```rbs
116
+ * # Comment1
117
+ * class Foo # This is the subject line for Comment1
118
+ *
119
+ * # Comment2
120
+ * %a{annotation} # This is the subject line for Comment2
121
+ * def foo: () -> void
122
+ * end
123
+ * ```
124
+ * */
125
+ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line);
126
+
127
+ void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);
128
+
129
+ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type);
130
+ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type);
131
+ bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature);
132
+
133
+ /**
134
+ * Parse an inline leading annotation from a string.
135
+ *
136
+ * @param parser The parser to use
137
+ * @param annotation Pointer to store the resulting annotation
138
+ * @return true if parsing succeeded, false otherwise
139
+ */
140
+ bool rbs_parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation);
141
+
142
+ /**
143
+ * Parse an inline trailing annotation from a string.
144
+ *
145
+ * @param parser The parser to use
146
+ * @param annotation Pointer to store the resulting annotation
147
+ * @return true if parsing succeeded, false otherwise
148
+ */
149
+ bool rbs_parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation);
150
+
151
+ #endif
@@ -0,0 +1,49 @@
1
+ #ifndef RBS__RBS_STRING_H
2
+ #define RBS__RBS_STRING_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdbool.h>
6
+ #include "rbs/util/rbs_allocator.h"
7
+
8
+ typedef struct {
9
+ const char *start;
10
+ const char *end;
11
+ } rbs_string_t;
12
+
13
+ #define RBS_STRING_NULL ((rbs_string_t) { \
14
+ .start = NULL, \
15
+ .end = NULL, \
16
+ })
17
+
18
+ /**
19
+ * Returns a new `rbs_string_t` struct
20
+ */
21
+ rbs_string_t rbs_string_new(const char *start, const char *end);
22
+
23
+ /**
24
+ * Copies a portion of the input string into a new owned string.
25
+ * @param start_inset Number of characters to exclude from the start
26
+ * @param length Number of characters to include
27
+ * @return A new owned string that will be freed when the allocator is freed.
28
+ */
29
+ rbs_string_t rbs_string_copy_slice(rbs_allocator_t *, rbs_string_t *self, size_t start_inset, size_t length);
30
+
31
+ /**
32
+ * Drops the leading and trailing whitespace from the given string, in-place.
33
+ * @returns A new string that provides a view into the original string `self`.
34
+ */
35
+ rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self);
36
+
37
+ /**
38
+ * Returns the length of the string.
39
+ */
40
+ size_t rbs_string_len(const rbs_string_t self);
41
+
42
+ /**
43
+ * Compares two strings for equality.
44
+ */
45
+ bool rbs_string_equal(const rbs_string_t lhs, const rbs_string_t rhs);
46
+
47
+ unsigned int rbs_utf8_string_to_codepoint(const rbs_string_t string);
48
+
49
+ #endif
@@ -0,0 +1,38 @@
1
+ #ifndef RBS_ALLOCATOR_H
2
+ #define RBS_ALLOCATOR_H
3
+
4
+ #include <stddef.h>
5
+
6
+ #ifndef alignof
7
+ #if defined(__GNUC__) || defined(__clang__)
8
+ #define alignof(type) __alignof__(type)
9
+ #elif defined(_MSC_VER)
10
+ #define alignof(type) __alignof(type)
11
+ #else
12
+ // Fallback using offset trick
13
+ #define alignof(type) offsetof(struct { char c; type member; }, member)
14
+ #endif
15
+ #endif
16
+
17
+ struct rbs_allocator;
18
+ typedef struct rbs_allocator rbs_allocator_t;
19
+
20
+ rbs_allocator_t *rbs_allocator_init(void);
21
+ void rbs_allocator_free(rbs_allocator_t *);
22
+ void *rbs_allocator_malloc_impl (rbs_allocator_t *, /* 1 */ size_t size, size_t alignment);
23
+ void *rbs_allocator_malloc_many_impl (rbs_allocator_t *, size_t count, size_t size, size_t alignment);
24
+ void *rbs_allocator_calloc_impl (rbs_allocator_t *, size_t count, size_t size, size_t alignment);
25
+
26
+ void *rbs_allocator_realloc_impl (rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
27
+
28
+ // Use this when allocating memory for a single instance of a type.
29
+ #define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type)))
30
+ // Use this when allocating memory that will be immediately written to in full.
31
+ // Such as allocating strings
32
+ #define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), alignof(type)))
33
+ // Use this when allocating memory that will NOT be immediately written to in full.
34
+ // Such as allocating buffers
35
+ #define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type)))
36
+ #define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type)))
37
+
38
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef RBS_ASSERT_H
2
+ #define RBS_ASSERT_H
3
+
4
+ #include "rbs/defines.h"
5
+ #include <stdbool.h>
6
+
7
+ void rbs_assert(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3);
8
+
9
+ #endif
@@ -0,0 +1,83 @@
1
+ #ifndef RBS__RBS_BUFFER_H
2
+ #define RBS__RBS_BUFFER_H
3
+
4
+ #include "rbs/util/rbs_allocator.h"
5
+ #include "rbs/string.h"
6
+
7
+ #include <stdbool.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /**
12
+ * The default capacity of a rbs_buffer_t.
13
+ * If the buffer needs to grow beyond this capacity, it will be doubled.
14
+ */
15
+ #define RBS_BUFFER_DEFAULT_CAPACITY 128
16
+
17
+ /**
18
+ * A rbs_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
19
+ */
20
+ typedef struct {
21
+ /** The length of the buffer in bytes. */
22
+ size_t length;
23
+
24
+ /** The capacity of the buffer in bytes that has been allocated. */
25
+ size_t capacity;
26
+
27
+ /** A pointer to the start of the buffer. */
28
+ char *value;
29
+ } rbs_buffer_t;
30
+
31
+ /**
32
+ * Initialize a rbs_buffer_t with its default values.
33
+ *
34
+ * @param allocator The allocator to use.
35
+ * @param buffer The buffer to initialize.
36
+ * @returns True if the buffer was initialized successfully, false otherwise.
37
+ */
38
+ bool rbs_buffer_init(rbs_allocator_t *, rbs_buffer_t *buffer);
39
+
40
+ /**
41
+ * Return the value of the buffer.
42
+ *
43
+ * @param buffer The buffer to get the value of.
44
+ * @returns The value of the buffer.
45
+ */
46
+ char *rbs_buffer_value(const rbs_buffer_t *buffer);
47
+
48
+ /**
49
+ * Return the length of the buffer.
50
+ *
51
+ * @param buffer The buffer to get the length of.
52
+ * @returns The length of the buffer.
53
+ */
54
+ size_t rbs_buffer_length(const rbs_buffer_t *buffer);
55
+
56
+ /**
57
+ * Append a C string to the buffer.
58
+ *
59
+ * @param allocator The allocator to use.
60
+ * @param buffer The buffer to append to.
61
+ * @param value The C string to append.
62
+ */
63
+ void rbs_buffer_append_cstr(rbs_allocator_t *, rbs_buffer_t *buffer, const char *value);
64
+
65
+ /**
66
+ * Append a string to the buffer.
67
+ *
68
+ * @param allocator The allocator to use.
69
+ * @param buffer The buffer to append to.
70
+ * @param value The string to append.
71
+ * @param length The length of the string to append.
72
+ */
73
+ void rbs_buffer_append_string(rbs_allocator_t *, rbs_buffer_t *buffer, const char *value, size_t length);
74
+
75
+ /**
76
+ * Convert the buffer to a rbs_string_t.
77
+ *
78
+ * @param buffer The buffer to convert.
79
+ * @returns The converted rbs_string_t.
80
+ */
81
+ rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer);
82
+
83
+ #endif
@@ -10,6 +10,8 @@
10
10
  #ifndef RBS_CONSTANT_POOL_H
11
11
  #define RBS_CONSTANT_POOL_H
12
12
 
13
+ #include "rbs/util/rbs_encoding.h"
14
+
13
15
  #include <assert.h>
14
16
  #include <stdbool.h>
15
17
  #include <stdint.h>
@@ -28,70 +30,6 @@
28
30
  */
29
31
  typedef uint32_t rbs_constant_id_t;
30
32
 
31
- /**
32
- * A list of constant IDs. Usually used to represent a set of locals.
33
- */
34
- typedef struct {
35
- /** The number of constant ids in the list. */
36
- size_t size;
37
-
38
- /** The number of constant ids that have been allocated in the list. */
39
- size_t capacity;
40
-
41
- /** The constant ids in the list. */
42
- rbs_constant_id_t *ids;
43
- } rbs_constant_id_list_t;
44
-
45
- /**
46
- * Initialize a list of constant ids.
47
- *
48
- * @param list The list to initialize.
49
- */
50
- void rbs_constant_id_list_init(rbs_constant_id_list_t *list);
51
-
52
- /**
53
- * Initialize a list of constant ids with a given capacity.
54
- *
55
- * @param list The list to initialize.
56
- * @param capacity The initial capacity of the list.
57
- */
58
- void rbs_constant_id_list_init_capacity(rbs_constant_id_list_t *list, size_t capacity);
59
-
60
- /**
61
- * Append a constant id to a list of constant ids. Returns false if any
62
- * potential reallocations fail.
63
- *
64
- * @param list The list to append to.
65
- * @param id The id to append.
66
- * @return Whether the append succeeded.
67
- */
68
- bool rbs_constant_id_list_append(rbs_constant_id_list_t *list, rbs_constant_id_t id);
69
-
70
- /**
71
- * Insert a constant id into a list of constant ids at the specified index.
72
- *
73
- * @param list The list to insert into.
74
- * @param index The index at which to insert.
75
- * @param id The id to insert.
76
- */
77
- void rbs_constant_id_list_insert(rbs_constant_id_list_t *list, size_t index, rbs_constant_id_t id);
78
-
79
- /**
80
- * Checks if the current constant id list includes the given constant id.
81
- *
82
- * @param list The list to check.
83
- * @param id The id to check for.
84
- * @return Whether the list includes the given id.
85
- */
86
- bool rbs_constant_id_list_includes(rbs_constant_id_list_t *list, rbs_constant_id_t id);
87
-
88
- /**
89
- * Free the memory associated with a list of constant ids.
90
- *
91
- * @param list The list to free.
92
- */
93
- void rbs_constant_id_list_free(rbs_constant_id_list_t *list);
94
-
95
33
  /**
96
34
  * The type of bucket in the constant pool hash map. This determines how the
97
35
  * bucket should be freed.
@@ -185,6 +123,7 @@ rbs_constant_id_t rbs_constant_pool_find(const rbs_constant_pool_t *pool, const
185
123
  * @return The id of the constant.
186
124
  */
187
125
  rbs_constant_id_t rbs_constant_pool_insert_shared(rbs_constant_pool_t *pool, const uint8_t *start, size_t length);
126
+ rbs_constant_id_t rbs_constant_pool_insert_shared_with_encoding(rbs_constant_pool_t *pool, const uint8_t *start, size_t length, const rbs_encoding_t *encoding);
188
127
 
189
128
  /**
190
129
  * Insert a constant into a constant pool from memory that is now owned by the