Merry strings and a happy new parser :D

7 views
Skip to first unread message

Ovidiu

unread,
Dec 29, 2014, 9:39:49 AM12/29/14
to professi...@googlegroups.com
Hello everybody!

First off, let me thank you all for your help regarding my posts and (if it applies to you) for that, I wish you the most glorious moments for the holidays and a well accomplished new year!

After all the holidays that are going on right now, there is nothing like a time off from all the season intended meals and drinks, and, like the geek that I am (sometimes), I am struggling with yet another logic problem regarding a XML parser; well, not exactly XML but a multidimensional array;

I've done many self-recursion parsers in my time but I've come to block myself from achieving a (somewhat) simple task involving a string compiler.

Before reading on, I am open to any opinions or criticism regarding my current code because I need some help. So, if you have some spare time or you have done such kind of scripting I appreciate your time.

The purpose of the compiler:

- create a string of callable functions and arguments; ex: scan('paper', from('folder')) && send('email', with('scanned_results')) && destroy('scanned_results') && laugh(); //notice here there can be functions without parameters (children)
- the string must be callable from javascript; the && syntax is just for this example, I'll use promises;

The given object is JSON, for the obvious reason: readability.

[
  1. {
    • "name":"scan",
    • "type":"function",
    • "children":[
      1. {
        • "name":"paper",
        • "type":"parameter"
        },
      2. {
        • "name":"from",
        • "type":"function",
        • "children":[
          1. {
            • "name":"folder",
            • "type":"parameter"
            }
          ]
        }
      ]
    },
  2. {
    • "name":"send",
    • "type":"function"
    }
]


 //the rest is obvious

So far so good, the script that I have works well, It does exactly what I wanted to do.

I'm going to talk about my code instead of just pasting it here because it's rather unrelated; the concept is :)

here's the concept example:

public static function parser( $params ) {

        $start_string = '';
        $middle_string = '';
        $end_string = '';
        $final_string = '';

    for( $x=0, $t=count($params); $x<$t; $x++) {

        if($params[$x]['type'] === 'parameter'){
            $start_string .= "' . $params[$x]['name'] . "'"; //quotation marks for parameters
        } else {
            $start_string .= $params[$x]['name'];
        }
        
        if($params[$x]['type'] === 'function'){
            $start_string .= '(';
            $end_string .= ')';
        }

        if(isset($params[$x]['children'])){
            $middle_string .= self::parser($params[$x]['children'];
        }

        if($t > ($x+1)){
            $middle_string .= ', '; //separate the parameters
        }

    }

    $final_string .= $end_string;

    return $final_string;

}

I have control over both the received JSON and the PHP script;

The main reason I'm writing to you nice :) folks is because, sometimes, the JSON hierarchy is messed up and I need to account for that from either PHP or the JSON-generating script.

Here's an example:

Say I have an array with a function without children -- meaning single function, no parameters, right? After that, follows all the rest of the JSON as a sibling. Well, what if that single function should be the up-most parent and instead of the && (or promises) syntax, the rest of the array must be nested as children? Let me make some sense:

[{name:'try', type:'function'}, {name:'scan', type:'function' [...]}

Following my current logic, the parsed string looks like this : try() && scan('paper', from('folder'))[...] -- the try() function does absolutely nothing here

As I mentioned before, I have absolute control over both the parser and the JSON script.

So, in order to try() something, the arguments must be inside the parenthesizes, right?

I'm currently stuck at this point because I don't know what to change: either the JSON script or put (some more) logic into the parser.


If you find this concept fascinating and you have some thoughts (or links) for this problem I hope that you give me a reply and point me to the right direction.

I'll make public the code after making it work, if any of you will have a similar need for a project.


Thank you in advance, I still love PHP :) but Javascript is the chosen solution here because of async and setTimeout -- infinite recursions;


Cheers!



 

Reply all
Reply to author
Forward
0 new messages