lib/fig/string_tokenizer.rb
# subexpression_matchers is an array of hashes.  Each hash is expected to
# contain two keys: :pattern and :action.
#
# The :pattern value needs to be a regular expression for the substring that
# needs special handling.
#
# The :action value needs to be a block that takes two parameters.
#
# The first parameter is the text that was matched and the second is the
# error block passed to #tokenize().
#
# On success the block returns either a String containing replacement text or
# a Fig::TokenizedString::Token representing the special handling of the
# consumed text.  If there was a problem, then the error block should have
# been invoked and the block should return nil.
#
#
# metacharacters is a regular expression character class for characters that
# need to be escaped when un-single quoting a string.
subexpression_matchers is an array of hashes.  Each hash is expected to
contain two keys: :pattern and :action.

The :pattern value needs to be a regular expression for the substring that
needs special handling.

The :action value needs to be a block that takes two parameters.

The first parameter is the text that was matched and the second is the
error block passed to #tokenize().

On success the block returns either a String containing replacement text or
a Fig::TokenizedString::Token representing the special handling of the
consumed text.  If there was a problem, then the error block should have
been invoked and the block should return nil.

metacharacters is a regular expression character class for characters that
need to be escaped when un-single quoting a string.
subexpression_matchers is an array of hashes . Each hash is expected to contain two keys : : pattern and : action . The : pattern value needs to be a regular expression for the substring that needs special handling . The : action value needs to be a block that takes two parameters . The first parameter is the text that was matched and the second is the error block passed to #tokenize () . On success the block returns either a String containing replacement text or a Fig :: TokenizedString :: Token representing the special handling of the consumed text . If there was a problem then the error block should have been invoked and the block should return nil . metacharacters is a regular expression character class for characters that need to be escaped when un - single quoting a string .
def generate_segments(was_quoted)
    plain_string = nil
    while ! @string.empty?
      if @string =~ %r< \A (\\+) ([^\\] .*)? \z >xm
        slashes, remainder = $1, $2
        if slashes.length % 2 == 1
          if remainder.nil?
            @error_block.call 'ends in an incomplete escape.'
            return
          end
          subexpression_matched = subexpression_match(remainder)
          return if subexpression_matched.nil?
          if (
            subexpression_matched                       ||
            remainder[0..0] == %q<">                    ||
            ! was_quoted && remainder[0..0] == %q<'>
          )
            plain_string ||= ''
            plain_string << slashes
            plain_string << remainder[0..0]
            @string = remainder[1..-1] || ''
          else
            @error_block.call \
              "contains a bad escape sequence (\\#{remainder[0..0]})."
            return
          end
        else
          plain_string ||= ''
          plain_string << slashes
          @string = remainder
        end
      else
        replacement, remainder = subexpression_match @string
        return if replacement.nil?
        if replacement
          if replacement.is_a? String
            plain_string << replacement
          else
            if ! plain_string.nil?
              @segments << Fig::TokenizedString::PlainSegment.new(plain_string)
              plain_string = nil
            end
            @segments << replacement
          end
          @string = remainder
        elsif @string =~ %r< \A " >xm
          @error_block.call 'contains an unescaped double quote.'
          return
        elsif ! was_quoted && @string =~ %r< \A ' >xm
          @error_block.call 'contains an unescaped single quote.'
          return
        else
          plain_string ||= ''
          plain_string << @string[0..0]
          @string = @string[1..-1] || ''
        end
      end
    end
    if plain_string
      @segments << Fig::TokenizedString::PlainSegment.new(plain_string)
    end
    return
  end
def generate_segments ( was_quoted ) plain_string = nil while ! @string . empty? if @string =~ %r< \A  ( \\ +) ([^ \\ ] .*)?  \z >xm slashes , remainder = $1 , $2 if slashes . length % 2 == 1 if remainder . nil? @error_block . call 'ends in an incomplete escape.' return end subexpression_matched = subexpression_match ( remainder ) return if subexpression_matched . nil? if ( subexpression_matched || remainder [ 0 .. 0 ] == %q<"> || ! was_quoted && remainder [ 0 .. 0 ] == %q<'> ) plain_string ||= '' plain_string << slashes plain_string << remainder [ 0 .. 0 ] @string = remainder [ 1 .. - 1 ] || '' else @error_block . call "contains a bad escape sequence (\\#{remainder[0..0]})." return end else plain_string ||= '' plain_string << slashes @string = remainder end else replacement , remainder = subexpression_match @string return if replacement . nil? if replacement if replacement . is_a? String plain_string << replacement else if ! plain_string . nil? @segments << Fig :: TokenizedString :: PlainSegment . new ( plain_string ) plain_string = nil end @segments << replacement end @string = remainder elsif @string =~ %r< \A  "  >xm @error_block . call 'contains an unescaped double quote.' return elsif ! was_quoted && @string =~ %r< \A  '  >xm @error_block . call 'contains an unescaped single quote.' return else plain_string ||= '' plain_string << @string [ 0 .. 0 ] @string = @string [ 1 .. - 1 ] || '' end end end if plain_string @segments << Fig :: TokenizedString :: PlainSegment . new ( plain_string ) end return end
subexpression_matchers is an array of hashes.
subexpression_matchers is an array of hashes .
was_quoted