Method: Usher::Util::Parser#parse

Defined in:
lib/usher/util/parser.rb

#parse(path, requirements = nil, default_values = nil) ⇒ Object



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
163
164
165
166
167
168
169
170
# File 'lib/usher/util/parser.rb', line 87

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)
      if variable
        variable_class = case variable.slice!(0)
        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, Regexp.new(pattern), requirements && requirements[variable_name])
      elsif simple
        static = Usher::Route::Static::Greedy.new(pattern)
        static.generate_with = pattern
        current_group << static
      else
        simple_parts = pattern.split(',', 2)
        static = Usher::Route::Static::Greedy.new(Regexp.new(simple_parts.last))
        static.generate_with = simple_parts.first
        current_group << static
      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