Class: Usher::Util::Parser
- Inherits:
-
Object
- Object
- Usher::Util::Parser
- Defined in:
- lib/usher/util/parser.rb
Instance Attribute Summary collapse
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Class Method Summary collapse
Instance Method Summary collapse
- #generate_route(unprocessed_path, conditions, requirements, default_values, generate_with, priority) ⇒ Object
-
#initialize(router, split_regex) ⇒ Parser
constructor
A new instance of Parser.
- #parse(path, requirements = nil, default_values = nil) ⇒ Object
- #parse_and_expand(path, requirements = nil, default_values = nil) ⇒ Object
Constructor Details
#initialize(router, split_regex) ⇒ Parser
Returns a new instance of Parser.
16 17 18 19 20 |
# File 'lib/usher/util/parser.rb', line 16 def initialize(router, split_regex) @router = router @split_regex = split_regex @delimiters_regex = Regexp.new(router.delimiters_regex) end |
Instance Attribute Details
#router ⇒ Object (readonly)
Returns the value of attribute router.
7 8 9 |
# File 'lib/usher/util/parser.rb', line 7 def router @router end |
Class Method Details
.for_delimiters(router, valid_regex) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/usher/util/parser.rb', line 9 def self.for_delimiters(router, valid_regex) new( router, Regexp.new('((:|\*)?' + valid_regex + '|' + router.delimiters_regex + '|\(|\)|\||\{)') ) end |
Instance Method Details
#generate_route(unprocessed_path, conditions, requirements, default_values, generate_with, priority) ⇒ Object
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 |
# File 'lib/usher/util/parser.rb', line 22 def generate_route(unprocessed_path, conditions, requirements, default_values, generate_with, priority) match_partially = false case unprocessed_path when String if unprocessed_path[-1] == ?* unprocessed_path.slice!(-1) match_partially = true end unprocessed_path = parse(unprocessed_path, requirements, default_values) when Regexp unprocessed_path = [Route::Static::Greedy.new(unprocessed_path)] else match_partially = false end unless unprocessed_path.first.is_a?(Route::Util::Group) group = Usher::Route::Util::Group.new(:all, nil) unprocessed_path.each{|p| group << p} unprocessed_path = group end paths = Route::Util.(unprocessed_path) paths.each do |path| path.each_with_index do |part, index| part.default_value = default_values[part.name] if part.is_a?(Usher::Route::Variable) && default_values && default_values[part.name] case part when Usher::Route::Variable::Glob if part.look_ahead && !part.look_ahead_priority part.look_ahead = nil part.look_ahead_priority = true else part.look_ahead = path[index + 1, path.size].find{|p| !p.is_a?(Usher::Route::Variable) && !router.delimiters.unescaped.include?(p)} || nil end when Usher::Route::Variable if part.look_ahead && !part.look_ahead_priority part.look_ahead = nil part.look_ahead_priority = true else part.look_ahead = router.delimiters.first_in(path[index + 1, path.size]) || router.delimiters.unescaped.first end end end end Route.new( paths, router, conditions, requirements, default_values, generate_with, match_partially, priority ) end |
#parse(path, requirements = nil, default_values = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/usher/util/parser.rb', line 82 def parse(path, requirements = nil, default_values = nil) parts = Usher::Route::Util::Group.new(:all, nil) scanner = StringScanner.new(path) current_group = parts part = nil while !scanner.eos? part ? (part << scanner.scan(@split_regex)) : (part = scanner.scan(@split_regex)) if scanner.match?(/\\/) and !scanner.match?(@delimiters_regex) scanner.skip(/\\/) part << scanner.getch next end case part[0] when ?* var_name = part[1, part.size - 1].to_sym current_group << Usher::Route::Variable::Glob.new(part[1, part.size - 1], nil, requirements && requirements[var_name]) when ?: var_name = part[1, part.size - 1].to_sym current_group << Usher::Route::Variable::Single.new(part[1, part.size - 1], nil, requirements && requirements[var_name]) when ?{ pattern = '' count = 1 simple = scanner.scan(/~/) variable = scanner.scan(/[!:\*]([^,]+),/) until count.zero? regex_part = scanner.scan(/\{|\}|[^\{\}]+/) case regex_part[0] when ?{ count += 1 when ?} count -= 1 end pattern << regex_part end pattern.slice!(pattern.length - 1) regex = Regexp.new(pattern) if variable variable_type = variable.slice!(0).chr.to_sym variable_class = case variable_type when :'!' then Usher::Route::Variable::Greedy when :* then Usher::Route::Variable::Glob when :':' then Usher::Route::Variable::Single end variable_name = variable[0, variable.size - 1].to_sym current_group << variable_class.new(variable_name, regex, requirements && requirements[variable_name]) elsif simple current_group << Usher::Route::Static::Greedy.new(Regexp.new(pattern)) else current_group << regex end when ?( new_group = Usher::Route::Util::Group.new(:any, current_group) current_group << new_group current_group = new_group when ?) current_group = current_group.parent.group_type == :one ? current_group.parent.parent : current_group.parent when ?| unless current_group.parent.group_type == :one detached_group = current_group.parent.pop new_group = Usher::Route::Util::Group.new(:one, detached_group.parent) detached_group.parent = new_group detached_group.group_type = :all new_group << detached_group new_group.parent << new_group end current_group.parent << Usher::Route::Util::Group.new(:all, current_group.parent) current_group = current_group.parent.last when ?\\ current_group << part[1..-1] else current_group << part end part = nil end unless !path || path.empty? parts end |