Skip to content

Various updates #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Autoload a bunch of stuff
  • Loading branch information
kddnewton committed Feb 10, 2023
commit 0cf3e858b2dc3cee1af05a6ee3c0913d261727be
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ This function takes an input string containing Ruby code, parses it into its und

### SyntaxTree.mutation(&block)

This function yields a new mutation visitor to the block, and then returns the initialized visitor. It's effectively a shortcut for creating a `SyntaxTree::Visitor::MutationVisitor` without having to remember the class name. For more information on that visitor, see the definition below.
This function yields a new mutation visitor to the block, and then returns the initialized visitor. It's effectively a shortcut for creating a `SyntaxTree::MutationVisitor` without having to remember the class name. For more information on that visitor, see the definition below.

### SyntaxTree.search(source, query, &block)

Expand Down Expand Up @@ -558,7 +558,7 @@ The `MutationVisitor` is a visitor that can be used to mutate the tree. It works

```ruby
# Create a new visitor
visitor = SyntaxTree::Visitor::MutationVisitor.new
visitor = SyntaxTree::MutationVisitor.new

# Specify that it should mutate If nodes with assignments in their predicates
visitor.mutate("IfNode[predicate: Assign | OpAssign]") do |node|
Expand Down
40 changes: 19 additions & 21 deletions lib/syntax_tree.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
# frozen_string_literal: true

require "etc"
require "json"
require "pp"
require "prettier_print"
require "ripper"
require "stringio"

require_relative "syntax_tree/formatter"
require_relative "syntax_tree/node"
require_relative "syntax_tree/version"

require_relative "syntax_tree/basic_visitor"
require_relative "syntax_tree/visitor"
require_relative "syntax_tree/visitor/field_visitor"
require_relative "syntax_tree/visitor/json_visitor"
require_relative "syntax_tree/visitor/match_visitor"
require_relative "syntax_tree/visitor/mermaid_visitor"
require_relative "syntax_tree/visitor/mutation_visitor"
require_relative "syntax_tree/visitor/pretty_print_visitor"
require_relative "syntax_tree/visitor/environment"
require_relative "syntax_tree/visitor/with_environment"

require_relative "syntax_tree/formatter"
require_relative "syntax_tree/parser"
require_relative "syntax_tree/pattern"
require_relative "syntax_tree/search"
require_relative "syntax_tree/index"
require_relative "syntax_tree/translation"
require_relative "syntax_tree/version"

# Syntax Tree is a suite of tools built on top of the internal CRuby parser. It
# provides the ability to generate a syntax tree from source, as well as the
Expand All @@ -38,7 +21,19 @@ module SyntaxTree
# as possible in order to keep the CLI as fast as possible.

autoload :DSL, "syntax_tree/dsl"
autoload :FieldVisitor, "syntax_tree/field_visitor"
autoload :Index, "syntax_tree/index"
autoload :JSONVisitor, "syntax_tree/json_visitor"
autoload :LanguageServer, "syntax_tree/language_server"
autoload :MatchVisitor, "syntax_tree/match_visitor"
autoload :Mermaid, "syntax_tree/mermaid"
autoload :MermaidVisitor, "syntax_tree/mermaid_visitor"
autoload :MutationVisitor, "syntax_tree/mutation_visitor"
autoload :Pattern, "syntax_tree/pattern"
autoload :PrettyPrintVisitor, "syntax_tree/pretty_print_visitor"
autoload :Search, "syntax_tree/search"
autoload :Translation, "syntax_tree/translation"
autoload :WithEnvironment, "syntax_tree/with_environment"
autoload :YARV, "syntax_tree/yarv"

# This holds references to objects that respond to both #parse and #format
Expand Down Expand Up @@ -89,7 +84,7 @@ def self.index_file(filepath)

# A convenience method for creating a new mutation visitor.
def self.mutation
visitor = Visitor::MutationVisitor.new
visitor = MutationVisitor.new
yield visitor
visitor
end
Expand Down Expand Up @@ -130,6 +125,9 @@ def self.register_handler(extension, handler)
# Searches through the given source using the given pattern and yields each
# node in the tree that matches the pattern to the given block.
def self.search(source, query, &block)
Search.new(Pattern.new(query).compile).scan(parse(source), &block)
pattern = Pattern.new(query).compile
program = parse(source)

Search.new(pattern).scan(program, &block)
end
end
4 changes: 2 additions & 2 deletions lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "etc"
require "optparse"

module SyntaxTree
Expand Down Expand Up @@ -238,7 +239,7 @@ def run(item)
# representation.
class Json < Action
def run(item)
object = Visitor::JSONVisitor.new.visit(item.handler.parse(item.source))
object = item.handler.parse(item.source).accept(JSONVisitor.new)
puts JSON.pretty_generate(object)
end
end
Expand Down Expand Up @@ -501,7 +502,6 @@ def run(argv)
when "j", "json"
Json.new(options)
when "lsp"
require "syntax_tree/language_server"
LanguageServer.new(print_width: options.print_width).run
return 0
when "m", "match"
Expand Down
Loading