but is CheckSyntax is enough mature to check the syntax of user defined function names, its parameters etc?
but is CheckSyntax is enough mature to check the syntax of user defined function names, its parameters etc?
It checks the syntax, not the semantics. If you call a function parameter "if" (which is a reserved keyword), it will detect it, if you use an undefined variable it will not detect it (as it is not a syntax error in JavaScript).
Exactly i know this.... that's why i asked question how can i provide compiler type functionality from qtscript, is any other tool/class available in qt where i can provide info about user defined functions and classes and can check semantics of script?
A compiler does not check the semantics of the script. It is simply not possible for javascript. Consider this piece of code:
javascript Code:
function fun(arg) { if(arg == 1) Array.prototype.foo = function() { return 7 } } fun(userInput) var x = Array.foo()To copy to clipboard, switch view to plain text mode
Depending on the external value provided from "userInput" the foo method in Array will exist or not. The script syntax is fine but its semantics may vary between runs.
A compiler, by definition, is a tool that translates a high-level program definition into a lower-level program definition that is easier to execute by the runtime environment. For example a C compiler will not bail out if you divide by 0 in your application. The runtime will.
OMG.......see for example , my application has different classes, and objects of these classes are created from script. I need a mechanism to check in script is user has written correct function names, parameters, object is created and used to call function- is that function belong to that class or not. All these things can be checked when we execute script. But it should not happen, i need to check all these and save script... user can run script at some other time, at that time any error or any problem should not appear. I hope you understand what i am saying.
So please guide me if you have any idea or any knowledge to achieve this. And i need something from qt so it will be implemented easily.
I am telling you this is not possible. JavaScript is not C++ where a simple syntax check would be sufficient to do what you want. In JavaScript there are no classes and you can add and remove methods from objects dynamically from within the script. If you want a static analysis that assumes nobody will do anything that modifies the environment, then do the parsing yourself or just execute the script in a controlled environment where each function that performs some task is substituted by a stub that will do nothing but report success. If you get an exception then the code is invalid in context of provided input parametrs. If you don't get an exception then the script is valid in context of provided input parameters and environment (but not valid in general).
For instance, is this script valid or not (as the whole script)?
Is this one valid?
javascript Code:
var a = "aaa" b = a+7To copy to clipboard, switch view to plain text mode
What about this one?
javascript Code:
var a; if(Math.random() < 0.5) a = new Array() else a = 7 a.splice(1, 1)To copy to clipboard, switch view to plain text mode
It's a bit like Schroedinger's cat, you never know if it's dead or alive until you check, so until you do it's both dead and alive at the same time![]()
Okies.. thanks, i understood it means i need to implement complete environment for such checking. directly nothing is available.
No, it means you cannot possibly do a foulproof check.
Consider this:
javascript Code:
function a() { } function b(arg) { print(arg) } a(1,2,3,4) b()To copy to clipboard, switch view to plain text mode
The code is correct as far as JavaScript is concerned (try running it and see for yourself). I understand that you'd like to get a "oh, your code is wrong, you messed up function arguments" message but the problem is the code is not wrong, it is a correct working JavaScript snippet.
Bookmarks