PDA

View Full Version : Regex for identifying functions/variables


necavi
02-13-2012, 21:31
I'm currently trying to create something that will allow me to list all functions, variables and defines in a plugin and the best way to do this seems to be a three small regex functions. All it would have to do is return true if what is passed to it is a function, variable or define (depending on the regex). If anyone would be able to help me do this it would be greatly appreciated!

asherkin
02-14-2012, 04:27
Please read http://stackoverflow.com/a/1732454 before deciding you want to do this with regular expressions, everything in there applies to what you're trying to do.

necavi
02-14-2012, 04:43
Heh, I have read that before (I was trying to do something similar, if I remember correctly) and have decided to instead try implementing it in a different way. My current implementation is absolutely atrocious, but I'll be improving it over time. (It does have some issue with certain types of comments, at the moment).
def SearchForFunction(string):
explosion = string.split(" ")
if(explosion[0]=="stock"):
if("(" in explosion[1]):
h = explosion[1].split("(")
stocks.append(h[0])
else:
variables.append(explosion[1])
elif(explosion[0]=="public"):
if("(" in explosion[1]):
h = explosion[1].split("(")
public.append(h[0])
elif(explosion[0]=="new"):
variables.append(explosion[1])
elif(explosion[0]=="forward"):
if("(" in explosion[1]):
h = explosion[1].split("(")
forwards.append(h[0])
elif(explosion[0]=="native"):
if("(" in explosion[1]):
h = explosion[1].split("(")
natives.append(h[0])
elif("\t" not in explosion[0] and "\n" not in explosion[0] and "}" not in explosion[0] and "//" not in explosion[0]):
if("(" in explosion[0]):
h = explosion[0].split("(")
private.append(h[0])
Thankfully this isn't meant to be run inside the game, meaning I am able to use something with better string handling than sourcepawn, in this case: python.

BAILOPAN
02-14-2012, 14:15
If you're trying to parse the source code, the compiler can already do this for you. It's easy to just insert printfs in it to output stuff as it finds it.