Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-01-2008, 04:16 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
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)
__________________
Reply With Quote
  #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
  #3  
Old 01-01-2008, 04:41 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
         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>

__________________
Reply With Quote
  #4  
Old 01-01-2008, 05:13 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Very nice! I think someone already made one of these though
__________________
Reply With Quote
  #5  
Old 01-01-2008, 05: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
Quote:
Originally Posted by cbkbud View Post
Very nice! I think someone already made one of these though
who and which one? :O
__________________
Reply With Quote
  #6  
Old 01-01-2008, 06:17 PM
Codein Codein is offline
jwd
Codein's Avatar
Join Date: Oct 2005
Location: Greater Manchester
Posts: 2,423
Codein has a spectacular aura aboutCodein has a spectacular aura about
Send a message via AIM to Codein Send a message via MSN to Codein
Quote:
Originally Posted by Chompy View Post
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
Reply With Quote
  #7  
Old 01-01-2008, 10:06 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
Quote:
Originally Posted by Codein View Post
I think TheGlory was trying to make Parser 3, but I did know that this was under your instruction =P
Gemini? Oh yeah
__________________
Reply With Quote
  #8  
Old 01-03-2008, 01:25 AM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
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
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #9  
Old 01-05-2008, 06:29 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Ahh, Chompy. Always parsing something. You, sir, are the parse-master.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #10  
Old 01-05-2008, 04: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
Quote:
Originally Posted by coreys View Post
Ahh, Chompy. Always parsing something. You, sir, are the parse-master.
Naw, PFA has the best parser that I've seen
__________________
Reply With Quote
  #11  
Old 01-08-2008, 02:29 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Chompy View Post
Naw, PFA has the best parser that I've seen
Thank you. I assume you mean the XML one, which I've updated to look more C++-like.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/

Last edited by Tolnaftate2004; 01-08-2008 at 02:40 AM..
Reply With Quote
  #12  
Old 01-08-2008, 04:18 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
Quote:
Originally Posted by Tolnaftate2004 View Post
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
__________________
Reply With Quote
  #13  
Old 01-08-2008, 09:54 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Chompy View Post
Yeah, I learned some nifty/nice stuff from that one, so thanks <3
I tried your latest:

PHP Code:
echo(cParseLine("The fourth root of 81 is <sqrt><sqrt>81</sqrt></sqrt>.")); 
The fourth root of 81 is </sqrt>.

PHP Code:
this.test 9;
echo(
cParseLine("The square root of `this.test` is <sqrt><variable>this.test</variable></sqrt>.")); 
The square root of `this.test` is
(I'd like to point out that the period disappeared.)



e: I see you're using if (~some_index). Helpful hint: the engine has to perform twice as many tasks this way. It is more efficient to use the old fashioned if (some_index >= 0). [I know that I used it too...]
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #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
  #15  
Old 01-08-2008, 12:49 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Chompy View Post
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!
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 08:51 PM.


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