Class: Spectre::StringParsing::StringParser

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

Overview

Matches a String. Is an implicit lexeme, i.e. works on character level only, i.e. it will ignore any skippers set on the InputIterator

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

Shortcut: string.

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(string) ⇒ StringParser

Returns a new instance of StringParser.



88
89
90
91
# File 'lib/spectre/string/primitives.rb', line 88

def initialize string
    super()
    @string = string
end

Instance Method Details

#inspectObject



107
108
109
# File 'lib/spectre/string/primitives.rb', line 107

def inspect
    "[string:\"#{@string}\"]"
end

#negationObject



93
# File 'lib/spectre/string/primitives.rb', line 93

def negation; Negations::NegatedZeroTokenParser.new; end

#scan(iter) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/spectre/string/primitives.rb', line 95

def scan iter
    return Match.new 0, @string if @string.length == 0
    return nil unless iter.valid?

    iter.ignore_skipper do
        return create_match iter, @string if
            (iter + @string.length) == @string
    end

    nil
end