PDA

View Full Version : Text Parser


cbk1994
05-07-2007, 12:26 AM
Someone needed this, so I made it.

This reads text, and uses a "Smart Parser" to go through it and change variables to the value. To signal a variable, put @[email protected] in the code. GS1 will not work, and things such as servername will also not. I have tested this with clientr, client, player, server, and serverr. They all work. Here are the commands:


parseFile( "path/to/file.txt" );
The way I used this was in a server message, I used Parser.parseFile( "data/servermessage.txt" ); This will return an array of the file, simply parsed.

parseText( string/array )
You can give a string, or an array as a paramater. Will only parse the first param though, so put things in an array if you want. If you give it an array, it will return an array. If you give it a string, it will return a string.


Code for wNPC Parser:

// Made by Chris Zakuto
function doInit()
{
// Must be a single character, sorry!
this.sepText = "@"; // The symbol that seperates variables from text.
}
function onCreated()
{
doInit();
}

public function parseFile( filepath )
{
if ( temp.fileContents != NULL )
{
temp.fileContents = NULL;
}

if ( temp.a != NULL )
{
temp.a.clear();
}
temp.fileContents.loadLines( filepath );

for ( temp.n: temp.fileContents )
{
temp.a.add( temp.n );
}

temp.fileText = parseText( temp.a );

return temp.fileText;
}
// "Smart Parser" returns the same type (array or string)
public function parseText( text )
{
if ( temp.returnText != NULL )
{
if ( temp.returnText.size() > 0 )
{
temp.returnText.clear();
}

temp.returnText = NULL;
}
if ( text.size() > 0 )
{
for ( temp.a: text )
{
temp.returnText.add( parseLine( temp.a ) );
}
}
else
{
temp.returnText = parseLine( text );
}

return temp.returnText;
}

// Actually parse the text.
function parseLine( realText )
{
if ( temp.chars != NULL )
{
temp.chars.clear();
}

for ( temp.i = 0; temp.i < realText.length(); temp.i ++ )
{
temp.chars.add( realText.substring( temp.i, 1 ) );
}

if ( temp.isVar != false )
{
temp.isVar = false;
}
for ( temp.n: temp.chars )
{
if ( temp.isVar == false ) // Variable has not been detected.
{
if ( temp.n == this.sepText ) // Start of a variable.
{
temp.isVar = true;
}
else
{
temp.parsedText = temp.parsedText @ temp.n;
}
}
else
{
if ( temp.n != this.sepText )
{
temp.varAdd = temp.varAdd @ temp.n;
}
else
{
temp.isVar = false;
temp.finalVar = makevar( temp.varAdd );
temp.parsedText = temp.parsedText @ temp.finalVar;
temp.varAdd = "";
}
}
}

// temp.parsedText is final output.
return temp.parsedText;
}


To use this, input text like this:

Welcome to @[email protected], @[email protected]:@[email protected] of @[email protected]! You currently have @[email protected] money, and you have been online for @[email protected] seconds! If we had a player.ip variable, yours would be @[email protected]


Have fun, please leave comments.

killerogue
05-07-2007, 01:19 AM
I've always wondered what the hell parsing even is.

Sum41Freeeeek
05-07-2007, 01:55 AM
this is pretty neat, good job :)

cbk1994
05-07-2007, 02:35 AM
this is pretty neat, good job :)

ego ++; thanks.

Rapidwolve
05-07-2007, 03:35 AM
An alternative is that you can use a substring to find the length of 'sepText' meaning that it can be more than one character, this is important because if there is are @'s in the file then they can easily be screwed up by this parser. I would make the 'sepText' something like '###obj.var###'

cbk1994
05-07-2007, 04:08 AM
An alternative is that you can use a substring to find the length of 'sepText' meaning that it can be more than one character, this is important because if there is are @'s in the file then they can easily be screwed up by this parser. I would make the 'sepText' something like '###obj.var###'

Well, you can always find something fancy. ` is hardly used, for example.

` (squiggly key)

Rapidwolve
05-07-2007, 04:09 AM
Or you can script it so its compatible with tags.

<variable>player.nick</variable>

cbk1994
05-07-2007, 03:17 PM
True, maybe I'll make an update for that.

Twinny
05-07-2007, 04:04 PM
Wouldn't it be better to use format(); ?

Rapidwolve
05-07-2007, 09:39 PM
Wouldn't it be better to use format(); ?

What does format() have to do with this? We're talking about concatination in text files.

Deadly_Killer
05-07-2007, 09:57 PM
Hmm. It may be better to do something like:


while (temp.something.pos("@") > 0)
{
// stuff here.
}


Either way, cool :)

Rapidwolve
05-07-2007, 09:59 PM
Hmm. It may be better to do something like:


while (temp.something.pos("@") > 0)
{
// stuff here.
}


Either way, cool :)

Didn't think of it like that thats a good idea. Could save alot of time too

cbk1994
05-08-2007, 02:26 AM
Didn't think of it like that thats a good idea. Could save alot of time too

I don't tend to trust while() in GS2. Never has worked right for me.

Rapidwolve
05-08-2007, 03:39 AM
I don't tend to trust while() in GS2. Never has worked right for me.

Most likely because you forgot to use the sleep function so you dont crash yourself. I've never liked using while() either

zokemon
05-08-2007, 04:11 AM
You guys must not realize that this:
for (i = 0; i < j; i++) {
func();
}
is simply:
i = 0;
while (i < j) {
func();
i++;
}

Also, checking every single letters seems to be A LOT of work. Much more effcient if you just found the first position of "@" and just moved like such. Hell, you could even tokenize("@") the whole thing and just do a for loop through the tokens (changing the mode of "variable" or "text" on every other token).

Deadly_Killer
05-08-2007, 05:20 AM
function onCreated()
{
temp.txt = "Hello <variable>player.account</variable>. How are you doing on this fine day?";

this.parseText(temp.txt);
}

function parseText(buf)
{
temp.tok = buf.tokenize(" ");
temp.hld = temp.tok;

for (temp.I = 0; temp.I < temp.tok.size(); temp.I++)
{
temp.var = temp.tok[temp.I];

if (temp.var.starts("<variable>"))
{
temp.str = temp.var.substring(10, temp.var.pos("</variable>") - 10);

temp.hld[temp.I] = makevar(temp.str);
}
}

for (temp.var : temp.hld) temp.r @= temp.var @ " ";

return temp.r;
}

I made something real quick.

cbk1994
05-08-2007, 05:51 AM
function onCreated()
{
temp.txt = "Hello <variable>player.account</variable>. How are you doing on this fine day?";

this.parseText(temp.txt);
}

function parseText(buf)
{
temp.tok = buf.tokenize(" ");
temp.hld = temp.tok;

for (temp.I = 0; temp.I < temp.tok.size(); temp.I++)
{
temp.var = temp.tok[temp.I];

if (temp.var.starts("<variable>"))
{
temp.str = temp.var.substring(10, temp.var.pos("</variable>") - 10);

temp.hld[temp.I] = makevar(temp.str);
}
}

for (temp.var : temp.hld) temp.r @= temp.var @ " ";

return temp.r;
}

I made something real quick.

eww makevar.

Deadly_Killer
05-08-2007, 08:02 AM
eww makevar.

A) makevar is good.
B) Please don't get me started... especially for someone who abuses his space bar badly.

Oh, and if you didn't know.. you used it in your OWN SCRIPT as well.

Inverness
05-08-2007, 09:40 AM
Oh, and if you didn't know.. you used it in your OWN SCRIPT as well.Classic example of being owned.
However, his script is a case where it is necessary.

Deadly_Killer
05-08-2007, 02:12 PM
Classic example of being owned.
However, his script is a case where it is necessary.

I used makevar once, and the same way he used it.

And i always use makevar anyway, it is a lot better then [email protected](osldol @ "lolz");

cbk1994
05-08-2007, 02:49 PM
A) makevar is good.
B) Please don't get me started... especially for someone who abuses his space bar badly.

Oh, and if you didn't know.. you used it in your OWN SCRIPT as well.

Please try not to be an ass when posting in my thread. If you don't want to help, by all means don't!

makevar() is untidy, it is much better in most cases to use var.( @ "lol" ); Skyld will probably be on to rave about that soon anyway.

Second, we have all already had a discussion about formatting of code. If you did not listen to any of it, that is not a problem of mine. I made a post, called Clean Coding: Common Sense which would explain to you exactly why I format the way I do. I did not rave about your code, because I realize that you have your own way of coding. Please do not try to tell me that the way I use spaces is wrong. There is nothing wrong with what I do. All it does is make it easier for me to read, and for the fellow scripters on servers I work on (any scripters I have worked with have all complimented on my formatting).

Please stay out of this thread if you do not wish to contribute something helpful.

Chompy
05-08-2007, 03:45 PM
Please try not to be an ass when posting in my thread. If you don't want to help, by all means don't!

makevar() is untidy, it is much better in most cases to use var.( @ "lol" ); Skyld will probably be on to rave about that soon anyway.

Second, we have all already had a discussion about formatting of code. If you did not listen to any of it, that is not a problem of mine. I made a post, called Clean Coding: Common Sense which would explain to you exactly why I format the way I do. I did not rave about your code, because I realize that you have your own way of coding. Please do not try to tell me that the way I use spaces is wrong. There is nothing wrong with what I do. All it does is make it easier for me to read, and for the fellow scripters on servers I work on (any scripters I have worked with have all complimented on my formatting).

Please stay out of this thread if you do not wish to contribute something helpful.

Explain how makevar() is untidy..
It's way easier to locate makevars then a bunch of () and @ etc..

I don't see why you think makevar() is untidy...

Btw;

use of obj.positions() would be more effective then obj.pos() if you ask me..

Deadly_Killer
05-09-2007, 12:06 AM
Please try not to be an ass when posting in my thread. If you don't want to help, by all means don't!

makevar() is untidy, it is much better in most cases to use var.( @ "lol" ); Skyld will probably be on to rave about that soon anyway.

Second, we have all already had a discussion about formatting of code. If you did not listen to any of it, that is not a problem of mine. I made a post, called Clean Coding: Common Sense which would explain to you exactly why I format the way I do. I did not rave about your code, because I realize that you have your own way of coding. Please do not try to tell me that the way I use spaces is wrong. There is nothing wrong with what I do. All it does is make it easier for me to read, and for the fellow scripters on servers I work on (any scripters I have worked with have all complimented on my formatting).

Please stay out of this thread if you do not wish to contribute something helpful.

Why do you try to provoke a fight? You even called something ugly that you used in your OWN script.

They can compliment it because they lack being able to script, any scripter will tell you that your script is a mess. Besides, please stop being jealous that I created a 20-30 line script to do what your 150-200 line script does and faster.

Refer to below about makevar:

Explain how makevar() is untidy..
It's way easier to locate makevars then a bunch of () and @ etc..

I don't see why you think makevar() is untidy...

cbk1994
05-09-2007, 12:57 AM
Why do you try to provoke a fight? You even called something ugly that you used in your OWN script.

They can compliment it because they lack being able to script, any scripter will tell you that your script is a mess. Besides, please stop being jealous that I created a 20-30 line script to do what your 150-200 line script does and faster.

Refer to below about makevar:

Please refrain from posting in my threads if this is how you are going to act.

Deadly_Killer
05-09-2007, 04:07 AM
Please refrain from posting in my threads if this is how you are going to act.

Do you not understand that I was only contributing when you attempted to attack me?

You should most likely refrain yourself from posting -period- if this is how you are going to act.

Rapidwolve
05-09-2007, 04:09 AM
I'd prefer using:

this.(@ obj) = value;


Rather then using:

makevar("this." @ obj) = value;


I just think its a matter of scripting preference.

Deadly_Killer
05-09-2007, 04:14 AM
I'd prefer using:

this.(@ obj) = value;


Rather then using:

makevar("this." @ obj) = value;


I just think its a matter of scripting preference.

This isn't about that at all. He attacked me for using makevar, and when I attacked him back he went all pissy on me... and all I did was try to contribute something that works pretty much the same way but uses tags.

cbk1994
05-09-2007, 04:50 AM
Okay, let's just stop this fight. It obviously started with a misunderstanding on both sides, so let's just stop it now.

Rapidwolve
05-09-2007, 04:59 AM
Okay, let's just stop this fight. It obviously started with a misunderstanding on both sides, so let's just stop it now.

Way to go :).