def add_pi_stmt(codebuf, code, pi_arg)  # :nodoc:
      case pi_arg
      when nil      ;  add_stmt(codebuf, code)
      when 'header' ;  @header = code
      when 'footer' ;  @footer = code
      when 'comment';  add_stmt(codebuf, "\n" * code.count("\n"))
      when 'value'  ;  add_expr_literal(codebuf, code)
      else          ;  add_stmt(codebuf, code)
      end
    end
def add_pi_stmt(codebuf, code, pi_arg)  # :nodoc:
      case pi_arg
      when nil      ;  add_stmt(codebuf, code)
      when 'header' ;  @header = code
      when 'footer' ;  @footer = code
      when 'comment';  add_stmt(codebuf, "\n" * code.count("\n"))
      when 'value'  ;  add_expr_literal(codebuf, code)
      else          ;  add_stmt(codebuf, code)
      end
    end
--
def convert_input(codebuf, input)
  parse_stmts(codebuf, input)
  #parse_stmts2(codebuf, input)
end

def parse_stmts(codebuf, input)
  #regexp = pattern_regexp(@pattern)
  @pi ||= 'e'
  @stmt_pattern ||= /<\?#{@pi}(?:-(\w+))?(\s.*?)\?>([ \t]*\r?\n)?/m
  is_bol = true
  pos = 0
  input.scan(@stmt_pattern) do |pi_arg, code, rspace|
    match = Regexp.last_match
    len  = match.begin(0) - pos
    text = input[pos, len]
    pos  = match.end(0)
    lspace = detect_spaces_at_bol(text, is_bol)
    is_bol = rspace ? true : false
    parse_exprs(codebuf, text) # unless text.empty?
    if @trim && lspace && rspace
      add_pi_stmt(codebuf, "#{lspace}#{code}#{rspace}", pi_arg)
    else
      add_text(codebuf, lspace)
      add_pi_stmt(codebuf, code, pi_arg)
      add_text(codebuf, rspace)
    end
  end
  rest = $' || input
  parse_exprs(codebuf, rest)
end

def parse_exprs(codebuf, input)
  unless @expr_pattern
    ch = Regexp.escape(@embchar)
    if @pattern
      left, right = @pattern.split(' ')
      @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}|#{left}(=+)(.*?)#{right}/
    else
      @expr_pattern = /#{ch}(!*)?\{(.*?)\}#{ch}/
    end
  end
  pos = 0
  input.scan(@expr_pattern) do |indicator1, code1, indicator2, code2|
    indicator = indicator1 || indicator2
    code = code1 || code2
    match = Regexp.last_match
    len  = match.begin(0) - pos
    text = input[pos, len]
    pos  = match.end(0)
    add_text(codebuf, text) # unless text.empty?
    add_pi_expr(codebuf, code, indicator)
  end
  rest = $' || input
  add_text(codebuf, rest)
end
++