Class: Spectre::Directive

Inherits:
Node
  • Object
show all
Defined in:
lib/spectre/base/directive.rb

Overview

Sets options for the Parser and InputIterator. Imitates the behaviour of a Node. Subclasses may call the singleton methods ::transformation! and ::policy! to set the input transformation and Parser policy hash to use. These hashes will get merged with the current options.

The Directive acts like a node in order to apply the parser policy. It keeps a DirectiveParser, which applies the input transformation.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Node

#actions, #backtrace, #left, #parent, #parser, #policy, #right, #symbols

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#%, #&, #*, #**, #+, #-, #-@, #>>, #^, #backtrack, #chain, #closure, #closure=, #closure?, #find, #initialize_copy, #inspect, #leaf?, #replace_with, #root?, #shallow_copy, #to_p, #|, #~@

Constructor Details

#initializeDirective

Retrieves the options from the subclassclass definition.



95
96
97
98
99
100
101
102
# File 'lib/spectre/base/directive.rb', line 95

def initialize
    @iter_transformation ||= self.class.transformation
    @iter_skipper ||= self.class.skipper
    @pre_skip = true
    super(DirectiveParser.new self)
    pol = self.class.policy
    @policy ? @policy.merge!(pol) : @policy = pol
end

Class Attribute Details

.skipperObject (readonly)

The skipper to apply to the InputIterator for this subclass - lambda block.



53
54
55
# File 'lib/spectre/base/directive.rb', line 53

def skipper
  @skipper
end

.transformationObject (readonly)

The transformation to apply to the InputIterator for this subclass - lambda block.



57
58
59
# File 'lib/spectre/base/directive.rb', line 57

def transformation
  @transformation
end

Instance Attribute Details

#iter_skipperObject (readonly)

The skipper to apply to the InputIterator for this Directive - lambda block.



39
40
41
# File 'lib/spectre/base/directive.rb', line 39

def iter_skipper
  @iter_skipper
end

#iter_transformationObject (readonly)

The transformation to apply to the InputIterator for this Directive - lambda block.



43
44
45
# File 'lib/spectre/base/directive.rb', line 43

def iter_transformation
  @iter_transformation
end

#pre_skipObject (readonly)

Whether or not the child parser should do pre-skipping.



47
48
49
# File 'lib/spectre/base/directive.rb', line 47

def pre_skip
  @pre_skip
end

Class Method Details

.policyObject

The options to merge with the Parser options for this subclass.



62
63
64
# File 'lib/spectre/base/directive.rb', line 62

def policy
    @policy || {}
end

.policy!(hash) ⇒ Object

Sets the Parser policy to use for this subclass. The provided hash will be merged with any already existing policy hash, thus enabling you to leave current specifications the way they are by simply not mentioning them.



87
88
89
# File 'lib/spectre/base/directive.rb', line 87

def policy! hash
    @policy = hash
end

.skipper!(*args, &block) ⇒ Object

Sets the skipper on the InputIterator for this subclass to either the block or if no block is given to the first argument given to the method.



70
71
72
# File 'lib/spectre/base/directive.rb', line 70

def skipper! *args, &block
    @skipper = block_given? ? block : args[0]
end

.transformation!(*args, &block) ⇒ Object

Sets the input transformation on the InputIterator for this subclass to either the block or if no block is given to the first argument given to the method.



78
79
80
# File 'lib/spectre/base/directive.rb', line 78

def transformation! *args, &block
    @transformation = block_given? ? block : args[0]
end

Instance Method Details

#[](node) ⇒ Object

Unlike Nodes, the Directive cannot store semantic actions. Instead this operator is used to deliver the subnodes for it. Performs auto-conversion on node.



109
110
111
112
113
# File 'lib/spectre/base/directive.rb', line 109

def [] node
    @left = node.to_p
    @left.parent = self
    self
end

#parse(iter, pre_skip = true) ⇒ Object

Saves the pre_skipping flag as directives pass that on to their children.



118
119
120
121
# File 'lib/spectre/base/directive.rb', line 118

def parse iter, pre_skip = true
    @pre_skip = pre_skip
    super(iter, pre_skip)
end