class FormatEngine::SpecInfo

A little package of info about the engine's progress.

Attributes

dst[R]

The destination of the process.

engine[R]

The formatting engine.

fmt[R]

The format specifier currently being processed.

src[R]

The source data for formatting, string input for parsing.

tmp[R]

A hash for state storage for the formatting/parsing process.

Public Class Methods

new(src, dst, engine) click to toggle source

Set up the spec info.

# File lib/format_engine/spec_info.rb, line 22
def initialize(src, dst, engine)
  @src, @dst, @engine, @tmp = src, dst, engine, {}
end

Public Instance Methods

cat(str) click to toggle source

Concatenate onto the formatted output string.

# File lib/format_engine/spec_info.rb, line 27
def cat(str)
  @dst << str
end
do_format(fmt) click to toggle source

Pass the formatting action along to the current format element.

# File lib/format_engine/spec_info.rb, line 37
def do_format(fmt)
  (@fmt = fmt).do_format(self)
end
do_parse(fmt) click to toggle source

Pass the parsing action along to the current format element.

# File lib/format_engine/spec_info.rb, line 42
def do_parse(fmt)
  (@fmt = fmt).do_parse(self)
end
found() click to toggle source

What was found by the last parse?

# File lib/format_engine/spec_info.rb, line 96
def found
  @match
end
found?() click to toggle source

Was the last parse a success?

# File lib/format_engine/spec_info.rb, line 91
def found?
   @prematch.empty? && !@match.empty?
end
grab() click to toggle source

Grab some text

# File lib/format_engine/spec_info.rb, line 74
def grab
  width = fmt.width

  if width > 0
    result, @src = src[0...width], src[width..-1] || ""
  elsif width == 0
    result, @src = src[0...1], src[1..-1] || ""
  elsif width == -1
    result, @src = src, ""
  else
    result, @src = src[0..width], src[(width+1)..-1] || ""
  end

  result
end
parse(target) click to toggle source

Parse the source string for a target string or regex or return nil.

# File lib/format_engine/spec_info.rb, line 47
def parse(target)
  #Handle the width option if specified.
  if (width = fmt.width) > 0
    head, tail = src[0...width], src[width..-1] || ""
  else
    head, tail = src, ""
  end

  #Do the parse on the input string or regex.
  @prematch, @match, @postmatch = head.partition(target)

  #Analyze the results.
  if found?
    @src = @postmatch + tail
    @match
  else
    nil
  end
end
parse!(target, msg = " click to toggle source

Parse the source string for a target string or regex or raise error.

# File lib/format_engine/spec_info.rb, line 68
def parse!(target, msg = "#{target.inspect} not found")
  fail "Parse error: #{msg}" unless parse(target)
  @match
end
set(obj) click to toggle source

Set the result of this parsing operation.

# File lib/format_engine/spec_info.rb, line 32
def set(obj)
  @dst = obj
end