Class: RBS::Parser
- Inherits:
-
Object
- Object
- RBS::Parser
- Defined in:
- lib/rbs/parser_aux.rb,
lib/rbs/parser/token.rb,
lib/rbs/parser/lex_result.rb,
ext/rbs_extension/main.c
Defined Under Namespace
Constant Summary collapse
- KEYWORDS =
%w( bool bot class instance interface nil self singleton top void type unchecked in out end def include extend prepend alias module attr_reader attr_writer attr_accessor public private untyped true false ).each_with_object({}) do |keyword, hash| #$ Hash[String, bot] hash[keyword] = _ = nil end
Class Method Summary collapse
- ._lex(buffer, end_pos) ⇒ Object
- ._parse_method_type(buffer, start_pos, end_pos, variables, require_eof) ⇒ Object
- ._parse_signature(buffer, start_pos, end_pos) ⇒ Object
- ._parse_type(buffer, start_pos, end_pos, variables, require_eof) ⇒ Object
- ._parse_type_params(buffer, start_pos, end_pos, module_type_params) ⇒ Object
- .buffer(source) ⇒ Object
- .lex(source) ⇒ Object
- .magic_comment(buf) ⇒ Object
- .parse_method_type(source, range: 0, variables: [], require_eof: false) ⇒ Object
- .parse_signature(source) ⇒ Object
- .parse_type(source, range: 0, variables: [], require_eof: false) ⇒ Object
- .parse_type_params(source, module_type_params: true) ⇒ Object
Class Method Details
._lex(buffer, end_pos) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'ext/rbs_extension/main.c', line 311 static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); rb_encoding *encoding = rb_enc_get(string); rbs_allocator_t *allocator = rbs_allocator_init(); rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos)); VALUE results = rb_ary_new(); rbs_token_t token = NullToken; while (token.type != pEOF) { token = rbs_lexer_next_token(lexer); VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type))); VALUE location = rbs_new_location(buffer, token.range); VALUE pair = rb_ary_new3(2, type, location); rb_ary_push(results, pair); } rbs_allocator_free(allocator); RB_GC_GUARD(string); return results; } |
._parse_method_type(buffer, start_pos, end_pos, variables, require_eof) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'ext/rbs_extension/main.c', line 203 static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); rb_encoding *encoding = rb_enc_get(string); rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); declare_type_variables(parser, variables, buffer); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, .parser = parser, .require_eof = require_eof }; VALUE result = rb_ensure(parse_method_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser); RB_GC_GUARD(string); return result; } |
._parse_signature(buffer, start_pos, end_pos) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'ext/rbs_extension/main.c', line 242 static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); rb_encoding *encoding = rb_enc_get(string); rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, .parser = parser, .require_eof = false }; VALUE result = rb_ensure(parse_signature_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser); RB_GC_GUARD(string); return result; } |
._parse_type(buffer, start_pos, end_pos, variables, require_eof) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'ext/rbs_extension/main.c', line 160 static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); rb_encoding *encoding = rb_enc_get(string); rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); declare_type_variables(parser, variables, buffer); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, .parser = parser, .require_eof = require_eof }; VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser); RB_GC_GUARD(string); return result; } |
._parse_type_params(buffer, start_pos, end_pos, module_type_params) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'ext/rbs_extension/main.c', line 291 static VALUE rbsparser_parse_type_params(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE module_type_params) { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); rb_encoding *encoding = rb_enc_get(string); rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); struct parse_type_params_arg arg = { .buffer = buffer, .encoding = encoding, .parser = parser, .module_type_params = module_type_params }; VALUE result = rb_ensure(parse_type_params_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser); RB_GC_GUARD(string); return result; } |
.buffer(source) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/rbs/parser_aux.rb', line 76 def self.buffer(source) case source when String Buffer.new(content: source, name: "a.rbs") when Buffer source end end |
.lex(source) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/rbs/parser_aux.rb', line 67 def self.lex(source) buf = buffer(source) list = _lex(buf, buf.last_position) value = list.map do |type, location| Token.new(type: type, location: location) end LexResult.new(buffer: buf, value: value) end |
.magic_comment(buf) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rbs/parser_aux.rb', line 43 def self.magic_comment(buf) start_pos = 0 while true case when match = /\A#\s*(?<keyword>resolve-type-names)\s*(?<colon>:)\s+(?<value>true|false)$/.match(buf.content, start_pos) value = match[:value] or raise kw_offset = match.offset(:keyword) #: [Integer, Integer] colon_offset = match.offset(:colon) #: [Integer, Integer] value_offset = match.offset(:value) #: [Integer, Integer] location = Location.new(buf, kw_offset[0], value_offset[1]) location.add_required_child(:keyword, kw_offset[0]...kw_offset[1]) location.add_required_child(:colon, colon_offset[0]...colon_offset[1]) location.add_required_child(:value, value_offset[0]...value_offset[1]) return AST::Directives::ResolveTypeNames.new(value: value == "true", location: location) else return end end end |
.parse_method_type(source, range: 0, variables: [], require_eof: false) ⇒ Object
13 14 15 16 |
# File 'lib/rbs/parser_aux.rb', line 13 def self.parse_method_type(source, range: 0..., variables: [], require_eof: false) buf = buffer(source) _parse_method_type(buf, range.begin || 0, range.end || buf.last_position, variables, require_eof) end |
.parse_signature(source) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rbs/parser_aux.rb', line 18 def self.parse_signature(source) buf = buffer(source) resolved = magic_comment(buf) start_pos = if resolved (resolved.location || raise).end_pos else 0 end dirs, decls = _parse_signature(buf, start_pos, buf.last_position) if resolved dirs = dirs.dup if dirs.frozen? dirs.unshift(resolved) end [buf, dirs, decls] end |
.parse_type(source, range: 0, variables: [], require_eof: false) ⇒ Object
8 9 10 11 |
# File 'lib/rbs/parser_aux.rb', line 8 def self.parse_type(source, range: 0..., variables: [], require_eof: false) buf = buffer(source) _parse_type(buf, range.begin || 0, range.end || buf.last_position, variables, require_eof) end |
.parse_type_params(source, module_type_params: true) ⇒ Object
38 39 40 41 |
# File 'lib/rbs/parser_aux.rb', line 38 def self.parse_type_params(source, module_type_params: true) buf = buffer(source) _parse_type_params(buf, 0, buf.last_position, module_type_params) end |