Abstract
Lisp and Scheme have demonstrated the power of macros to enable programmers to evolve and craft languages. In languages with more complex syntax, macros have had less success. In part, this has been due to the difficulty in building expressive hygienic macro systems for such languages. JavaScript in particular presents unique challenges for macro systems due to ambiguities in the lexing stage that force the JavaScript lexer and parser to be intertwined.
In this paper we present a novel solution to the lexing ambiguity of JavaScript that enables us to cleanly separate the JavaScript lexer and parser by recording enough history during lexing to resolve ambiguities. We give an algorithm for this solution along with a proof that it does in fact correctly resolve ambiguities in the language. Though the algorithm and proof we present is specific to JavaScript, the general technique can be applied to other languages with ambiguous grammars. With lexer and parser separated, we then implement an expressive hygienic macro system for JavaScript called sweet.js.
- The Esprima JavaScript Parser. http://esprima.org/.Google Scholar
- The Rust Language. http://www.rust-lang.org/.Google Scholar
- A. Alexandrescu. Modern CGoogle Scholar
- design: generic programming and design patterns applied. 2001. Google Scholar
Digital Library
- E. Allen, R. Culpepper, and J. Nielsen. Growing a syntax. Proceedings of Workshop on Foundations of Object-Oriented Languages, 2009.Google Scholar
- J. Bachrach, K. Playford, and C. Street. D-Expressions: Lisp Power, Dylan Style. Style DeKalb IL, 1999.Google Scholar
- M. Bravenboer and E. Visser. Concrete syntax for objects: domain-specific language embedding and assimilation without restrictions. OOPSLA '04 Proceedings of the 19th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, 2004. Google Scholar
Digital Library
- W. Clinger. Macros that work. In Proceedings of the 18th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '91, pages 155--162, New York, New York, USA, Jan. 1991. ACM Press. Google Scholar
Digital Library
- R. Cox, T. Bergan, and A. Clements. Xoc, an extension-oriented compiler for systems programming. ACM SIGARCH Computer Architecture News, 2008. Google Scholar
Digital Library
- R. Culpepper and M. Felleisen. Fortifying macros. In Proceedings of the 15th ACM SIGPLAN international conference on Functional programming - ICFP '10, volume 45, page 235, New York, New York, USA, Sept. 2010. ACM Press. Google Scholar
Digital Library
- T. Disney, N. Faubion, D. Herman, and C. Flanagan. The Sweet.js Appendix. https://github.com/mozilla/sweet.js/blob/master/doc/dls2014/sweetjs-appendix.pdf .Google Scholar
- S. Erdweg, T. Rendel, C. K\"astner, and K. Ostermann. SugarJ: library-based syntactic language extensibility. OOPSLA '11 Proceedings of the 2011 ACM international conference on Object oriented programming systems languages and applications, 2011. Google Scholar
Digital Library
- N. Faubion. The Sparkler project. https://github.com/natefaubion/sparkler.Google Scholar
- M. Flatt. Composable and compilable macros: You Want it When? ICFP '02 Proceedings of the seventh ACM SIGPLAN international conference on Functional programming, 37(9):72--83, Sept. 2002. Google Scholar
Digital Library
- M. Flatt and R. Culpepper. Macros that Work Together. Journal of Functional Programming, 2012.Google Scholar
- J. Foderaro, K. Sklower, and K. Layer. The FRANZ Lisp Manual. 1983.Google Scholar
- S. Ganz, A. Sabry, and W. Taha. Macros as multi-stage computations: type-safe, generative, binding macros in MacroML. ICFP '01 Proceedings of the sixth ACM SIGPLAN international conference on Functional programming, 2001. Google Scholar
Digital Library
- A. Ghuloum and R. K. Dybvig. Implicit phasing for R6RS libraries. ICFP '07 Proceedings of the 12th ACM SIGPLAN international conference on Functional programming, 42(9):303, Oct. 2007. Google Scholar
Digital Library
- R. Grimm. Better extensibility through modular syntax. PLDI '06 Proceedings of the 2006 ACM SIGPLAN conference on Programming language design and implementation, 2006. Google Scholar
Digital Library
- S. P. Harbison and J. Steele, G. L. C: A Reference Manual. Prentice-Hall, 1984. Google Scholar
Digital Library
- R. Hieb, R. Dybvig, and C. Bruggeman. Syntactic abstraction in scheme. Lisp and symbolic computation, 5(4):295--326, 1992. Google Scholar
Digital Library
- E. C. M. A. International. ECMA-262 ECMAScript Language Specification. Number June. ECMA (European Association for Standardizing Information and Communication Systems), 5.1 edition, 2011.Google Scholar
- E. E. Kohlbecker and M. Wand. Macro-by-example: Deriving syntactic transformations from their specifications. In Proceedings of the 14th ACM SIGACT-SIGPLAN symposium on Principles of programming languages - POPL '87, pages 77--84, New York, New York, USA, Oct. 1987. ACM Press. Google Scholar
Digital Library
- B. Lee, R. Grimm, M. Hirzel, and K. McKinley. Marco: safe, expressive macros for any language. ECOOP 2012-Object-Oriented łdots, 2012. Google Scholar
Digital Library
- J. Long. The es6-macros project. https://github.com/jlongster/es6-macros.Google Scholar
- M. Martel and T. Sheard. Introduction to multi-stage programming using metaml. 1997.Google Scholar
- N. Nystrom, M. Clarkson, and A. Myers. Polyglot: An extensible compiler framework for Java. Compiler Construction, 2003. Google Scholar
Digital Library
- PerlMonks. On Parsing Perl. http://www.perlmonks.org/?node_id=44722.Google Scholar
- K. M. Pitman. Special forms in Lisp. In Proceedings of the 1980 ACM conference on LISP and functional programming - LFP '80, pages 179--187, New York, New York, USA, Aug. 1980. ACM Press. Google Scholar
Digital Library
- J. Rafkind. Syntactic extension for languages with implicitly delimited and infix syntax. PhD thesis, 2013. Google Scholar
Digital Library
- J. Rafkind and M. Flatt. Honu: Syntactic Extension for Algebraic Notation through Enforestation. Proceedings of the 11th International Conference on Generative Programming and Component Engineering, 2012. Google Scholar
Digital Library
- J. Riehl. Language embedding and optimization in mython. DLS '09 Proceedings of the 5th symposium on Dynamic languages, 2009. Google Scholar
Digital Library
- T. Sheard and S. Jones. Template meta-programming for Haskell. Proceedings of the 2002 ACM SIGPLAN workshop on Haskell, 2002. Google Scholar
Digital Library
- K. Skalski, M. Moskal, and P. Olszta. Meta-programming in Nemerle. Proceedings Generative Programming and Component Engineering, 2004.Google Scholar
- W. Taha and T. Sheard. Multi-stage programming with explicit annotations. PEPM '97 Proceedings of the 1997 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation, 1997. Google Scholar
Digital Library
- S. Tobin-Hochstadt and V. St-Amour. Languages as libraries. PLDI '11 Proceedings of the 32nd ACM SIGPLAN conference on Programming language design and implementation, 2011. Google Scholar
Digital Library
- E. Visser. Program transformation with Stratego/XT. Domain-Specific Program Generation, 2004.Google Scholar
Cross Ref
- A. Warth and I. Piumarta. OMeta: an object-oriented language for pattern matching. Proceedings of the 2007 symposium on Dynamic languages, 2007. Google Scholar
Digital Library
Index Terms
Sweeten your JavaScript: hygienic macros for ES5
Recommendations
Hygienic Macro System for JavaScript and Its Light-weight Implementation Framework
ILC '14: Proceedings of ILC 2014 on 8th International Lisp ConferenceIn spite of its soundness, ease of use, and descriptive power, few hygienic macro systems have been incorporated to non-LISP programming languages. The difficulty lies in the self-describing nature of the macro system. Proposed is the design and a ...
Sweeten your JavaScript: hygienic macros for ES5
DLS '14: Proceedings of the 10th ACM Symposium on Dynamic languagesLisp and Scheme have demonstrated the power of macros to enable programmers to evolve and craft languages. In languages with more complex syntax, macros have had less success. In part, this has been due to the difficulty in building expressive hygienic ...
Honu: syntactic extension for algebraic notation through enforestation
GPCE '12: Proceedings of the 11th International Conference on Generative Programming and Component EngineeringHonu is a new language that fuses traditional algebraic notation (e.g., infix binary operators) with Scheme-style language extensibility. A key element of Honu's design is an enforestation parsing step, which converts a flat stream of tokens into an S-...







Comments