Javascript Language Extension with Language Workbenches

Javascript Language Extension with Language Workbenches

Master Thesis JavaScript language extension with language workbenches Author: Supervisor: Matthisk Heimensen dr. Tijs van der Storm [email protected] [email protected] August 2015 Host organization: Centrum Wiskunde & Informatica http://www.cwi.nl Universiteit van Amsterdam Faculteit der Natuurwetenschappen, Wiskunde en Informatica Master Software Engineering http://www.software-engineering-amsterdam.nl UNIVERSITEIT VAN AMSTERDAM Abstract Faculteit der Natuurwetenschappen, Wiskunde en Informatica Master of Science JavaScript language extension with language workbenches by Matthisk Heimensen Extending programming languages is an activity undertaken by many programmers, mostly through the use of syntax macros but there exist different manners through which to achieve extensible programming languages. Extensible programming languages enable any programmer to introduce new language constructs, not just the creators of the language. Language extensions are dedicated modular grammars including a transformation step to the base language. They enable programmers to create their own syntax on top of a base language and embed domain specific knowledge not just in semantics but also in syntax. For implementation of language extensions a tool called a language workbench can be used. Language workbenches promise to provide an environment which improves the efficiency of language oriented programming. To evaluate this promise of the language workbench we implement the latest JavaScript specification (ECMAScript 6) as a set of language extensions on top of current JavaScript and compare our implementation against tools created without the help of a language workbench. Contents Abstract i Contents ii 1 Introduction and Motivation1 1.1 Outline.....................................2 2 Problem Analysis3 2.1 Motivation...................................3 2.2 Research approach...............................4 2.3 Research questions...............................4 2.4 Related Work..................................4 2.4.1 Metaborg................................4 2.4.2 Sugar* and SugarJ...........................5 2.4.3 Type Specific Languages........................5 2.4.4 Concrete Syntax............................6 2.4.5 Macro systems.............................6 2.4.6 Taxonomy of program transformations................6 2.4.7 Program transformation hygiene...................7 3 Background8 3.1 ECMAScript..................................8 3.2 Program Transformations........................... 10 3.3 Language Extensions.............................. 10 3.3.1 Hygiene................................. 11 3.4 Parsing..................................... 13 3.5 Program Representation............................ 14 3.6 Language Workbenches............................ 14 4 Taxonomy 17 4.1 Structure.................................... 17 4.2 Dimensions................................... 18 4.3 Introducing ECMAScript 6.......................... 22 4.3.1 Functions................................ 22 4.3.2 Syntax.................................. 23 4.3.3 Binding................................. 25 4.3.4 Optimization.............................. 26 ii Contents iii 5 Implementation of RMonia 29 5.1 Basics...................................... 29 5.2 RMonia..................................... 30 5.2.1 Visitor.................................. 31 5.2.2 Introducing bindings.......................... 32 5.2.3 Mutually dependent language extensions............... 32 5.2.4 Variable capture............................ 34 5.2.5 Block scoping.............................. 36 5.2.6 IDE integration............................. 38 6 Results and Comparison 39 6.1 Evaluation.................................... 39 6.2 Reflecting on the taxonomy.......................... 47 6.3 Engineering trade-offs............................. 48 6.3.1 Benefits................................. 48 6.3.2 Limitations............................... 48 7 Conclusion 50 A Artifact Description 53 B Language feature categorization 56 B.1 Arrow Functions................................ 56 B.2 Classes...................................... 57 B.3 Destructuring.................................. 58 B.4 Extended object literals............................ 59 B.5 For of loop................................... 59 B.6 Spread operator................................. 60 B.7 Default parameter............................... 61 B.8 Octal and binary literals............................ 61 B.9 Regexp "y" and "u" flags........................... 62 B.10 Unicode code point escapes.......................... 62 B.11 Rest parameter................................. 62 B.12 Template Literal................................ 63 B.13 Tail call optimization.............................. 63 B.14 Generators................................... 64 B.15 Let and Const declarators........................... 65 C ES6 Test Examples 68 Bibliography 70 Chapter 1 Introduction and Motivation Language extensions allow programmers to introduce new language constructs to a base language, with two main purposes. \First a programmer can define language extensions for language constructs that are missing in the base language" [1], these missing con- structs can range from more advanced looping constructs (e.g. foreach/for-of loops) to shorthand function notation (e.g. lambda functions). \Second, extensible program- ming languages serve as an excellent base for language embedding" [1] for instance to enable the programmer to use a markup language (e.g. HTML) inside a programming language. One of the techniques to realize the implementation of language extensions is program transformations, they are used to transform language extensions from the source program to base language code. Many systems for program transformation exist but in recent years a specific type of tool has become more popular for this job, the language workbench. This tool aids the (meta-)programmer in creating meta-programs that integrate with modern developer tools. In this thesis we investigate the ability of language workbenches in helping the meta-programmer to create a set of language extensions. As an experiment we extend the JavaScript programming language with a subset of features introduced by the new specification document of the language, ECMAScript 6 (ES6). The current specification of JavaScript implemented in all major run-times (be it web-browsers or dedicated) is ECMAScript 5 (ES5). The language extensions are implemented in the Rascal [2] language workbench and the resulting tool is named RMonia. A second contribution of this thesis is a taxonomy for language extensions. With this taxonomy we try to capture the distinctive characteristics of each language extension in a generic way, by answering the following questions: How can the language extension be implemented, what information does transformation of a language extension require, and what are guarantees we give of the target program produced by a language extension? We are not the first to implement a solution for transformation of ES6 JavaScript to ES5. Several tools are build specifically with this goal in mind (e.g. Babel JS or Traceur compiler). These tools are not implemented as language extensions on top of an ES5 base-grammar but as a full compiler with separate parsing and transformation stage, these compilers specifically engineered to migrate between programming languages and perform a source-to-source translation is often called a transpiler1. 1http://martinfowler.com/bliki/TransparentCompilation.html\/footnote-transpiler 1 Chapter 1. Introduction 2 To evaluate RMonia we compare it to three of these ES6 to ES5 transformers along several dimensions: coverage of the transformation suite (i.e. amount of ES6 features implemented by the transformer), correctness of the transformations according to a set of compatibility tests, size in source lines of code used for transformation and parsing code, the ease with which the language extension suite can be extended, or so called modularity, and output \noise" generated by the transformations. With this evaluation we create insight into the engineering trade-offs of implementing language extensions inside the language workbench. What limitations does the workbench impose, where can we benefit from the power of a language workbench, and what are the generic problems of language extensions. For instance do all types of new language features lend themselves to be implemented as language extensions or how can we guarantee the hygiene of the performed transformations. The Rascal language workbench made it possible for us to implement ES6 language extensions in a short time period with fewer lines of code. We cover most of the new language features from ES6, something only other large-scale open-source projects are able to achieve. We deliver editor support for reference hyperlinking, undeclared ref- erence errors, illegal redeclaration errors, and hover documentation preview of target program. The language workbench does however constraint us to one specific IDE and our solution is less portable than other implementations. Syntax definition of the lan- guage workbench constrained us from implementing empty element matching in the destructuring (see appendix B.3) language extension of ES6 (see appendixA). We were able to guarantee the hygiene of program transformations by reusing the v-fix algorithm. Finally we uncovered that most but not all new language constructs lend themselves to be implemented as language extensions, the new block binding construct let/const was the exception

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    76 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us