Class: Attentive::Matcher
- Inherits:
-
Object
- Object
- Attentive::Matcher
- Defined in:
- lib/attentive/matcher.rb
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#phrase ⇒ Object
readonly
Returns the value of attribute phrase.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(phrase, message, params = {}) ⇒ Matcher
constructor
A new instance of Matcher.
- #match! ⇒ Object
- #match_any!(messages) ⇒ Object
- #matching? ⇒ Boolean
- #mismatch? ⇒ Boolean
- #pos ⇒ Object
Constructor Details
#initialize(phrase, message, params = {}) ⇒ Matcher
Returns a new instance of Matcher.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/attentive/matcher.rb', line 13 def initialize(phrase, , params={}) @phrase = phrase @match_start = .pos @cursor = Cursor.new(phrase, params.fetch(:pos, 0)) @message = self..pop while self..peek.whitespace? @match_params = params.merge(message: ., match_start: .pos) @match_data = {} @state = :matching cursor.pop while cursor.peek.whitespace? end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
5 6 7 |
# File 'lib/attentive/matcher.rb', line 5 def cursor @cursor end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
5 6 7 |
# File 'lib/attentive/matcher.rb', line 5 def @message end |
#phrase ⇒ Object (readonly)
Returns the value of attribute phrase.
5 6 7 |
# File 'lib/attentive/matcher.rb', line 5 def phrase @phrase end |
Class Method Details
.match!(phrase, message) ⇒ Object
7 8 9 10 11 |
# File 'lib/attentive/matcher.rb', line 7 def self.match!(phrase, ) phrase = Attentive::Tokenizer.tokenize(phrase, entities: true) unless phrase.is_a?(Attentive::Phrase) = Attentive::Message.new() unless .is_a?(Attentive::Message) self.new(phrase, Attentive::Cursor.new()).match! end |
Instance Method Details
#match! ⇒ Object
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 65 66 67 68 69 70 71 |
# File 'lib/attentive/matcher.rb', line 38 def match! until (token = .peek).eof? if token.ambiguous? unless match_any!(token.possibilities) @state = :mismatch break end .pop cursor.pop while cursor.peek.whitespace? elsif match_data = cursor.peek.matches?() @match_data.merge!(match_data) unless match_data == true cursor.pop cursor.pop while cursor.peek.whitespace? @state = :found # -> This is the one spot where we instantiate a Match return Attentive::Match.new(phrase, @match_params.merge( match_end: .pos, match_data: @match_data)) if cursor.eof? elsif token.skippable? .pop else @state = :mismatch break end .pop while .peek.whitespace? end nil end |
#match_any!(messages) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/attentive/matcher.rb', line 73 def match_any!() .each do || matcher = Matcher.new(phrase[pos..-1], Cursor.new()) matcher.match! unless matcher.mismatch? cursor.advance matcher.pos return true end end false end |
#matching? ⇒ Boolean
30 31 32 |
# File 'lib/attentive/matcher.rb', line 30 def matching? @state == :matching end |
#mismatch? ⇒ Boolean
34 35 36 |
# File 'lib/attentive/matcher.rb', line 34 def mismatch? @state == :mismatch end |
#pos ⇒ Object
26 27 28 |
# File 'lib/attentive/matcher.rb', line 26 def pos cursor.pos end |