Class: RBS::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/parser_aux.rb,
lib/rbs/parser/token.rb,
lib/rbs/parser/lex_result.rb,
ext/rbs_extension/parser.c

Defined Under Namespace

Classes: LexResult, Token

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[keyword] = nil
end

Class Method Summary collapse

Class Method Details

._lex(buffer, end_pos) ⇒ Object



2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
# File 'ext/rbs_extension/parser.c', line 2888

static VALUE
rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
  lexstate *lexer = alloc_lexer(buffer, 0, FIX2INT(end_pos));
  VALUE results = rb_ary_new();

  token token = NullToken;
  while (token.type != pEOF) {
    token = rbsparser_next_token(lexer);
    VALUE type = ID2SYM(rb_intern(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);
  }

  free(lexer);

  return results;
}

._parse_method_type(buffer, start_pos, end_pos, variables, require_eof) ⇒ Object



2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
# File 'ext/rbs_extension/parser.c', line 2864

static VALUE
rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
{
  parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
  struct parse_type_arg arg = {
    parser,
    require_eof
  };
  return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
}

._parse_signature(buffer, end_pos) ⇒ Object



2881
2882
2883
2884
2885
2886
# File 'ext/rbs_extension/parser.c', line 2881

static VALUE
rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE end_pos)
{
  parserstate *parser = alloc_parser(buffer, 0, FIX2INT(end_pos), Qnil);
  return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser);
}

._parse_type(buffer, start_pos, end_pos, variables, require_eof) ⇒ Object



2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
# File 'ext/rbs_extension/parser.c', line 2836

static VALUE
rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
{
  parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
  struct parse_type_arg arg = {
    parser,
    require_eof
  };
  return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser);
}

.buffer(source) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/rbs/parser_aux.rb', line 34

def self.buffer(source)
  case source
  when String
    Buffer.new(content: source, name: "a.rbs")
  when Buffer
    source
  end
end

.lex(source) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rbs/parser_aux.rb', line 25

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

.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
# File 'lib/rbs/parser_aux.rb', line 18

def self.parse_signature(source)
  buf = buffer(source)
  dirs, decls = _parse_signature(buf, buf.last_position)

  [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