Thread: Some parsers
View Single Post
  #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