Class: Connie::Parser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary) ⇒ Parser

Returns a new instance of Parser.



32
33
34
# File 'lib/connie/parser.rb', line 32

def initialize(dictionary)
  @dictionary = dictionary
end

Instance Attribute Details

#dictionaryObject

TODO: implement negative lookahead to allow escaping : and escaping the escaping symbol itself \



5
6
7
# File 'lib/connie/parser.rb', line 5

def dictionary
  @dictionary
end

Class Method Details

.process(string_to_parse, dictionary = Connie[:names]) ⇒ Object



26
27
28
29
30
# File 'lib/connie/parser.rb', line 26

def self.process string_to_parse, dictionary = Connie[:names]
  tokenized = string_to_parse.split Regexp.union(@syntax.keys)
        
  Connie::Parser.new(dictionary).apply_syntax(tokenized).join
end

Instance Method Details

#apply_syntax(tokens) ⇒ Object

calls trasform on the tokens marked for interpolation and deals with escaping



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/connie/parser.rb', line 37

def apply_syntax tokens
  tokens.map do |t|
    if t.match '\:[a-z]'
      transform t
    elsif t[0] && t[0].chr == '\\' # some level of escaping is present
      t.match %r{([\\]+)\:}
      raise 'I don\' speak escapeese yet!'
    else
      t
    end
  end
end

#transform(string) ⇒ Object

Interpolates a syntax token



51
52
53
54
55
# File 'lib/connie/parser.rb', line 51

def transform string
  result = nil
  syntax.each_pair { |k,func| result = instance_exec(string, &func) if string.match(k) }
  return result
end