Class: Org::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/org/rule.rb

Constant Summary collapse

DEFAULT =
{
  :bol => false,
  :start => nil,
  :end => nil,
  :unscan => false,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, regex, options = {}, &block) ⇒ Rule

Returns a new instance of Rule.



12
13
14
15
# File 'lib/org/rule.rb', line 12

def initialize(name, regex, options = {}, &block)
  @name, @regex, @block = name, regex, block
  @options = DEFAULT.merge(options)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/org/rule.rb', line 10

def name
  @name
end

#regexObject

Returns the value of attribute regex.



10
11
12
# File 'lib/org/rule.rb', line 10

def regex
  @regex
end

Instance Method Details

#match(state) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/org/rule.rb', line 17

def match(state)
  scope, parent, scanner = state.scope, state.parent, state.scanner
  return if @options[:bol] and not scanner.bol?

  return false unless scanner.scan(@regex)
  scanner.unscan if @options[:unscan]

  name = @options[:tag] || @name
  token = Token.new(name, scanner.captures, &@block)
  return true if @options[:ignore]

  if mode = @options[:start]
    # puts "Start #{mode}"
    state.scope = mode.is_a?(Scope) ? mode : scope.scopes[mode]
    parent << token
    state.parent = token
  elsif mode = @options[:end]
    # puts "End #{mode}"
    state.parent = parent.parent
    state.scope = scope.parent
  else
    parent << token
    return true
  end
end