Declarative Specification of Template-Based Textual Editors
Total Page:16
File Type:pdf, Size:1020Kb
Declarative Specification of Template-Based Textual Editors Tobi Vollebregt Lennart C. L. Kats Eelco Visser [email protected] [email protected] [email protected] Software Engineering Research Group Delft University of Technology The Netherlands ABSTRACT bench [19]. Structure editors can be used for general-purpose lan- Syntax discoverability has been a crucial advantage of structure ed- guages or for domain-specific languages (DSLs). A modern exam- itors for new users of a language. Despite this advantage, structure ple of the former is the structure editor in MPS [23], for extensible editors have not been widely adopted. Based on immediate pars- languages based on Java. An example of the latter category is the ing and analyses, modern textual code editors are also increasingly DSL for modeling tax-benefit rules developed by IT services com- syntax-aware: structure and textual editors are converging into a pany Capgemini using the Cheetah system. Cheetah’s facilities for new editing paradigm that combines text and templates. Current discoverability and the use of templates are particularly effective text-based language workbenches require redundant specification to aid a small audience of domain expert programmers manage the of the ingredients for a template-based editor, which is detrimental verbose syntax based on legal texts. to the quality of syntactic completion, as consistency and complete- Despite their good support for discoverability, structure editors ness of the definition cannot be guaranteed. have not been widely adopted. Pure structure editors tend to in- In this paper we describe the design and implementation of a troduce an increased learning curve for basic editing operations. specification language for syntax definition based on templates. It For example, they only support copy-pasting operations that main- unifies the specification of parsers, unparsers and template-based tain well-formedness of the tree and require small, yet non-trivial editors. We evaluate the template language by application to two “refactoring” operations for editing existing code, e.g. when con- if if-else domain-specific languages used for tax benefits and mobile appli- verting an statement to an statement. They also lack cations. integration with other tools and expose the user to vendor lock-in. Transferring code across tools requires a shared representation that is generally not available. With software engineering tools such as Categories and Subject Descriptors issue trackers, forums, search, and version control being based on D.2.3 [Software Engineering]: Coding Tools and Techniques— text, a textual representation is preferable, but requires the use of a pretty printers, program editors; D.3.1 [Programming Languages]: parser and a parseable language syntax. This forces tools based on Processors—parsing, code generation; D.3.1 [Programming Lan- structure editors to find new solutions to problems long solved in guages]: Formal Definitions and Theory—syntax; D.2.6 [Software the text domain. Engineering]: Programming Environments; D.2.11 [Software Ar- To alleviate the problems of structure editors, there has been a chitectures]: Languages long history of hybrid structure editors that introduce textual edit- ing features to structure editors [24, 18, 11]. Conversely, modern 1. INTRODUCTION textual code editors such as those in Eclipse and Visual Studio are increasingly syntax-aware, based on parsers that run while a pro- Language-aware structure editors provide a template-based para- gram is edited. Over time, they have acquired features ranging digm for editing programs. They allow composing programs by se- from code folding to syntactic completions allowing programmers lecting a template and filling in the placeholders, which can again to fill in textual templates. Indeed, structure and textual editors be extended using templates. A crucial advantage of structure edi- are converging into a new editing paradigm that combines text and tors is syntax discoverability, helping new users to learn a language templates. by presenting possible syntactic completions in a menu. Struc- In order to provide the advantages of text editing to the tax- ture editors can be automatically generated from a syntax defini- benefit DSL of Capgemini, we converted the language from the tion. Notable projects aiming at automatic generation of struc- Cheetah system, which uses a structure editor, to the parser-based ture editors include MPS [23] and the Intentional Domain Work- Spoofax language workbench [10]. We quickly realized that it would be impossible for a user to write new models in such a ver- bose language in the textual editor of Spoofax, without accurate and complete syntax discovery. In syntax-aware text editors this Permission to make digital or hard copies of all or part of this work for discovery is provided in the form of syntactic completion. Accu- personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies rate and complete syntactic completion depends critically on two bear this notice and the full citation on the first page. To copy otherwise, to features of a language workbench: first, the syntactic completion republish, to post on servers or to redistribute to lists, requires prior specific proposals presented to the user must be relevant and complete, and permission and/or a fee. second, it must be feasible to create and maintain the specification LDTA 2012 Tallinn, Estonia necessary to make the editor aware of these completion proposals. Copyright 2012 ACM 978-1-4503-1536-4 ...$15.00. context-free syntax "module" ID Definition* -> Start {cons("Module")} "entity" ID "{" Property* "}" -> Definition {cons("Entity")} ID ":" Type -> Property {cons("Property")} ID -> Type {cons("Type")} (a) Grammar of the language, in SDF completion template : Start = "module " <m> (blank) (a) Completion is triggered: a pop- (b) Completion proposals are filtered completion template : Definition = "entity " <e> " {}" (blank) up menu shows the available com- by typing the fun prefix into the edi- completion template : Property = <x> " : " <T> (blank) pletion proposals. tor. (b) Completion templates, for syntactic completion, in the EditorService language [ Module -- V[H[KW["module"] _1] _2], Entity -- V is=2 [H[KW["entity"] _1 KW["{"]] _2] KW["}"], Property -- H[_1 KW[":"] _2] ] (c) Pretty printer specification, in the PP language (c) The type of function argument is (d) Back to the start: completion is Figure 2: Redundant concrete syntax specification in Spoofax selected. triggered for the body of the func- tion. Figure 1: Template-oriented editing in a textual editor. The tem- plates are editable as text and mark placeholders with rectangles, edited. The abstract representation is used for editor services, i.e. providing context menus to assign their values. facilities such as an outline of the program and reference resolving to navigate to the definition sites of identifiers. Some of these fa- cilities can be implemented directly at the level of concrete syntax, Text-based language workbenches currently require redundant using regular expressions or other forms of pattern matching, but a specification of the ingredients for a template-based editor, i.e. con- structured, abstract representation ensures a uniform interface for crete syntax, abstract syntax, completion templates, and pretty- editor services. print rules, which is detrimental to the quality of syntactic com- A central part of the definition of a language is the mapping be- pletion in syntax-aware editors. Evolution of the language requires tween the concrete syntax of a language and its abstract represen- maintenance of all ingredients in order to maintain completeness tation. In structure editors, the mapping is defined as a projection and consistency. It is tedious and therefore easy to make mistakes from abstract representation to concrete syntax. In textual editors, while adding or adapting a completion template for each new or the mapping is defined through a parser that constructs the abstract modified language construct. representation from a textual concrete syntax. Syntax-aware textual In this paper, we present the design of a template-based syntax editors also apply a reverse mapping in editor services such as con- definition language1 that unifies the specification of parsers, un- tent completion, pretty printing, code formatting, and refactoring. parsers, and template-based editors in order to support the efficient In particular, modern syntax-aware textual editors support genera- construction of template-based editing facilities in textual editors. tion of code snippets via textual templates, triggered in a context- In order to improve the runtime support for these template-based sensitive fashion by means of a content completion user interface. editing facilities, we describe an approach to compute the set of For example, Figure 1 demonstrates how a simple function is cre- applicable templates at the location of the cursor. ated using content completion. We have implemented the template language in an extension of In order to support the various applications of syntax definition, the Spoofax language workbench [10] and validated the approach the different aspects of parsing, unparsing, formatting, and comple- by applying the techniques in two mature DSLs. tion templates are often specified separately. For instance, Figure 2 We proceed as follows. In the next section, we provide back- shows an example of concrete