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.p = 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", line, var2)) {
temp.value = request("getvalue", line);
this.(@ prefix).(@ var2) = value;
}else{
if (!line.starts("</")) {
temp.data = request("getvalue2", line);
if (request("hasendkey", line, data[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(0, data.length()-1);
if (this.directory[this.directory.size()-1] == data) {
this.directory.delete(this.directory.size()-1);
prefix = this.directory[0];
for(temp.d : this.directory.subarray(1)) {
prefix @= "."@d;
}
}
}
}
}
}
}
}
function request(mode, a, b, c) {
switch(mode) {
case "getevent":
temp.out = a;
if (a.starts(this.directory_delim@"<")) {
out = a.substring(2, a.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(2, a.length() - 3);
}
return out;
break;
case "getfunction":
temp.out = a.tokenize(">");
return out[0].tokenize()[1];
break;
case "hasendkey":
if (a.pos("</"@ b @">") > -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(0, out.pos("<"));
return out;
break;
case "getvalue2":
temp.out2 = 0;
temp.out = a;
temp.t = out.tokenize();
out2.add(t[0].substring(1));
temp.t2 = t[1].substring(t[1].pos("'")+1);
out2.add(t2.substring(0, t2.pos("'")));
return out2;
break;
case "getvar":
temp.out = a.substring(1);
out = out.substring(0, out.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
}