skip to main content
research-article

Sweeten your JavaScript: hygienic macros for ES5

Published:14 October 2014Publication History
Skip Abstract Section

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.

References

  1. The Esprima JavaScript Parser. http://esprima.org/.Google ScholarGoogle Scholar
  2. The Rust Language. http://www.rust-lang.org/.Google ScholarGoogle Scholar
  3. A. Alexandrescu. Modern CGoogle ScholarGoogle Scholar
  4. design: generic programming and design patterns applied. 2001. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. E. Allen, R. Culpepper, and J. Nielsen. Growing a syntax. Proceedings of Workshop on Foundations of Object-Oriented Languages, 2009.Google ScholarGoogle Scholar
  6. J. Bachrach, K. Playford, and C. Street. D-Expressions: Lisp Power, Dylan Style. Style DeKalb IL, 1999.Google ScholarGoogle Scholar
  7. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  8. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  9. R. Cox, T. Bergan, and A. Clements. Xoc, an extension-oriented compiler for systems programming. ACM SIGARCH Computer Architecture News, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle Scholar
  12. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  13. N. Faubion. The Sparkler project. https://github.com/natefaubion/sparkler.Google ScholarGoogle Scholar
  14. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  15. M. Flatt and R. Culpepper. Macros that Work Together. Journal of Functional Programming, 2012.Google ScholarGoogle Scholar
  16. J. Foderaro, K. Sklower, and K. Layer. The FRANZ Lisp Manual. 1983.Google ScholarGoogle Scholar
  17. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  18. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  19. R. Grimm. Better extensibility through modular syntax. PLDI '06 Proceedings of the 2006 ACM SIGPLAN conference on Programming language design and implementation, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. S. P. Harbison and J. Steele, G. L. C: A Reference Manual. Prentice-Hall, 1984. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. R. Hieb, R. Dybvig, and C. Bruggeman. Syntactic abstraction in scheme. Lisp and symbolic computation, 5(4):295--326, 1992. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. 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 ScholarGoogle Scholar
  23. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  24. B. Lee, R. Grimm, M. Hirzel, and K. McKinley. Marco: safe, expressive macros for any language. ECOOP 2012-Object-Oriented łdots, 2012. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. J. Long. The es6-macros project. https://github.com/jlongster/es6-macros.Google ScholarGoogle Scholar
  26. M. Martel and T. Sheard. Introduction to multi-stage programming using metaml. 1997.Google ScholarGoogle Scholar
  27. N. Nystrom, M. Clarkson, and A. Myers. Polyglot: An extensible compiler framework for Java. Compiler Construction, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. PerlMonks. On Parsing Perl. http://www.perlmonks.org/?node_id=44722.Google ScholarGoogle Scholar
  29. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  30. J. Rafkind. Syntactic extension for languages with implicitly delimited and infix syntax. PhD thesis, 2013. Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  32. J. Riehl. Language embedding and optimization in mython. DLS '09 Proceedings of the 5th symposium on Dynamic languages, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. T. Sheard and S. Jones. Template meta-programming for Haskell. Proceedings of the 2002 ACM SIGPLAN workshop on Haskell, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  34. K. Skalski, M. Moskal, and P. Olszta. Meta-programming in Nemerle. Proceedings Generative Programming and Component Engineering, 2004.Google ScholarGoogle Scholar
  35. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  36. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  37. E. Visser. Program transformation with Stratego/XT. Domain-Specific Program Generation, 2004.Google ScholarGoogle ScholarCross RefCross Ref
  38. A. Warth and I. Piumarta. OMeta: an object-oriented language for pattern matching. Proceedings of the 2007 symposium on Dynamic languages, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Sweeten your JavaScript: hygienic macros for ES5

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    • Published in

      cover image ACM SIGPLAN Notices
      ACM SIGPLAN Notices  Volume 50, Issue 2
      DLS '14
      February 2015
      146 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/2775052
      • Editor:
      • Andy Gill
      Issue’s Table of Contents
      • cover image ACM Conferences
        DLS '14: Proceedings of the 10th ACM Symposium on Dynamic languages
        October 2014
        160 pages
        ISBN:9781450332118
        DOI:10.1145/2661088

      Copyright © 2014 ACM

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 14 October 2014

      Check for updates

      Qualifiers

      • research-article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!