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
  #16  
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
  #17  
Old 01-08-2008, 11:08 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
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 View Post
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

Will improve it, thanks PFA
__________________
Reply With Quote
  #18  
Old 03-16-2008, 10:32 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Tag Parser v0.1

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

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

   This parser only do tags :)
*/
function onCreated() {
  
this.var_tag "variable";
  
/* This is the variable tag the parser will look for,
    this can be edited so you can make it look for
    something shorter like <var></var>*/

  
this.func_tag "function"/* Same as above, but for functions */

  /* Custom functions, if a tag is not listed in 'this.var_tag' or
     'this.func_tag' it will look in this array for any custom functions
      to run. If a tag isn't in this command and is not a variable
      or function, the parser will return an error. */
  
this.custom_tags = {
    
"sqrt""percentage"
  
};
}
function 
parsetags(line) {
  
temp.line2 line;
  
temp.tags 0;
  
temp.line3 line2;
  
temp.tags_amount line2.positions("<").size();
  if (
tags_amount/== 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

__________________
Reply With Quote
  #19  
Old 03-16-2008, 10:53 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
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.
__________________
◕‿‿◕ · 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
  #20  
Old 03-16-2008, 11:02 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
Thanks PFA <3
__________________
Reply With Quote
  #21  
Old 04-01-2008, 11:05 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Tag Parser v0.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
__________________

Last edited by Chompy; 04-01-2008 at 11:24 PM..
Reply With Quote
  #22  
Old 04-02-2008, 06:12 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
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"?
__________________

Last edited by Chompy; 04-02-2008 at 06:23 PM..
Reply With Quote
  #23  
Old 05-02-2008, 03:11 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
         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

__________________
Reply With Quote
  #24  
Old 05-02-2008, 03:14 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
CHOMPY!!!!!!!!!!!

UR GOING TO GO INSANE!!!!!

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

Good job though
__________________
Reply With Quote
  #25  
Old 05-02-2008, 07:21 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
Quote:
Originally Posted by cbkbud View Post
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
__________________

Last edited by Chompy; 05-02-2008 at 07:49 AM..
Reply With Quote
  #26  
Old 05-02-2008, 09:25 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
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
__________________

Last edited by Chompy; 05-02-2008 at 02:22 PM..
Reply With Quote
  #27  
Old 05-02-2008, 05:10 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
         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)
__________________
Reply With Quote
  #28  
Old 05-02-2008, 05:21 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
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?
Reply With Quote
  #29  
Old 05-02-2008, 06:28 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 Skyld View Post
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 >_>
__________________
Reply With Quote
  #30  
Old 05-02-2008, 07:14 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Chompy View Post
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).
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 01:41 AM.


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