forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast
executable file
·95 lines (74 loc) · 1.93 KB
/
ast
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env jruby
require 'optparse'
opts = {}
options = {
:print_source => false,
:print_sexp => false,
:print_ast => true,
}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.on('-h', '--help', 'Display this help') do |h|
puts opts
exit true
end
opts.on('-s', '--sexp', 'Display the S-Expression for the AST') do |t|
options[:print_sexp] = true
end
opts.on('--source', 'Display the source') do |s|
options[:print_source] = true
end
opts.on('--no-ast', 'Do not print out the AST for this (only useful with -s)') do |a|
options[:print_ast] = false
end
opts.on('-e exp', '--expression') do |e|
options[:expression] = e
end
end.parse!
if ARGV.length > 1
abort "You may only specify one script (see --help)"
elsif ARGV.length == 1
if options[:expression]
abort "-e and a script is not a valid combination (see --help)"
end
options[:expression] = File.read(ARGV.shift)
elsif ! options.has_key?(:expression)
abort "No script specified (see --help)"
end
require 'jruby'
$indent_string = " "
def indexes(string, lindex, rindex)
lindex = string.index("(", lindex) if lindex != nil
rindex = string.index(")", rindex) if rindex != nil
return lindex, rindex
end
def indent(string)
depth = -1
lindex, rindex = indexes(string, 0, 0)
while (lindex != nil || rindex != nil)
if (lindex != nil && lindex < rindex)
depth += 1
string[lindex, 1] = "\n#{$indent_string * depth}"
else
depth -= 1
string[rindex, 1] = "\n"
end
lindex, rindex = indexes(string, lindex, rindex)
end
string.gsub(/,\s*$/, '').squeeze("\n")
end
root = JRuby.parse(options[:expression])
if options[:print_source]
puts "Source:"
puts options[:expression]
puts
end
if options[:print_ast]
print "AST:"
puts indent(root.to_string)
puts
end
if options[:print_sexp]
puts "SEXP:"
puts org.jruby.ast.util.SexpMaker.create(root)
end