JParser now with Automatic Semicolon Insertion

I finally found a spare few hours to implement Automatic Semicolon Insertion into my JavaScript Parser.
Check out the test interface here.

Section 7.9 of the ECMAScript language specification details the circumstances under which you are permitted to omit a semicolon in a statement which otherwise requires it. In short, there are situations where this lazy practice is not the end of the world; it is fairly clear what the programmer is trying to do, so it lets you off.

I've managed to implement most of these conditions, but not all. I still have some work to do on the more peculiar conditions, but the ones that are relevant to most common practice should be covered. Give it a bash, and let me know if you can break it!

If you don't know what I'm talking about, consider this:

a = b
c = d

Most JavaScript programmers would be disciplined enough write:

a = b;
c = d;

However, there are some conditions that probably catch us all out.
What is the difference between this:

function xfunc(){
}

and this?:

var xfunc = function(){
}

The former is a Function Declaration, and does not require termination, but the latter is a Function Expression, which in this case forms an Expression Statement and so it requires a semicolon to terminate it. A very common ‘mistake', but solved quite comfortably by the rules of Automatic Semicolon Insertion, which transform it into:

var xfunc = function(){
};

There are plenty more cases I could discuss, but if you're really interested, I suggest reading the ECMA-262 specification yourself.

Tags: , ,

2 Responses to “JParser now with Automatic Semicolon Insertion”

  1. Sean Says:

    This is cool. Are you generating the PHP with ANTLR? If so, anyway I could get the ANTLR grammar?

    Cheers,
    Sean

  2. Tim Says:

    Actually the parse table generator is also native PHP. I’m loading my own variant of BNF notation. I’ll upload the grammar for your interest to a new post later on.. cheers!

    Also with regard to this post specifically: the automatic semicolon insertion is actually handled at parse-time, (i.e. the grammar says it’s mandatory). If the parser is in error in certain situations it inserts a semicolon and tries again. This technique is more-or-less what is described in the ECMAScript specification.

    P.S. This post on jParser is more recent

Leave a Reply