Search code examples
javascripterror-handlingtypecheckingsyntax-checking

Syntax / Logical checker In Javascript?


I'm building a solution for a client which allows them to create very basic code, now i've done some basic syntax validation but I'm stuck at variable verification.

I know JSLint does this using Javascript and i was wondering if anyone knew of a good way to do this.

So for example say the user wrote the code

moose = "barry"
base = 0
if(moose == "barry"){base += 100}

Then i'm trying to find a way to clarify that the "if" expression is in the correct syntax, if the variable moose has been initialized etc etc but I want to do this without scanning character by character, the code is a mini language built just for this application so is very very basic and doesn't need to manage memory or anything like that.

I had thought about splitting first by Carriage Return and then by Space but there is nothing to say the user won't write something like moose="barry" or if(moose=="barry") and there is nothing to say the user won't keep the result of a condition inline.

Obviously compilers and interpreters do this on a much more extensive scale but i'm not sure if they do do it character by character and if they do how have they optimized?

(Other option is I could send it back to PHP to process which would then releave the browser of responsibility)

Any suggestions?

Thanks

The use case is limited, the syntax will never be extended in this case, the language is a simple scripted language to enable the client to create a unique cost based on their users input the end result will be processed by PHP regardless to ensure the calculation can't be adjusted by the end user and to ensure there is some consistency.

So for example, say there is a base cost of £1.00 and there is a field on the form called "Additional Cost", the language will allow them manipulate the base cost relative to the "additional cost" field.

So

base = 1;
if(additional > 100 && additional < 150){base += 50}
elseif(additional == 150){base *= 150}
else{base += additional;}

This is a basic example of how the language would be used.


Thank you for all your answers, I've investigated a parser and creating one would be far more complex than is required having run several tests with 1000's of lines of code and found that character by character it only takes a few seconds to process even on a single core P4 with 512mb of memory (which is far less than the customer uses)

I've decided to build a PHP based syntax checker which will check the information and convert the variables etc into valid PHP code whilst it's checking it (so that it's ready to be called later without recompilation) using this instead of javascript this seems more appropriate and will allow for more complex code to arise without hindering the validation process

It's only taken an hour and I have code which is able to check the validity of an if statement and isn't confused by nested if's, spaces or odd expressions, there is very little left to be checked whereas a parser and full blown scripting language would have taken a lot longer

You've all given me a lot to think about and i've rated relevant answers thank you


Solution

  • If you really want to do this — and by that I mean if you really want your software to work properly and predictably, without a bunch of weird "don't do this" special cases — you're going to have to write a real parser for your language. Once you have that, you can transform any program in your language into a data structure. With that data structure you'll be able to conduct all sorts of analyses of the code, including procedures that at least used to be called use-definition and definition-use chain analysis.

    If you concoct a "programming language" that enables some scripting in an application, then no matter how trivial you think it is, somebody will eventually write a shockingly large program with it.

    I don't know of any readily-available parser generators that generate JavaScript parsers. Recursive descent parsers are not too hard to write, but they can get ugly to maintain and they make it a little difficult to extend the syntax (esp. if you're not very experienced crafting the original version).