I want to write a specific purpose parser / lexer, but I feel depressed. Could you help me with this little example?

So, I am writing a php templating engine called Slade , inspired by Ruby Slim and laravel Blade .

And now, many people recommend that I rewrite it to the lexer / parser file, rather than relying entirely on regular expressions. So I searched for the words lexers and parsers and tried to find out how they work, and although I think I'm getting a general idea, it's still hard for me to start writing.

So, I was hoping that someone would help me along my way, showing how to make one example. How would I exactly lex (is it even a verb?) And analyzed 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... 

To 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 do not expect anyone to write a function that 100% lexes / analyzes this, but I would like to see a general pseudo-code on how to do this. Can anyone help me with this?

+6
source share

Source: https://habr.com/ru/post/985952/


All Articles