The web is moving in an enormous speed. New standards like HTML5, CSS3 and the 6th edition of the ECMAScript are under development. Browser vendors have highly improved the performance of their JavaScript engines which now serve as Petri dishes for the development of new languages and JavaScript derivatives. The term “transpiler” has been coined to subsume the cross-compilation from one edition of ECMAScript to another.
Set in this volatile environment, the goal of this thesis is to provide a foundation to compose individual extensions. Thus, a custom language derivative can be built which fits the individual likings. It is not scope of this thesis to design such a language but to provide a framework supporting the creation of JavaScript language extensions.
The thesis is targeted at intermediate JavaScript developers who are interested in creating their own language. No expert knowledge in building compilers is required. The thesis is designed as a guide and hence necessary theoretical knowledge of compilers and parser generators is briefly discussed in the first few chapters.
In oder to experiment with language extensions in the most flexible and convenient way OMeta/JS is chosen as parser generator. Both a JavaScript parser and a translator are implemented in a way that allows an easy extension. JsonML is chosen to internally represent the abstract syntax tree. The creation of the tree is accomplished by the means of node-constructors. A generic walker grammar is provided in order to easily traverse the syntax tree.
The design of the framework highly aims at the reuse of the single components. Hence, the re-factored implementation of OMeta/JS can be used as a standalone package for Node.js. The interdependencies between the different modules are clearly separated to allow reasonable maintainability.
A fictional language EJS is introduced to demonstrate the possibilities of the framework at hand. For this purpose four example extensions are discussed, each with increasing complexity. The last extension finally introduces a class based object orientation.
Many improvements of the framework are imaginable. Yet, the architecture renders itself to be a good foundation to quickly develop JavaScript language extensions.
Original title:
Building compiled language extensions for JavaScript
Bachelor thesis written by Jonathan Brachthäuser, February 2012.
First supervisor: Prof. Walter Kriha, Stuttgart Media University
Additional second supervisor: Andreas Stiegler, M.Sc., Stuttgart Media University