Thread: Some parsers
View Single Post
  #18  
Old 03-16-2008, 10:32 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.1

Ok, I just noticed this thread, and I felt like I had to continue on it. So, I did what PFA suggested/pointed out and added support for multiple tags:

PHP Code:
/*      Made by Chompy
         Tag Parser v0.1

   This parser only do tags :)
*/
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 (
t2 == this.var_tag) {
          
value2 = makevar(value2);
        }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(var, p, p1, p2, p3, p5, p6) {
  switch(var) {
    case 
"sqrt": return p^0.5;
    break;
    case 
"percentage": return ((p1*p)/100);
    break;
    default: return 
"error";
  }
} 
And now, examples:

PHP Code:
function onCreated() {
  
this.a = 49;
  
temp.line = "<sqrt><variable>this.a</variable></sqrt>";
  echo(
parsetags(line)); // 7
} 
PHP Code:
function onCreated() {
  
temp.line = "<sqrt>49</sqrt>";
  
temp.line2 = "<sqrt><sqrt>49</sqrt></sqrt>";
  
temp.line3 = "<sqrt><sqrt><sqrt>49</sqrt></sqrt></sqrt>";
  echo(
parsetags(line));  // 7
  
echo(parsetags(line2)); // 2.645751311
  
echo(parsetags(line3)); // 1.626576561
} 
__________________
Reply With Quote