flotob / es-lab

Automatically exported from code.google.com/p/es-lab
0 stars 0 forks source link

Need PrologueDecl AST type #5

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently, 

    "use strict";

even in directive prologue position produces

    ["LiteralExpr",
      {
        "type":"string",
        "value":"use strict"
      }
    ]

Since this captures only the value of the string, and not the original text
of the literal, it isn't even possible to determine in a post-pass whether
this was a Use Strict Directive. (See ES5 section 14.1)

Instead, we should introduce a new kind of AST declaration node:

    ["PrologueDecl", { "directive":"use strict" }]

where the value of the directive attribute is a string whose contents are
the source code of the part of the directive prologue between the outer
quotes. For example, the above represents a valid Use Strict Directive.
However,

    "use\ strict"

would parse to

    ["PrologueDecl", { "directive":"use\\ strict" }]

which will be interpreted as an unrecognized directive prologue.

Original issue reported on code.google.com by erights on 25 Dec 2009 at 1:18

GoogleCodeExporter commented 9 years ago

Original comment by erights on 25 Dec 2009 at 5:44

GoogleCodeExporter commented 9 years ago
Added in r86.

Example input:
"use strict";
'use test';
'use\ strict';

Output:
["Program",
  {},
  ["PrologueDecl",
    {
      "directive":"use strict"
    }
  ],
  ["PrologueDecl",
    {
      "directive":"use test"
    }
  ],
  ["LiteralExpr",
    {
      "type":"string",
      "value":"use strict"
    }
  ]
]

Original comment by to...@google.com on 7 Jan 2010 at 7:58

GoogleCodeExporter commented 9 years ago
After realizing that PrologueDecl nodes also have a runtime semantics (during 
which
they behave as StringLiterals), I added a 'value:' attribute to these nodes. The
evaluator can then use the 'directive' attribute to access the raw string 
value, and
the 'value' attribute to return upon evaluation. The above example now parses 
as:

["Program",
  {},
  ["PrologueDecl",
    {
      "value":"use strict",
      "directive":"use strict"
    }
  ],
  ["PrologueDecl",
    {
      "value":"use test",
      "directive":"use test"
    }
  ],
  ["PrologueDecl",
    {
      "value":"use strict",
      "directive":"use\\ strict"
    }
  ]
]

Note also that the third string now parses correctly as a PrologueDecl. 
Previously, I
thought PrologueDecls were not allowed to contain escape sequences. Apparently 
they
are allowed to contain escape sequences. It's just that a Use Strict directive 
in
particular does not allow them.

Original comment by to...@google.com on 8 Jan 2010 at 12:38