diff --git a/gems/parser/3.2/_test/test.rb b/gems/parser/3.2/_test/test.rb index c41269383..65ece5019 100644 --- a/gems/parser/3.2/_test/test.rb +++ b/gems/parser/3.2/_test/test.rb @@ -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 diff --git a/gems/parser/3.2/parser.rbs b/gems/parser/3.2/parser.rbs index 7f7aea348..a386951a9 100644 --- a/gems/parser/3.2/parser.rbs +++ b/gems/parser/3.2/parser.rbs @@ -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