Class: Spectre::SequenceParser

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/spectre/generic/primitives.rb

Overview

Matches a sequence of tokens.

Can be used on anything that is convertible to an array of tokens, i.e. the given object must either respond to #split(Regexp) or #to_a.

NOTE: It’s negation is a zero token parser, i.e. ~seq() will only test if the input at that location contains the sequence and return a failure if so and a successful match of length 0 if not.

Shortcut: seq.

Instance Attribute Summary

Attributes included from Parser

#node

Instance Method Summary collapse

Methods included from Parser

#backtrack, #create_match, from_POD, #pre_skip?, #to_p

Constructor Details

#initialize(seq) ⇒ SequenceParser

Returns a new instance of SequenceParser.



90
91
92
93
94
95
96
97
# File 'lib/spectre/generic/primitives.rb', line 90

def initialize seq
    raise "given object neither responds to #to_a nor #split, one of which is required for SequenceParser" unless
        seq.respond_to? :to_a or seq.respond_to? :split

    super()
    @seq = seq
    @seq = seq.respond_to?(:split) ? seq.split(//) : seq.to_a
end

Instance Method Details

#inspectObject



115
116
117
# File 'lib/spectre/generic/primitives.rb', line 115

def inspect
    "[seq:#{@seq.inject(''){ |memo,i| memo + i + ', ' }[0..-3] }]"
end

#negationObject



99
# File 'lib/spectre/generic/primitives.rb', line 99

def negation; Negations::NegatedZeroTokenParser.new; end

#scan(iter) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/spectre/generic/primitives.rb', line 101

def scan iter
    return nil unless iter.valid?
    buf = iter.empty

    @seq.each { |s|
        token = iter.get
        return nil if not iter.valid? or token != s
        +iter
        buf = iter.concat buf, token
    }

    create_match iter, buf
end