Parsing a nested if statement with javascript
I have some nested if statements with the following syntax:
// Comment for condition 1
if(condition1
,expression1
,
// Comment for condition 2
if(condition2
,expression2
,else2))
The if statements do not conform to JavaScript syntax. They are written in a language which is "not publicly known, like a mix of VBscript and JavaScript."
Now I would like to parse these if statements to see if they are syntactically correct or not. How could I do that?开发者_如何学Python
Source code for an existing JS parser can be found here: JSLint Source Code
Aside from that, you would need to build a language parser, possibly using these tools.
Well, to check whether the basics are correct (correct number of arguments, open/close brackets, number of commas, etc) would not be difficult - you could easily cater for the nested IFs using recursive techniques.
But you would also need to tell us whether you also need to check whether the "condition" and "expression" syntax is correct?
And are commas or brackets, for example going to be legal inside an expression or condition? If so, you'll need to define your rules for string qualification, escape characters, etc, so your parser interprets these as being part of the expression or condition and doesn't confuse them as being control characters.
In its most simple form however, some psuedo-code:
var stmt = "IF(condition,expression,IF(condition,expr2,expr3))"
function checkIfSyntax(stmt){
var tokens = splitStmt(stmt); // careful to take into account where
// control chars (that is, commas and
// brackets) are qualified, escaped,
// or inside a nested IF()
checkCondition(tokens.condition); //with whatever rules you have, plus [1]
checkExpression(tokens.expr1); //with whatever rules, plus [1]
checkExpression(tokens.expr2); //with whatever rules, plus [1]
/* [1]: if any of the tokens are nested IF() statements,
call checkIfSyntax() from within checkCondition()
or checkExpression()
- (this is the recursive bit) */
}
This is just hopefully to get you on the right track. To actually answer your question fully would be to code it for you - I don't think anyone is going to do that. Try to understand the above as well as reading the links others have provided, and hopefully you should get on the right track
Parsing Javascript code to see if it is syntactically correct is not a trivial task.. (actually this is true for most programming code)
You might want to consider actually running a Javascript interpreter to verify the syntax of the code. If you're running in a Java enviroment, you can use the Rhino interpreter to do this.
Maybe specify a bit more in which context you want to do this.
There is a section in the book 'Beautiful Code' which has a tutorial (with a working parser at the end) on writing a Parser in Javascript:
http://www.amazon.co.uk/gp/product/product-description/0596510047/ref=dp_proddesc_0?ie=UTF8&n=266239&s=books
IMHO: really good book as well - its kinda like the Geek's version of "Comic Relief" or "Band Aid"...[In the sense that I seem to remember some of the proceeds go to charity or something - but basically lots of interesting articles and code snippets from great programmers from all generations and in lots of computing-languages].
精彩评论