Class: Ariel::RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/ariel/rule_set.rb

Overview

A RuleSet acts as a container for a StructureNode’s start and end rules. These are stored as an ordered array and are applied in turn until there is a successful match. A RuleSet takes responsibility for applying start and end rules to extract an ExtractedNode.

Instance Method Summary collapse

Constructor Details

#initialize(start_rules, end_rules) ⇒ RuleSet

Returns a new instance of RuleSet.



8
9
10
11
# File 'lib/ariel/rule_set.rb', line 8

def initialize(start_rules, end_rules)
  @start_rules=start_rules
  @end_rules=end_rules
end

Instance Method Details

#apply_to(tokenstream) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ariel/rule_set.rb', line 13

def apply_to(tokenstream)
  start_idx=nil
  end_idx=nil
  @start_rules.each do |rule|
    start_idx=rule.apply_to tokenstream
    break if start_idx
  end
  @end_rules.each do |rule|
    end_idx=rule.apply_to tokenstream
    break if end_idx
  end
  if start_idx && end_idx
    debug "RuleSet matched with start_idx=#{start_idx} and end_idx=#{end_idx}"
    return nil if end_idx < start_idx
    return tokenstream.slice_by_token_index(start_idx, end_idx)
  else
    debug "No valid match was found"
    return nil
  end
end