PHP Code:
/* 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 "@look@"'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:
PHP Code:
function onCreated() {
temp.PLAYERNAME = "Chompy";
echo(parse("Hi $PLAYERNAME$!"));
// Outputs "Hi Chompy!";
}
Edit: Could a moderator rename the thread to "Some parsers"?