       parseAttribute
       parseText
       (s) ->
-        [s, 'text', null, -> s]
+        name: s
+        type: 'text'
+        value: null
+        write: -> s
     ].some (f) ->
       parsed = f node
     new Node {
       level, parsed,
-      name: parsed[0], type: parsed[1], value: parsed[2]
+      name: parsed.name, type: parsed.type, value: parsed.value
     }
 
   setAttribute: (name, value) ->

parseEmptyElement = (s) ->
  if s.match(/^<\/\S+$/)
    name = s.substring(2)
    [name, 'empty element', null, ->
      attributes = (" #{k}=\"#{v}\"" for k, v of @attributes).join ''
      '<' + name + attributes + ' />'
    ]

parseElement = (s) ->
  if s.match(/^<\S+$/)
    name = s.substring(1)
    [name, 'element', null, ->
      attributes = (" #{k}=\"#{v}\"" for k, v of @attributes).join ''
      children = @children.map((i) -> i.write()).join('')
      '<' + name + attributes + '>' + children + '</' + name + '>'
    ]

parseAttribute = (s) ->
  m = s.match(/^@(\S+)\s+(.+)$/)
  if m
    [m[1], 'attribute', m[2], null]

parseText = (s) ->
  if s.match(/^>.+$/)
    name = s.substring(1)
    [name, 'text', null, -> name]

class Node
  constructor: ({ @level, @parsed, @name, @type, @value }) ->

    n.parent = @

  write: ->
    @parsed.write.apply @, []

parse = (s) ->
  root = Node.parse '<root'
