Class: Spectre::RangeParser

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

Overview

Matches a range of tokens. Can be supplied with anything that implements the methods #include?, #first and #last.

Shortcut: range.

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(range) ⇒ RangeParser

Returns a new instance of RangeParser.



47
48
49
50
51
52
53
54
55
# File 'lib/spectre/generic/primitives.rb', line 47

def initialize range
    [:include?, :first, :last].each { |meth|
        raise "#{range.inspect} does not respond to ##{meth}, which is required for RangeParser" unless
            range.respond_to? meth
    }

    super()
    @range = range
end

Instance Method Details

#inspectObject



70
71
72
# File 'lib/spectre/generic/primitives.rb', line 70

def inspect
    "[range:#{@range.first}..#{@range.last}]"
end

#negationObject



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

def negation; Negations::NegatedSingleTokenParser.new; end

#scan(iter) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/spectre/generic/primitives.rb', line 59

def scan iter
    return nil unless iter.valid?
    token = +iter

    if @range.include? token
        create_match iter, token
    else
        nil
    end
end