Skip to content

Commit c04a25b

Browse files
committed
Reimplement context validation on parse by struct
1 parent 55640d1 commit c04a25b

File tree

3 files changed

+133
-68
lines changed

3 files changed

+133
-68
lines changed

ext/rbs_extension/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "rbs_string_bridging.h"
88

99
#include "ruby/vm.h"
10+
#include "rbs/parser.h"
1011

1112
/**
1213
* Raises `RBS::ParsingError` or `RuntimeError` on `tok` with message constructed with given `fmt`.
@@ -101,7 +102,7 @@ static VALUE parse_type_try(VALUE a) {
101102
}
102103

103104
rbs_node_t *type;
104-
rbs_parse_type(parser, &type);
105+
rbs_parse_type(parser, &type, SkipValidation);
105106

106107
raise_error_if_any(parser, arg->buffer);
107108

@@ -187,7 +188,7 @@ static VALUE parse_method_type_try(VALUE a) {
187188
}
188189

189190
rbs_method_type_t *method_type = NULL;
190-
rbs_parse_method_type(parser, &method_type);
191+
rbs_parse_method_type(parser, &method_type, SkipValidation);
191192

192193
raise_error_if_any(parser, arg->buffer);
193194

include/rbs/parser.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,24 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line
126126

127127
void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);
128128

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);
129+
/**
130+
* TypeValidation represents the validation rules for type parsing.
131+
* It controls whether certain types are allowed in specific contexts.
132+
* */
133+
typedef struct {
134+
bool no_void; /* If true, `void` type is not allowed.*/
135+
bool no_void_allowed_here; /* If true, `void` type is not allowed, but it's allowed in one depth.*/
136+
bool no_self; /* If true, `self` type is not allowed.*/
137+
bool no_classish; /* If true, `class` or `instance` types are not allowed.*/
138+
} TypeValidation;
139+
140+
/**
141+
* SkipValidation is a TypeValidation that allows all types.
142+
* */
143+
extern const TypeValidation SkipValidation;
144+
145+
bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, TypeValidation validation);
146+
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, TypeValidation validation);
131147
bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature);
132148

133149
bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params);

0 commit comments

Comments
 (0)