Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions gems/parser/3.2/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,29 @@
node.loc.begin
node.loc.end
end

parser = Parser::CurrentRuby.new
parser.version + 1
parser.diagnostics.all_errors_are_fatal = true
parser.diagnostics.ignore_warnings = true
parser.diagnostics.consumer = ->(diagnostic) { diagnostic.render.join("\n") }
source_buffer = Parser::Source::Buffer.new("(string)", source: "foo(1)")
parser.parse(source_buffer)&.type
ast, ast_comments = parser.parse_with_comments(source_buffer)
ast&.type
ast_comments.map(&:text)
Parser::CurrentRuby.parse("1", "(file)", 1)
Parser::CurrentRuby.parse_with_comments("1", "(file)", 1)

begin
Parser::CurrentRuby.parse("foo(")
rescue Parser::SyntaxError => error
error.diagnostic.level.to_s
error.diagnostic.reason.to_s
error.diagnostic.message.upcase
error.diagnostic.location.source
error.diagnostic.highlights.map(&:source)
error.diagnostic.arguments.keys
rescue Parser::UnknownEncodingInMagicComment => error
error.message
end
44 changes: 41 additions & 3 deletions gems/parser/3.2/parser.rbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
module Parser
CurrentRuby: Parser::Base
CurrentRuby: singleton(Parser::Base)

class Base < Racc::Parser
def parse: (String string, ?String file, ?Integer line) -> Parser::AST::Node?
def parse_with_comments: (String string, ?String file, ?Integer line) -> [Parser::AST::Node?, Array[Source::Comment]]
def self.parse: (String string, ?String file, ?Integer line) -> Parser::AST::Node?
def self.parse_with_comments: (String string, ?String file, ?Integer line) -> [Parser::AST::Node?, Array[Source::Comment]]

attr_reader diagnostics: Diagnostic::Engine

def version: () -> Integer
def parse: (Source::Buffer source_buffer) -> Parser::AST::Node?
def parse_with_comments: (Source::Buffer source_buffer) -> [Parser::AST::Node?, Array[Source::Comment]]
end

class SyntaxError < StandardError
attr_reader diagnostic: Diagnostic

def initialize: (Diagnostic diagnostic) -> void
end

class UnknownEncodingInMagicComment < ArgumentError
end

class Diagnostic
LEVELS: Array[Symbol]

attr_reader level: Symbol
attr_reader reason: Symbol
attr_reader arguments: Hash[Symbol, untyped]
attr_reader location: Source::Range
attr_reader highlights: Array[Source::Range]

def initialize: (Symbol level, Symbol reason, Hash[Symbol, untyped]? arguments, Source::Range location, ?Array[Source::Range] highlights) -> void
def message: () -> String
def render: () -> Array[String]

class Engine
attr_accessor consumer: (^(Diagnostic) -> void)?
attr_accessor all_errors_are_fatal: bool
attr_accessor ignore_warnings: bool

def initialize: (?(^(Diagnostic) -> void)? consumer) -> void
def process: (Diagnostic diagnostic) -> self
end
end

class Ruby18 < Base
Expand Down
Loading