Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Some parsers (https://forums.graalonline.com/forums/showthread.php?t=78172)

Chompy 01-01-2008 04:16 PM

Some parsers
 
Well, lately when I didn't have subscription I felt like.. making parsers for some really odd reason, don't ask why :(

Anyways,
PHP Code:

/*      Made by Chompy
         Parser v1.0

    Usage:
    
      <$var|this.var$>
      
      <$function|function(params)$>
         * params can't be spaced (ex. (foo, bar)), have to be (foo,bar)
*/
public function cParse(a) {
  
temp.temp.a.tokenize(" ");
  
  
temp.0;
  for(
temp.b) {
    if (
c.pos("<$") > -1) {
      
temp.c.substring(0c.pos("<$") + 2);
      
temp.c.substring(d.length(), c.pos("|") - d.length());
      
temp.c.substring(c.pos("|") + 1c.pos("$>") - (d.length() + e.length()) - 1);
      
temp.c.substring(c.pos("$>") + 2);
      switch(
e) {
        case 
"var":
          
b[i] = d.substring(0d.length() - 2);
          
b[i] @= makevar(f);
          
b[i] @= g;
        break;
        case 
"function":
          
temp.f.substring(0f.pos("("));
          
temp.f.substring(f.pos("(") + 1f.pos(")") - (j.length() + 1));
          if (
makevar(k) != NULL) {
            
makevar(k);
          }
          
temp.k.tokenize(",");
          
temp.= (@ j)(@ p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
          
b[i] = d.substring(0d.length() - 2);
          
b[i] @= l;
          
b[i] @= g;
        break;
        default:
          if (
0) {
            
b[i-1] = b[i-1].substring(0b[i-1].length());
          }
          
b[i] = d.substring(0d.length() - 2);
          
b[i] @= g;
        break;
      }
    }
    
++;
  }
  
temp."";
  for(
temp.str b) {
    
@= str " ";
  }
  return 
h;


Basicly, you can do:

PHP Code:

function onCreated() {
  
this.nick "Chompi";
  echo(
cParse("Hi <$var|this.nick$>!"));


And it will output "Hi Chompi!".

It will parse all variables and functions it finds :)


I'll also use this thread to post my other parsers that I've made, so.. have fun? xD
(PS: I know they probably isn't for good use, butI learnt much out of making parsers so by reading them or making your own, you learn how things works :o)

Chompy 01-01-2008 04:25 PM

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.

Chompy 01-01-2008 04:41 PM

PHP Code:

/*      Made by Chompy
         Compile v1.0

    Usage:
    
      compile("toobj", temp.lines);
        Will parse/compile an array of lines (XML Like lines, see
        usage under the script itself. Anyways, will convert
        those lines to an object that is readable
          
          return = obj - [type2]

      compile("fromobj", temp.object);
        Will parse/compile an object into XML-Like lines

          return = arr - [type3]

      Currently doesn't support multi-dimensional arrays in 'fromobj' mode :(
      Might add later tho..
*/ 
function compile(modeobj) {
  
// config
  
temp.array = {"float""string""object""integer"};
  if (
mode == "fromobj" && (obj.type() == 2)) {
    
temp.dynamic obj.getDynamicVarNames();
    
temp.vars request("filter"obj.getVarNames(), dynamic);
  
    
temp.lines = {"<objecttype='"obj.objecttype() @"'>"};
    
temp.0;
    for(
temp.var : dynamic) {
      
1;
      if (
makevar("obj." @ var).type() == 3) {
        
lines.add(request("addspace""<array='"@ var @"'>"i));
        
2;
        
temp.0;
        for(
temp.var2 makevar("obj." @ var)) {
          
lines.add(request("addspace""<index["@"]="makevar("obj." @ var)[c] @">"i));
          
++;
        }
        
lines.add(request("addspace""</array>"1));
      }else{
        
temp.suffix = array[makevar("obj." @ var).type()];
        if (
suffix == "float") {
          if (
makevar("obj." @ var) == int(makevar("obj." @ var))) suffix "integer";
        }
        
lines.add(request("addspace""<"suffix @"='"@ var @"'>" makevar("obj." @ var) @"</"suffix @">"i));
      }
    }
    
temp.lines.add("</objecttype>");
    return 
temp.lines;
  }else if (
mode == "toobj") {
    if (
obj.type() == 3) {
      
temp.prefix request("getvar"obj[0]);
      
temp.object 0;
      if (
prefix == "TStaticVar"object = new TStaticVar();
      else 
object = new TGraalVar();
      if (
request("hasendkey"obj)) {
        
temp.isarray false;
        
temp.compilearray false;
        
temp.compiledarray = {};
        
temp.arraystart 0;
        
temp.arrayend 0;
        for(
temp.line obj.subarray(1obj.size() - 2)) {
          
temp.key request("getkey"line);
          if (
key == "array") {
            
arraystart obj.index(line);
            
arrayend request("hasarrayend"objarraystart);
            if (
arrayend[0]) {
              
isarray true;
              continue;
            }
          }else if (
key in {"float""integer""string"}) {
            if (
request("hasendlinekey"keyline)) {
              
object.(@ request("getvar"line)) = request("getvalue"line);
            }
          }
          if (
isarray) {
            if (
obj.index(line) == arrayend[1]) {
              
isarray false;
              
compilearray true;
            }else 
compiledarray.add(request("getvalue2"line));
          }
          if (
compilearray) {
            
temp.varname request("getvar"obj[arraystart]);
            
object.(@ varname) = compiledarray;
            
compiledarray 0;
            
compilearray false;
            continue;
          }
          if (
line == "</objecttype>") break;
        }
        return 
object;
      }
    }
  }
}
function 
request(modeabc) {
  switch(
mode) {
    case 
"filter":
      
temp.out a;
      for(
temp.b) {
        
temp.ind a.index(i) > -1;
        if (
ind > -1out.remove(i);
      }
      return 
out;
    break;
    case 
"addspace"
      
temp.0temp.out "";;
      while(
b*2) {
        
out @= "  ";
        
+= 2;
      }
      
out @= a;
      return 
out;
    break;
    case 
"getvar":
      if (!
a.starts("</")) {
        
temp.out a;
             
out a.substring(a.pos("'") + 1);
             
out out.substring(0out.pos("'"));
      }else 
out a.substring(2a.length() - 3);
      return 
out;
    break;
    case 
"getvalue":
      
temp.out a;
           
out a.substring(a.pos(">") + 1);
           
out out.substring(0out.pos("<"));
      return 
out;
    break;
    case 
"getvalue2":
      
temp.out a;
           
out a.substring(a.pos("=") + 1);
           
out out.substring(0out.pos(">"));
      return 
out;
    case 
"hasendkey":
      
temp.compare request("getvar"a[a.size() - 1]);
      if (
compare == "objecttype") return true;
      return 
false;
    break;
    case 
"hasarrayend":
      
temp.out = {false, -1};
      for(
temp.a.indices("  </array>")) {
        if (
b) {
          
out = {trued};
          break;
        }
      }
      return 
out;
    break;
    case 
"hasendlinekey":
      if (
b.pos("</"@">") > 2) return true;
      else return 
false;
    break;
    case 
"getkey":
      
temp.out a.trim();
      return 
out.substring(1out.pos("=") - 1);
    break;
  }


Usage1:
PHP Code:

function onCreated() {
  
temp.lines = {
    
"<objecttype='TStaticVar'>",
    
"  <integer='foo'>bar</integer>",
    
"  <array='array'>",
    
"    <attr[0]=foo>",
    
"    <attr[1]=bar>",
    
"  </array>",
    
"  <string='test'>FOOBARBAZ</string>",
    
"</objecttype>"
  
};
  
temp.object2 compile("toobj"temp.lines);
  echo(
object2.getdynamicvarnames());   // Output: 'array,foo,test'
  
echo(object2.array[0]); // Output: 'foo'
  
echo(object2.test); // Output: 'FOOBARBAZ'


Usage2:
PHP Code:

function onCreated() {
  
temp.object = new TStaticVar();
       
object.var = "foo";
       
object.var2 "bar";
       
object.array = {"foo""bar""baz"};
       
  
temp.object2 compile("fromobj"temp.object);
  
  for(
temp.var : object2) {
    echo(var);
  }


Output:
NPC Code:

<objecttype='TStaticVar'>
<array='array'>
<index[0]=foo>
<index[1]=bar>
<index[2]=baz>
</array>
<string='var'>foo</string>
<string='var2'>bar</string>
</objecttype>


cbk1994 01-01-2008 05:13 PM

Very nice! I think someone already made one of these though

Chompy 01-01-2008 05:25 PM

Quote:

Originally Posted by cbkbud (Post 1367357)
Very nice! I think someone already made one of these though

who and which one? :O

Codein 01-01-2008 06:17 PM

Quote:

Originally Posted by Chompy (Post 1367361)
who and which one? :O

I think TheGlory was trying to make Parser 3, but I did know that this was under your instruction =P

Chompy 01-01-2008 10:06 PM

Quote:

Originally Posted by Codein (Post 1367376)
I think TheGlory was trying to make Parser 3, but I did know that this was under your instruction =P

Gemini? Oh yeah :p

JkWhoSaysNi 01-03-2008 01:25 AM

Actually I made an xml parser/generator: http://forums.graalonline.com/forums....php?p=1314210
http://forums.graalonline.com/forums...ad.php?t=74882

the object from XML is slightly buggy, it doesn't support objects in arrays under 2 levels.

Nice work though :)

Chompy 01-03-2008 06:40 PM

Quote:

Originally Posted by JkWhoSaysNi (Post 1367737)
Actually I made an xml parser/generator: http://forums.graalonline.com/forums....php?p=1314210
http://forums.graalonline.com/forums...ad.php?t=74882

the object from XML is slightly buggy, it doesn't support objects in arrays under 2 levels.

Nice work though :)

Yeah, that xml parser inpired me for parser3 and an other project I'm doing, will post it later when I'm done :O (the other project)

coreys 01-05-2008 06:29 AM

Ahh, Chompy. Always parsing something. You, sir, are the parse-master.

Chompy 01-05-2008 04:32 PM

Quote:

Originally Posted by coreys (Post 1368171)
Ahh, Chompy. Always parsing something. You, sir, are the parse-master.

Naw, PFA has the best parser that I've seen :p

Chompy 01-08-2008 12:27 AM

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! :p 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"?

cbk1994 01-08-2008 12:49 AM

Quote:

Originally Posted by Chompy (Post 1368730)
NOTE TO MODS: Do you guys mind renaming the thread to "Some parsers"?

No, they are too busy. How dare you ask that they do something. You are way out of line, mister!

Tolnaftate2004 01-08-2008 02:29 AM

Quote:

Originally Posted by Chompy (Post 1368216)
Naw, PFA has the best parser that I've seen :p

Thank you. I assume you mean the XML one, which I've updated to look more C++-like.

Chompy 01-08-2008 04:18 PM

Quote:

Originally Posted by Tolnaftate2004 (Post 1368751)
Thank you. I assume you mean the XML one, which I've updated to look more C++-like.

Yeah, I learned some nifty/nice stuff from that one, so thanks :) <3


All times are GMT +2. The time now is 05:30 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.