class FuzzBert::Template::Parser

Public Class Methods

new(template) click to toggle source
# File lib/fuzzbert/template.rb, line 25
def initialize(template)
  @io = StringIO.new(template)
  @template = []
end

Public Instance Methods

determine_state() click to toggle source
# File lib/fuzzbert/template.rb, line 49
def determine_state
  begin
    @buf = @io.readchar

    case @buf
    when '$'
      c = @io.readchar
      if c == "{"
        @buf = ""
        :IDENTIFIER
      else
        @buf << c
        :TEXT
      end
    when '\\'
      @buf = ""
      :TEXT
    else
      :TEXT
    end
  rescue EOFError
    :EOF
  end
end
parse() click to toggle source
# File lib/fuzzbert/template.rb, line 30
def parse
  @state = determine_state 
  while token = parse_token
    @template << token
  end
  @template
end
parse_escape() click to toggle source
# File lib/fuzzbert/template.rb, line 119
def parse_escape
  begin
    @io.readchar
  rescue EOFError
    '\\'
  end
end
parse_identifier() click to toggle source
# File lib/fuzzbert/template.rb, line 74
def parse_identifier
  name = ""
  begin
    until (c = @io.readchar) == '}'
      name << c
    end

    if name.empty?
      raise RuntimeError.new("No identifier name given")
    end

    @state = determine_state
    Identifier.new(name)
  rescue EOFError
    raise RuntimeError.new("Unclosed identifier")
  end
end
parse_text() click to toggle source
# File lib/fuzzbert/template.rb, line 92
def parse_text
  text = @buf
  begin
    loop do
      until (c = @io.readchar) == '$'
        if c == '\\'
          text << parse_escape
        else
          text << c
        end
      end

      d = @io.readchar
      if d == "{"
        @state = :IDENTIFIER
        return Text.new(text)
      else
        text << c
        text << d
      end
    end
  rescue EOFError
    @state = :EOF
    Text.new(text)
  end
end
parse_token() click to toggle source
# File lib/fuzzbert/template.rb, line 38
def parse_token
  case @state
  when :TEXT
    parse_text
  when :IDENTIFIER
    parse_identifier
  else
    nil
  end
end