PDA

View Full Version : Some parsers


Chompy
01-01-2008, 04:16 PM
Well, lately when I didn't have subscription I felt like.. making parsers for some really odd reason, don't ask why :(

Anyways,

/* Made by Chompy
Parser v1.0

Usage:

<$var|this.var$>

<$function|function(params)$>
* params can't be spaced (ex. (foo, bar)), have to be (foo,bar)
*/
public function cParse(a) {
temp.b = temp.a.tokenize(" ");

temp.i = 0;
for(temp.c : b) {
if (c.pos("<$") > -1) {
temp.d = c.substring(0, c.pos("<$") + 2);
temp.e = c.substring(d.length(), c.pos("|") - d.length());
temp.f = c.substring(c.pos("|") + 1, c.pos("$>") - (d.length() + e.length()) - 1);
temp.g = c.substring(c.pos("$>") + 2);
switch(e) {
case "var":
b[i] = d.substring(0, d.length() - 2);
b[i] @= makevar(f);
b[i] @= g;
break;
case "function":
temp.j = f.substring(0, f.pos("("));
temp.k = f.substring(f.pos("(") + 1, f.pos(")") - (j.length() + 1));
if (makevar(k) != NULL) {
k = makevar(k);
}
temp.p = k.tokenize(",");
temp.l = (@ j)(@ p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
b[i] = d.substring(0, d.length() - 2);
b[i] @= l;
b[i] @= g;
break;
default:
if (i > 0) {
b[i-1] = b[i-1].substring(0, b[i-1].length());
}
b[i] = d.substring(0, d.length() - 2);
b[i] @= g;
break;
}
}
i ++;
}
temp.h = "";
for(temp.str : b) {
h @= str @ " ";
}
return h;
}


Basicly, you can do:


function onCreated() {
this.nick = "Chompi";
echo(cParse("Hi <$var|this.nick$>!"));
}


And it will output "Hi Chompi!".

It will parse all variables and functions it finds :)


I'll also use this thread to post my other parsers that I've made, so.. have fun? xD
(PS: I know they probably isn't for good use, butI learnt much out of making parsers so by reading them or making your own, you learn how things works :o)

Chompy
01-01-2008, 04:25 PM
/* Made by Chompy
Parser2 v1.0

Usage:

<?this.var?>

<?function()?>
* params can't be spaced (ex. (foo, bar)), have to be (foo,bar)
*/
public function parseLine(line) {
temp.t = line.tokenize(" ");
temp.t2 = t;

temp.ind = 0;
for(temp.a : t) {
if (a.pos("<?") > -1) {
temp.b = "";
if (a.starts("<?")) temp.b = a.substring(2);
else{
temp.b = a.substring(a.pos("<?") + 2);
temp.e = a.substring(0, a.pos("<?"));
}
temp.c = b.substring(b.pos("?>") + 2);
temp.b = b.substring(0, b.pos("?>"));

temp.mode = 0;
if (b.pos("(") > -1) mode = 2;
else mode = -1;

switch(mode) {
case -1:
t2 = "";
if (e.length() > 0) t2[ind] @= e;
t2[ind] @= makevar(b);
if (c.length() > 0) t2[ind] @= c;
break;
case 2:
temp.h = b.substring(0, b.pos("("));
temp.k = b.substring(b.pos("(") + 1);
k = k.substring(0, k.length() - 1);

temp.p = k.tokenize(",");
t2[ind] = "";
if (e.length() > 0) t2[ind] @= e;
t2[ind] @= (@ h)(p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
if (c.length() > 0) t2[ind] @= c;
break;
}
}
ind ++;
}
temp.parse = "";
for(temp.g : t2) {
parse @= g @ " ";
}
return parse;
}


Simple usage:


function onCreated() {
func = function() {
return "CHOMPI";
};
this.var = "Zeo";
temp.line = "Hi <?this.var?> and <?func()?>!";
echo(parseLine(temp.line));
}
(Parser will also work clientside)

output is "Hi Zeo and CHOMPI!" (OH, who see it? Hidden [I]'easter egg')

It will parse all variables and functions it finds

Basicly this parser detects if it should parse a variable or a function, and works from there.

Chompy
01-01-2008, 04:41 PM
/* 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:

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:

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:

<objecttype='TStaticVar'>
<array='array'>
<index[0]=foo>
<index[1]=bar>
<index[2]=baz>
</array>
<string='var'>foo</string>
<string='var2'>bar</string>
</objecttype>

cbk1994
01-01-2008, 05:13 PM
Very nice! I think someone already made one of these though

Chompy
01-01-2008, 05:25 PM
Very nice! I think someone already made one of these though

who and which one? :O

Codein
01-01-2008, 06:17 PM
who and which one? :O

I think TheGlory was trying to make Parser 3, but I did know that this was under your instruction =P

Chompy
01-01-2008, 10:06 PM
I think TheGlory was trying to make Parser 3, but I did know that this was under your instruction =P

Gemini? Oh yeah :p

JkWhoSaysNi
01-03-2008, 01:25 AM
Actually I made an xml parser/generator: http://forums.graalonline.com/forums/showthread.php?p=1314210
http://forums.graalonline.com/forums/showthread.php?t=74882

the object from XML is slightly buggy, it doesn't support objects in arrays under 2 levels.

Nice work though :)

Chompy
01-03-2008, 06:40 PM
Actually I made an xml parser/generator: http://forums.graalonline.com/forums/showthread.php?p=1314210
http://forums.graalonline.com/forums/showthread.php?t=74882

the object from XML is slightly buggy, it doesn't support objects in arrays under 2 levels.

Nice work though :)

Yeah, that xml parser inpired me for parser3 and an other project I'm doing, will post it later when I'm done :O (the other project)

coreys
01-05-2008, 06:29 AM
Ahh, Chompy. Always parsing something. You, sir, are the parse-master.

Chompy
01-05-2008, 04:32 PM
Ahh, Chompy. Always parsing something. You, sir, are the parse-master.

Naw, PFA has the best parser that I've seen :p

Chompy
01-08-2008, 12:27 AM
/* Made by Chompy
Parser4 v1.1

Usage:

cParselines(lines) - return arr[]

Will loop through the lines and parse each line with
cParseline() and then return an array of all the
lines parsed. Can be laggy with A Lot of lines.


cParseline(line) - return = str

Will parse all tags listed in the onCreated event
this.var_tag - change this to customize the
variable tag to look for
Ex.
<variable>player.account</variable>

this.func_tag - change this to customize the
function tag to look for
ex.
<function>test(a,b,c)</function>

this.custom_tags
- The tags listed in this array will be sent
to a function to compile an own action for
the tag sent, I've made some examples.
Look at the bottom of the script (custom()
function).

ex.
<sqrt>49</sqrt> = 7
<percentage>50,100</percentage> = 50

Supports:
*Customization of tags
*Custom tag actions (must be scripted yourself,
look at the bottom of the
script for some examples)

*Several params (Just don't space them
correct: a,b,c
incorrect: a, b, c)

*/

// Configuration is done in the OnCreated event!
function onCreated() {
this.var_tag = "variable"; // variable tag to look for
this.func_tag = "function"; // function tag to look for

this.custom_tags = { // custom tags to look for
"sqrt", "stringify", "percentage"
};
}
// OBS!
// Don't change 'cParselines()' and 'cParseline()'!
public function cParselines(lines) {
if (lines.type() == 3) {
temp.out = 0;
for(temp.line : lines) {
out.add(cParseline(line));
}
return out;
}
}
public function cParseline(line) {
temp.tokens = line.tokenize(" ");
temp.parsed = "";

for(temp.token : tokens) {
temp.out = "";
temp.tag = token.pos("<");
if (~tag) {
if (tag>0) temp.prefix = token.substring(0, tag);
temp.var = token.substring(tag+1, token.pos(">")-(tag+1));
if (token.pos("</" @ var @ ">") < 0) continue;
temp.ctag = "";
if (var == this.var_tag) ctag = this.var_tag;
else if (var == this.func_tag) ctag = this.func_tag;
temp.mode = (ctag==""&&var in this.custom_tags)?"custom":"normal";
temp.value = token.substring(prefix.length()+(var.length()+2));
temp.value = value.substring(0,value.pos("</"));
temp.endtag = token.substring(token.pos("</"));
temp.suffix = endtag.substring(endtag.pos(">")+1);
temp.endtag = endtag.substring(2, endtag.pos(">")-2);
if (var == endtag) {
switch(mode) {
case "normal":
if (ctag == this.var_tag) {
if (prefix.length() > 0) out @= prefix;
out @= makevar(value);
if (suffix.length() > 0) out @= suffix;
parsed @= out @ " ";
}else if (ctag == this.func_tag) {
temp.h = value.substring(0, value.pos("("));
temp.k = value.substring(value.pos("(")+1);
k = k.substring(0, k.length()-1);
temp.p = k.tokenize(",");
if (prefix.length() > 0) out @= prefix;
out @= (@ h)(p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
if (suffix.length() > 0) out @= suffix;
parsed @= out @ " ";
}
break;
case "custom":
if (prefix.length() > 0) out @= prefix;
if (value.pos(",") == -1) out @= custom(var, value);
else{
temp.h = value.substring(0, value.pos(">"));
temp.p = h.tokenize(",");
out @= custom(var, p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
}
if (suffix.length() > 0) out @= suffix;
parsed @= out @ " ";
break;
}
}
}else parsed @= token @ " ";;
}
return parsed;
}

// Change these to add custom tag actions/functions.

// These will not be executed if they are not listed
// in 'this.custom_tabs'
function custom(var, p, p1, p2, p3, p4, p5, p6) {
switch(var) {
case "stringify": return "\"" @ p @ "\"";
break;
case "sqrt": return p^0.5;
break;
case "percentage": return ((p1*p)/100);
break;
}
}


Some examples are written in the note blocks on the top of the script, but I'll post some more examples if someone wants/needs :o

Btw, this parses lines, not just only tags! :p In next update of this (1.2) I'll add a function to parse tags only

So it can parse:

echo(cParseline("Square root of 9 is <sqrt>9</sqrt>"));
// would output 'Square root of 9 is 3'

etc.

(Parsers that I make will work clientside aswell, if they don't, not much is needed to make them work clientside!)




NOTE TO MODS: Do you guys mind renaming the thread to "Some parsers"?

cbk1994
01-08-2008, 12:49 AM
NOTE TO MODS: Do you guys mind renaming the thread to "Some parsers"?

No, they are too busy. How dare you ask that they do something. You are way out of line, mister!

Tolnaftate2004
01-08-2008, 02:29 AM
Naw, PFA has the best parser that I've seen :p

Thank you. I assume you mean the XML one, which I've updated to look more C++-like.

Chompy
01-08-2008, 04:18 PM
Thank you. I assume you mean the XML one, which I've updated to look more C++-like.

Yeah, I learned some nifty/nice stuff from that one, so thanks :) <3

Tolnaftate2004
01-08-2008, 09:54 PM
Yeah, I learned some nifty/nice stuff from that one, so thanks :) <3

I tried your latest:

echo(cParseLine("The fourth root of 81 is <sqrt><sqrt>81</sqrt></sqrt>."));
The fourth root of 81 is </sqrt>.

this.test = 9;
echo(cParseLine("The square root of `this.test` is <sqrt><variable>this.test</variable></sqrt>."));
The square root of `this.test` is
(I'd like to point out that the period disappeared.)

:(

e: I see you're using if (~some_index). Helpful hint: the engine has to perform twice as many tasks this way. It is more efficient to use the old fashioned if (some_index >= 0). [I know that I used it too...]

Chompy
01-08-2008, 11:08 PM
I tried your latest:

echo(cParseLine("The fourth root of 81 is <sqrt><sqrt>81</sqrt></sqrt>."));
The fourth root of 81 is </sqrt>.

this.test = 9;
echo(cParseLine("The square root of `this.test` is <sqrt><variable>this.test</variable></sqrt>."));
The square root of `this.test` is
(I'd like to point out that the period disappeared.)

:(

Hmm, well atm it looks for the first endtag that it finds and will work from there, will need to add some more checks to support multiple tags inside eachother :o


e: I see you're using if (~some_index). Helpful hint: the engine has to perform twice as many tasks this way. It is more efficient to use the old fashioned if (some_index >= 0). [I know that I used it too...]

hehe, thanks :p

Will improve it, thanks PFA :D

Chompy
03-16-2008, 10:32 PM
Ok, I just noticed this thread, and I felt like I had to continue on it. So, I did what PFA suggested/pointed out and added support for multiple tags:


/* Made by Chompy
Tag Parser v0.1

This parser only do tags :)
*/
function onCreated() {
this.var_tag = "variable";
/* This is the variable tag the parser will look for,
this can be edited so you can make it look for
something shorter like <var></var>*/

this.func_tag = "function"; /* Same as above, but for functions */

/* Custom functions, if a tag is not listed in 'this.var_tag' or
'this.func_tag' it will look in this array for any custom functions
to run. If a tag isn't in this command and is not a variable
or function, the parser will return an error. */
this.custom_tags = {
"sqrt", "percentage"
};
}
function parsetags(line) {
temp.line2 = line;
temp.tags = 0;
temp.line3 = line2;
temp.tags_amount = line2.positions("<").size();
if (tags_amount/2 == int(tags_amount/2)) {
while(tags.size() != tags_amount/2) {
tags.add(line3.substring(1, line3.pos(">")-1));
line3 = line3.substring(line3.pos(">")+1);
}
temp.value = temp.line.substring(line.positions(">")[tags_amount/2-1]+1,line.pos("</")-(line.positions(">")[tags_amount/2-1]+1));
temp.starttags = temp.endtags = 0;
for(temp.tag : tags) {
while(line2.pos("<"@[email protected]">") > -1) {
starttags.add(line2.substring(0, line2.pos(">")+1));
line2 = line2.substring(line2.pos(">")+1);
}
}
for(temp.tag : tags) {
while(line2.pos("</"@[email protected]">") > -1) {
endtags.add(line2.substring(line2.pos("</"), line2.pos(">")-line2.pos("</")+1));
line2 = line2.substring(line2.pos(">")+1);
}
}
temp.i = 0, temp.check = "";
for(temp.t : starttags) {
temp.ei = endtags.size()-1-i;
if (t.substring(1, t.length()-2) == endtags[ei].substring(2,endtags[ei].length()-3))
check @= "y"; else check @= "n";
i ++;
}
temp.value2 = value;
if (check.pos("n") < 0) {
for(temp.i = starttags.size()-1; i > -1; i --) {
temp.t = starttags[i];
temp.t2 = t.substring(1, t.length()-2);
if (t2 == this.var_tag) {
value2 = makevar(value2);
}else if (t2 == this.func_tag) {
temp.h = value.substring(0, value.pos("("));
temp.k = value.substring(value.pos("(")+1);
k = k.substring(0, k.length()-1);
temp.p = k.tokenize(",");
value2 = (@ h)(p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
if (value2 == 0) return "function return error";
}else if (t2 in this.custom_tags) {
temp.p = value2.tokenize(",");
value2 = custom(t2, p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
if (value2 == "error") return "custom return error";
}else return "non-existant tag error";
}
}
return value2;
}
}
/* Custom functions needs to be listed in custom() as a case, and they
need to return data, or else the parser will return an error. I've added
two examples so you can see how it's done :) */
function custom(var, p, p1, p2, p3, p5, p6) {
switch(var) {
case "sqrt": return p^0.5;
break;
case "percentage": return ((p1*p)/100);
break;
default: return "error";
}
}


And now, examples:


function onCreated() {
this.a = 49;
temp.line = "<sqrt><variable>this.a</variable></sqrt>";
echo(parsetags(line)); // 7
}



function onCreated() {
temp.line = "<sqrt>49</sqrt>";
temp.line2 = "<sqrt><sqrt>49</sqrt></sqrt>";
temp.line3 = "<sqrt><sqrt><sqrt>49</sqrt></sqrt></sqrt>";
echo(parsetags(line)); // 7
echo(parsetags(line2)); // 2.645751311
echo(parsetags(line3)); // 1.626576561
}

Tolnaftate2004
03-16-2008, 10:53 PM
Well done!
Tried with this:
this.a = "this.b";
this.b = 81;
echo(parsetags("<sqrt><sqrt><variable><variable>this.a</variable></variable></sqrt></sqrt>"));
Worked like a charm.

Chompy
03-16-2008, 11:02 PM
Thanks PFA <3 :D

Chompy
04-02-2008, 12:05 AM
/* Made by Chompy
Tag Parser v0.2
This parser only do tags :)

Changelog:

v0.2
Added support for "root searching"* for variable tags
(check out the example) (* dunno what to call it xD)

Added support for auto detecting tags as values
(check out the example).
*/
function onCreated() {
this.var_tag = "variable";
/* This is the variable tag the parser will look for,
this can be edited so you can make it look for
something shorter like <var></var>*/

this.func_tag = "function"; /* Same as above, but for functions */

/* Custom functions, if a tag is not listed in 'this.var_tag' or
'this.func_tag' it will look in this array for any custom functions
to run. If a tag isn't in this command and is not a variable
or function, the parser will return an error. */
this.custom_tags = {
"sqrt", "percentage"
};
}
function parsetags(line) {
temp.line2 = line;
temp.tags = 0;
temp.line3 = line2;
temp.tags_amount = line2.positions("<").size();
if (tags_amount/2 == int(tags_amount/2)) {
while(tags.size() != tags_amount/2) {
tags.add(line3.substring(1, line3.pos(">")-1));
line3 = line3.substring(line3.pos(">")+1);
}
temp.value = temp.line.substring(line.positions(">")[tags_amount/2-1]+1,line.pos("</")-(line.positions(">")[tags_amount/2-1]+1));
temp.starttags = temp.endtags = 0;
for(temp.tag : tags) {
while(line2.pos("<"@[email protected]">") > -1) {
starttags.add(line2.substring(0, line2.pos(">")+1));
line2 = line2.substring(line2.pos(">")+1);
}
}
for(temp.tag : tags) {
while(line2.pos("</"@[email protected]">") > -1) {
endtags.add(line2.substring(line2.pos("</"), line2.pos(">")-line2.pos("</")+1));
line2 = line2.substring(line2.pos(">")+1);
}
}
temp.i = 0, temp.check = "";
for(temp.t : starttags) {
temp.ei = endtags.size()-1-i;
if (t.substring(1, t.length()-2) == endtags[ei].substring(2,endtags[ei].length()-3))
check @= "y"; else check @= "n";
i ++;
}
temp.value2 = value;
if (check.pos("n") < 0) {
for(temp.i = starttags.size()-1; i > -1; i --) {
temp.t = starttags[i];
temp.t2 = t.substring(1, t.length()-2);
if (value2.pos("</") > 3) {
do {
value2 = parsetags(value2);
} while(value2.pos("</") > 3);
}
if (t2 == this.var_tag) {
do {
value2 = makevar(value2);
} while(makevar(value2) != NULL);
}else if (t2 == this.func_tag) {
temp.h = value.substring(0, value.pos("("));
temp.k = value.substring(value.pos("(")+1);
k = k.substring(0, k.length()-1);
temp.p = k.tokenize(",");
value2 = (@ h)(p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
if (value2 == 0) return "function return error";
}else if (t2 in this.custom_tags) {
temp.p = value2.tokenize(",");
value2 = custom(t2, p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
if (value2 == "error") return "custom return error";
}else return "non-existant tag error";
}
}
return value2;
}
}
/* Custom functions needs to be listed in custom() as a case, and they
need to return data, or else the parser will return an error. I've added
two examples so you can see how it's done :) */
function custom(var2, p, p1, p2, p3, p5, p6) {
switch(var2) {
case "sqrt": return p^0.5;
break;
case "percentage": return ((p1*p)/100);
break;
default: return "error";
}
}



Now, example:


function onCreated() {
this.b = "this.a";
this.a = "<sqrt>81</sqrt>";
echo(parsetags("<sqrt><variable>this.b</variable></sqrt>"));
// Output: 3
}


So, enjoy..?

And, suggestions for what more to support etc. are welcome :)

Chompy
04-02-2008, 07:12 PM
/* Made by Chompy
Variable Parser v0.1

Features:

- Parses $var$ tags (Also added a temp.look var, so you can change the symbol)
(Atm only support single character symbols, will add support for more later)
- Auto detects the type of the var and works with that
- Can look up temp vars from the caller object (object calling parse())
- Variables don't need a prefix, but recommended

Was originally made to only support percentage symbols which is often used for parsing
variables in many programs etc. But since the forums don't allow the percentage symbol
I made it so you could define the symbol it will use, 'temp.look' in the start of parse()
*/
public function parse(string) {
temp.tokens = string.tokenize();
temp.tokens2 = tokens;
temp.out = "";
temp.look = "$";

for(temp.token : tokens) {
temp.pos = token.positions(look);
if (pos.size() > 0) {
out @= token.substring(0, pos[0]);
if (pos.size()/2 == int(pos.size()/2)) {
temp.out2 = "";
for(temp.i = 0; i < pos.size(); i += 2;) {
temp.value = token.substring(pos[i]+1, pos[i+1]-pos[i]-1);
temp.check = isVariable(value);
if (check[0]) {
switch(check[2]) {
case "this":
case "server":
case "serverr":
case "global":
out2 @= makevar(check[1]);
break;
case "client":
case "clientr":
case "player":
if (player.account == 0) return "No player object";
out2 @= player.(@check[1]);
break;
case "temp":
temp.callstack = getCallStack();
temp.b = callstack[callstack.size()-2].(@check[1].substring(check[1].pos(".")+1));

if (b != 0)
out2 @= b;
}
}else return "Invalid variable prefix";
}
out @= out2;
}else return "Amount of "@[email protected]"'s doesn't match";
out @= token.substring(pos[pos.size()-1]+1) @ " ";
}else out @= token @ " ";
}
return out;
}
function isVariable(var) {
temp.check = {false, ""};
if (var.pos(".") < 0) {
check = {true, "temp."@var, "temp"};

}else{
temp.a = var.substring(0, var.pos("."));
if (a in {"this", "player", "server", "serverr", "clientr", "client", "temp"}) {
check = {true, var, a};
}
}
return check;
}


Now, example:


function onCreated() {
temp.PLAYERNAME = "Chompy";
echo(parse("Hi $PLAYERNAME$!"));
// Outputs "Hi Chompy!";
}




Edit: Could a moderator rename the thread to "Some parsers"?

Chompy
05-02-2008, 04:11 AM
/* 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([email protected]"<")) {
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([email protected]"<")) {
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:


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
}

cbk1994
05-02-2008, 04:14 AM
CHOMPY!!!!!!!!!!!

UR GOING TO GO INSANE!!!!!

TOO .... MANY ... PARSERS!!!!

Good job though :)

Chompy
05-02-2008, 08:21 AM
CHOMPY!!!!!!!!!!!

UR GOING TO GO INSANE!!!!!

TOO .... MANY ... PARSERS!!!!

Good job though :)

Hmm, I must be bored alot >_> xD

Well, they're.. sometimes frustrating to make but you learn from it.. Anyways, going to update some of the older parsers soon if I become bored..!

Hehe, I've released 8 parsers in this thread :p

Chompy
05-02-2008, 10:25 AM
Ok, well I figured to try to make a simple DTD parser *cough* pfa *cough*

Anyways, here's my try on it..


/* Made by Chompy
DTD parser v1.0

Usage:

dtd_parse(arr[])
*/
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.line : script) {
if (line.starts("<!DOCTYPE ")) {
if (line.ends("[")) {
doctype_end = true;
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;
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 || [email protected]"?" in vars) {
object.(@directory).(@v) = v2;
compiled.add(v);
vars.remove(v);
vars.remove([email protected]"?");
}
}
}
}
}
}
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([email protected]"?") < 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;
}


and here, an example


function onCreated() {
// DTD
temp.script = {
"<!DOCTYPE note [",
"<!ELEMENT note (to,from,heading,body,signature?)>",
"<!ELEMENT to (#PCDATA)>",
"<!ELEMENT from (#PCDATA)>",
"<!ELEMENT heading (#PCDATA)>",
"<!ELEMENT body (#PCDATA)>",
"<!ELEMENT signature (#PCDATA)>",
"]>",
"<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.to); // John
}

(BTW, PCDATA stands for "Parsed Character Data")


Atm it supports optional variables (The ? indicates that an element is optional).

The thing is, if you look at the line <!ELEMENT note (to,from,heading,body,signature?)>, in the parenthesis, are the variables that will be declared. If the variable has a '?' behind it, then it's optional. If it does not have a '?' behind, it must be set inside the <note> ... </note> declaration or it will return an error

Currently stuff it supports:
-Making variables optional
-Error and Syntax checking


At the moment it doesn't support sub roots (it doesn't support:

<!DOCTYPE items [
<!ELEMENT items (item*)>
<!ELEMENT item (name,icon,price,description?)>
<!ELEMENT name (#DATA)>
<!ELEMENT icon (#DATA)>
<!ELEMENT price (#DATA)>
<!ELEMENT description (#DATA)>
]>
<items>
<item>
<name>Wooden Sword</name>
<icon>icon.png</icon>
<price>40</price>
</item>
</item>
<name>Ice Sword</name>
<icon>icon.png</icon>
<price>80</price>
</item>
</items>

yet since I haven't added support for multiple elements). I might add that later though.. if I get into editing this again..

Ang again, I dunno if this is a DTD parser at all or if it is any good.. so.. feedback would be nice :o

Chompy
05-02-2008, 06:10 PM
/* 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 || [email protected]"?" in vars) {
object.(@directory).(@v) = v2;
compiled.add(v);
vars.remove(v);
vars.remove([email protected]"?");
}
}
}
}
}
}
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([email protected]"?") < 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:


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)

Skyld
05-02-2008, 06:21 PM
So, essentially, it's an XML parser with a small amount of recognition for doctype declarations where doctype declarations aren't even needed, or are barely even used by the script? What would I use this for in GScript?

Chompy
05-02-2008, 07:28 PM
So, essentially, it's an XML parser with a small amount of recognition for doctype declarations where doctype declarations aren't even needed, or are barely even used by the script? What would I use this for in GScript?

Well, I don't know.

I make these for practice and when I'm bored, but if you see no use for them I suppose I should be making something that is usable..?

I just make different parsers, what other people will use them for, I don't know.

But fine, won't release anymore parsers.. Need to find something else to make then >_>

Skyld
05-02-2008, 08:14 PM
But fine, won't release anymore parsers.. Need to find something else to make then >_>
I'm not saying not to release parsers. It's just there are probably infinitely more useful formats out there. For instance, nobody's invented a JSON interpreter yet, and that could be very beneficial with the rising use of HTTP requests (and occasionally, sockets).

Chompy
05-02-2008, 11:37 PM
I'm not saying not to release parsers. It's just there are probably infinitely more useful formats out there. For instance, nobody's invented a JSON interpreter yet, and that could be very beneficial with the rising use of HTTP requests (and occasionally, sockets).

;o Well, I'm out of ideas of what to make parsers for.. So ideas/suggestions would be nice :o

And, by JSON, you mean parsing something like..


{
"firstName": "John",
"lastName": "Connor",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
]
}


and turn it into something like

this.firstName = "John";
this.lastName = "Connor";

this.address = new TStaticVar();
this.address.streetAddress = "21 2nd Street";
this.address.city = "New York";
this.address.state = "NY";
this.address.postalCode = 10021;

this.phoneNumbers = {
"212 555-1234",
"646 555-4567"
};


just curious, a JSON interpreter sounds interesting, but I dunno if I fully understand what 'interpreter' means.. so.. dunno if the stuff above is correct

cbk1994
05-02-2008, 11:52 PM
So, essentially, it's an XML parser with a small amount of recognition for doctype declarations where doctype declarations aren't even needed, or are barely even used by the script? What would I use this for in GScript?

Now you've gone an hurt his feelings :\