Suppose I let the user to write a condition using Javascript, the user can write conditions to perform a test and return true or false. E.g.:
INS>5 || ASTO.valueBetween(10,210)
I want to find which variables are used in the script that the user wrote. I tried to find a way to get the identifier names in Java. The Rhino library didn’t help a lot. However I found that via handling exceptions I could get all the identifiers. So this problem is solved.
So everything is great, but there is one little problem. How can I replace these identifiers with a numeric identifier? E.g. INS should be _234 and ASTO should be _331.
INS and ASTO etc are entities in my database. I want to replace them, because the name may change. I could do it using a replace but this isn’t easy because:
- It should be reversible. E.g.
ASTOto_234and_234toASTOagain. - Replacing
_23withMPLAHmay also replace_234. This could be fixed with regexp somehow. - What if
_23is in a comment section? Rare to happen, but possible/* _23 fdsafd ktl */. It should also be replaced. - What if it is a name of a function? E.g.
_32() {}. Also rare, but it shouldn’t be replaced. - What if it is enclosed in
""or''?
I am sure that there are a lot more cases. Any ideas?
Parhs – what you really need is a JavaScript parser. Basically, you will be re-implementing pieces of Rhino although it’s theoretically possible that Rhino already has hooks to do what you need (I’m not familiar with it so not sure); or you can extend Rhino since its source is 100% avialable from Mozilla. Another possible direction to look at is Google’s GWT.