Thread: Some parsers
View Single Post
  #2  
Old 01-01-2008, 04:25 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
PHP Code:
/*      Made by Chompy
         Parser2 v1.0

    Usage:
    
      <?this.var?>
      
      <?function()?>
         * params can't be spaced (ex. (foo, bar)), have to be (foo,bar)
*/
public function parseLine(line) {
  temp.t = line.tokenize(" ");
  temp.t2 = t;
  
  temp.ind = 0;
  for(temp.a : t) {
    if (a.pos("<?") > -1) {
      temp.b = "";
      if (a.starts("<?")) temp.b = a.substring(2);
      else{
        temp.b = a.substring(a.pos("<?") + 2);
        temp.e = a.substring(0, a.pos("<?"));
      }
      temp.c = b.substring(b.pos("?>") + 2);
      temp.b = b.substring(0, b.pos("?>"));
       
      temp.mode = 0;
      if (b.pos("(") > -1) mode = 2;
      else mode = -1;
      
      switch(mode) {
        case -1:
          t2[ind] = "";
          if (e.length() > 0) t2[ind] @= e;
          t2[ind] @= makevar(b);
          if (c.length() > 0) t2[ind] @= c;
        break;
        case 2:
          temp.h = b.substring(0, b.pos("("));
          temp.k = b.substring(b.pos("(") + 1);
               k = k.substring(0, k.length() - 1);
          
          temp.p = k.tokenize(",");
          t2[ind] = "";
          if (e.length() > 0) t2[ind] @= e;
          t2[ind] @= (@ h)(p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
          if (c.length() > 0) t2[ind] @= c;
        break;
      }
    }
    ind ++;
  }
  temp.parse = "";
  for(temp.g : t2) {
    parse @= g @ " ";
  }
  return parse;
}
Simple usage:

PHP Code:
function onCreated() {
  func = function() {
    return "CHOMPI";
  };
  this.var = "Zeo";
  temp.line = "Hi <?this.var?> and <?func()?>!";
  echo(parseLine(temp.line));
}
(Parser will also work clientside)

output is "Hi Zeo and CHOMPI!" (OH, who see it? Hidden 'easter egg')

It will parse all variables and functions it finds

Basicly this parser detects if it should parse a variable or a function, and works from there.
__________________
Reply With Quote