Class: XDry::SelectorDef

Inherits:
Object
  • Object
show all
Defined in:
lib/xdry/parsing/parts/selectors.rb

Direct Known Subclasses

CompoundSelectorDef, SimpleSelectorDef

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/xdry/parsing/parts/selectors.rb', line 7

def self.parse string
  string = string.strip
  if string =~ /^\w+$/
    SimpleSelectorDef.new(string)
  else
    ss = StringScanner.new(string)
    comps = []
    while not ss.eos?
      keyword = ss.scan(/\w+\s*:/) or raise(StandardError, "Cannot parse selector '#{string}': keyword expected at '#{ss.rest}'")
      keyword = keyword.gsub(/\s/, '')

      ss.skip(/\s+/)
      if ss.skip(/\(/)
        res = ss.scan_until(/\)/) or raise(StandardError, "Cannot parse selector '#{string}': missing closing paren at '#{ss.rest}'")
        type_decl = res[0..-2].strip
      else
        type_decl = nil
      end

      ss.skip(/\s+/)
      unless ss.match?(/\w+\s*:/)
        arg_name = ss.scan(/\w+/)
      else
        arg_name = nil
      end
      ss.skip(/\s+/)

      type = if type_decl then VarType.parse(type_decl) else nil end
      comps << SelectorComponent.new(keyword, arg_name, type)
    end

    CompoundSelectorDef.new(comps)
  end
end