Class: Gary::Parser
- Inherits:
-
Object
- Object
- Gary::Parser
- Defined in:
- lib/gary/parser.rb
Instance Attribute Summary collapse
-
#string ⇒ Object
Returns the value of attribute string.
-
#tokens ⇒ Object
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(string) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #tokenize ⇒ Object
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
#string ⇒ Object
Returns the value of attribute string.
13 14 15 |
# File 'lib/gary/parser.rb', line 13 def string @string end |
#tokens ⇒ Object
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
#parse ⇒ Object
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 |
#tokenize ⇒ Object
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 |