title: I want to write a specific-purpose parser / lexer, but I feel overwhelmed. Could you help me with this little example?
body: <p>So I'm writing a php templating engine called <a href="https://github.com/Evertt/Slade#example">Slade</a> inspired by <a href="http://slim-lang.com/">Ruby Slim</a> and <a href="http://laravel.com/docs/5.0/templates">laravel Blade</a>.</p>
  
  <p>And now many people are recommending me to rewrite it to a lexer / parser thing, instead of relying completely on regexes. So I googled about lexers and parsers and tried to learn how they work, and while I think I get the general idea, I still find it hard to start write one.</p>
  
  <p>So I was hoping if someone would help me on my way, by showing how to do one example. How would I exactly lex (is that even a verb?) and parse this:</p>
  
  <pre><code>#wrapper.container.well first-attr="a literal attribute" second-attr=variable.name And here some text that will be the content of the div...
  </code></pre>
  
  <p>Into these nodes:</p>
  
  <pre><code>[
      'tagName' =&gt; 'div', // since there was no tagname, a div is assumed
      'attributes' =&gt; [
          'id' =&gt; 'wrapper',
          'class' =&gt; 'container well',
          'first-attr' =&gt; 'a literal attribute',
          'second-attr' =&gt; 'the value of the variable',
      ],
      'textContent' =&gt; 'And here some text that will be the content of the div...'
  ]
  </code></pre>
  
  <p>Of course I don't expect anyone to write out a function that 100% lexes / parses this, but I'd like to see the general pseudo code of how to go about this. Could anyone help me with this?</p>