PDA

View Full Version : My weird file parser?


Chompy
02-02-2008, 04:03 PM
/* scripted by Chompy

# prefix = notes
$ prefix = global variables (ie. author)

$ variables Always on top!

All spaces must be double spaced!
(Not in params/variables though!)

Global vars can only contain 1 member/value,
it's a string, not an array
*/
function onCreted() {
this.global_variables = {"author"};
}
public function compile(info, mode) {
temp.lines = cleanfile(info, mode);
temp.global = findglobalvars(lines);

for(temp.g : global) {
if (g[0] in this.global_variables) {
this.global.(@ g[0]) = g[1];
}
}
this.info.variables = 0;
this.info.id = 0;
for (temp.line : lines.subarray(global.size() - 1)) {
//lets jump over the global vars
temp.tokens = line.tokenize(" ");
temp.str = convertParamsToString(tokens.subarray(3));
temp.arr = convertStringToArray(str);

this.data.(@ tokens[1]) = arr;
this.info.variables.add({tokens[2], tokens[1]});
this.info.id.add({tokens[0], tokens[1]});
}
this.info.iscompiled = true;
}
public function findidbyname(str) {
if (this.info.iscompiled) {
for(temp.key : this.info.id) {
if (key[1] == str) return key[0];
}
}return -1;
}
public function findvarbyid(id) {
if (this.info.iscompiled) {
for(temp.key : this.info.id) {
if (key[0] == id) return this.data.(@key[1]);
}
}return 0;
}
public function findvarbyname(name) {
if (this.info.iscompiled) {
for(temp.key : this.info.variables) {
if (key[0] == name) return this.data.(@key[1]);
}
}return 0;
}
function convertStringToArray(s) {
out = 0;
for(temp.a : s.tokenize(",")) out.add(a);
return out;
}
function convertParamsToString(p) {
temp.out = 0;
temp.ar = 0;
for(temp.a : p) {
if (a.starts("|")) out = a.substring(1, a.length() - 1);
else if (a.starts("'")) {
ar = 1;
out @= " " @ a.substring(1, a.length() - 1);
continue;
}else if (a.ends("|")) {
out @= " " @ a.substring(0, a.length() - 1);
}else if (a.ends(",") && !a.ends("',")) {
out @= " " @ a.substring(0, a.length() - 1) @ ",";
}
if (ar) {
if (a.ends(".'|")) {
ar = 0;
out = out.substring(0, out.length() - 1);
break;
}else if (a.ends("',")) {
ar = 0;
out @= " " @ a.substring(0, a.length() - 3);
out @= a.substring(a.pos("',") - 1, 1) @ ",";
}else out @= " " @ a;
}
}
return out;
}
function cleanfile(file, mode) {
temp.fakefile = 0;
if (mode) fakefile.loadlines(file);
else fakefile = file;
if (fakefile.type() == 3) {
temp.out = 0;
for(temp.line : fakefile) {
if (!(line.starts("#")) && line != "" && !(line.length() in {0, 4})) {
out.add(line);
}
}
return out;
}
return 0;
}
function findglobalvars(lines) {
temp.out = {-1};
temp.i = 0;
for(temp.line : lines) {
if (line.starts("$")) {
out[0] = i;
out.add({line.tokenize(" ")[0].substring(1), line.tokenize(" ")[1]});
}
i ++;
}
return out;
}



For example, put this in a class named.. err... maybe 'test_file'?

Now, we need a test file, or an array with members responding to the lines that the file has:


# Global variables listed below
$author Chompy
# Skills listed below
# ID GLOBALNAME VARNAME |VARIABLES/PARAMS|
1 SKILL_RUN run |Running, 'Will allow you to run.', 1, 1.5|
2 SKILL_JUMP jump |Jump, 'Will allow you to jump.'|



Now, how the functions work, example:


function onCreated() {
join("test_file");

compile("mud/testfile.txt", true);

/* First param is either the filepath (param2 must be true then), or
an actual array (Read above code tags and param2 must be false).
Basicly what it does is reading the array/file, then convert it into
readable variables. How to read them; Example: */

echo(this.global.author); // Chompy
echo(this.info.variables); // "run,SKILL_RUN","jump,SKILL_JUMP"
echo(this.info.id); // "1,SKILL_RUN","2,SKILL_JUMP"
echo(this.data.SKILL_RUN); // Running,"Will allow you to run.",1,1.5
echo(this.data.SKILL_JUMP); // Jump,"Will allow you to jump."

echo(findvarbyid(1)); // Running,"Will allow you to run.",1,1.5
echo(findvarbyname("run")); // Running,"Will allow you to run.",1,1.5
echo(findidbyname("SKILL_RUN")); // 1

if (this.info.iscompiled) {
echo("The file has been compiled through compile()!");
// echoes: The file has been compiled through compile()!
}
}


AND, I was bored once again :p. Enjoy.. my boredness? ^^

cbk1994
02-02-2008, 06:03 PM
Very nice. I have no clue what it does, but it looks wicked. rep++ in like 8 hours.

bscharff
03-16-2008, 11:55 PM
function onCreted isNotSpelledRight on line 14 "function onCreted() {"
lol.
Awesome stuff :)

Chompy
03-17-2008, 12:02 AM
function onCreted isNotSpelledRight on line 14 "function onCreted() {"
lol.
Awesome stuff :)

Lol, well, I edited it on the go though xD So I might of done a typo there, since when I make stuff I use the onCreated event, and when I'm done then make the function for the parsing itself and make it return instead of echo'ing