@@ -103,7 +103,7 @@ def failure
103
103
# An action of the CLI that prints out the doc tree IR for the given source.
104
104
class Doc < Action
105
105
def run ( handler , filepath , source )
106
- formatter = Formatter . new ( [ ] )
106
+ formatter = Formatter . new ( source , [ ] )
107
107
handler . parse ( source ) . format ( formatter )
108
108
pp formatter . groups . first
109
109
end
@@ -116,6 +116,26 @@ def run(handler, filepath, source)
116
116
end
117
117
end
118
118
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
+
119
139
# An action of the CLI that formats the input source and writes the
120
140
# formatted output back to the file.
121
141
class Write < Action
@@ -154,6 +174,12 @@ def run(handler, filepath, source)
154
174
#{ Color . bold ( "stree format [OPTIONS] [FILE]" ) }
155
175
Print out the formatted version of the given files
156
176
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
+
157
183
#{ Color . bold ( "stree help" ) }
158
184
Display this help message
159
185
@@ -201,6 +227,10 @@ def run(argv)
201
227
Debug . new
202
228
when "doc"
203
229
Doc . new
230
+ when "j" , "json"
231
+ Json . new
232
+ when "m" , "match"
233
+ Match . new
204
234
when "f" , "format"
205
235
Format . new
206
236
when "w" , "write"
0 commit comments