Directive prologue members, escapes, and retroactive syntax errors

Jeff Walden jwalden+es at MIT.EDU
Wed Oct 13 15:02:20 PDT 2010

On 10/13/2010 02:40 PM, Oliver Hunt wrote:
> On Oct 13, 2010, at 12:09 PM, Jeff Walden wrote:
>> function error()
>> {
>>   "\145"; // ohai, octal escape
>>   "use strict"; // causes a RETROACTIVE SYNTAX ERROR
>> }
> In terms of complexity it wasn't too difficult in JSC -- as the directives have essentially no side effects we just run through the directives until we reach one we recognise (eg. "use strict").  At that point we push ourselves into strict mode, rollback the lexer, and start again.

The lexer rollback bit is the tricky part.  In SpiderMonkey we throw away source code as we read it, so we only have a fixed number of characters available at any given time, in a circular buffer containing both scanned and unscanned characters.  (The get-next-token call uses a separate buffer to accumulate characters in unbounded-length tokens.)  The presence of an octal escape has to be explicitly noted and saved away somewhere, and then we have to check that when the "use strict" is encountered arbitrarily far away (beyond that fixed number of characters we could look back in the best case).  In contrast, if directives end when a string literal containing an escape is encountered, octal character escape prohibition requires no rollback or reparsing at all -- it's an error iff you reach it in strict mode.


More information about the es5-discuss mailing list