 #  2. kvstream = require('kvstream')
 #     stream = kvstream(options)
 #  3. stream = require('kvstream') (options)
-module.exports = exporter = (options) -> new Kevas options
-exporter.Kevas = Kevas
+module.exports = (options) -> new Kevas options
+module.exports.Kevas = Kevas

      if stream.__slashIndex isnt (i - 1) # (when i=0, slashIndex may be -1 from previous chunk)
        stream.push string[start...i] unless start is i
        start = i
        if i is string.length - 1 then stream._parse = openingDelim ; return
        else if string[i+1] is '{' then return -> closing stream, string[i+2..]
      else # the opening brace has been escaped, so, treat it like a regular char
        delete stream.__slashIndex #start++ # skip the slash tho because it was *used* to escape the brace

  if start < string.length then stream.push string[start..]
  return

closing = (stream, string) ->
  stream._parse = closing
  start = 0
  for ch,i in string
    if ch is '\\' # if we see an escape character
      if stream.__slashIndex isnt (i - 1) # if previous character is NOT an escape character
        keyOf stream, string[start...i] unless i is start # push all content up to the character
        if i is string.length - 1 then stream.__slashIndex = -1 ; return
        else
          stream.__slashIndex = i # record the index of this character
          start = i + 1 # move next start to this character

        start = i
        if i is string.length - 1
          stream._parse = closingDelim
          return -> done()
        else if string[i+1] is '}'
          return emitKey stream, string[i+2...], done
      else # the opening brace has been escaped, so, treat it like a regular char
        delete stream.__slashIndex

  if start < string.length then keyOf stream, string[start..]
  return -> done()

emitKey = (stream, string, done) ->
  key = keyOf stream
  #stream.emit 'key', key:key, values:[], next:-> opening stream, string
  stream._emitKey key, ->
    stream._parse = opening
    stream._parser string, done
  return

openingDelim = (stream, string, done) -> delim stream, string, done, opening, '{', closing, true
closingDelim = (stream, string, done) -> delim stream, string, done, closing, '}', emitKey, false

delim = (stream, string, done, continuing, char, switching, openingMode) ->
  # we're starting with a delimeter from previous chunk
  if string[0] is char
    return -> switching stream, string[1..], done
  else
    if openingMode then stream.push(char) else keyOf(stream, char)
    return -> continuing stream, string, done

keyOf = (stream, key) ->
  if key? then stream.__key = (stream.__key ? '') + key
  else
    key = stream.__key.trim()
    delete stream.__key
    return key

