Class: CmdCreator::SpecParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cmd_creator.rb

Instance Method Summary collapse

Constructor Details

#initialize(words, args) ⇒ SpecParser

Returns a new instance of SpecParser.



16
17
18
19
# File 'lib/cmd_creator.rb', line 16

def initialize(words, args)
  @args = args
  @words = words
end

Instance Method Details

#asub(atom) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cmd_creator.rb', line 68

def asub(atom)
  arr = atom.split(CmdCreator::Argument)
  tmp = []
  arr.each_with_index {|ea, ind|
    if 1 == ind % 2
      if @args[ea.to_sym]
        tmp << @args[ea.to_sym].to_s
      else
        return nil
      end
    else
      tmp << ea
    end
  }
  
  tmp.join("")
end

#parseObject



21
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
# File 'lib/cmd_creator.rb', line 21

def parse
  spec = Spec.new
  spec.type = true
  
  atom = ""
  while word = @words[0]
    case word
    when CmdCreator::MandatoryStart
      @words.shift
      
      ssp = SubSpecParser.new(
        CmdCreator::MandatoryStop, @words, @args
      ).parse
      unless replace(spec, atom, ssp)
        raise CmdCreator::NotMandatoryError.new(atom)
      end
      
      atom = ""
      
    when CmdCreator::OptionalStart
      @words.shift
      ssp = SubSpecParser.new(
        CmdCreator::OptionalStop, @words, @args
      ).parse
      unless replace(spec, atom, ssp)
        raise CmdCreator::NotMandatoryError.new(atom)
      end
      atom = ""
    when CmdCreator::MandatoryStop
      return spec
    when CmdCreator::OptionalStop
      return spec
    else
      @words.shift
      atom << word
    end
  end
  
  if atom.size > 0
    unless replace(spec, atom)
      raise CmdCreator::NotMandatoryError.new(atom)
    end
  end
  
  return spec
end

#replace(spec, atom, sspe = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cmd_creator.rb', line 86

def replace(spec, atom, sspe = nil)
  if atom.size > 0
    subs = asub(atom)
    if subs
      spec << subs
    else
      return false
    end
  end
  spec << sspe if sspe.size > 0
  return true
end