Class: Fabulator::Grammar::Expr::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/fabulator/grammar/expr/rule.rb

Instance Method Summary collapse

Constructor Details

#initializeRule

Returns a new instance of Rule.



3
4
5
6
7
# File 'lib/fabulator/grammar/expr/rule.rb', line 3

def initialize
  @sequences = [ ]
  @anchor_start = false
  @anchor_end = false
end

Instance Method Details

#add_sequence(s) ⇒ Object



17
18
19
# File 'lib/fabulator/grammar/expr/rule.rb', line 17

def add_sequence(s)
  @sequences << s
end

#anchor_endObject



13
14
15
# File 'lib/fabulator/grammar/expr/rule.rb', line 13

def anchor_end
  @anchor_end = true
end

#anchor_startObject



9
10
11
# File 'lib/fabulator/grammar/expr/rule.rb', line 9

def anchor_start
  @anchor_start = true
end

#to_regexObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fabulator/grammar/expr/rule.rb', line 21

def to_regex
  r = %r{#{@sequences.collect{ |s| s.to_regex }}}
  if @anchor_start
    if @anchor_end
      %r{^#{r}$}
    else
      %r{^#{r}}
    end
  elsif @anchor_end
    %r{#{r}$}
  else
    r
  end
end