我想写一个特定用途的解析器/词法分析器,但我感到不知所措.你能用这个小例子帮我吗?

Eve*_*ert 6 php parsing lexer slim-lang blade

所以我正在编写一个名为Slade的php模板引擎,灵感来自Ruby Slimlaravel Blade.

现在很多人都建议我将它重写为词法分析器/解析器,而不是完全依赖正则表达式.所以我搜索了词法分析器和解析器并试图了解它们是如何工作的,虽然我认为我得到了一般的想法,但我仍然觉得很难开始写一个.

所以我希望有人能通过展示如何做一个例子来帮助我.我怎么会完全lex(甚至是一个动词?)并解析这个:

#wrapper.container.well first-attr="a literal attribute" second-attr=variable.name And here some text that will be the content of the div...
Run Code Online (Sandbox Code Playgroud)

进入这些节点:

[
    '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...'
]
Run Code Online (Sandbox Code Playgroud)

当然我不希望有人写出100%lexes/parses这个函数,但是我想看看如何解决这个问题的一般伪代码.任何人都可以帮我这个吗?