Thread: Some parsers
View Single Post
  #19  
Old 04-01-2008, 11:05 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
Tag Parser v0.2

PHP Code:
/*      Made by Chompy
         Tag Parser v0.2
         This parser only do tags :)

  Changelog:
  
    v0.2
      Added support for "root searching"* for variable tags
      (check out the example) (* dunno what to call it xD)

      Added support for auto detecting tags as values
      (check out the example).
*/
function onCreated() {
  
this.var_tag = "variable";
  
/* This is the variable tag the parser will look for,
    this can be edited so you can make it look for
    something shorter like <var></var>*/

  
this.func_tag = "function"; /* Same as above, but for functions */

  /* Custom functions, if a tag is not listed in 'this.var_tag' or
     'this.func_tag' it will look in this array for any custom functions
      to run. If a tag isn't in this command and is not a variable
      or function, the parser will return an error. */
  
this.custom_tags = {
    
"sqrt", "percentage"
  
};
}
function 
parsetags(line) {
  
temp.line2 = line;
  
temp.tags = 0;
  
temp.line3 = line2;
  
temp.tags_amount = line2.positions("<").size();
  if (
tags_amount/2 == int(tags_amount/2)) {
    while(
tags.size() != tags_amount/2) {
      
tags.add(line3.substring(1, line3.pos(">")-1));
      
line3 = line3.substring(line3.pos(">")+1);
    }
    
temp.value = temp.line.substring(line.positions(">")[tags_amount/2-1]+1,line.pos("</")-(line.positions(">")[tags_amount/2-1]+1));
    
temp.starttags = temp.endtags = 0;
    for(
temp.tag : tags) {
      while(
line2.pos("<"@tag@">") > -1) {
        
starttags.add(line2.substring(0, line2.pos(">")+1));
        
line2 = line2.substring(line2.pos(">")+1);
      }
    }
    for(
temp.tag : tags) {
      while(
line2.pos("</"@tag@">") > -1) {
        
endtags.add(line2.substring(line2.pos("</"), line2.pos(">")-line2.pos("</")+1));
        
line2 = line2.substring(line2.pos(">")+1);
      }
    }
    
temp.i = 0, temp.check = "";
    for(
temp.t : starttags) {
      
temp.ei = endtags.size()-1-i;
      if (
t.substring(1, t.length()-2) == endtags[ei].substring(2,endtags[ei].length()-3))
        
check @= "y"; else check @= "n";
      
i ++;
    }
    
temp.value2 = value;
    if (
check.pos("n") < 0) { 
      for(
temp.i = starttags.size()-1; i > -1; i --) {
        
temp.t = starttags[i];
        
temp.t2 = t.substring(1, t.length()-2);
        if (
value2.pos("</") > 3) {
          do {
            
value2 = parsetags(value2);
          } while(
value2.pos("</") > 3);
        }
        if (
t2 == this.var_tag) {
          do {
            
value2 = makevar(value2);
          } while(
makevar(value2) != NULL);
        }else if (
t2 == this.func_tag) {
          
temp.h = value.substring(0, value.pos("("));
          
temp.k = value.substring(value.pos("(")+1);
               
k = k.substring(0, k.length()-1);
          
temp.p = k.tokenize(",");
          
value2 = (@ h)(p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
          if (
value2 == 0) return "function return error";
        }else if (
t2 in this.custom_tags) {
          
temp.p = value2.tokenize(",");
          
value2 = custom(t2, p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
          if (
value2 == "error") return "custom return error";
        }else return 
"non-existant tag error";
      }
    }
    return 
value2;
  }
}
/*  Custom functions needs to be listed in custom() as a case, and they
    need to return data, or else the parser will return an error. I've added
    two examples so you can see how it's done :) */
function custom(var2, p, p1, p2, p3, p5, p6) {
  switch(
var2) {
    case 
"sqrt": return p^0.5;
    break;
    case 
"percentage": return ((p1*p)/100);
    break;
    default: return 
"error";
  }
} 

Now, example:

PHP Code:
function onCreated() {
  
this.b = "this.a";
  
this.a = "<sqrt>81</sqrt>";
  echo(
parsetags("<sqrt><variable>this.b</variable></sqrt>"));
// Output: 3
} 
So, enjoy..?

And, suggestions for what more to support etc. are welcome
__________________

Last edited by Chompy; 04-01-2008 at 11:24 PM..
Reply With Quote