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



2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
# File 'ext/rbs_extension/parser.c', line 2961

static VALUE
rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
  VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
  StringValue(string);
  lexstate *lexer = alloc_lexer(string, 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



2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
# File 'ext/rbs_extension/parser.c', line 2931

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);
  lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
  parserstate *parser = alloc_parser(buffer, lexer, 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



2951
2952
2953
2954
2955
2956
2957
2958
2959
# File 'ext/rbs_extension/parser.c', line 2951

static VALUE
rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE end_pos)
{
  VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
  StringValue(string);
  lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos));
  parserstate *parser = alloc_parser(buffer, lexer, 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



2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
# File 'ext/rbs_extension/parser.c', line 2900

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);
  lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
  parserstate *parser = alloc_parser(buffer, lexer, 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