anon
anon

Reputation:

Custom parser, how to make conditional statment?

I have my own template engine. It can converts variable like this:

html html html (=array.value) html html

with something like this (using regex):

html html html $array['value'] html html

Now I would be able to use conditional statament. Something like:

html html html
(if (condition))
 (=array.value)
(endif)
html html html

How can I make such parser without having to use eval?

Upvotes: 2

Views: 183

Answers (1)

lawl0r
lawl0r

Reputation: 870

I guess to do such kind of evaluations you need to actually implement a full parser/lexer. I.e. split everything into tokens, build an AST and parse it. of course that depends on how complex your conditions can be.

But if you want to do stuff like (=array.value) you probalby need to code your own mini-scripting language.

While not exactly PHP/HTML related, i found this pretty helpful to get a grasp on what compilers/parsers/lexers are actually doing.

Or of couse, as you mentioned, just use php and eval it, that's the easiest way.

Upvotes: 1

Related Questions