Module: HelpParser::Validate

Defined in:
lib/help_parser/validate.rb

Class Method Summary collapse

Class Method Details

.balanced_brackets(chars) ⇒ Object

Raises:



3
4
5
6
7
8
9
10
# File 'lib/help_parser/validate.rb', line 3

def self.balanced_brackets(chars)
  count = 0
  chars.each do |c|
    c=='[' && (count+=1) or c==']' && (count-=1)
    break if count.negative?
  end
  raise HelpError, MSG[UNBALANCED,chars.join] unless count.zero?
end

.k2t2r(specs, k2t, t2r) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/help_parser/validate.rb', line 70

def self.k2t2r(specs, k2t, t2r)
  a,b = k2t.values.uniq.sort,t2r.keys.sort
  unless a==b
    c = (a+b).uniq.reject{|x|a.include?(x) && b.include?(x)}
    raise HelpError, MSG[UNCOMPLETED_TYPES,c.join(',')]
  end
  specs.each do |section,tokens|
    next if RESERVED.include? section
    tokens.each do |words|
      next if words.size<2
      default = words[-1]
      next if default[0]=='-'
      long_type = words[-2]
      i = long_type.index('=')
      next if i.nil?
      long = long_type[2..(i-1)]
      type = long_type[(i+1)..]
      regex = t2r[type]
      unless regex=~default
        raise HelpError, MSG[BAD_DEFAULT,long,default,type,regex.inspect]
      end
    end
  end
end

.usage_specs(specs) ⇒ Object



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
# File 'lib/help_parser/validate.rb', line 24

def self.usage_specs(specs)
  flags = specs.except(*RESERVED).values.flatten
          .select{_1[0]=='-'}.map{F2K[_1]}
  FLAG_CLUMPS.each do |k|
    next unless (a=specs[k])
    seen = {}
    a.each do |xs|
      k = xs.sort.join(' ').to_sym
      if seen[k] || xs.length!=xs.uniq.length
        raise HelpError, MSG[DUP_X,k]
      end
      seen[k] = true
      xs.each do |x|
        raise HelpError, MSG[UNSEEN_FLAG, x] unless flags.include?(x)
      end
    end
  end
  flags.each_with_index do |flag,i|
    raise HelpError, MSG[DUP_FLAG,flag] unless i==flags.rindex(flag)
  end
  group,var = [],{}
  specs_usage = specs[USAGE]
  specs_usage&.flatten&.each do |token|
    case token
    when VARIABLE
      key,bool = $~[:k],$~[:p].nil?
      if var.key? key
        unless var[key]==bool
          raise HelpError, MSG[INCONSISTENT,"<#{key}>","<#{key}>+"]
        end
      else
        var[key]=bool
      end
    when FLAG_GROUP
      key = $~[:k]
      raise HelpError, MSG[UNDEFINED_SECTION,key] unless specs[key]
      group.push(key)
    end
  end
  specs.each do |key,tokens|
    raise HelpError, MSG[MISSING_CASES,key] if tokens.empty?
    next if specs_usage.nil? || RESERVED.include?(key)
    raise HelpError, MSG[MISSING_USAGE,key] unless group.include?(key)
  end
end

.usage_tokens(tokens) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/help_parser/validate.rb', line 12

def self.usage_tokens(tokens)
  words = []
  tokens.flatten.each do |token|
    raise HelpError, MSG[UNRECOGNIZED_TOKEN,token] unless
    [FLAG,LITERAL,VARIABLE,FLAG_GROUP]
    .detect{(_=token.match _1) && words.push(_[:k])}
  end
  words.each_with_index do |word,i|
    raise HelpError, MSG[DUP_WORD,word] unless i==words.rindex(word)
  end
end