Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 66f6155

Browse files
committed
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.
1 parent 1ea3b6a commit 66f6155

File tree

6 files changed

+1367
-2463
lines changed

6 files changed

+1367
-2463
lines changed

lib/syntax_tree.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
require_relative "syntax_tree/parser"
1212
require_relative "syntax_tree/version"
1313
require_relative "syntax_tree/visitor"
14+
require_relative "syntax_tree/visitor/field_visitor"
1415
require_relative "syntax_tree/visitor/json_visitor"
16+
require_relative "syntax_tree/visitor/match_visitor"
1517
require_relative "syntax_tree/visitor/pretty_print_visitor"
1618

1719
# If PrettyPrint::Align isn't defined, then we haven't gotten the updated

lib/syntax_tree/cli.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def failure
103103
# An action of the CLI that prints out the doc tree IR for the given source.
104104
class Doc < Action
105105
def run(handler, filepath, source)
106-
formatter = Formatter.new([])
106+
formatter = Formatter.new(source, [])
107107
handler.parse(source).format(formatter)
108108
pp formatter.groups.first
109109
end
@@ -116,6 +116,26 @@ def run(handler, filepath, source)
116116
end
117117
end
118118

119+
# An action of the CLI that converts the source into its equivalent JSON
120+
# representation.
121+
class Json < Action
122+
def run(handler, filepath, source)
123+
object = Visitor::JSONVisitor.new.visit(handler.parse(source))
124+
puts JSON.pretty_generate(object)
125+
end
126+
end
127+
128+
# An action of the CLI that outputs a pattern-matching Ruby expression that
129+
# would match the input given.
130+
class Match < Action
131+
def run(handler, filepath, source)
132+
formatter = Formatter.new(source, [])
133+
Visitor::MatchVisitor.new(formatter).visit(handler.parse(source))
134+
formatter.flush
135+
puts formatter.output.join
136+
end
137+
end
138+
119139
# An action of the CLI that formats the input source and writes the
120140
# formatted output back to the file.
121141
class Write < Action
@@ -154,6 +174,12 @@ def run(handler, filepath, source)
154174
#{Color.bold("stree format [OPTIONS] [FILE]")}
155175
Print out the formatted version of the given files
156176
177+
#{Color.bold("stree json [OPTIONS] [FILE]")}
178+
Print out the JSON representation of the given files
179+
180+
#{Color.bold("stree match [OPTIONS] [FILE]")}
181+
Print out a pattern-matching Ruby expression that would match the given files
182+
157183
#{Color.bold("stree help")}
158184
Display this help message
159185
@@ -201,6 +227,10 @@ def run(argv)
201227
Debug.new
202228
when "doc"
203229
Doc.new
230+
when "j", "json"
231+
Json.new
232+
when "m", "match"
233+
Match.new
204234
when "f", "format"
205235
Format.new
206236
when "w", "write"

0 commit comments

Comments
 (0)