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