4

I'm writing a JavaScript parser based on ECMA-262. I'd be interested to know how much I'd need to change to make it ECMA-357 compatible.

Are there any syntactic differences?

Charles
  • 48,924
  • 13
  • 96
  • 136
Nick Brunt
  • 8,361
  • 6
  • 48
  • 80
  • 2
    It's probably worth noting that, as far as browsers are concerned, E4X is only supported by Firefox, and it's deprecated there. It looks like dead tech at this point. – duskwuff -inactive- Feb 16 '12 at 22:43
  • @duskwuff: Ouch... After you said that I found https://bugzilla.mozilla.org/show_bug.cgi?id=389123#c9 - looks like it has indeed been quietly deprecated. I guess that they are just being consequent given that no other browser vendor implemented it in 7 years. – Wladimir Palant Feb 16 '12 at 23:09

1 Answers1

3

There is a number of syntax extensions. The most important one are the XML literals (see section 11.1.4 and 11.1.5):

var foo = <xml>
  foo
</xml>;
var bar = <>
  <tag attr={(1+2).toFixed(2)}/>
  {foo}
</>;

The example above shows the special case of an empty root tag and JavaScript expressions in XML code.

You also have some expressions that aren't valid in ECMA-262 (see section 11.2):

xml.@attr           // get attribute attr
xml.*               // get all child elements
xml.@*              // get all attributes
xml..foo            // get all <foo> tags
xml..foo.(@id == 1) // filter <foo> tags by id attribute

There are namespaces (see section 11.1.2):

xml.soap::foo       // get <foo> child tags with namespace soap
xml.@soap::attr     // get attribute with namespace soap

There is the default XML namespace statement which is syntactically a very unusual construct (see section 12.1):

default xml namespace = new Namespace("http://foo/bar");

Finally, there is the for each .. in loop which is similar to for .. in (see section 12.3):

for each (var foo in xml)
{

}

As far as I know these are all the syntax differences - but you've probably got more than enough already.

Wladimir Palant
  • 53,866
  • 11
  • 93
  • 123
  • Wow. Is this for real ? It just looks awful :-/ – copy Feb 17 '12 at 02:29
  • @copy: If you have to work with XML data then this is a lot better (as in: cleaner and easier to read) than the usual DOM calls, often combined with loops as you fetch the part of the tree that you are interested in. The examples here merely illustrate what's possible syntactically, not what you would do in real code. – Wladimir Palant Feb 17 '12 at 12:44