-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathwhitequark
executable file
·79 lines (62 loc) · 1.87 KB
/
whitequark
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
require "parser/current"
$:.unshift(File.expand_path("../lib", __dir__))
require "syntax_tree"
# First, opt in to every AST feature.
Parser::Builders::Default.modernize
# Modify the source map == check so that it doesn't check against the node
# itself so we don't get into a recursive loop.
Parser::Source::Map.prepend(
Module.new {
def ==(other)
self.class == other.class &&
(instance_variables - %i[@node]).map do |ivar|
instance_variable_get(ivar) == other.instance_variable_get(ivar)
end.reduce(:&)
end
}
)
# Next, ensure that we're comparing the nodes and also comparing the source
# ranges so that we're getting all of the necessary information.
Parser::AST::Node.prepend(
Module.new {
def ==(other)
super && (location == other.location)
end
}
)
source = ARGF.read
parser = Parser::CurrentRuby.new
parser.diagnostics.all_errors_are_fatal = true
buffer = Parser::Source::Buffer.new("(string)", 1)
buffer.source = source.dup.force_encoding(parser.default_encoding)
stree = SyntaxTree::Translation.to_parser(SyntaxTree.parse(source), buffer)
ptree = parser.parse(buffer)
if stree == ptree
puts "Syntax trees are equivalent."
elsif stree.inspect == ptree.inspect
warn "Syntax tree locations are different."
queue = [[stree, ptree]]
while (left, right = queue.shift)
if left.location != right.location
warn "Different node:"
pp left
warn "Different location:"
warn "Syntax Tree:"
pp left.location
warn "whitequark/parser:"
pp right.location
exit
end
left.children.zip(right.children).each do |left_child, right_child|
queue << [left_child, right_child] if left_child.is_a?(Parser::AST::Node)
end
end
else
warn "Syntax Tree:"
pp stree
warn "whitequark/parser:"
pp ptree
end