class RBS::Parser
WebAssembly-backed implementation of the parser primitives.
On CRuby these come from the C extension (ext/rbs_extension/main.c). JRuby loads this instead: it runs the parser inside WebAssembly, then rebuilds the AST with RBS::WASM::Deserializer. rbs/parser_aux.rb layers the public RBS::Parser API on top, exactly as it does for the C extension.
Constants
- KEYWORDS
- LexerError
- SemanticsError
- SyntaxError
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/parser.rb, line 53 def _lex(buffer, end_pos) encoding = buffer.content.encoding.name _success, bytes = WASM::Runtime.instance.lex(buffer.content, encoding, end_pos) WASM::Deserializer.deserialize_tokens(bytes, buffer) end
(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.1.3/lib/rbs/wasm/parser.rb, line 60 def _parse_inline_leading_annotation(buffer, start_pos, end_pos, variables) validate_position_range(start_pos, end_pos) validate_variables(variables) encoding = buffer.content.encoding.name success, bytes = WASM::Runtime.instance.parse_inline_leading_annotation(buffer.content, encoding, start_pos, end_pos, variables) raise_parsing_error(buffer, bytes) unless success deserialize_or_nil(bytes, buffer) end
(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.1.3/lib/rbs/wasm/parser.rb, line 70 def _parse_inline_trailing_annotation(buffer, start_pos, end_pos, variables) validate_position_range(start_pos, end_pos) validate_variables(variables) encoding = buffer.content.encoding.name success, bytes = WASM::Runtime.instance.parse_inline_trailing_annotation(buffer.content, encoding, start_pos, end_pos, variables) raise_parsing_error(buffer, bytes) unless success deserialize_or_nil(bytes, buffer) end
(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.1.3/lib/rbs/wasm/parser.rb, line 34 def _parse_method_type(buffer, start_pos, end_pos, variables, require_eof) validate_position_range(start_pos, end_pos) validate_variables(variables) encoding = buffer.content.encoding.name success, bytes = WASM::Runtime.instance.parse_method_type(buffer.content, encoding, start_pos, end_pos, variables, require_eof) raise_parsing_error(buffer, bytes) unless success deserialize_or_nil(bytes, buffer) end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/sig/parser.rbs, line 149
def self._parse_method_type_to_bytes: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof) -> String?
(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.1.3/lib/rbs/wasm/parser.rb, line 15 def _parse_signature(buffer, start_pos, end_pos) validate_position_range(start_pos, end_pos) encoding = buffer.content.encoding.name success, bytes = WASM::Runtime.instance.parse_signature(buffer.content, encoding, start_pos, end_pos) raise_parsing_error(buffer, bytes) unless success WASM::Deserializer.deserialize(bytes, buffer) end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/sig/parser.rbs, line 151
def self._parse_signature_to_bytes: (Buffer, Integer start_pos, Integer end_pos) -> String
(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.1.3/lib/rbs/wasm/parser.rb, line 24 def _parse_type(buffer, start_pos, end_pos, variables, require_eof, void_allowed, self_allowed, classish_allowed) validate_position_range(start_pos, end_pos) validate_variables(variables) encoding = buffer.content.encoding.name success, bytes = WASM::Runtime.instance.parse_type(buffer.content, encoding, start_pos, end_pos, variables, require_eof, void_allowed, self_allowed, classish_allowed) raise_parsing_error(buffer, bytes) unless success deserialize_or_nil(bytes, buffer) end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/parser.rb, line 44 def _parse_type_params(buffer, start_pos, end_pos, module_type_params) validate_position_range(start_pos, end_pos) encoding = buffer.content.encoding.name success, bytes = WASM::Runtime.instance.parse_type_params(buffer.content, encoding, start_pos, end_pos, module_type_params) raise_parsing_error(buffer, bytes) unless success bytes.empty? ? nil : WASM::Deserializer.deserialize_node_list(bytes, buffer) end
(Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof, bool void_allowed, bool self_allowed, bool classish_allowed) → String?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/sig/parser.rbs, line 147
def self._parse_type_to_bytes: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof, bool void_allowed, bool self_allowed, bool classish_allowed) -> String?
Parse and serialize the result to the binary format consumed by RBS::WASM::Deserializer (see ext/rbs_extension/main.c and docs/wasm_serialization.md). The _to_bytes variants exist so the round-trip can be exercised on CRuby.
(String | Buffer source) → Buffer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.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.1.3/lib/rbs/parser_aux.rb, line 134 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.1.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.1.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.1.3/lib/rbs/parser_aux.rb, line 122 def self.parse_inline_leading_annotation(source, range, variables: []) buf = buffer(source) byte_range = byte_range(range, buf.content) _parse_inline_leading_annotation(buf, byte_range.begin || 0, byte_range.end || buf.content.bytesize, 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.1.3/lib/rbs/parser_aux.rb, line 128 def self.parse_inline_trailing_annotation(source, range, variables: []) buf = buffer(source) byte_range = byte_range(range, buf.content) _parse_inline_trailing_annotation(buf, byte_range.begin || 0, byte_range.end || buf.content.bytesize, 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.1.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.1.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.1.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.1.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