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.i = 0; i < script.size(); i ++) {
temp.line = script[i];
if (line.starts("<!DOCTYPE ")) {
temp.t = line.tokenize();
if (line.ends("[")) {
doctype_end = true;
continue;
}else if (t[2] in {"SYSTEM", "GLOBAL"}) {
if (line.ends(">")) {
temp.aa = t[3].substring(0, t[3].length()-1);
doctype_end = true;
temp.file.loadlines(this.filepath @ aa);
for(temp.l = file.size(); l > -1; l --) {
script.insert(i+1, file[l]);
}
}
continue;
}else{
echo("Syntax error.");
error = true;
break;
}
}else if (line.starts("<!ELEMENT ")) {
if (!doctype_end) break;
temp.t = line.tokenize(" ");
elements.add(t[1]);
if (elements.size() == 1) {
if (line.ends("?)>")) {
temp.data = t[2].substring(1, t[2].length()-3);
for(temp.var : data) {
vars.add(var);
if (var.ends("?")) {
optional.add(var);
}
}
vars2 = vars;
}
}else{
temp.end = t[2].substring(0, t[2].length()-1);
if (!doctype_end) break;
if (end == "(#PCDATA)") {
if (t[1] in 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.v = line.substring(1, line.pos(">")-1);
temp.v2 = line.substring(line.pos(">")+1);
v2 = v2.substring(0, v2.pos("<"));
if (line.starts("<"@ v @">") && line.ends("</"@ v @">")) {
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.a : vars2) {
temp.b = a;
if (b.ends("?")) b = a.substring(0, a.length()-1);
if (compiled.index(b) < 0) {
check.add(b);
}
}
if (check.size() > 0) {
for(temp.c : check) {
if (optional.index(c@"?") < 0) {
echo("Variable '"@ c @"' 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)