Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
I'm writing a JavaScript preprocessor which automatically inserts semicolons in places where it's necessary. Don't ask why.
Now I know that the general way to tackle this problem is to write a JavaScript parser and add semicolons where necessary according to the rules in the specs. However I don't want to do so for the following reasons:
- I don't want to write a full fledged parser.
- I want to preserve comments and whitespace.
I've already (correctly) implemented the second and third rule for automatic semicolon insertion using a simple scanner.
The first rule however proves to be more of a challenge to implement. So I have three questions:
- Is it possible to implement the first rule using a simple scanner with lookaheads and lookbehinds?
- If it's possible then has someone already done it?
- If not then how should I tackle this problem?
For the sake of completeness here are the three rules:
When, as the program is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true:
The offending token is separated from the previous token by at least one LineTerminator.
The offending token is }.
When, as the program is parsed from left to right, the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete ECMAScript Program, then a semicolon is automatically inserted at the end of the input stream.
When, as the program is parsed from left to right, a token is encountered that is allowed by some production of the grammar, but the production is a restricted production and the token would be the first token for a terminal or nonterminal immediately following the annotation "[no LineTerminator here]" within the restricted production (and therefore such a token is called a restricted token), and the restricted token is separated from the previous token by at least one LineTerminator, then a semicolon is automatically inserted before the restricted token.
However, there is an additional overriding condition on the preceding rules: a semicolon is never inserted automatically if the semicolon would then be parsed as an empty statement or if that semicolon would become one of the two semicolons in the header of a for statement (section 12.6.3).