Skip to content

Commit 1d99f10

Browse files
zenspidergdiggs
authored andcommitted
Update flay and remove code pushed upstream.
This code also adds a bunch of node names to Sexp::NODE_NAMES for JS and PHP. It is probably not exhaustive.
1 parent de72a93 commit 1d99f10

File tree

3 files changed

+51
-82
lines changed

3 files changed

+51
-82
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
source "https://rubygems.org"
33

44
gem "concurrent-ruby", "~> 1.0.0"
5-
gem "flay"
5+
gem "flay", "~> 2.9"
66
gem "json"
77

88
group :test do

Gemfile.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ GEM
55
concurrent-ruby (1.0.0)
66
diff-lcs (1.2.5)
77
erubis (2.7.0)
8-
flay (2.8.1)
8+
flay (2.9.0)
99
erubis (~> 2.7.0)
1010
path_expander (~> 1.0)
1111
ruby_parser (~> 3.0)
@@ -31,17 +31,17 @@ GEM
3131
diff-lcs (>= 1.2.0, < 2.0)
3232
rspec-support (~> 3.3.0)
3333
rspec-support (3.3.0)
34-
ruby_parser (3.8.4)
34+
ruby_parser (3.9.0)
3535
sexp_processor (~> 4.1)
36-
sexp_processor (4.8.0)
36+
sexp_processor (4.9.0)
3737
slop (3.6.0)
3838

3939
PLATFORMS
4040
ruby
4141

4242
DEPENDENCIES
4343
concurrent-ruby (~> 1.0.0)
44-
flay
44+
flay (~> 2.9)
4545
json
4646
pry
4747
rake

lib/ccflay.rb

+46-77
Original file line numberDiff line numberDiff line change
@@ -18,71 +18,54 @@ def initialize(option = nil)
1818
self.identical = Concurrent::Hash.new
1919
self.masses = Concurrent::Hash.new
2020
end
21-
22-
##
23-
# Calculate the structural hash for this sexp. Cached, so don't
24-
# modify the sexp afterwards and expect it to be correct.
25-
26-
if ENV["PURE_HASH"]
27-
def structural_hash
28-
@structural_hash ||= pure_ruby_hash
29-
end
30-
else
31-
def structural_hash
32-
@structural_hash ||= Digest::MD5.hexdigest(structure.to_s)
33-
end
34-
end
3521
end
3622

37-
# TODO: move this to flay proper... it's benchmarking faster
38-
class Sexp # straight from flay-persistent
39-
names = %w[alias and arglist args array attrasgn attrset back_ref
40-
begin block block_pass break call case cdecl class colon2
41-
colon3 const cvar cvasgn cvdecl defined defn defs dot2
42-
dot3 dregx dregx_once dstr dsym dxstr ensure evstr false
43-
flip2 flip3 for gasgn gvar hash iasgn if iter ivar lasgn
44-
lit lvar masgn match match2 match3 module next nil not
45-
nth_ref op_asgn op_asgn1 op_asgn2 op_asgn_and op_asgn_or or
46-
postexe redo resbody rescue retry return sclass self
47-
splat str super svalue to_ary true undef until valias
48-
when while xstr yield zsuper kwarg kwsplat safe_call]
49-
50-
##
51-
# All ruby_parser nodes in an index hash. Used by jenkins algorithm.
52-
53-
NODE_NAMES = Hash[names.each_with_index.map { |n, i| [n.to_sym, i] }]
54-
55-
NODE_NAMES.default_proc = lambda { |h, k|
56-
$stderr.puts "ERROR: couldn't find node type #{k} in Sexp::NODE_NAMES."
57-
h[k] = NODE_NAMES.size
58-
}
59-
60-
MAX_INT32 = 2**32 - 1 # :nodoc:
61-
62-
def pure_ruby_hash # :nodoc: see above
63-
hash = 0
64-
65-
n = NODE_NAMES.fetch first
66-
67-
raise "Bad lookup: #{first} in #{sexp.inspect}" unless n
68-
69-
hash += n & MAX_INT32
70-
hash += hash << 10 & MAX_INT32
71-
hash ^= hash >> 6 & MAX_INT32
72-
73-
each do |o|
74-
next unless o.is_a? Sexp
75-
hash = hash + o.pure_ruby_hash & MAX_INT32
76-
hash = (hash + (hash << 10)) & MAX_INT32
77-
hash = (hash ^ (hash >> 6)) & MAX_INT32
78-
end
79-
80-
hash = (hash + (hash << 3)) & MAX_INT32
81-
hash = (hash ^ (hash >> 11)) & MAX_INT32
82-
hash = (hash + (hash << 15)) & MAX_INT32
83-
84-
hash
85-
end
23+
new_nodes = [
24+
:And, :ArrayExpression, :ArrowFunctionExpression,
25+
:Assign, :AssignmentExpression, :AssignmentPattern,
26+
:Attribute, :BinaryExpression, :BlockStatement, :BoolOp,
27+
:BooleanLiteral, :Break, :BreakStatement, :Call,
28+
:CallExpression, :CatchClause, :ClassBody,
29+
:ClassDeclaration, :ClassMethod, :Compare,
30+
:ConditionalExpression, :Continue, :ContinueStatement,
31+
:Dict, :Directive, :DirectiveLiteral, :DirectiveLiteral,
32+
:DoWhileStatement, :EmptyStatement, :Eq, :ExceptHandler,
33+
:ExportDefaultDeclaration, :ExportNamedDeclaration,
34+
:ExportSpecifier, :Expr, :ExpressionStatement, :For,
35+
:ForInStatement, :ForStatement, :FunctionDeclaration,
36+
:FunctionDef, :FunctionExpression, :Gt, :Identifier, :If,
37+
:IfExp, :IfStatement, :Import, :ImportDeclaration,
38+
:ImportDefaultSpecifier, :ImportFrom, :ImportSpecifier,
39+
:Index, :LabeledStatement, :LogicalExpression, :LtE,
40+
:MemberExpression, :Name, :NewExpression, :NotIn,
41+
:NullLiteral, :Num, :NumericLiteral, :ObjectExpression,
42+
:ObjectMethod, :ObjectPattern, :ObjectProperty, :Or,
43+
:Print, :RegExpLiteral, :ReturnStatement,
44+
:SequenceExpression, :Slice, :Str, :StringLiteral,
45+
:Subscript, :Super, :SwitchCase, :SwitchStatement,
46+
:TaggedTemplateExpression, :TemplateElement,
47+
:TemplateLiteral, :ThisExpression, :ThrowStatement,
48+
:TryExcept, :TryStatement, :Tuple, :UnaryExpression,
49+
:UpdateExpression, :VariableDeclaration,
50+
:VariableDeclarator, :WhileStatement, :Yield, :alternate,
51+
:argument, :arguments, :array_dim_fetch, :assign,
52+
:assign_op_minus, :binary_op_bitwise_and,
53+
:binary_op_bitwise_or, :binary_op_concat,
54+
:binary_op_shift_right, :binary_op_smaller_or_equal,
55+
:body, :callee, :cases, :comparators, :consequent,
56+
:declaration, :declarations, :directives, :elements,
57+
:elts, :exp, :expression, :expressions, :extra,
58+
:finalizer, :foreach, :func_call, :function, :id, :init,
59+
:init, :key, :keyword, :left, :list, :lnumber, :name,
60+
:object, :param, :params, :properties, :property,
61+
:quasis, :right, :specifiers, :string, :superClass,
62+
:target, :test, :update, :value, :values,
63+
:variable
64+
]
65+
66+
# Add known javascript and php nodes to the hash registry.
67+
new_nodes.each do |name|
68+
Sexp::NODE_NAMES[name] = Sexp::NODE_NAMES.size
8669
end
8770

8871
class Sexp
@@ -108,17 +91,3 @@ def flatter
10891
end
10992
end
11093
end
111-
112-
class Sexp # TODO: push this back to flay
113-
alias old_mass mass
114-
115-
def mass
116-
@mass ||= inject(1) do |t, s|
117-
if s.is_a?(Sexp)
118-
t + s.mass
119-
else
120-
t
121-
end
122-
end
123-
end
124-
end

0 commit comments

Comments
 (0)