Look for simple script tut to do special parsing

I want to be able to parse in php a structure like this:


[word1 word2 [
   word3 [
       ...
   ]
  ]
]

I don’t want a full library just the shortest script as possible to get started myself.

Thanks.

Maybe someone else will understand, but I don’t get it. Some more clarification and an example of how it should work might be nice.

This would be a real primitive way to parse into a stream of tokens


$tokens = array();
$len = strlen($source);
$i = 0;

while ($i < $len) {

    if (ctype_space($source[$i])) {
        $i++;
    }

    elseif ($source[$i] === '[') {
        $tokens[] = '[';
        $i++;
    }
    
    elseif ($source[$i] === ']') {
        $tokens[] = ']';
        $i++;
    }

    elseif (ctype_alnum($source[$i])) {
        $buf = '';
        do {
            $buf .= $source[$i];
            $i++;
        } while ($i < $len && ctype_alnum($source[$i]));
        $tokens[] = $buf;
    }

    else {
        //unknown
    }
}

Sorry it maybe my english as I’m not native :slight_smile: I want to parse like if I was parsing XML format except it’s much simpler since instead of tag I have brackets :


[Family 
   [Parents [
      Father: "Joe"
      Mother: "Marie"
   ]
   Children [
      Robert Nelly
   ]
]

Thanks a lot will look at it.

Not sure if it will work but couldn’t you do a replace on the square brackets so it would look like xml and then use simple_xml to parse it?

Well. You need to describe the grammar that you want to parse first. You have already expanded it since your first post. Going from:

To:

It’s also a bit inconsistent looking to me. Why aren’t the brackets balanced on “Parents”? Why do you use a colon to separate label and value in some places, but not in other places?

Well my purpose is to have something like yaml or json but compatible with rebol so that I can mix Rebol and PHP almost like one single language but thanks for suggestion that may be an intermediate solution.