Thread: Some parsers
View Single Post
  #22  
Old 04-02-2008, 06:12 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Variable parser v0.1

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(0pos[0]);
      if (
pos.size()/== int(pos.size()/2)) {
        
temp.out2 "";
        for(
temp.0pos.size(); += 2;) {
          
temp.value token.substring(pos[i]+1pos[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.callstack[callstack.size()-2].(@check[1].substring(check[1].pos(".")+1));
                
                if (
!= 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.= 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"?
__________________

Last edited by Chompy; 04-02-2008 at 06:23 PM..
Reply With Quote