Thread: Some parsers
View Single Post
  #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(mode, obj) {
  
// 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.i = 0;
    for(
temp.var : dynamic) {
      
i = 1;
      if (
makevar("obj." @ var).type() == 3) {
        
lines.add(request("addspace", "<array='"@ var @"'>", i));
        
i = 2;
        
temp.c = 0;
        for(
temp.var2 : makevar("obj." @ var)) {
          
lines.add(request("addspace", "<index["@ c @"]="@ makevar("obj." @ var)[c] @">", i));
          
c ++;
        }
        
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(1, obj.size() - 2)) {
          
temp.key = request("getkey", line);
          if (
key == "array") {
            
arraystart = obj.index(line);
            
arrayend = request("hasarrayend", obj, arraystart);
            if (
arrayend[0]) {
              
isarray = true;
              continue;
            }
          }else if (
key in {"float", "integer", "string"}) {
            if (
request("hasendlinekey", key, line)) {
              
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(mode, a, b, c) {
  switch(
mode) {
    case 
"filter":
      
temp.out = a;
      for(
temp.i : b) {
        
temp.ind = a.index(i) > -1;
        if (
ind > -1) out.remove(i);
      }
      return 
out;
    break;
    case 
"addspace": 
      
temp.i = 0, temp.out = "";;
      while(
i < b*2) {
        
out @= "  ";
        
i += 2;
      }
      
out @= a;
      return 
out;
    break;
    case 
"getvar":
      if (!
a.starts("</")) {
        
temp.out = a;
             
out = a.substring(a.pos("'") + 1);
             
out = out.substring(0, out.pos("'"));
      }else 
out = a.substring(2, a.length() - 3);
      return 
out;
    break;
    case 
"getvalue":
      
temp.out = a;
           
out = a.substring(a.pos(">") + 1);
           
out = out.substring(0, out.pos("<"));
      return 
out;
    break;
    case 
"getvalue2":
      
temp.out = a;
           
out = a.substring(a.pos("=") + 1);
           
out = out.substring(0, out.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.d : a.indices("  </array>")) {
        if (
d > b) {
          
out = {true, d};
          break;
        }
      }
      return 
out;
    break;
    case 
"hasendlinekey":
      if (
b.pos("</"@ a @">") > 2) return true;
      else return 
false;
    break;
    case 
"getkey":
      
temp.out = a.trim();
      return 
out.substring(1, out.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