Checking For Script Syntax Errors

Checking For Script Syntax Errors

  • Comments 8

A reader asked me recently whether there was a way to check a chunk of JScript or VBScript for syntax errors without actually running the code. I'm sure that there are many third-party tools which you could find that do this. If you have your own script host, you can do it yourself quite easily.

The trick is to initialize the engine by setting the script site, but do not do anything that would move the engine from initialized into started state. (Note that attempting to evaluate an expression moves the engine to started state.)

If the engine is initialzed but not started then calling ParseScriptText will check the text passed in for syntax errors but will not run the script. Rather, if the script compiles successfully, it is simply marked as "needs to run when the engine is started". 

Unfortunately the script engines do not have an error-recovering parser, so they will detect only the first syntax error and then bail out. Still, better than nothing.

 

  • Sorry, could you clarify? I have a saved script named blah.vbs. Exactly what do I do to have it syntax checked via the method that you describe?
  • Start by writing your own script host. You'll need to implement IActiveScriptSite, but since you won't actually be running any script, most of the methods can simply return E_NOTIMPL.

    Then cocreate the script engine, queryinterface it for IActiveScript, set the script site to your site object to initialize the engine, and call ParseScriptText with the contents of the file you want to check. Your script site's error handler will be called back if there is a syntax error.

  • What's wrong with:
    var aScripts = ["x=false; x|=!x", "x=true; x||=!x"]
    for (var i=0;i<aScripts.length;++i)
     jsValidator(aScripts[i]) ? alert('Script'+i+' is OK') :
                                alert ('Script '+i+' parsing error with\n'+aScripts[i]);
    function jsValidator(scriptChunk) {
     try {
       eval("function() {"+scriptChunk+"}");
       return true;
     } catch(e) { return false; }
    }

    This idea is also seen at:
    http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/de19d5a5c5a17092/

    Csaba Gabor from Vienna
  • If the code contains functions and subroutines only (no event handlers),

    the code will validate without running.

  • You may want to consider Javascript Lint or JSLint

  • What is the function name in VB.NET that is equal to eval function in VBScript?

  • I am working with VB.NET, I have many mathematical equations in my windows application, which differ according to many options I selected from previous form. So I m using thesse equation as strings in database and according to the selected options from the previous form it drag the its equation, which I concatinate to a variable (number) according to the selected age. So this equation doesn't work because it defined as nvchar in the databse and the variable is number, I tried the function eval() which I use in asp to solve this but it didn't work. I want to know if there is a function in VB.NET can solve this problem or what can I do to solve it.

  • There is an easy way to do syntax testing when hosting the script control: At runtime, just add the line "err.raise 9999" as very first line to the script and enable error handling in your code that hosts the control. After calling .AddCode(...), test the error object of the script control: If it contains err.number=9999, then there is no syntax error. No code will be run, except the err.raise statement.

Page 1 of 1 (8 items)