forked from ruby/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse
executable file
·38 lines (31 loc) · 919 Bytes
/
parse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby
# frozen_string_literal: true
# Usage:
# bin/parse # defaults to test.rb
# bin/parse <filename>
# bin/parse -e "<source-code>"
$:.unshift(File.expand_path("../lib", __dir__))
require "prism"
if ARGV[0] == "-e"
result = Prism.parse(ARGV[1])
else
result = Prism.parse_file(ARGV[0] || "test.rb")
end
result.mark_newlines! if ENV["MARK_NEWLINES"]
value = result.value
value = value.accept(Prism::DesugarCompiler.new) if ENV["DESUGAR"]
parts = {}
parts["Comments"] = result.comments if result.comments.any?
parts["Magic comments"] = result.magic_comments if result.magic_comments.any?
parts["Warnings"] = result.warnings if result.warnings.any?
parts["Errors"] = result.errors if result.errors.any?
if parts.empty?
puts value.inspect
else
parts["AST"] = value
parts.each_with_index do |(key, value), index|
puts if index > 0
puts "#{key}:"
puts value.inspect
end
end