Thread: Some parsers
View Single Post
  #14  
Old 01-08-2008, 12:27 AM
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
         Parser4 v1.1

    Usage:
    
      cParselines(lines) - return arr[]
      
        Will loop through the lines and parse each line with
        cParseline() and then return an array of all the
        lines parsed. Can be laggy with A Lot of lines.
        
    
      cParseline(line) - return = str

        Will parse all tags listed in the onCreated event
          this.var_tag - change this to customize the
                         variable tag to look for
                         Ex.
                         <variable>player.account</variable>
                         
          this.func_tag - change this to customize the
                          function tag to look for
                          ex.
                          <function>test(a,b,c)</function>
                          
          this.custom_tags 
            - The tags listed in this array will be sent
              to a function to compile an own action for
              the tag sent, I've made some examples.
              Look at the bottom of the script (custom()
              function).
              
              ex.
              <sqrt>49</sqrt> = 7
              <percentage>50,100</percentage> = 50
              
        Supports:
          *Customization of tags
          *Custom tag actions (must be scripted yourself,
                               look at the bottom of the
                               script for some examples)
                               
          *Several params (Just don't space them
                           correct:   a,b,c
                           incorrect: a, b, c)
              
*/ 

// Configuration is done in the OnCreated event!
function onCreated() {
  
this.var_tag "variable";  // variable tag to look for
  
this.func_tag "function"// function tag to look for

  
this.custom_tags = { // custom tags to look for
    
"sqrt""stringify""percentage"
  
};
}
// OBS!
// Don't change 'cParselines()' and 'cParseline()'!
public function cParselines(lines) {
  if (
lines.type() == 3) {
    
temp.out 0;
    for(
temp.line lines) {
      
out.add(cParseline(line));
    }
    return 
out;
  }
}
public function 
cParseline(line) {
  
temp.tokens line.tokenize(" ");
  
temp.parsed "";
  
  for(
temp.token tokens) {
    
temp.out "";
    
temp.tag token.pos("<");
    if (~
tag) {
      if (
tag>0temp.prefix token.substring(0tag);
      
temp.var = token.substring(tag+1token.pos(">")-(tag+1));
      if (
token.pos("</" @ var @ ">") < 0) continue;
      
temp.ctag "";
      if (var == 
this.var_tagctag this.var_tag;
      else if (var == 
this.func_tagctag this.func_tag;
      
temp.mode = (ctag==""&&var in this.custom_tags)?"custom":"normal";
      
temp.value token.substring(prefix.length()+(var.length()+2));
      
temp.value value.substring(0,value.pos("</"));
      
temp.endtag token.substring(token.pos("</"));
      
temp.suffix endtag.substring(endtag.pos(">")+1);
      
temp.endtag endtag.substring(2endtag.pos(">")-2);
      if (var == 
endtag) {
        switch(
mode) {
          case 
"normal":
            if (
ctag == this.var_tag) {
              if (
prefix.length() > 0out @= prefix;
              
out @= makevar(value);
              if (
suffix.length() > 0out @= suffix;
              
parsed @= out " ";
            }else if (
ctag == this.func_tag) {
              
temp.value.substring(0value.pos("("));
              
temp.value.substring(value.pos("(")+1);
                   
k.substring(0k.length()-1);
              
temp.k.tokenize(",");
              if (
prefix.length() > 0out @= prefix;
              
out @= (@ h)(p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
              if (
suffix.length() > 0out @= suffix
              
parsed @= out " ";
            }
          break;
          case 
"custom":
            if (
prefix.length() > 0out @= prefix;
            if (
value.pos(",") == -1out @= custom(var, value);
            else{
              
temp.value.substring(0value.pos(">"));
              
temp.h.tokenize(",");
              
out @= custom(var, p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
            }
            if (
suffix.length() > 0out @= suffix
            
parsed @= out " ";
          break;
        }
      }
    }else 
parsed @= token " ";;
  }
  return 
parsed;
}

// Change these to add custom tag actions/functions.

// These will not be executed if they are not listed
// in 'this.custom_tabs'
function custom(var, pp1p2p3p4p5p6) {
  switch(var) {
    case 
"stringify": return "\"" "\"";
    break;
    case 
"sqrt": return p^0.5;
    break;
    case 
"percentage":  return ((p1*p)/100);
    break;
  }

Some examples are written in the note blocks on the top of the script, but I'll post some more examples if someone wants/needs :o

Btw, this parses lines, not just only tags! In next update of this (1.2) I'll add a function to parse tags only

So it can parse:
PHP Code:
echo(cParseline("Square root of 9 is <sqrt>9</sqrt>"));
// would output 'Square root of 9 is 3' 
etc.

(Parsers that I make will work clientside aswell, if they don't, not much is needed to make them work clientside!)




NOTE TO MODS: Do you guys mind renaming the thread to "Some parsers"?
__________________

Last edited by Chompy; 01-08-2008 at 12:38 AM..
Reply With Quote