Note that there are some explanatory texts on larger screens.

plurals
  1. POSyntax / Logical checker In Javascript?
    text
    copied!<p>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.</p> <p>I know JSLint does this using Javascript and i was wondering if anyone knew of a good way to do this.</p> <p>So for example say the user wrote the code</p> <pre><code>moose = "barry" base = 0 if(moose == "barry"){base += 100} </code></pre> <p>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.</p> <p>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 <code>moose="barry"</code> or <code>if(moose=="barry")</code> and there is nothing to say the user won't keep the result of a condition inline.</p> <p>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?</p> <p>(Other option is I could send it back to PHP to process which would then releave the browser of responsibility)</p> <p>Any suggestions?</p> <p>Thanks</p> <p>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.</p> <p>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.</p> <p>So </p> <pre><code>base = 1; if(additional &gt; 100 &amp;&amp; additional &lt; 150){base += 50} elseif(additional == 150){base *= 150} else{base += additional;} </code></pre> <p>This is a basic example of how the language would be used.</p> <hr> <p>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)</p> <p>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</p> <p>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</p> <p>You've all given me a lot to think about and i've rated relevant answers thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload