From 185c4f2d11510f870b8b1a7862b34bff108942ec Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Fri, 24 Jul 2020 22:30:20 -0400 Subject: [PATCH] Build symbols descending from stars with regexp Before this commit we initialized all Symbols with the default regexp, then later on reassigned any symbols descending from stars with either their regexp from `@requirements` or the default greedy regexp. With this commit we initialize all Symbols descending from Stars with the greedy regexp at parse time. This allows us to get rid of the star branch in path/pattern, since any regexps from `@requirements` will already have been set in the symbol branch of this code. This is essentially an alternate version of #38901. Getting rid of the extra branch makes some performance work I am doing a bit easier, plus it saves us a few method calls. Also the constant saves us from creating the same regexp multiple times, and it is nice to give that regexp a name. --- .../lib/action_dispatch/journey/nodes/node.rb | 7 +++-- .../lib/action_dispatch/journey/parser.rb | 28 +++++++++---------- .../lib/action_dispatch/journey/parser.y | 2 +- .../action_dispatch/journey/path/pattern.rb | 11 ++------ 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/actionpack/lib/action_dispatch/journey/nodes/node.rb b/actionpack/lib/action_dispatch/journey/nodes/node.rb index 086d6a3e07..3593246cf4 100644 --- a/actionpack/lib/action_dispatch/journey/nodes/node.rb +++ b/actionpack/lib/action_dispatch/journey/nodes/node.rb @@ -79,9 +79,10 @@ module ActionDispatch attr_reader :name DEFAULT_EXP = /[^\.\/\?]+/ - def initialize(left) - super - @regexp = DEFAULT_EXP + GREEDY_EXP = /(.+)/ + def initialize(left, regexp = DEFAULT_EXP) + super(left) + @regexp = regexp @name = -left.tr("*:", "") end diff --git a/actionpack/lib/action_dispatch/journey/parser.rb b/actionpack/lib/action_dispatch/journey/parser.rb index 9418b87b40..6f49680914 100644 --- a/actionpack/lib/action_dispatch/journey/parser.rb +++ b/actionpack/lib/action_dispatch/journey/parser.rb @@ -1,10 +1,10 @@ # # DO NOT MODIFY!!!! -# This file is automatically generated by Racc 1.4.14 +# This file is automatically generated by Racc 1.4.16 # from Racc grammar file "". # -require "racc/parser.rb" +require 'racc/parser.rb' # :stopdoc: @@ -135,11 +135,11 @@ Racc_debug_parser = false # reduce 0 omitted def _reduce_1(val, _values) - Cat.new(val.first, val.last) + Cat.new(val.first, val.last) end def _reduce_2(val, _values) - val.first + val.first end # reduce 3 omitted @@ -151,19 +151,19 @@ end # reduce 6 omitted def _reduce_7(val, _values) - Group.new(val[1]) + Group.new(val[1]) end def _reduce_8(val, _values) - Or.new([val.first, val.last]) + Or.new([val.first, val.last]) end def _reduce_9(val, _values) - Or.new([val.first, val.last]) + Or.new([val.first, val.last]) end def _reduce_10(val, _values) - Star.new(Symbol.new(val.last)) + Star.new(Symbol.new(val.last, Symbol::GREEDY_EXP)) end # reduce 11 omitted @@ -175,19 +175,19 @@ end # reduce 14 omitted def _reduce_15(val, _values) - Slash.new(val.first) + Slash.new(val.first) end def _reduce_16(val, _values) - Symbol.new(val.first) + Symbol.new(val.first) end def _reduce_17(val, _values) - Literal.new(val.first) + Literal.new(val.first) end def _reduce_18(val, _values) - Dot.new(val.first) + Dot.new(val.first) end def _reduce_none(val, _values) @@ -195,5 +195,5 @@ def _reduce_none(val, _values) end end # class Parser - end # module Journey - end # module ActionDispatch + end # module Journey +end # module ActionDispatch diff --git a/actionpack/lib/action_dispatch/journey/parser.y b/actionpack/lib/action_dispatch/journey/parser.y index f9b1a7a958..f8cc4afbcb 100644 --- a/actionpack/lib/action_dispatch/journey/parser.y +++ b/actionpack/lib/action_dispatch/journey/parser.y @@ -21,7 +21,7 @@ rule | expression OR or { Or.new([val.first, val.last]) } ; star - : STAR { Star.new(Symbol.new(val.last)) } + : STAR { Star.new(Symbol.new(val.last, Symbol::GREEDY_EXP)) } ; terminal : symbol diff --git a/actionpack/lib/action_dispatch/journey/path/pattern.rb b/actionpack/lib/action_dispatch/journey/path/pattern.rb index 6f06215be7..4ea502ce29 100644 --- a/actionpack/lib/action_dispatch/journey/path/pattern.rb +++ b/actionpack/lib/action_dispatch/journey/path/pattern.rb @@ -41,14 +41,9 @@ module ActionDispatch end def ast - @spec.each do |node| - if node.symbol? - re = @requirements[node.to_sym] - node.regexp = re if re - elsif node.star? - node = node.left - node.regexp = @requirements[node.to_sym] || /(.+)/ - end + @spec.find_all(&:symbol?).each do |node| + re = @requirements[node.to_sym] + node.regexp = re if re end @spec