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

Tolnaftate2004 01-08-2008 09:54 PM

Quote:

Originally Posted by Chompy (Post 1368825)
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...]

Chompy 01-08-2008 11:08 PM

Quote:

Originally Posted by Tolnaftate2004 (Post 1368856)
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.)

:(

Hmm, well atm it looks for the first endtag that it finds and will work from there, will need to add some more checks to support multiple tags inside eachother :o

Quote:

Originally Posted by Tolnaftate2004 (Post 1368856)
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...]

hehe, thanks :p

Will improve it, thanks PFA :D

Chompy 03-16-2008 10:32 PM

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/== int(tags_amount/2)) {
    while(
tags.size() != tags_amount/2) {
      
tags.add(line3.substring(1line3.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(0line2.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.0temp.check "";
    for(
temp.starttags) {
      
temp.ei endtags.size()-1-i;
      if (
t.substring(1t.length()-2) == endtags[ei].substring(2,endtags[ei].length()-3))
        
check @= "y"; else check @= "n";
      
++;
    }
    
temp.value2 value;
    if (
check.pos("n") < 0) { 
      for(
temp.starttags.size()-1> -1--) {
        
temp.starttags[i];
        
temp.t2 t.substring(1t.length()-2);
        if (
t2 == this.var_tag) {
          
value2 makevar(value2);
        }else if (
t2 == this.func_tag) {
          
temp.value.substring(0value.pos("("));
          
temp.value.substring(value.pos("(")+1);
               
k.substring(0k.length()-1);
          
temp.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.value2.tokenize(",");
          
value2 custom(t2p[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, pp1p2p3p5p6) {
  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.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



Tolnaftate2004 03-16-2008 10:53 PM

Well done!
Tried with this:
PHP Code:

this."this.b";
this.81;
echo(
parsetags("<sqrt><sqrt><variable><variable>this.a</variable></variable></sqrt></sqrt>")); 

Worked like a charm.

Chompy 03-16-2008 11:02 PM

Thanks PFA <3 :D

Chompy 04-01-2008 11:05 PM

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/== int(tags_amount/2)) {
    while(
tags.size() != tags_amount/2) {
      
tags.add(line3.substring(1line3.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(0line2.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.0temp.check "";
    for(
temp.starttags) {
      
temp.ei endtags.size()-1-i;
      if (
t.substring(1t.length()-2) == endtags[ei].substring(2,endtags[ei].length()-3))
        
check @= "y"; else check @= "n";
      
++;
    }
    
temp.value2 value;
    if (
check.pos("n") < 0) { 
      for(
temp.starttags.size()-1> -1--) {
        
temp.starttags[i];
        
temp.t2 t.substring(1t.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.value.substring(0value.pos("("));
          
temp.value.substring(value.pos("(")+1);
               
k.substring(0k.length()-1);
          
temp.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.value2.tokenize(",");
          
value2 custom(t2p[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(var2pp1p2p3p5p6) {
  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."this.a";
  
this."<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 :)

Chompy 04-02-2008 06:12 PM

Variable parser v0.1
 
PHP Code:

/*      Made by Chompy
         Variable Parser v0.1

    Features:
      
      - Parses $var$ tags (Also added a temp.look var, so you can change the symbol)
        (Atm only support single character symbols, will add support for more later)
      - Auto detects the type of the var and works with that
      - Can look up temp vars from the caller object (object calling parse())
      - Variables don't need a prefix, but recommended

    Was originally made to only support percentage symbols which is often used for parsing
    variables in many programs etc. But since the forums don't allow the percentage symbol
    I made it so you could define the symbol it will use, 'temp.look' in the start of parse()
*/
public function parse(string) {
  
temp.tokens string.tokenize();
  
temp.tokens2 tokens;
  
temp.out "";
  
temp.look "$";  

  for(
temp.token tokens) {
    
temp.pos token.positions(look);
    if (
pos.size() > 0) {
      
out @= token.substring(0pos[0]);
      if (
pos.size()/== int(pos.size()/2)) {
        
temp.out2 "";
        for(
temp.0pos.size(); += 2;) {
          
temp.value token.substring(pos[i]+1pos[i+1]-pos[i]-1);
          
temp.check isVariable(value);
          if (
check[0]) {
            switch(
check[2]) {
              case 
"this":
              case 
"server":
              case 
"serverr":
              case 
"global":
                
out2 @= makevar(check[1]);
              break;
              case 
"client":
              case 
"clientr":
              case 
"player":
                if (
player.account == 0) return "No player object";
                
out2 @= player.(@check[1]);
              break;
              case 
"temp":
                
temp.callstack getCallStack();
                
temp.callstack[callstack.size()-2].(@check[1].substring(check[1].pos(".")+1));
                
                if (
!= 0)
                  
out2 @= b;
            } 
          }else return 
"Invalid variable prefix";
        }
        
out @= out2;
      }else return 
"Amount of "@look@"'s doesn't match";
      
out @= token.substring(pos[pos.size()-1]+1) @ " ";
    }else 
out @= token " ";
  }
  return 
out;
}
function 
isVariable(var) {
  
temp.check = {false""};
  if (var.
pos(".") < 0) {
    
check = {true"temp."@var, "temp"};
      
  }else{
    
temp.= var.substring(0, var.pos("."));
    if (
a in {"this""player""server""serverr""clientr""client""temp"}) {
      
check = {true, var, a};
    }
  }
  return 
check;


Now, example:

PHP Code:

function onCreated() {
  
temp.PLAYERNAME "Chompy";
  echo(
parse("Hi $PLAYERNAME$!"));
  
// Outputs "Hi Chompy!";




Edit: Could a moderator rename the thread to "Some parsers"?

Chompy 05-02-2008 03:11 AM

PHP Code:

/*      Made by Chompy
         Some Parser v0.1

      Features
        
        -Customize directory delim (default '-')
        -Allows unlimited directories
        -Assigns id to var (<var id='1'>test</var> -> var1=test)
        -Smart directory and ending

      Todo

        -Add params to vars (Used in functions, explained under --V)
        -Add functions (<var function param=value>data</function>)
        -Add error checking for syntax errors etc.
           (Atm it's working successful if 'this.directory' size is 0),
        -Reverse object into lines

*/
function onCreated() {
  
/* This is used to detect the prefix for directories,
      (-<test> means that test will be a directory)
  */
  
this.directory_delim "-";
}
public function 
parse(script) {
  
this.directory = {};
  
temp.prefix 0;

  for (
temp.line2 script) {
    
temp.line line2.trim();
    if (
line.starts(this.directory_delim@"<")) {
      
request("getevent"line);
      if (
this.directory.size() == 1) {
        
prefix this.directory[0];
        
this.(@ prefix) = new TStaticVar();
      }else{
        
temp.prefix;
        
temp.p2 this.directory[this.directory.size()-1];
        
prefix @= "." p2;
        
this.(@ p).(@ p2) = new TStaticVar();
      }
      continue;
    }else if (
line.starts("<")) {
      
temp.analyze request("getevent"line);
      if (
analyze != "isfunction") {
        
temp.var2 request("getvar"line);
        if (
request("hasendkey"linevar2)) {
          
temp.value request("getvalue"line);
          
this.(@ prefix).(@ var2) = value;
        }else{
          if (!
line.starts("</")) {
            
temp.data request("getvalue2"line);
            if (
request("hasendkey"linedata[0])) {
              
temp.val request("getvalue"line);
              
temp.suffix data[0] @ data[1];
              
with(makevar("this."@prefix)) {
                
makevar("this."@suffix) = val;
              }
            }
          }else{
            
temp.data request("getvalue2"line)[0].substring(1);
            
data data.substring(0data.length()-1);
            if (
this.directory[this.directory.size()-1] == data) {
              
this.directory.delete(this.directory.size()-1);
              
prefix this.directory[0];
              for(
temp.this.directory.subarray(1)) {
                
prefix @= "."@d;
              }
            }
          }
        }
      }
    }
  }
}
function 
request(modeabc) {
  switch(
mode) {
    case 
"getevent":
      
temp.out a;
      if (
a.starts(this.directory_delim@"<")) {
        
out a.substring(2a.length() - 3);
        
this.directory.add(out);
        
      }else if (
a.starts("<")) {
        
temp.token a.tokenize(">");
        
temp.token token[0].tokenize();
        if (
token.size() == 2) { 
          if (
token.pos("=") < 0) return "isfunction";
        }else 
out a.substring(2a.length() - 3);
      }
      return 
out;
    break;
    case 
"getfunction":
      
temp.out a.tokenize(">");
      return 
out[0].tokenize()[1];
    break;
    case 
"hasendkey":
      if (
a.pos("</"@">") > -1) return true;
      else return 
false;
    break;
    case 
"emptyvar":
      if (
a.ends(" />")) return true;
      else return 
false;
    break;
    case 
"getvalue":
      
temp.out a;
           
out a.substring(a.pos(">") + 1);
           
out out.substring(0out.pos("<"));
      return 
out;
    break;
    case 
"getvalue2":
      
temp.out2 0;
      
temp.out a;
      
temp.out.tokenize();
      
out2.add(t[0].substring(1));
      
temp.t2 t[1].substring(t[1].pos("'")+1);
      
out2.add(t2.substring(0t2.pos("'")));
      return 
out2;
    break;
    case 
"getvar":
      
temp.out a.substring(1);
           
out out.substring(0out.pos(">"));
      return 
out;
  }


Well, I got bored again.. so I figured to make something to parse something (Had xml into my mind though)..

An example of usage:

PHP Code:

function onCreated() {
    
temp.script = {
    
"-<object>",
      
"<name>Test Object</name>",
      
"<type>obj</type>",
      
"-<actions>",
        
"<action id='1'>examine</action>",
        
"<action id='3'>drop</action>",
      
"</actions>",
    
"</object>"
  
};
  
parse(script);
  
  echo(
this.object.actions.action1); // examine



cbk1994 05-02-2008 03:14 AM

CHOMPY!!!!!!!!!!!

UR GOING TO GO INSANE!!!!!

TOO .... MANY ... PARSERS!!!!

Good job though :)

Chompy 05-02-2008 07:21 AM

Quote:

Originally Posted by cbkbud (Post 1388578)
CHOMPY!!!!!!!!!!!

UR GOING TO GO INSANE!!!!!

TOO .... MANY ... PARSERS!!!!

Good job though :)

Hmm, I must be bored alot >_> xD

Well, they're.. sometimes frustrating to make but you learn from it.. Anyways, going to update some of the older parsers soon if I become bored..!

Hehe, I've released 8 parsers in this thread :p

Chompy 05-02-2008 09:25 AM

Ok, well I figured to try to make a simple DTD parser *cough* pfa *cough*

Anyways, here's my try on it..

PHP Code:

/*      Made by Chompy
         DTD parser v1.0

    Usage:

      dtd_parse(arr[])
*/
public function dtd_parse(script) {
  
temp.object 0;
  
object = new TStaticVar();
  
temp.error false;
  
temp.directory 0;
  
temp.elements 0;
  
temp.vars 0;
  
temp.vars2 0;
  
temp.compiled 0;
  
temp.optional 0;
  
temp.doctype_end false;
  for(
temp.line script) {
    if (
line.starts("<!DOCTYPE ")) {
      if (
line.ends("[")) {
        
doctype_end true;
        continue;
      }else{
        echo(
"Syntax error.");
        
error true;
        break;;
      }
    }else if (
line.starts("<!ELEMENT ")) {
      if (!
doctype_end) break;
      
temp.line.tokenize(" ");
      
elements.add(t[1]);
      if (
elements.size() == 1) {
        if (
line.ends("?)>")) {
          
temp.data t[2].substring(1t[2].length()-3);
          for(
temp.var : data) {
            
vars.add(var);
            if (var.
ends("?")) {
              
optional.add(var);
            }
          }
          
vars2 vars;
        }
      }else{
        
temp.end t[2].substring(0t[2].length()-1);
        if (!
doctype_end) break;
        if (
end == "(#PCDATA)") {
          if (
t[1in vars || t[1]@"?" in vars) {
            
object.(@elements[0]).(@t[1]) = 0// init
          
}else{
            echo(
"Variables is not declared in the root element.");
            
error true;
            break;
          }
        }else{
          echo(
"(#PCDATA) is at the moment the only type available.");
          
error true;
          break;
        }
      }
      continue;
    }else if (
line == "]>") {
      
doctype_end false;
      continue;
    }else if (
line.starts("<") && line.ends(">")) {
      if (
line.starts("</")) {
        if (
line == "</"directory @">") {
          
directory 0;
          break;
        }else{
          echo(
"Odd ending of root element.");
          
error true;
          break;
        }
      }else{
        
temp.check line.positions("<").size();
        if (
check == 1) {
          if (
line == "<"elements[0] @">") {
            
directory elements[0];
            
object.(@elements[0]) = new TStaticVar();
            continue;
          }
        }else{
          
temp.line.substring(1line.pos(">")-1);
          
temp.v2 line.substring(line.pos(">")+1);
               
v2 v2.substring(0v2.pos("<"));
          if (
line.starts("<"@">") && line.ends("</"@">")) {
            if (
v in vars || v@"?" in vars) {
              
object.(@directory).(@v) = v2;
              
compiled.add(v);
              
vars.remove(v);
              
vars.remove(v@"?");
            }
          }
        }
      }
    }
  }
  
temp.check 0;
  for(
temp.vars2) {
    
temp.a;
    if (
b.ends("?")) a.substring(0a.length()-1);
    if (
compiled.index(b) < 0) {
      
check.add(b);
    }
  }
  if (
check.size() > 0) {
    for(
temp.check) {
      if (
optional.index(c@"?") < 0) {
        echo(
"Variable '"@"' must have a value, terminating parent root.");
        if (
this.(@elements[0]) != NULL)
          
this.(@elements[0]).destroy();
        
error true;
        break;
      }
    }
  }
  if (
error) return 0;
  
  return 
object;


and here, an example

PHP Code:

function onCreated() {
  
// DTD
  
temp.script = {
    
"<!DOCTYPE note [",
      
"<!ELEMENT note (to,from,heading,body,signature?)>",
      
"<!ELEMENT to (#PCDATA)>",
      
"<!ELEMENT from (#PCDATA)>",
      
"<!ELEMENT heading (#PCDATA)>",
      
"<!ELEMENT body (#PCDATA)>",
      
"<!ELEMENT signature (#PCDATA)>",
    
"]>",
    
"<note>",
      
"<to>John</to>",
      
"<from>Alex</from>",
      
"<heading>Reminder</heading>",
      
"<body>Hi there John!</body>",
      
"<signature>Signed, Alexander</signature>",
    
"</note>"
  
};
  
  
temp.obj dtd_parse(script);

  echo(
obj.note.to); // John


(BTW, PCDATA stands for "Parsed Character Data")


Atm it supports optional variables (The ? indicates that an element is optional).

The thing is, if you look at the line <!ELEMENT note (to,from,heading,body,signature?)>, in the parenthesis, are the variables that will be declared. If the variable has a '?' behind it, then it's optional. If it does not have a '?' behind, it must be set inside the <note> ... </note> declaration or it will return an error

Currently stuff it supports:
-Making variables optional
-Error and Syntax checking


At the moment it doesn't support sub roots (it doesn't support:
NPC Code:

<!DOCTYPE items [
<!ELEMENT items (item*)>
<!ELEMENT item (name,icon,price,description?)>
<!ELEMENT name (#DATA)>
<!ELEMENT icon (#DATA)>
<!ELEMENT price (#DATA)>
<!ELEMENT description (#DATA)>
]>
<items>
<item>
<name>Wooden Sword</name>
<icon>icon.png</icon>
<price>40</price>
</item>
</item>
<name>Ice Sword</name>
<icon>icon.png</icon>
<price>80</price>
</item>
</items>


yet since I haven't added support for multiple elements). I might add that later though.. if I get into editing this again..

Ang again, I dunno if this is a DTD parser at all or if it is any good.. so.. feedback would be nice :o

Chompy 05-02-2008 05:10 PM

PHP Code:

/*      Made by Chompy
         DTD parser v1.01

    Usage:

      dtd_parse(arr[])

    New Features:
    
      -Allows loading DTD data from text files
*/
function onCreated() {
  
/* This is where it will load the .txt files from,
      make it scriptfiles/<server>/DTD/ or something when doing
      it clientside
  */
  
this.filepath "files/";
}
public function 
dtd_parse(script) {
  
temp.object 0;
  
object = new TStaticVar();
  
temp.error false;
  
temp.directory 0;
  
temp.elements 0;
  
temp.vars 0;
  
temp.vars2 0;
  
temp.compiled 0;
  
temp.optional 0;
  
temp.doctype_end false;
  for(
temp.0script.size(); ++) {
    
temp.line script[i];
    
    if (
line.starts("<!DOCTYPE ")) {
      
temp.line.tokenize();
      if (
line.ends("[")) {
        
doctype_end true;
        continue;
        
      }else if (
t[2in {"SYSTEM""GLOBAL"}) {
        if (
line.ends(">")) {
          
temp.aa t[3].substring(0t[3].length()-1);
          
doctype_end true;
          
temp.file.loadlines(this.filepath aa);
          for(
temp.file.size(); > -1--) {
            
script.insert(i+1file[l]);
          }
        }
        continue;
        
      }else{
        echo(
"Syntax error.");
        
error true;
        break;
      }
    }else if (
line.starts("<!ELEMENT ")) {
      if (!
doctype_end) break;
      
temp.line.tokenize(" ");
      
elements.add(t[1]);
      if (
elements.size() == 1) {
        if (
line.ends("?)>")) {
          
temp.data t[2].substring(1t[2].length()-3);
          for(
temp.var : data) {
            
vars.add(var);
            if (var.
ends("?")) {
              
optional.add(var);
            }
          }
          
vars2 vars;
        }
      }else{
        
temp.end t[2].substring(0t[2].length()-1);
        if (!
doctype_end) break;
        if (
end == "(#PCDATA)") {
          if (
t[1in vars || t[1]@"?" in vars) {
            
object.(@elements[0]).(@t[1]) = 0// init
          
}else{
            echo(
"Variables is not declared in the root element.");
            
error true;
            break;
          }
        }else{
          echo(
"(#PCDATA) is at the moment the only type available.");
          
error true;
          break;
        }
      }
      continue;
    }else if (
line == "]>") {
      
doctype_end false;
      continue;
    }else if (
line.starts("<") && line.ends(">")) {
      if (
line.starts("</")) {
        if (
line == "</"directory @">") {
          
directory 0;
          
doctype_end false;
          break;
        }else{
          echo(
"Odd ending of root element.");
          
error true;
          break;
        }
      }else{
        
temp.check line.positions("<").size();
        if (
check == 1) {
          if (
line == "<"elements[0] @">") {
            
directory elements[0];
            
object.(@elements[0]) = new TStaticVar();
            continue;
          }
        }else{
          
temp.line.substring(1line.pos(">")-1);
          
temp.v2 line.substring(line.pos(">")+1);
               
v2 v2.substring(0v2.pos("<"));
          if (
line.starts("<"@">") && line.ends("</"@">")) {
            if (
v in vars || v@"?" in vars) {
              
object.(@directory).(@v) = v2;
              
compiled.add(v);
              
vars.remove(v);
              
vars.remove(v@"?");
            }
          }
        }
      }
    }
  }
  
temp.check 0;
  for(
temp.vars2) {
    
temp.a;
    if (
b.ends("?")) a.substring(0a.length()-1);
    if (
compiled.index(b) < 0) {
      
check.add(b);
    }
  }
  if (
check.size() > 0) {
    for(
temp.check) {
      if (
optional.index(c@"?") < 0) {
        echo(
"Variable '"@"' must have a value, terminating parent root.");
        if (
this.(@elements[0]) != NULL)
          
this.(@elements[0]).destroy();
        
error true;
        break;
      }
    }
  }
  if (
error) return 0;
  
  return 
object;



example:

PHP Code:

function onCreated() {
  
temp.script = {
    
"<!DOCTYPE note SYSTEM test.txt>",
    
"<note>",
      
"<to>John</to>",
      
"<from>Alex</from>",
      
"<heading>Reminder</heading>",
      
"<body>Hi there John!</body>",
      
"<signature>Signed, Alexander</signature>",
    
"</note>"
  
};
  
  
temp.obj dtd_parse(script);

  echo(
obj.note.body); // Hi there John!




Todo:

-Multiple roots (plus root container)
-Maybe add more types

(woohoo, I've released 10 parsers in all in this thread xD)

Skyld 05-02-2008 05:21 PM

So, essentially, it's an XML parser with a small amount of recognition for doctype declarations where doctype declarations aren't even needed, or are barely even used by the script? What would I use this for in GScript?

Chompy 05-02-2008 06:28 PM

Quote:

Originally Posted by Skyld (Post 1388653)
So, essentially, it's an XML parser with a small amount of recognition for doctype declarations where doctype declarations aren't even needed, or are barely even used by the script? What would I use this for in GScript?

Well, I don't know.

I make these for practice and when I'm bored, but if you see no use for them I suppose I should be making something that is usable..?

I just make different parsers, what other people will use them for, I don't know.

But fine, won't release anymore parsers.. Need to find something else to make then >_>

Skyld 05-02-2008 07:14 PM

Quote:

Originally Posted by Chompy (Post 1388655)
But fine, won't release anymore parsers.. Need to find something else to make then >_>

I'm not saying not to release parsers. It's just there are probably infinitely more useful formats out there. For instance, nobody's invented a JSON interpreter yet, and that could be very beneficial with the rising use of HTTP requests (and occasionally, sockets).

Chompy 05-02-2008 10:37 PM

Quote:

Originally Posted by Skyld (Post 1388665)
I'm not saying not to release parsers. It's just there are probably infinitely more useful formats out there. For instance, nobody's invented a JSON interpreter yet, and that could be very beneficial with the rising use of HTTP requests (and occasionally, sockets).

;o Well, I'm out of ideas of what to make parsers for.. So ideas/suggestions would be nice :o

And, by JSON, you mean parsing something like..

NPC Code:

{
"firstName": "John",
"lastName": "Connor",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
]
}



and turn it into something like
NPC Code:

this.firstName = "John";
this.lastName = "Connor";

this.address = new TStaticVar();
this.address.streetAddress = "21 2nd Street";
this.address.city = "New York";
this.address.state = "NY";
this.address.postalCode = 10021;

this.phoneNumbers = {
"212 555-1234",
"646 555-4567"
};



just curious, a JSON interpreter sounds interesting, but I dunno if I fully understand what 'interpreter' means.. so.. dunno if the stuff above is correct

cbk1994 05-02-2008 10:52 PM

Quote:

Originally Posted by Skyld (Post 1388653)
So, essentially, it's an XML parser with a small amount of recognition for doctype declarations where doctype declarations aren't even needed, or are barely even used by the script? What would I use this for in GScript?

Now you've gone an hurt his feelings :\


All times are GMT +2. The time now is 04:21 AM.

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