Page 1 of 1

How would I create a Parser for this?

Posted: Wed Sep 06, 2006 9:08 am
by AlexC
Hey,

I'm am trying to create what will be called, for my CMS - Module Display Rules which will do exactly what they say on the Tin, - Display Rules for each module that will be 'plugged' into the Template 'sectors'. A table structure will probably help:

*--- id ---+--- sector ---+--- modname ---+--- section ---*
* 1 ---------- SC
* 2 ---------- S1 -------------- title --------------- 1
* 2 ---------- S2 -------------- menu ------------- 1
* 2 ---------- S3 -------------- poll --------------- 1
* -------------------------------------------------------------------*

The main template file ( main_template.html ) has Tags in it which are for each Sector - eg {SC} {S1} {S2} {S3} - SC stands for Sector Content which will be the requested module from the url ( index.php?mod=news )

Now what I would like to add onto the table above ( tcm_template ) are Display Rules, so the table structure will be something like this:

*--- id ---+--- sector ---+--- modname ---+--- section --- +---------------------------------- displayRule ------------------------------*
* 1 -------- SC
* 2 -------- S1 ------------ title -------------- 1 ----------- DISPLAY 'news' IN 'S1' IF 'S3' == 'poll'
* 2 -------- S2 ------------ menu ------------- 1 ----------- DISPLAY 'title IN 'S2' IF 'S1' == 'news
* 2 --------- S3 ----------- poll --------------- 1 -------- DISPLAY 'download' IN 'S3' IF 'S1' != 'title' && 'S2' == 'title'
* ----------------------------------------------------------------------------------------------------------------------------------------------------*

Something like that anyway. I do not know how I would parse the Display Rules ( DISPLAY 'news' IN 'S1' IF 'S3' == 'poll' )

Gah I'm the worse person to explain something so it probably makes zero sense :(

Posted: Wed Sep 06, 2006 11:48 am
by Mordred
I think you are trying to make some complicated system, where a simple one will suffice. That, or are too stubborn to use a ready-made template system ;)

I see no reason why the logic of choosing "sectors" should be inside the template itself.
If you are 100% positive that you want that, then I suggest you choose a simpler format for the display rules, thattaway you won't wonder how to parse it ;)

Code: Select all

DISPLAY 'news' IN 'S1' IF 'S3' == 'poll'
This also has the problem of not defining what is to be used if S3 is not 'poll'

If you really really really want to parse this exact format, read up a bit on how compilers are made - generally you build a parser that turns a series of characters to a series of syntax tokens, then you implement your logic on them token thingies. I wouldn't recommend this approach really, but you ask how one would approach parsing code.

But my ultimate advice is KISS
Good luck.

Posted: Wed Sep 06, 2006 12:45 pm
by AlexC
That, or are too stubborn to use a ready-made template system
A ready-made template system would not help in what I want to do - Anyway I already have made my own template system which works fine.
I see no reason why the logic of choosing "sectors" should be inside the template itself.
The template file consists of tags in the form of {S1} {S2} - S standing for Sector - this is a good way of doing it Imo because this allows the user to move the content around for their site without editing any HTML Files and can be done via PHP/MySQL.
This also has the problem of not defining what is to be used if S3 is not 'poll'
If there was no display rule, or if the rule did not match - it would use what ever is in the field 'modname'

EDIT: I already have the structure above, and it works perfectly - all I am trying to implement are the Module Display Rules

Posted: Wed Sep 06, 2006 1:40 pm
by Christopher
Maybe if you broke your displayRules column in to two columns: display and rules. This would simplify "DISPLAY 'news' IN 'S1' IF 'S3' == 'poll' " to just 'news' and "'S3' == 'poll'" which would be easier to deal with.

Posted: Wed Sep 06, 2006 1:44 pm
by AlexC
arborint, that's not a bad idea - BUT how could I parse the == ? Because I would also like to incorperate != at some stage to.

Posted: Wed Sep 06, 2006 1:59 pm
by DrTom
strpos,str_split.. strtok
There's about a dozen different ways. here's one I like.

Code: Select all

$str = "b == a && c == d";
$stuff = str_replace(Array('a','b','c','d'),Array('a value','b value','c value','d value'),$str);
eval('$bool = '.$stuff);
if($bool === true)
{
// The rule was true
}
else
{
//The rule was false
}
In this method you don't really have to worry too much about parsing the exact syntax. Any normal boolean expression will work.

Posted: Wed Sep 06, 2006 2:45 pm
by Mordred
AlexC_ wrote:
I see no reason why the logic of choosing "sectors" should be inside the template itself.
The template file consists of tags in the form of {S1} {S2} - S standing for Sector - this is a good way of doing it Imo because this allows the user to move the content around for their site without editing any HTML Files and can be done via PHP/MySQL.
You're not paying attention. I didn't say not to have sectors. You just move the logic of choosing which one to use in the php code. You must have some method of setting module per sector from php - use this for that logic.

Actually it cannot be done from the template only - since you have defaults, the logic would either choose one and the same module for each sector, or loop indefinitely (if it is applied continuously). Since you must have logic in the php about the sectors, I don't see why part of it should be in the templates.

Posted: Wed Sep 06, 2006 2:51 pm
by AlexC
I can't hand code what Modules are to be Plugged into which Sectors, otherwise if the user wanted to change their content layout that would mean manually changing the PHP files - which is not pratical for a CMS. Storing which Module goes in what Sector in the MySQL database allows for this flexibility in terms of Content Layout, and the MDR's should increase this further.

EDIT: Would showing my entire code what I have so far, for plugging a Module into a Sector help?

Posted: Wed Sep 06, 2006 3:05 pm
by Christopher
AlexC_ wrote:arborint, that's not a bad idea - BUT how could I parse the == ? Because I would also like to incorperate != at some stage to.
You could do splits in order of precidence. So split on '&&' first, then split the parts on '||', then loop through that array and split on '==' or '!='. You might need to find the patterns first to verify what you are splitting.