So I'm writing a php templating engine called Slade inspired by Ruby Slim and laravel Blade.
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.
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:
#wrapper.container.well first-attr="a literal attribute" second-attr=variable.name And here some text that will be the content of the div...
Into these nodes:
[
'tagName' => 'div', // since there was no tagname, a div is assumed
'attributes' => [
'id' => 'wrapper',
'class' => 'container well',
'first-attr' => 'a literal attribute',
'second-attr' => 'the value of the variable',
],
'textContent' => 'And here some text that will be the content of the div...'
]
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?