@@ -13,6 +13,7 @@ module Prism
1313 when "console" then console
1414 when "dot" then dot ( argv )
1515 when "encoding" then encoding ( argv )
16+ when "error" then error ( argv )
1617 when "lex" then lex ( argv )
1718 when "locals" then locals ( argv )
1819 when "parse" then parse ( argv )
@@ -27,6 +28,7 @@ module Prism
2728 bin/prism console
2829 bin/prism dot [source]
2930 bin/prism encoding [encoding]
31+ bin/prism error [name] [source]
3032 bin/prism lex [source]
3133 bin/prism locals [source]
3234 bin/prism parse [source]
@@ -157,6 +159,29 @@ module Prism
157159 unicode_lists ( found ) if found == Encoding ::UTF_8 || found == Encoding ::UTF8_MAC
158160 end
159161
162+ # bin/prism error [name] [source]
163+ def error ( argv )
164+ name = argv . shift
165+ source , _ = read_source ( argv )
166+
167+ result = Prism . parse ( source )
168+ raise "Expected #{ source . inspect } to have errors" if result . success?
169+
170+ filepath = File . expand_path ( "../test/prism/errors/#{ name } .txt" , __dir__ )
171+
172+ if File . file? ( filepath )
173+ counter = 1
174+
175+ begin
176+ current = "#{ File . dirname ( filepath ) } /#{ File . basename ( filepath , ".txt" ) } _#{ counter += 1 } .txt"
177+ end while File . file? ( current )
178+
179+ filepath = current
180+ end
181+
182+ File . write ( filepath , result . errors_format )
183+ end
184+
160185 # bin/prism lex [source]
161186 def lex ( argv )
162187 source , filepath = read_source ( argv )
0 commit comments