Class: Korekto::Symbols

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSymbols

Returns a new instance of Symbols.



4
5
6
7
8
9
# File 'lib/korekto/symbols.rb', line 4

def initialize
  @h   = {}
  @t2p = {}
  @v2t = {}
  @scanner = /:\w+|./
end

Instance Attribute Details

#t2pObject (readonly)

Returns the value of attribute t2p.



3
4
5
# File 'lib/korekto/symbols.rb', line 3

def t2p
  @t2p
end

#v2tObject (readonly)

Returns the value of attribute v2t.



3
4
5
# File 'lib/korekto/symbols.rb', line 3

def v2t
  @v2t
end

Instance Method Details

#define!(statement) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/korekto/symbols.rb', line 25

def define!(statement)
  if statement.pattern?
    unless statement.literal_regexp?
      statement.scan(@scanner){|w| @h[w]=nil unless @v2t.include?(w) or @h.include?(w)}
    end
  else
    statement.scan(@scanner){|w| @h[w]=nil unless @h.include?(w)}
  end
end

#s2r(statement) ⇒ Object



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
# File 'lib/korekto/symbols.rb', line 35

def s2r(statement)
  if statement[0]=='/' and statement[-1]=='/'
    Regexp.new(statement[1..-2])
  else
    pattern,count,seen = '\A',0,{}
    statement.scan(@scanner) do |v|
      if n=seen[v]
        pattern << '\\'+n
      elsif type = @v2t[v]
        regex = @t2p[type]
        if type[0]=='.'
          pattern << regex
        else
          count += 1
          seen[v]=count.to_s
          pattern << '('+regex+')'
        end
      else
        # Escape Regexp specials
        v = Regexp.quote v
        # To avoid collisions with back-references, isolate digit in square brackets:
        '0123456789'.include?(_=v[0]) and v[0]='['+_+']'
        pattern << v
      end
    end
    raise Error, 'pattern with no captures' if count < 1
    pattern << '\Z'
    Regexp.new(pattern)
  end
end

#set_scanner(value) ⇒ Object



11
# File 'lib/korekto/symbols.rb', line 11

def set_scanner(value) = @scanner=Regexp.new(value)

#undefined(statement) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/korekto/symbols.rb', line 13

def undefined(statement)
  undefined = []
  if statement.pattern?
    unless statement.literal_regexp?
      statement.scan(@scanner){|w| undefined.push(w) unless @v2t.include?(w) or @h.include?(w)}
    end
  else
    statement.scan(@scanner){|w| undefined.push(w) unless @h.include?(w)}
  end
  return undefined.uniq
end