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.



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

def initialize
  @set   = Set.new # Set of Korekto symbols(strings)
  @v2t = {} # Variable to Type
  @t2p = {} # Type to Pattern
  @scanner = /:\w+|./ # Default scanner
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



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

def define!(statement) = undefined(statement).each{|w| @set<<w}

#s2r(statement) ⇒ Object

Raises:



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

def s2r(statement)
  return Regexp.new statement[1..-2] if statement[0]=='/' &&
                                        statement[-1]=='/'
  pattern,count,seen = '\A',0,{}
  # Build pattern from statement token by token, v.
  statement.scan(@scanner) do |v|
    if (n=seen[v])
      pattern << '\\'+n
    elsif (type = @v2t[v])
      regex = @t2p[type]
      if type[0]=='.'
        # No capture patterns start with '.'
        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

#set_scanner(value) ⇒ Object

rubocop: disable Naming/AccessorMethodName



13
14
# File 'lib/korekto/symbols.rb', line 13

def set_scanner(value) = @scanner=Regexp.new(value)
# rubocop: enable Naming/AccessorMethodName

#undefined(statement) ⇒ Object

rubocop: enable Naming/AccessorMethodName



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/korekto/symbols.rb', line 16

def undefined(statement)
  undefined = Set.new
  if statement.pattern?
    unless statement.literal_regexp?
      statement.scan(@scanner) do |w|
        undefined<<w unless @v2t.include?(w) || @set.include?(w)
      end
    end
  else
    statement.scan(@scanner){|w| undefined<<w unless @set.include?(w)}
  end
  undefined
end