Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
skip to main content
10.1145/2661088.2661097acmconferencesArticle/Chapter ViewAbstractPublication PagessplashConference Proceedingsconference-collections
research-article

Sweeten your JavaScript: hygienic macros for ES5

Published: 14 October 2014 Publication History

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

Cited By

View all
  • (2024)Compiled, Extensible, Multi-language DSLs (Functional Pearl)Proceedings of the ACM on Programming Languages10.1145/36746278:ICFP(64-87)Online publication date: 15-Aug-2024
  • (2023)Rhombus: A New Spin on Macros without All the ParenthesesProceedings of the ACM on Programming Languages10.1145/36228187:OOPSLA2(574-603)Online publication date: 16-Oct-2023
  • (2020)Hygienic macro technologyProceedings of the ACM on Programming Languages10.1145/33863304:HOPL(1-110)Online publication date: 12-Jun-2020
  • Show More Cited By

Index Terms

  1. Sweeten your JavaScript: hygienic macros for ES5

    Recommendations

    Comments

    Information & Contributors

    Information

    Published In

    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
    • 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
    Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected].

    Sponsors

    In-Cooperation

    Publisher

    Association for Computing Machinery

    New York, NY, United States

    Publication History

    Published: 14 October 2014

    Permissions

    Request permissions for this article.

    Check for updates

    Author Tags

    1. hygiene
    2. javascript
    3. macros

    Qualifiers

    • Research-article

    Conference

    SPLASH '14
    Sponsor:

    Acceptance Rates

    DLS '14 Paper Acceptance Rate 13 of 28 submissions, 46%;
    Overall Acceptance Rate 32 of 77 submissions, 42%

    Upcoming Conference

    Contributors

    Other Metrics

    Bibliometrics & Citations

    Bibliometrics

    Article Metrics

    • Downloads (Last 12 months)12
    • Downloads (Last 6 weeks)2
    Reflects downloads up to 16 Oct 2024

    Other Metrics

    Citations

    Cited By

    View all
    • (2024)Compiled, Extensible, Multi-language DSLs (Functional Pearl)Proceedings of the ACM on Programming Languages10.1145/36746278:ICFP(64-87)Online publication date: 15-Aug-2024
    • (2023)Rhombus: A New Spin on Macros without All the ParenthesesProceedings of the ACM on Programming Languages10.1145/36228187:OOPSLA2(574-603)Online publication date: 16-Oct-2023
    • (2020)Hygienic macro technologyProceedings of the ACM on Programming Languages10.1145/33863304:HOPL(1-110)Online publication date: 12-Jun-2020
    • (2019)A Survey of Metaprogramming LanguagesACM Computing Surveys10.1145/335458452:6(1-39)Online publication date: 16-Oct-2019
    • (2019)Platform-Independent Dynamic Taint Analysis for JavaScriptIEEE Transactions on Software Engineering10.1109/TSE.2018.2878020(1-1)Online publication date: 2019
    • (2016)MacroficationProceedings of the 25th European Symposium on Programming Languages and Systems - Volume 963210.5555/3089528.3089553(644-671)Online publication date: 2-Apr-2016
    • (2016)Binding as sets of scopesACM SIGPLAN Notices10.1145/2914770.283762051:1(705-717)Online publication date: 11-Jan-2016
    • (2016)Reactive programming with reactive variablesCompanion Proceedings of the 15th International Conference on Modularity10.1145/2892664.2892666(29-33)Online publication date: 14-Mar-2016
    • (2016)Binding as sets of scopesProceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages10.1145/2837614.2837620(705-717)Online publication date: 11-Jan-2016
    • (2016)Macrofication: Refactoring by Reverse Macro ExpansionProgramming Languages and Systems10.1007/978-3-662-49498-1_25(644-671)Online publication date: 2016
    • Show More Cited By

    View Options

    Get Access

    Login options

    View options

    PDF

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader

    Media

    Figures

    Other

    Tables

    Share

    Share

    Share this Publication link

    Share on social media