 #-----------------------------------------------
 class Tokenizer
 
-  constructor: (@html) ->
-    @originalHtml = @html
-
-  actionHandler: (key, val) ->
-
-  bindingFragment: (tag, key, val) ->
+  constructor: (@buffer) ->
+    @next = []
+
+  none: -> { type: T_NONE }
+
+  action: (t, eventName, handler) ->
+    t.actions[eventName] = handler
+
+  binding: (t, tag, key, val) ->
     globalAttrs = ATTR_PRESERVED['*']
     tagSpecificAttrs = ATTR_PRESERVED[tag]
 
     if key.match(globalAttrs) || tagSpecificAttrs && key.match tagSpecificAttrs
-      @token.attrBindings[key] = val
+      t.attrBindings[key] = val
     else
-      @token.localBindings[key] = val
-
-  attrFragment: (attrs, tag) ->
+      t.localBindings[key] = val
+
+  tagAttr: (t, attrs, tag) ->
+    t.attrs = {}
+    t.attrBindings = {}
+    t.localBindings = {}
+    t.actions = {}
+
     attrs = " #{attrs} ".match ATTR_REGEXP
 
     for attr in attrs

      val = m[3] || m[4]

      if '$' == binding
        @bindingFragment tag, key, val
      else if '@' == binding
        @actionHandler key, val
      else
        @token.attrs[key] = val

  tagFragment: (buffer) ->
    match = TAG_REGEXP.exec buffer

    return { type: T_NONE } unless match

    @token.buffer = match[0]
    @token.length = @token.buffer.length
    @token.name = match[2]
    @token.attrs = {}
    @token.attrBindings = {}
    @token.localBindings = {}
    @attrFragment match[3], @token.name

    @token.type =
      if match[1]
        T_TAG_CLOSE
      else if match[4] || @token.name.match TAG_SELF_CLOSING
        T_TAG_SELF
      else
        T_TAG_OPEN

  getToken: ->
    @token = {}




#  Patterns
#-----------------------------------------------
TEXT_INTERP_REGEXP = ///
  (?:
    \{\{\{
      (.+?)
    \}\}\}
  ) | (?:
    \{\{
      (.+?)
    \}\}
  )
///g

TAG_REGEXP = /<(\/?)(\w+)([^>]*)(\/?)>/
TAG_SELF_CLOSING = /^(img|input|hr|br|wbr)$/

