Class: Spectre::Directives::SkipDirective

Inherits:
NoPreSkipDirective show all
Defined in:
lib/spectre/generic/directives.rb

Overview

Sets a skip Parser or block to use. If a Parser is given, it will be passed the input stream. If it matches, the input stream will be advanced as much as the Match’s length, so all the matched tokens are skipped. Any skip Parser will be explicitly wrapped inside a noaction_d and InputIterator#ignore_skipper. If a block is given instead of a Parser, the block will be given each input token and must return true if the token should be skipped, and false otherwise.

Shortcut: skip_d

Instance Attribute Summary

Attributes inherited from Spectre::Directive

#iter_skipper, #iter_transformation, #pre_skip

Attributes inherited from Node

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

Instance Method Summary collapse

Methods inherited from NoPreSkipDirective

#parse

Methods inherited from Spectre::Directive

#[], #parse, policy, policy!, skipper!, transformation!

Methods inherited from Node

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

Constructor Details

#initialize(skipper) ⇒ SkipDirective

Returns a new instance of SkipDirective.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/spectre/generic/directives.rb', line 134

def initialize skipper
    if skipper.respond_to? :call
        @iter_skipper = lambda { |token,iter|
            skipper.call(token) ? 1 : nil
        }
    elsif skipper.respond_to? :parse
        skipper = NoActionDirective.new[skipper]

        @iter_skipper = lambda { |token,iter|
            match = nil
            iter.ignore_skipper { |i| match = skipper.parse i }

            if match then match.length
            else nil
            end
        }
    else
        raise "SkipDirective expects either lambda block or parser Node"
    end

    super()
end

Instance Method Details

#inspectObject



157
158
159
# File 'lib/spectre/generic/directives.rb', line 157

def inspect
    "[skip_d:#{@left.inspect}]"
end