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.
This commit is contained in:
Daniel Colson 2020-07-24 22:30:20 -04:00
parent 7556f7c09c
commit 185c4f2d11
No known key found for this signature in database
GPG Key ID: 88A364BBE77B1353
4 changed files with 22 additions and 26 deletions

@ -79,9 +79,10 @@ class Symbol < Terminal # :nodoc:
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

@ -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 @@ class Parser < Racc::Parser
# 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 @@ def _reduce_2(val, _values)
# 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 @@ def _reduce_10(val, _values)
# 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

@ -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

@ -41,14 +41,9 @@ def eager_load!
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