Where do I start when writing a new scripting “language”?

一世执手 提交于 2019-12-04 11:08:50
user187291

Another option is to parse your template into an xml document and transform it to another xml document, with your custom tags replaced with other tags (e.g. <?php processing instructions). In this case, XSL is what you're looking for.

It sounds like what you need is a templating language that supports being extended by custom tokens. Given that PHP itself meets that need, I'm guessing you also want sandboxing of some sort.

For that, I'd suggest TWIG.

By default, it uses the same basic syntax as Django and Jinja2 for Python or Liquid for Ruby (though, while not recommended, that is configurable) and it's compiled to cached PHP for speed.

It supports sandboxing and parameter auto-escaping as well as block substitution and inheritance, you choose what variables it gets access to, and you can set up any combination you want of default and custom tokens and filters.

Smarty might also meet your needs, but I'm not sure whether it has all the aforementioned features, its syntax is, in my opinion, not as elegant, and I'm told it's more pain than it's worth.

Whatever you do, think long and hard before inventing your own templating language. It's generally a huge pain in the long run and tends to end up on on The Daily WTF next to BobX sooner or later.

Update: I get the impression you're obsessed with using namespaced XML for your templating. Is it really worth reinventing an entire templating engine just so your users can use <wpml:header /> rather than {{header}}? TWIG doesn't let users embed arbitrary scripts... just variables and flow-control constructs you've explicitly OKed.

For custom XML you could use PHP XML parser preferably SAX for the performances.

Smarty is a very good PHP template engine with built-in tags, blocks and functions. You can extend those to create your own and even remove the built-in ones (for Smarty 3).

If you need to create your own script, I suggest you check language parser like Lex and Yacc. You'll have to define your language in a way like those SQLite images just not in a graphical manner but textually. There are other grammatical language parser available. Those I gave are among the oldest and most famous, but it was done for C++.

You'll probably want to avoid doing that yourself (like by using RegExp). Very soon you'll have many inconsistencies in your script. Even though RegExp are themself a kind of language interpreted by an automate.

You can mix the two: XML parser and general parser. Check out Finite-state machine (FSM).

I'd start within XML by defining what a typical page markup would look like and then move on to deciphering the XML in your chosen language and then taking that and creating HTML.

The xml should be a bunch of nodes that describes your particular language.

So...

<MyPage>
  <MyElement id="myid" type="MyType1">
    <MyElement id="myid" type="MyType1" Text="Some text"/>
  </MyElement>
  etc...

I'd be looking more carefully on the internet to see if there is already something re-built that would suit your needs before embarking on something like this which has the very real potential of becoming one of those things that gets out of control and impossible to maintain.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!