<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web 2.1 &#187; ECMAScript</title>
	<atom:link href="http://web.2point1.com/tag/ecmascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://web.2point1.com</link>
	<description>Tim Whitlock&#039;s home in the Blogohedron</description>
	<lastBuildDate>Thu, 13 May 2010 21:26:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript Obfuscator and Minifier</title>
		<link>http://web.2point1.com/2008/06/14/javascript-obfuscator-and-minifier/</link>
		<comments>http://web.2point1.com/2008/06/14/javascript-obfuscator-and-minifier/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 15:23:04 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[parsing]]></category>

		<guid isPermaLink="false">http://web.2point1.com/2008/06/14/javascript-obfuscator-and-minifier/</guid>
		<description><![CDATA[This tool is based on a full JavaScript parser that is part of a much bigger plan. I won&#8217;t go into that just yet, but along the way I&#8217;m going to be releasing useful tools like this as they come about. It&#8217;s useful to have some short term goals to keep up morale and ensure [...]]]></description>
			<content:encoded><![CDATA[<p>This tool is based on a <a href="http://web.2point1.com/2008/05/09/full-javascript-parser-for-php/">full JavaScript parser</a> that is part of a much bigger plan. I won&#8217;t go into that just yet, but along the way I&#8217;m going to be releasing useful tools like this as they come about. It&#8217;s useful to have some short term goals to keep up morale and ensure that the framework is working well.</p>
<p><strong>&gt; <a href="http://timwhitlock.info/plug/examples/JavaScript/j_obfuscate.php" target="_blank">Try it here:</a></strong><a href="http://timwhitlock.info/plug/examples/JavaScript/j_obfuscate.php" target="_blank"> Obfuscate and minify your JavaScript code</a></p>
<h3><span id="more-49"></span> Minifying</h3>
<p>There are other minifiers out there;</p>
<p><em>[ Link to project removed due to incredibly rude email from its author ]</em></p>
<p>The parser framework behind my attempt allows a great deal more power at the expense of being a much heftier package. The source code is about 700k, so this is anything but light-weight. The extra power means that, unlike many minifiers, line breaks are not required at all. Any JavaScript program can be stripped down to one, very long line of code. The reason for this is that it performs <a href="http://web.2point1.com/2008/06/01/jparser-now-with-automatic-semicolon-insertion/">automatic semicolon insertion</a>. It can do this because rather than just performing a lexical scan of the source, it compiles the full syntax of the program into a parse tree. Of course, a program with a syntax error will not be minified, but then why would you want it to?</p>
<h3>Obfuscation</h3>
<p>Another advantage of the syntactical parsing is that we stand a better chance of safely altering identifier names. This still has serious limitations though, because the code is still not actually being executed there are many situations that could result in disaster. The current version obfuscates all explicitly named, top-level entities, such as function names, function arguments, labels, and variable declarations. Member expressions are particularly problematic, and so I have not attempted to obfuscate these. I may at some point in future work on improving this, but as this is only a side project, I am not going to hold my breath.</p>
<p>You can always take additional steps in your original code to better preprare for effective obfuscation. Consider this;</p>
<pre class="code">var myDocument = document;
var myElement = myDocument.getElementById('myId');</pre>
<p><strong><a href="http://timwhitlock.info/plug/examples/JavaScript/j_obfuscate.php" target="_blank">Try it and see</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://web.2point1.com/2008/06/14/javascript-obfuscator-and-minifier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JParser now with Unicode support</title>
		<link>http://web.2point1.com/2008/06/08/jparser-now-with-unicode-support/</link>
		<comments>http://web.2point1.com/2008/06/08/jparser-now-with-unicode-support/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 21:37:54 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tokenizing]]></category>

		<guid isPermaLink="false">http://web.2point1.com/2008/06/08/jparser-now-with-unicode-support/</guid>
		<description><![CDATA[I&#8217;ve updated my JavaScript parser to include full Unicode support.
Check out the test interfaces for:
» Full parser;
» Code highlighting.
Code highlighting does not require the full syntactical parser, it just uses the tokenizer and does not break when a bad character is found.
What&#8217;s in?
When I say full Unicode support, what I mean is that Unicode characters [...]]]></description>
			<content:encoded><![CDATA[<p><strong>I&#8217;ve updated my JavaScript parser to include full Unicode support.</strong><br />
Check out the test interfaces for:<br />
<strong><span class="separator">»</span> <a href="http://timwhitlock.info/plug/examples/JavaScript/JParser.php" target="_blank">Full parser</a></strong>;<br />
<strong><span class="separator">»</span> <a href="http://timwhitlock.info/plug/examples/JavaScript/j_token_html.php" target="_blank">Code highlighting</a></strong>.</p>
<p><span id="more-47"></span>Code highlighting does not require the full syntactical parser, it just uses the tokenizer and does not break when a bad character is found.</p>
<h3>What&#8217;s in?</h3>
<p>When I say <em>full</em> Unicode support, what I mean is that Unicode characters inside string literals and comments were always implicitly supported, but now it can cope with Unicode characters in identifiers too, (as per the ECMAScript standard). Support also includes the use of Unicode escape sequences, although for the sake of speed, the validity of these is not checked. Checking could be done at a later stage when the parse tree is employed to do something useful. The full range of whitespace and line-terminating characters have also been added, although not tested.</p>
<p>Unicode support makes the tokenizing process slower. Because of this it may be switched off if it is not needed. Most programmers with English as their first language are unlikely to use Unicode characters in &#8216;hand-written&#8217; identifiers, and I have to wonder if anyone has ever done so with an escape sequence.</p>
]]></content:encoded>
			<wfw:commentRss>http://web.2point1.com/2008/06/08/jparser-now-with-unicode-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Syntax Nuances</title>
		<link>http://web.2point1.com/2008/06/07/javascript-syntax-nuances/</link>
		<comments>http://web.2point1.com/2008/06/07/javascript-syntax-nuances/#comments</comments>
		<pubDate>Sat, 07 Jun 2008 12:49:32 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[parsing]]></category>

		<guid isPermaLink="false">http://web.2point1.com/2008/06/07/javascript-syntax-nuances/</guid>
		<description><![CDATA[If you learn a programming language it is unlikely that you will read the formal language specification that defines all the laws of the syntax. You may never read it at all. It is more useful to learn by example, or at least topic-by-topic. However, a mere ten years after writing my first few lines [...]]]></description>
			<content:encoded><![CDATA[<p>If you learn a programming language it is unlikely that you will read the formal language specification that defines all the laws of the syntax. You may never read it at all. It is more useful to learn by example, or at least topic-by-topic. However, a mere ten years after writing my first few lines of JavaScript, I read the ECMAScript standard and it threw up some things I did not know.</p>
<p>There are many things that you can write in JavaScript that are perfectly valid syntax, but that you probably never will write. Here are a few that raised an eyebrow or two.</p>
<p><span id="more-34"></span></p>
<h3>Comma Operator</h3>
<p>What would you expect to see if you ran this code in your browser?</p>
<pre class="code">var test = [ 'a', 'b', 'c' ];
alert( test[ 0, 1, 2 ] );</pre>
<p>You might expect it to be a syntax error, but in fact <code>test[0,1,2]</code> evaluates in this example to <code>"c"</code>. The expressions ( <code>0</code>, <code>1</code>, and <code>2</code> ) are all evaluated, but only the final one can return the single value of the expression.</p>
<p>Similarly pointless constructs:</p>
<pre class="code">if( alert("Hello"), alert("World"), false ){
    alert("You will never see this");
}
var a = ( 1, 2, 3 ); // a will be set to 3</pre>
<h3>The &#8220;/&#8221; character</h3>
<p>The syntax does not discriminate between operand types. For example, you can <em>attempt</em> to divide any expression by any other expression, even if it makes no sense to do so; such as:</p>
<pre class="code">['a'] / 2</pre>
<p>This will evaluate to <code>NaN</code>, because the array&#8217;s value can never be a number, but it is perfectly valid syntax. The real point I&#8217;m getting to is that the following is an exception and will raise syntax error:</p>
<pre class="code">{ a:1 } / 2;</pre>
<p>Why? Because the lexical analyser will expect <code>"/"</code> to be the start of a Regular Expression Literal, which it isn&#8217;t. It gets it &#8216;wrong&#8217; in this case, because the <code>"}"</code> a is tricky so-and-so; it could either be the end of an expression, or the termination of a block statement. The lexical analyser is not a full syntax parser; it knows nothing of the full grammar of the language, and so it makes a choice based on some fairly weak rules.</p>
<p>The following IS valid, but it is not a division operation, it is just a pointless mess:</p>
<pre class="code">{ a:1 } / 2 /i;</pre>
<h3>undefined vs null</h3>
<p><code>undefined</code> is not a literal, or even a reserved word, whereas <code>null</code> is. <code>undefined</code> is a special built-in object, so if you were stupid enough, you could do this:</p>
<pre class="code">undefined = true;</pre>
<p>This is allowed by the same token that allows you to foolishly do this:</p>
<pre class="code">Array = null;
var a = new Array();
// TypeError: Array is not a constructor</pre>
]]></content:encoded>
			<wfw:commentRss>http://web.2point1.com/2008/06/07/javascript-syntax-nuances/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JParser now with Automatic Semicolon Insertion</title>
		<link>http://web.2point1.com/2008/06/01/jparser-now-with-automatic-semicolon-insertion/</link>
		<comments>http://web.2point1.com/2008/06/01/jparser-now-with-automatic-semicolon-insertion/#comments</comments>
		<pubDate>Sun, 01 Jun 2008 15:34:19 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[parsing]]></category>

		<guid isPermaLink="false">http://web.2point1.com/2008/06/01/jparser-now-with-automatic-semicolon-insertion/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>I finally found a spare few hours to implement Automatic Semicolon Insertion into my JavaScript  Parser.<br />
<strong><a href="http://timwhitlock.info/plug/examples/JavaScript/JParser.php" target="_blank">Check out the test interface here</a></strong>.</p>
<p><span id="more-41"></span>Section 7.9 of the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" target="_blank">ECMAScript language specification</a> 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.</p>
<p>I&#8217;ve managed to implement <em>most </em>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!</p>
<p>If you don&#8217;t know what I&#8217;m talking about, consider this:</p>
<pre class="code">a = b
c = d</pre>
<p>Most JavaScript programmers would be disciplined enough write:</p>
<pre class="code">a = b;
c = d;</pre>
<p>However, there are some conditions that probably catch us all out.<br />
What is the difference between this:</p>
<pre class="code">function xfunc(){
}</pre>
<p>and this?:</p>
<pre class="code">var xfunc = function(){
}</pre>
<p>The former is a <em>Function Declaration</em>, and does not require termination, but the latter is <em>a Function Expression</em>, which in this case forms an <em>Expression Statement</em> and so it requires a semicolon to terminate it. A very common &#8216;mistake&#8217;, but solved quite comfortably by the rules of Automatic Semicolon Insertion, which transform it into:</p>
<pre class="code">var xfunc = function(){
};</pre>
<p>There are plenty more cases I could discuss, but if you&#8217;re really interested, I suggest reading the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" target="_blank">ECMA-262</a> specification yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://web.2point1.com/2008/06/01/jparser-now-with-automatic-semicolon-insertion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Full JavaScript parser for PHP</title>
		<link>http://web.2point1.com/2008/05/09/full-javascript-parser-for-php/</link>
		<comments>http://web.2point1.com/2008/05/09/full-javascript-parser-for-php/#comments</comments>
		<pubDate>Fri, 09 May 2008 21:29:15 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://web.2point1.com/2008/05/09/full-javascript-parser-for-php/</guid>
		<description><![CDATA[[ Update 18 Nov 2009 ]
This article is rather old now &#8211; the jParser code has been released
&#8211;
Despite the glorious sunshine this week, my week off, I managed to put some time into my pet project of developing a full JavaScript parser written in 100% native PHP. Actually, I&#8217;ve been developing a generic parser suite [...]]]></description>
			<content:encoded><![CDATA[<p><strong>[ Update 18 Nov 2009 ]</strong></p>
<p>This article is rather old now &#8211; the <a href="http://web.2point1.com/2009/11/14/jparser-and-jtokenizer-released/">jParser code has been released</a><br />
&#8211;</p>
<p>Despite the glorious sunshine this week, my week off, I managed to put some time into my pet project of developing a full JavaScript parser written in 100% native PHP. Actually, I&#8217;ve been developing a generic parser suite for some time, and using it to build a full JavaScript parser was my ultimate goal to be satisfied that it all works and is powerful enough to be useful. I&#8217;ve written a bunch of blogs about developing a parser generator in PHP, (<a title="Parsing blogs" href="/tag/parsing/">click &#8220;parsing&#8221;</a> to do a tag search).</p>
<p>Before I start wittering on,<br />
<strong><a href="http://timwhitlock.info/plug/examples/JavaScript/JParser.php" target="_blank">Click here to play with the online example of JParser<br />
</a></strong></p>
<p><span id="more-35"></span></p>
<p>Here are the main difficulties I encountered while building the JavaScript parser:</p>
<p><strong>1. Performance<br />
</strong> Generating the parse table was taking about 30 minutes and using several hundred megabytes of memory. Going back to the drawing board with certain parts of the parse table generator, I&#8217;ve managed to get this down to about 7 minutes on my humble Mac Mini.</p>
<p><strong>2. Special rules<br />
</strong> The <a title="ECMA 262 Edition 3" href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" target="_blank">ECMAScript standard</a> states certain special cases in the grammar rules. One of particular note (clause 12.4) says that an <em>ExpressionStatement</em> may not begin with a <code>"{"</code> or a <code>"function"</code>. This special rule avoids ambiguity and therefore avoids parse table conflicts, but the rule is effectively outside of the grammar. I&#8217;ve finally found the right part of the parser architecture to implement such rules</p>
<p><strong> 3. Automatic semicolon insertion</strong><br />
As you probably know just from writing JavaScript, the <a title="ECMA 262 Edition 3" href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" target="_blank">ECMAScript standard</a> permits the lazy omission of semicolons at the end of some statements, as long as you terminate with a line break instead. This is actually more complex than it sounds, but more to the point, it is another special rule that is not directly a part of the grammar and is handled at parse time.<br />
<span style="color: #ff0000;">[<strong>UPDATE</strong>: Automatic semicolon insertion now implemented, <strong><a href="http://web.2point1.com/2008/06/01/jparser-now-with-automatic-semicolon-insertion/">See</a></strong>! ]</span></p>
]]></content:encoded>
			<wfw:commentRss>http://web.2point1.com/2008/05/09/full-javascript-parser-for-php/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
