Class: Gary::Parser

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Parser

Returns a new instance of Parser.



15
16
17
# File 'lib/gary/parser.rb', line 15

def initialize(string)
  @string = string
end

Instance Attribute Details

#stringObject

Returns the value of attribute string.



13
14
15
# File 'lib/gary/parser.rb', line 13

def string
  @string
end

#tokensObject

Returns the value of attribute tokens.



13
14
15
# File 'lib/gary/parser.rb', line 13

def tokens
  @tokens
end

Class Method Details

.parse(string) ⇒ Object



4
5
6
# File 'lib/gary/parser.rb', line 4

def parse(string)
  new(string).parse
end

.tokenize(string) ⇒ Object



8
9
10
# File 'lib/gary/parser.rb', line 8

def tokenize(string)
  new(string).tokenize
end

Instance Method Details

#parseObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gary/parser.rb', line 25

def parse
  tokenize.map do |token|
    Gary::Rules.rules.inject(token) do |token, rule|
      if token.match(rule[:matcher])
        rule[:replacement]
      else
        token
      end
    end
  end
end

#tokenizeObject



19
20
21
22
23
# File 'lib/gary/parser.rb', line 19

def tokenize
  @tokens ||= @string.split("\n").flat_map do |line| 
    line.scan(/[\w|'|"|\.|\<|\>|\=|\!|\?|\)|\(|\&|\%]+/) << "\n"
  end
end