Jump to content

need help matching template variables


deviantgeek6966

Recommended Posts

im writing a template engine and im stuck on matching all the variables with a regular expression. here are what they can look like:

 

$foo.bar

$foo[0].bar

$foo[$bar]

$foo[$bar].foobar

$foo[0].$bar.blah

$foo[$bar + 1].bar

$foo[$bar[0].$blah[$bleh].bluh + 1].bar

$foo.$bar

$foo[1 + 10].bleh.$bar

$foo

$foo.bar[$blah.foobar[0]].bleh[1 + 1]

$foo.bar.$blah[$f + 1][0].bleh

 

and so on, hopefully you get the idea. similar to accessing childen in php arrays except you can access children by using brackets ([ ]) or with the period (.). brackets are for mainly calculating an index or grabbing a certain char in a string. no need for referencing, i just need these variables whole so i can disassemble them myself. ive checked out smarty to see how they do it, but it seems they have a limit on how much you can nest things and im trying to avoid that. if anyone knows a better syntax for variables and a way of capturing them im open for suggestions. thanks

 

 

Link to comment
Share on other sites

I'm assuming you also want the ability to use ++, --, -, /, *, %, &, |, ^, ~, <<, and >> where you have used "+" in your example? What have you tried so far? This can be a big undertaking depending on how much nesting you're going to allow, and is most likely the reason you see limits in Smarty: heed the paths of those who have gone before you. The more nesting you allow, the slower your engine may be.

Link to comment
Share on other sites

yea i want the ability to use those as well. yea i know its a big undertaking. i might just make something simpler than this because i finally got an expression that works with limitless nesting in it but, i dunno if its my server thats going slow or if the expression is taking too long. i will have caching on my template engine so speed isnt a major problem but i dont want it to take too long to render something either. anyways here is what im using now, maybe someone can improve on it for me? if anyone wants to tackle decifering it, i will have to repost with a decifer of it because its really complicated lol. but! it works exactly how i wanted it to:

 

\$[a-zA-Z][\w]*(?:\[[\s]*(?:[\d]+|\$[a-zA-Z][\w]*(?:\[.*?\])*(?:[\.][a-zA-Z][\w]*(?:\[.*?\])*)*)(?:[\s]*[\+\-\%\*][\s]*(?:[\d]+||\$[a-zA-Z][\w]*(?:\[.*?\])*(?:[\.][a-zA-Z][\w]*(?:\[.*?\])*)*))*[\s]*\])*(?:[\.][a-zA-Z][\w]*(?:\[[\s]*(?:[\d]+|\$[a-zA-Z][\w]*(?:\[.*?\])*(?:[\.][a-zA-Z][\w]*(?:\[.*?\])*)*)(?:[\s]*[\+\-\%\*][\s]*(?:[\d]+||\$[a-zA-Z][\w]*(?:\[.*?\])*(?:[\.][a-zA-Z][\w]*(?:\[.*?\])*)*))*[\s]*\])*)*[^\.\[\]]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.