Skip to content

Instantly share code, notes, and snippets.

@tompng
Created March 3, 2026 18:12
Show Gist options
  • Select an option

  • Save tompng/e6cfa660a0552d080cc07382849a3011 to your computer and use it in GitHub Desktop.

Select an option

Save tompng/e6cfa660a0552d080cc07382849a3011 to your computer and use it in GitHub Desktop.
Originally a code to check`IRB::RubyLex#code_terminated?`
require 'prism'
# Collect syntax-valid code snippets from the given directory
def each_snippet(dir, max_size)
files = Dir.glob("#{dir.delete_suffix('/')}/**/*.rb")
snippets = Set.new
handle_snippet = ->(code) {
return if snippets.include?(code)
snippets << code
yield code
}
files.each do |file|
p(file:)
code = File.read(file) rescue next
node = Prism.parse(code).value
node.breadth_first_search do |n|
if n.slice.size < max_size && Prism.parse_success?(n.slice)
handle_snippet.call(n.slice)
end
# Test code may include ruby-code as a string literal
if n.is_a?(Prism::StringNode) && n.slice.size < max_size && Prism.parse_success?(n.unescaped)
handle_snippet.call(n.unescaped)
end
false
end
end
end
# Split the snippet into code, newline, post and create an incomplete but recoverable code
def test_snippet(snippet)
[*0..snippet.size].sample(10).each do |len|
code = snippet[0, len].rstrip
post = "\n" + snippet[len..]
if !Prism.parse_success?(code) && Prism.parse_success?(code + post) && !Prism.parse(code).continuable?
p code
end
end
end
each_snippet('/path/to/ruby-repo', 100) do |snippet|
test_snippet(snippet)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment