From cd8d1018bbca086427f66bdba646bb4f387c32b4 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 28 Feb 2024 15:48:17 -0500 Subject: [ruby/prism] Resync RBI and test it in CI https://github.com/ruby/prism/commit/4ef4032774 --- lib/prism.rb | 6 +++ lib/prism/ffi.rb | 3 +- lib/prism/pack.rb | 1 + lib/prism/prism.gemspec | 12 +++++- lib/prism/translation/parser/rubocop.rb | 1 + prism/extension.c | 7 ---- prism/templates/lib/prism/serialize.rb.erb | 1 - prism/templates/template.rb | 59 +++++++++++++++++++++--------- test/prism/compiler_test.rb | 1 + test/prism/ripper_test.rb | 18 ++++----- 10 files changed, 72 insertions(+), 37 deletions(-) diff --git a/lib/prism.rb b/lib/prism.rb index ac746630fc..0e3cb2c0a6 100644 --- a/lib/prism.rb +++ b/lib/prism.rb @@ -94,6 +94,12 @@ require_relative "prism/parse_result/newlines" # module that uses FFI to call into the library. if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"] require "prism/prism" + + # Using a C extension is the default backend for the parser. + Prism::BACKEND = :CEXT else require_relative "prism/ffi" + + # On platforms that don't support C extensions, we use FFI. + Prism::BACKEND = :FFI end diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb index 0f262573de..347c35d26f 100644 --- a/lib/prism/ffi.rb +++ b/lib/prism/ffi.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# typed: ignore # This file is responsible for mirroring the API provided by the C extension by # using FFI to call into the shared library. @@ -7,8 +8,6 @@ require "rbconfig" require "ffi" module Prism - BACKEND = :FFI - module LibRubyParser # :nodoc: extend FFI::Library diff --git a/lib/prism/pack.rb b/lib/prism/pack.rb index 31c7686123..c0de8ab8b7 100644 --- a/lib/prism/pack.rb +++ b/lib/prism/pack.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# typed: ignore module Prism # A parser for the pack template language. diff --git a/lib/prism/prism.gemspec b/lib/prism/prism.gemspec index 58201ed53a..548754185c 100644 --- a/lib/prism/prism.gemspec +++ b/lib/prism/prism.gemspec @@ -136,7 +136,17 @@ Gem::Specification.new do |spec| "sig/prism/serialize.rbs", "sig/prism/visitor.rbs", "rbi/prism.rbi", - "rbi/prism_static.rbi" + "rbi/prism/compiler.rbi", + "rbi/prism/desugar_compiler.rbi", + "rbi/prism/mutation_compiler.rbi", + "rbi/prism/node_ext.rbi", + "rbi/prism/node.rbi", + "rbi/prism/parse_result.rbi", + "rbi/prism/translation/parser/compiler.rbi", + "rbi/prism/translation/ripper.rbi", + "rbi/prism/translation/ripper/ripper_compiler.rbi", + "rbi/prism/translation/ruby_parser.rbi", + "rbi/prism/visitor.rbi" ] spec.extensions = ["ext/prism/extconf.rb"] diff --git a/lib/prism/translation/parser/rubocop.rb b/lib/prism/translation/parser/rubocop.rb index c791ba74a2..e6fd8db290 100644 --- a/lib/prism/translation/parser/rubocop.rb +++ b/lib/prism/translation/parser/rubocop.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# typed: ignore require "parser" require "rubocop" diff --git a/prism/extension.c b/prism/extension.c index 7bcb5f65c1..ed56a6c513 100644 --- a/prism/extension.c +++ b/prism/extension.c @@ -1254,13 +1254,6 @@ Init_prism(void) { */ rb_define_const(rb_cPrism, "VERSION", rb_str_new2(EXPECTED_PRISM_VERSION)); - /** - * The backend of the parser that prism is using to parse Ruby code. This - * can be either :CEXT or :FFI. On runtimes that support C extensions, we - * default to :CEXT. Otherwise we use :FFI. - */ - rb_define_const(rb_cPrism, "BACKEND", ID2SYM(rb_intern("CEXT"))); - // First, the functions that have to do with lexing and parsing. rb_define_singleton_method(rb_cPrism, "dump", dump, -1); rb_define_singleton_method(rb_cPrism, "dump_file", dump_file, -1); diff --git a/prism/templates/lib/prism/serialize.rb.erb b/prism/templates/lib/prism/serialize.rb.erb index 1356496a5e..5e195d9bbc 100644 --- a/prism/templates/lib/prism/serialize.rb.erb +++ b/prism/templates/lib/prism/serialize.rb.erb @@ -91,7 +91,6 @@ module Prism case load_varuint when 0 then InlineComment.new(load_location) when 1 then EmbDocComment.new(load_location) - when 2 then DATAComment.new(load_location) end end end diff --git a/prism/templates/template.rb b/prism/templates/template.rb index fd55d5228b..a6da307e04 100755 --- a/prism/templates/template.rb +++ b/prism/templates/template.rb @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# typed: false require "erb" require "fileutils" @@ -28,7 +29,7 @@ module Prism end # A comment attached to a field or node. - class Comment + class ConfigComment attr_reader :value def initialize(value) @@ -40,7 +41,7 @@ module Prism end def each_java_line(&block) - Comment.new(JavaDoc.escape(value)).each_line(&block) + ConfigComment.new(JavaDoc.escape(value)).each_line(&block) end end @@ -56,11 +57,11 @@ module Prism end def each_comment_line(&block) - Comment.new(comment).each_line(&block) if comment + ConfigComment.new(comment).each_line(&block) if comment end def each_comment_java_line(&block) - Comment.new(comment).each_java_line(&block) if comment + ConfigComment.new(comment).each_java_line(&block) if comment end def semantic_field? @@ -122,7 +123,13 @@ module Prism end def rbi_class - "Prism::#{ruby_type}" + if specific_kind + "Prism::#{specific_kind}" + elsif union_kind + "T.any(#{union_kind.map { |kind| "Prism::#{kind}" }.join(", ")})" + else + "Prism::Node" + end end def check_field_kind @@ -148,7 +155,13 @@ module Prism end def rbi_class - "T.nilable(Prism::#{ruby_type})" + if specific_kind + "T.nilable(Prism::#{specific_kind})" + elsif union_kind + "T.nilable(T.any(#{union_kind.map { |kind| "Prism::#{kind}" }.join(", ")}))" + else + "T.nilable(Prism::Node)" + end end def check_field_kind @@ -174,7 +187,13 @@ module Prism end def rbi_class - "T::Array[Prism::#{ruby_type}]" + if specific_kind + "T::Array[Prism::#{specific_kind}]" + elsif union_kind + "T::Array[T.any(#{union_kind.map { |kind| "Prism::#{kind}" }.join(", ")})]" + else + "T::Array[Prism::Node]" + end end def java_type @@ -406,11 +425,11 @@ module Prism end def each_comment_line(&block) - Comment.new(comment).each_line(&block) + ConfigComment.new(comment).each_line(&block) end def each_comment_java_line(&block) - Comment.new(comment).each_java_line(&block) + ConfigComment.new(comment).each_java_line(&block) end def semantic_fields @@ -494,6 +513,7 @@ module Prism when ".rb" <<~HEADING # frozen_string_literal: true + =begin This file is generated by the templates/template.rb script and should not be modified manually. See #{filepath} @@ -509,13 +529,16 @@ module Prism HEADING when ".rbi" - <<~HEADING - =begin - This file is generated by the templates/template.rb script and should not be - modified manually. See #{filepath} - if you are looking to modify the template - =end - HEADING + <<~HEADING + # typed: strict + + =begin + This file is generated by the templates/template.rb script and should not be + modified manually. See #{filepath} + if you are looking to modify the template + =end + + HEADING else <<~HEADING /******************************************************************************/ @@ -525,6 +548,7 @@ module Prism /* if you are looking to modify the */ /* template */ /******************************************************************************/ + HEADING end @@ -598,7 +622,8 @@ module Prism "src/prettyprint.c", "src/serialize.c", "src/token_type.c", - "rbi/prism.rbi", + "rbi/prism/node.rbi", + "rbi/prism/visitor.rbi", "sig/prism.rbs", "sig/prism/dsl.rbs", "sig/prism/mutation_compiler.rbs", diff --git a/test/prism/compiler_test.rb b/test/prism/compiler_test.rb index ed028d03d8..9a326eb8d6 100644 --- a/test/prism/compiler_test.rb +++ b/test/prism/compiler_test.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# typed: ignore require_relative "test_helper" diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index 9ff052ff04..a179116ce2 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -3,15 +3,13 @@ require_relative "test_helper" module Prism - class RipperTest < TestCase + class RipperTestCase < TestCase + private + def truffleruby? RUBY_ENGINE == "truffleruby" end - def windows? - Gem.win_platform? - end - # Ripper produces certain ambiguous structures. For instance, it often # adds an :args_add_block with "false" as the block meaning there is # no block call. It can be hard to tell which of multiple equivalent @@ -44,10 +42,9 @@ module Prism actual = normalized_sexp(actual) assert_equal expected, actual, "Expected Ripper and Prism to give equivalent output for #{path}!" end - end - class RipperShortSourceTest < RipperTest + class RipperShortSourceTest < RipperTestCase def test_binary assert_equivalent("1 + 2") assert_equivalent("3 - 4 * 5") @@ -258,13 +255,16 @@ module Prism assert_equivalent("alias :'' :foo") end + Translation::Ripper + RUBY_KEYWORDS = Translation.const_get(:RipperCompiler)::RUBY_KEYWORDS + # This is *exactly* the kind of thing where Ripper would have a weird # special case we didn't handle correctly. We're still testing with # a leading colon since putting random keywords there will often get # parse errors. Mostly we want to know that Ripper will use :@kw # instead of :@ident for the lexer symbol for all of these. def test_keyword_aliases - Prism::Translation::Ripper::RUBY_KEYWORDS.each do |keyword| + RUBY_KEYWORDS.each do |keyword| assert_equivalent("alias :foo :#{keyword}") end end @@ -276,7 +276,7 @@ module Prism end end - class RipperFixturesTest < RipperTest + class RipperFixturesTest < RipperTestCase #base = File.join(__dir__, "fixtures") #relatives = ENV["FOCUS"] ? [ENV["FOCUS"]] : Dir["**/*.txt", base: base] relatives = [ -- cgit v1.2.3