class RBS::Parser
Constants
- KEYWORDS
- LexerError
- SemanticsError
- SyntaxError
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/sig/parser.rbs, line 145
def self._lex: (Buffer, Integer end_pos) -> Array[[Symbol, Location[untyped, untyped]]]
(Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables) → AST::Ruby::Annotations::leading_annotation
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/sig/parser.rbs, line 147
def self._parse_inline_leading_annotation: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables) -> AST::Ruby::Annotations::leading_annotation
(Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables) → AST::Ruby::Annotations::trailing_annotation
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/sig/parser.rbs, line 149
def self._parse_inline_trailing_annotation: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables) -> AST::Ruby::Annotations::trailing_annotation
(Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof) → MethodType?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/sig/parser.rbs, line 139
def self._parse_method_type: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof) -> MethodType?
(Buffer, Integer start_pos, Integer end_pos) → [ Array[AST::Directives::t], Array[AST::Declarations::t] ]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/sig/parser.rbs, line 141
def self._parse_signature: (Buffer, Integer start_pos, Integer end_pos) -> [Array[AST::Directives::t], Array[AST::Declarations::t]]
(Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof, bool void_allowed, bool self_allowed, bool classish_allowed) → Types::t?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/sig/parser.rbs, line 137
def self._parse_type: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof, bool void_allowed, bool self_allowed, bool classish_allowed) -> Types::t?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/sig/parser.rbs, line 143
def self._parse_type_params: (Buffer, Integer start_pos, Integer end_pos, bool module_type_params) -> Array[AST::TypeParam]
(String | Buffer source) → Buffer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 79 def self.buffer(source) case source when String Buffer.new(content: source, name: Pathname("a.rbs")) when Buffer source end end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 132 def self.byte_range(char_range, content) start_offset = char_range.begin end_offset = char_range.end start_prefix = content[0, start_offset] or raise if start_offset end_prefix = content[0, end_offset] or raise if end_offset start_prefix&.bytesize...end_prefix&.bytesize end
(Buffer | String) → LexResult
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 70 def self.lex(source) buf = buffer(source) list = _lex(buf, buf.content.bytesize) value = list.map do |type, location| Token.new(type: type, location: location) end LexResult.new(buffer: buf, value: value) end
Parse whole RBS file and return result.
RBS::Parser.lex("# Comment\nmodule A\nend\n").value.map(&:type) # => [:tLINECOMMENT, :kMODULE, :tUIDENT, :kEND, :pEOF]
(Buffer) → AST::Directives::ResolveTypeNames?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 46 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
Returns the magic comment from the buffer
(Buffer | String, Range[Integer?], ?variables: Array[Symbol]) → AST::Ruby::Annotations::leading_annotation
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 122 def self.parse_inline_leading_annotation(source, range, variables: []) buf = buffer(source) _parse_inline_leading_annotation(buf, range.begin || 0, range.end || buf.last_position, variables) end
Parse a leading annotation and return it
Raises an exception if the source text contains a syntax error.
(Buffer | String, Range[Integer?], ?variables: Array[Symbol]) → AST::Ruby::Annotations::trailing_annotation
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 127 def self.parse_inline_trailing_annotation(source, range, variables: []) buf = buffer(source) _parse_inline_trailing_annotation(buf, range.begin || 0, range.end || buf.last_position, variables) end
Parse a leading annotation and return it
Raises an exception if the source text contains a syntax error.
(Buffer | String, ?byte_range: Range[Integer?], ?variables: Array[Symbol], ?require_eof: bool) → MethodType?
(Buffer | String, range: Range[Integer?], ?variables: Array[Symbol], ?require_eof: bool) → MethodType?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 14 def self.parse_method_type(source, range: nil, byte_range: 0..., variables: [], require_eof: false) buf = buffer(source) byte_range = byte_range(range, buf.content) if range _parse_method_type(buf, byte_range.begin || 0, byte_range.end || buf.content.bytesize, variables, require_eof) end
Parse a method type and return it
When byte_range keyword is specified, it starts parsing from the begin to the end of the range.
RBS::Parser.parse_method_type("() -> void") # => `() -> void` RBS::Parser.parse_method_type("() -> void", byte_range: 0...) # => `() -> void` RBS::Parser.parse_method_type("() -> void () -> String", byte_range: 11...) # => `() -> String` RBS::Parser.parse_method_type("() -> void () -> String", byte_range: 23...) # => nil
When require_eof is true, an error is raised if more tokens are left in the input. (Defaults to false.)
RBS::Parser.parse_method_type("() -> void () -> String", require_eof: false) # => `() -> void` RBS::Parser.parse_method_type("() -> void () -> String", require_eof: true) # => Raises an error RBS::Parser.parse_method_type("", require_eof: true) # => nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 20 def self.parse_signature(source) buf = buffer(source) resolved = magic_comment(buf) start_pos = if resolved (resolved.location || raise).end_pos else 0 end content = buf.content dirs, decls = _parse_signature(buf, start_pos, content.bytesize) if resolved dirs = dirs.dup if dirs.frozen? dirs.unshift(resolved) end [buf, dirs, decls] end
Parse whole RBS file and return an array of declarations
(Buffer | String, ?byte_range: Range[Integer?], ?variables: Array[Symbol], ?require_eof: bool, ?void_allowed: bool, ?self_allowed: bool, ?classish_allowed: bool) → Types::t?
(Buffer | String, range: Range[Integer?], ?variables: Array[Symbol], ?require_eof: bool, ?void_allowed: bool, ?self_allowed: bool, ?classish_allowed: bool) → Types::t?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 8 def self.parse_type(source, range: nil, byte_range: 0..., variables: [], require_eof: false, void_allowed: true, self_allowed: true, classish_allowed: true) buf = buffer(source) byte_range = byte_range(range, buf.content) if range _parse_type(buf, byte_range.begin || 0, byte_range.end || buf.content.bytesize, variables, require_eof, void_allowed, self_allowed, classish_allowed) end
Parse a type and return it
When byte_range keyword is specified, it starts parsing from the begin to the end of the range.
RBS::Parser.parse_type("String") # => `String` RBS::Parser.parse_type("String", byte_range: 0...) # => `String` RBS::Parser.parse_type("String Integer", byte_range: 7...) # => `Integer` RBS::Parser.parse_type("String Integer", byte_range: 14...) # => nil
When require_eof is true, an error is raised if more tokens are left in the input. (Defaults to false.)
RBS::Parser.parse_type("String untyped", require_eof: false) # => `String` RBS::Parser.parse_type("String untyped", require_eof: true) # => Raises an error RBS::Parser.parse_type("", require_eof: true) # => nil
The void_allowed keyword controls whether void is allowed as a type.
RBS::Parser.parse_type("void", void_allowed: true) # => `void` RBS::Parser.parse_type("void", void_allowed: false) # => Raises an syntax error
The self_allowed keyword controls whether self is allowed as a type.
RBS::Parser.parse_type("self", self_allowed: true) # => `self` RBS::Parser.parse_type("self", self_allowed: false) # => Raises an syntax error
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/parser_aux.rb, line 41 def self.parse_type_params(source, module_type_params: true) buf = buffer(source) _parse_type_params(buf, 0, buf.content.bytesize, module_type_params) end
Parse a list of type parameters and return it
RBS::Parser.parse_type_params("") # => nil RBS::Parser.parse_type_params("[U, V]") # => `[:U, :V]` RBS::Parser.parse_type_params("[in U, V < Integer]") # => `[:U, :V]`
When module_type_params is false, an error is raised if unchecked, in or out are used.
RBS::Parser.parse_type_params("[unchecked U]", module_type_params: false) # => Raises an error RBS::Parser.parse_type_params("[out U]", module_type_params: false) # => Raises an error RBS::Parser.parse_type_params("[in U]", module_type_params: false) # => Raises an error