Skip to content

Commit

Permalink
Change inspect from recursive to a queue
Browse files Browse the repository at this point in the history
We would previously cause a stack overflow if we parsed a file that
was too deeply nested when we were calling inspect. Instead, we now
use a queue of commands to do it linearly so we don't.
  • Loading branch information
kddnewton committed Apr 24, 2024
1 parent 096d01c commit 0f21f5b
Show file tree
Hide file tree
Showing 17 changed files with 253 additions and 197 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ jobs:
env:
LANG: "C"

# typecheck:
# runs-on: ubuntu-latest
# env:
# BUNDLE_GEMFILE: gemfiles/typecheck/Gemfile
# steps:
# - uses: actions/checkout@v4
# - name: Set up Ruby
# uses: ruby/setup-ruby@v1
# with:
# ruby-version: "3.3"
# bundler-cache: true
# - name: Check Sorbet
# run: bundle exec rake typecheck:tapioca typecheck:sorbet
# - name: Check Steep
# run: bundle exec rake typecheck:steep
# - name: Check field kinds
# run: rm lib/prism/node.rb && CHECK_FIELD_KIND=true bundle exec rake
typecheck:
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: gemfiles/typecheck/Gemfile
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"
bundler-cache: true
# - name: Check Sorbet
# run: bundle exec rake typecheck:tapioca typecheck:sorbet
- name: Check Steep
run: bundle exec rake typecheck:steep
- name: Check field kinds
run: rm lib/prism/node.rb && CHECK_FIELD_KIND=true bundle exec rake

build:
strategy:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ out.svg
/lib/prism/dispatcher.rb
/lib/prism/dot_visitor.rb
/lib/prism/dsl.rb
/lib/prism/inspect_visitor.rb
/lib/prism/mutation_compiler.rb
/lib/prism/node.rb
/lib/prism/reflection.rb
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ A lot of code in prism's repository is templated from a single configuration fil
* `lib/prism/dispatcher.rb` - for defining the dispatch visitors for the nodes in Ruby
* `lib/prism/dot_visitor.rb` - for defining the dot visitor for the nodes in Ruby
* `lib/prism/dsl.rb` - for defining the DSL for the nodes in Ruby
* `lib/prism/inspect_visitor.rb` - for defining the `#inspect` methods on nodes in Ruby
* `lib/prism/mutation_compiler.rb` - for defining the mutation compiler for the nodes in Ruby
* `lib/prism/node.rb` - for defining the nodes in Ruby
* `lib/prism/reflection.rb` - for defining the reflection API in Ruby
Expand Down
22 changes: 11 additions & 11 deletions gemfiles/typecheck/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbi (0.1.11)
prism (>= 0.18.0, < 0.27)
rbi (0.1.12)
prism (>= 0.18.0, < 0.28)
sorbet-runtime (>= 0.5.9204)
rbs (3.4.4)
abbrev
Expand All @@ -61,15 +61,15 @@ GEM
sexp_processor (~> 4.16)
securerandom (0.3.1)
sexp_processor (4.17.1)
sorbet (0.5.11352)
sorbet-static (= 0.5.11352)
sorbet-runtime (0.5.11352)
sorbet-static (0.5.11352-aarch64-linux)
sorbet-static (0.5.11352-universal-darwin)
sorbet-static (0.5.11352-x86_64-linux)
sorbet-static-and-runtime (0.5.11352)
sorbet (= 0.5.11352)
sorbet-runtime (= 0.5.11352)
sorbet (0.5.11353)
sorbet-static (= 0.5.11353)
sorbet-runtime (0.5.11353)
sorbet-static (0.5.11353-aarch64-linux)
sorbet-static (0.5.11353-universal-darwin)
sorbet-static (0.5.11353-x86_64-linux)
sorbet-static-and-runtime (0.5.11353)
sorbet (= 0.5.11353)
sorbet-runtime (= 0.5.11353)
spoom (1.3.0)
erubi (>= 1.10.0)
prism (>= 0.19.0)
Expand Down
2 changes: 1 addition & 1 deletion lib/prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ module Prism
autoload :Dispatcher, "prism/dispatcher"
autoload :DotVisitor, "prism/dot_visitor"
autoload :DSL, "prism/dsl"
autoload :InspectVisitor, "prism/inspect_visitor"
autoload :LexCompat, "prism/lex_compat"
autoload :LexRipper, "prism/lex_compat"
autoload :MutationCompiler, "prism/mutation_compiler"
autoload :NodeInspector, "prism/node_inspector"
autoload :Pack, "prism/pack"
autoload :Pattern, "prism/pattern"
autoload :Reflection, "prism/reflection"
Expand Down
68 changes: 0 additions & 68 deletions lib/prism/node_inspector.rb

This file was deleted.

74 changes: 38 additions & 36 deletions prism.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ Gem::Specification.new do |spec|
"lib/prism/dot_visitor.rb",
"lib/prism/dsl.rb",
"lib/prism/ffi.rb",
"lib/prism/inspect_visitor.rb",
"lib/prism/lex_compat.rb",
"lib/prism/mutation_compiler.rb",
"lib/prism/node_ext.rb",
"lib/prism/node_inspector.rb",
"lib/prism/node.rb",
"lib/prism/pack.rb",
"lib/prism/parse_result.rb",
Expand All @@ -101,11 +101,46 @@ Gem::Specification.new do |spec|
"lib/prism/translation/ripper/shim.rb",
"lib/prism/translation/ruby_parser.rb",
"lib/prism/visitor.rb",
"prism.gemspec",
"rbi/prism.rbi",
"rbi/prism/compiler.rbi",
"rbi/prism/desugar_compiler.rbi",
"rbi/prism/inspect_visitor.rbi",
"rbi/prism/mutation_compiler.rbi",
"rbi/prism/node_ext.rbi",
"rbi/prism/node.rbi",
"rbi/prism/parse_result.rbi",
"rbi/prism/reflection.rbi",
"rbi/prism/translation/parser.rbi",
"rbi/prism/translation/parser/compiler.rbi",
"rbi/prism/translation/parser33.rbi",
"rbi/prism/translation/parser34.rbi",
"rbi/prism/translation/ripper.rbi",
"rbi/prism/translation/ripper/ripper_compiler.rbi",
"rbi/prism/translation/ruby_parser.rbi",
"rbi/prism/visitor.rbi",
"sig/prism.rbs",
"sig/prism/compiler.rbs",
"sig/prism/dispatcher.rbs",
"sig/prism/dot_visitor.rbs",
"sig/prism/dsl.rbs",
"sig/prism/inspect_visitor.rbs",
"sig/prism/mutation_compiler.rbs",
"sig/prism/node_ext.rbs",
"sig/prism/node.rbs",
"sig/prism/pack.rbs",
"sig/prism/parse_result.rbs",
"sig/prism/pattern.rbs",
"sig/prism/reflection.rbs",
"sig/prism/serialize.rbs",
"sig/prism/visitor.rbs",
"src/diagnostic.c",
"src/encoding.c",
"src/node.c",
"src/options.c",
"src/pack.c",
"src/prettyprint.c",
"src/prism.c",
"src/regexp.c",
"src/serialize.c",
"src/static_literals.c",
Expand All @@ -117,43 +152,10 @@ Gem::Specification.new do |spec|
"src/util/pm_list.c",
"src/util/pm_memchr.c",
"src/util/pm_newline_list.c",
"src/util/pm_string.c",
"src/util/pm_string_list.c",
"src/util/pm_string.c",
"src/util/pm_strncasecmp.c",
"src/util/pm_strpbrk.c",
"src/options.c",
"src/prism.c",
"prism.gemspec",
"sig/prism.rbs",
"sig/prism/compiler.rbs",
"sig/prism/dispatcher.rbs",
"sig/prism/dot_visitor.rbs",
"sig/prism/dsl.rbs",
"sig/prism/mutation_compiler.rbs",
"sig/prism/node.rbs",
"sig/prism/node_ext.rbs",
"sig/prism/pack.rbs",
"sig/prism/parse_result.rbs",
"sig/prism/pattern.rbs",
"sig/prism/reflection.rbs",
"sig/prism/serialize.rbs",
"sig/prism/visitor.rbs",
"rbi/prism.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/reflection.rbi",
"rbi/prism/translation/parser.rbi",
"rbi/prism/translation/parser/compiler.rbi",
"rbi/prism/translation/parser33.rbi",
"rbi/prism/translation/parser34.rbi",
"rbi/prism/translation/ripper.rbi",
"rbi/prism/translation/ripper/ripper_compiler.rbi",
"rbi/prism/translation/ruby_parser.rbi",
"rbi/prism/visitor.rbi"
"src/util/pm_strpbrk.c"
]

spec.extensions = ["ext/prism/extconf.rb"]
Expand Down
1 change: 1 addition & 0 deletions rakelib/typecheck.rake
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace :typecheck do
- ./lib/prism/translation/ripper/ripper_compiler.rb
- ./lib/prism/translation/ripper/sexp.rb
- ./lib/prism/translation/ruby_parser.rb
- ./lib/prism/inspect_visitor.rb
# We want to treat all polyfill files as "typed: false"
<% polyfills.each do |file| -%>
- ./<%= file %>
Expand Down
12 changes: 12 additions & 0 deletions rbi/prism/inspect_visitor.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# typed: strict

class Prism::InspectVisitor < Prism::Visitor
sig { params(String).void }
def initialize(indent = ""); end

sig { params(Prism::Node).returns(String) }
def self.compose(node); end

sig { returns(String) }
def compose; end
end
17 changes: 0 additions & 17 deletions sig/prism/_private/node_inspector.rbs

This file was deleted.

22 changes: 22 additions & 0 deletions sig/prism/inspect_visitor.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Prism
class InspectVisitor < Visitor
class Replace
attr_reader value: String

def initialize: (String value) -> void
end

attr_reader indent: String
attr_reader commands: Array[[String | node | Replace, String]]

def initialize: (?String indent) -> void
def compose: () -> String

def self.compose: (node node) -> String

private

def inspect_node: (String name, node node) -> String
def inspect_location: (Location? location) -> String
end
end
Loading

0 comments on commit 0f21f5b

Please sign in to comment.