Skip to content
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
Next Next commit
Standardize visitors
Add a parent visitor to visit each of the fields on visitors. Then add a MatchVisitor that transforms into a Ruby pattern matching expression.
  • Loading branch information
kddnewton committed May 3, 2022
commit 66f6155ac48e1e21d02e97bf005a583b8bda3932
2 changes: 2 additions & 0 deletions lib/syntax_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
require_relative "syntax_tree/parser"
require_relative "syntax_tree/version"
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/pretty_print_visitor"

# If PrettyPrint::Align isn't defined, then we haven't gotten the updated
Expand Down
32 changes: 31 additions & 1 deletion lib/syntax_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def failure
# An action of the CLI that prints out the doc tree IR for the given source.
class Doc < Action
def run(handler, filepath, source)
formatter = Formatter.new([])
formatter = Formatter.new(source, [])
handler.parse(source).format(formatter)
pp formatter.groups.first
end
Expand All @@ -116,6 +116,26 @@ def run(handler, filepath, source)
end
end

# An action of the CLI that converts the source into its equivalent JSON
# representation.
class Json < Action
def run(handler, filepath, source)
object = Visitor::JSONVisitor.new.visit(handler.parse(source))
puts JSON.pretty_generate(object)
end
end

# An action of the CLI that outputs a pattern-matching Ruby expression that
# would match the input given.
class Match < Action
def run(handler, filepath, source)
formatter = Formatter.new(source, [])
Visitor::MatchVisitor.new(formatter).visit(handler.parse(source))
formatter.flush
puts formatter.output.join
end
end

# An action of the CLI that formats the input source and writes the
# formatted output back to the file.
class Write < Action
Expand Down Expand Up @@ -154,6 +174,12 @@ def run(handler, filepath, source)
#{Color.bold("stree format [OPTIONS] [FILE]")}
Print out the formatted version of the given files

#{Color.bold("stree json [OPTIONS] [FILE]")}
Print out the JSON representation of the given files

#{Color.bold("stree match [OPTIONS] [FILE]")}
Print out a pattern-matching Ruby expression that would match the given files

#{Color.bold("stree help")}
Display this help message

Expand Down Expand Up @@ -201,6 +227,10 @@ def run(argv)
Debug.new
when "doc"
Doc.new
when "j", "json"
Json.new
when "m", "match"
Match.new
when "f", "format"
Format.new
when "w", "write"
Expand Down
Loading