Speaking Your Language

Creating a custom DSL editor for

Ian Graham

1 Speaking Your Language | © 2012 Ian Graham Background highlights

ƒ 2D graphics, Fortran, Pascal, ƒ Unix device drivers/handlers – trackball, display devices ƒ Smalltalk ƒ Simulation ƒ Sensor “data fusion” electronic warfare test-bed for army ƒ CASE tools for telecommunications ƒ Java ƒ Interactive visual tools for QC of seismic processing ƒ Seismic processing DSL development tools ƒ Defining parameter constraints and doc of each seismic process in XML ƒ Automating help generation (plain text and HTML) ƒ Smart editor that leverages DSL definition ƒ Now at Markit using custom language for financial risk analysis

Speaking Your Language | © 2012 Ian Graham Overview

ƒ Introduction: Speaking Your Language ƒ What is a DSL? ƒ What is Eclipse? ƒ Demo: Java Editor ƒ Eclipse architecture ƒ Implementing an editor ƒ Implementing a simple editor ƒ Demo ƒ Adding language awareness to your editor ƒ Demo ƒ Learning from Eclipse examples ƒ Resources

Speaking Your Language | © 2012 Ian Graham Introduction: Speaking Your Language

ƒ Goal ƒ illustrate power of Eclipse platform

ƒ Context ƒ widening use of Domain Specific Languages

ƒ Focus ƒ editing support for custom languages in Eclipse

Speaking Your Language | © 2012 Ian Graham Recipe Editor

5 Text Editor Recipes | © 2006 IBM | Made available under the EPL v1.0 What is a DSL?

ƒ Domain Specific Language - a simplified language for specific problem domain ƒ Specialized ƒ E.g. Excel formula, SQL, seismic processing script ƒ Specification language ƒ E.g. HTML, Regular expressions, XML, YACC grammar ƒ Transformation language ƒ E.g. XSLT (Turing complete!)

Speaking Your Language | © 2012 Ian Graham Structured data without DSL

ƒ XML often used ƒ Advantages: ƒ Many existing parsers ƒ DTD/Schemas can define domain-specific constraints ƒ Powerful XML editors that leverage schemas ƒ Disadvantage: ƒ Syntactic clutter unfriendly to humans

Speaking Your Language | © 2012 Ian Graham Eclipse Java IDE

Speaking Your Language | © 2012 Ian Graham NASA JPL, Maestro & Ensemble

ƒ From Maestro Robot Interface Laboratory website

Speaking Your Language | © 2012 Ian Graham Demo: Eclipse Java Editor

ƒ Syntax highlighting ƒ Hover help ƒ Error annotation ƒ Vertical and overview bars ƒ Problems View

ƒ Code navigation ƒ Highlight occurrences ƒ Content assist (code completion) ƒ Refactoring

Speaking Your Language | © 2012 Ian Graham Eclipse Architecture

12 Speaking Your Language | © 2012 Ian Graham Speaking Your Language | © 2012 Ian Graham Speaking Your Language | © 2012 Ian Graham Platform Text Architecture

Speaking Your Language | © 2012 Ian Graham Speaking Your Language | © 2012 Ian Graham Speaking Your Language | © 2012 Ian Graham Implementing an Editor

18 Speaking Your Language | © 2012 Ian Graham Implementing a simple editor

1. Subclass an existing editor ƒ AbstractTextEditor ƒ Find/Replace, hyperlinks ƒ AbstractDecoratedTextEditor ƒ Adds ruler, line numbers, quick diff, configurable preferences 2. Define a document provider ƒ Creates/obtains document ƒ Defines partitioning

3. Define a source viewer configuration ƒ Central class for customizing editor

Speaking Your Language | © 2012 Ian Graham Speaking Your Language | © 2012 Ian Graham Speaking Your Language | © 2012 Ian Graham Speaking Your Language | © 2012 Ian Graham Demo: Simple recipe editor

23 Speaking Your Language | © 2012 Ian Graham Adding language awareness to your editor

24 Speaking Your Language | © 2012 Ian Graham Modeling your document

ƒ Must design a model of your language ƒ E.g. Recipe, IngredientsSection, Ingredient, PreparationSection, Step

ƒ Need parser to build model from document ƒ Roll your own (recipe editor does this, but out of scope of talk) ƒ Generate from a grammar using parser generator such as ANTLR

ƒ Implement a reconciler strategy that invokes parser ƒ Reconciler invoked by Eclipse after a typing break ƒ Runs in background thread ƒ Reports errors as annotations on the document

Speaking Your Language | © 2012 Ian Graham Model Challenges

ƒ Scalability ƒ Probably can’t afford AST of every file, so model definitely of value for reducing scale ƒ Scalability definitely an issue for general purpose languages ƒ May be important even for DSL if need scope beyond file being edited ƒ To scale well, model would likely require proxies and caching of model components so entire model not required to be in memory ƒ Saved state vs. dirty state ƒ Lighter weight errors determined during typically refer to modified in- memory document ƒ Error markers typically persisted based on saved state of document ƒ Files whose models reference each other need consistent way of managing error annotation and persistence

Speaking Your Language | © 2012 Ian Graham Using your model to support power editing

ƒ Navigable Outline view of document structure ƒ Code folding ƒ Context-aware hover help ƒ Content-assist (templates and code completion) ƒ Does not require model, but model necessary for smart context- aware code completion ƒ Semantic highlighting ƒ Colouring member, parameter, and local variables differently ƒ Occurrences of a selected variable ƒ Refactoring

Speaking Your Language | © 2012 Ian Graham Demo: Language-aware recipe editor

28 Speaking Your Language | © 2012 Ian Graham Learning from Eclipse Examples

29 Speaking Your Language | © 2012 Ian Graham Downloading and Installing Eclipse

ƒ Install recent Java JRE, or preferably JDK ƒ Oracle JDK or OpenJDK (GCJ Gnu Java compiler won’t work) ƒ IBM JDK only for IBM machines (does BIOS check) ƒ Obtain Eclipse “Classic” package (eclipse.org) ƒ Best starting point for plug-in developers ƒ includes Platform and JDT source -- great learning resource ƒ Platform specific ƒ Linux/MacOS/Windows ƒ 32-bit(x86)/64-bit(x64) ƒ Extracts to directory named eclipse ƒ I rename to current release, e.g. eclipse_3.7.2 ƒ Create desktop shortcut or alias to eclipse executable ƒ Run eclipse

Speaking Your Language | © 2012 Ian Graham Eclipse Samples and Templates

ƒ Generating projects from Samples ƒ On Welcome screen, select Samples, then Java editor ƒ Dialog will prompt to download Samples ƒ Additional sample projects can be generated by ƒ File→New→Project… ƒ Expand Code Samples→Workbench ƒ Generating projects from built-in templates ƒ File→New→Project… ƒ Select Plugin Project →Next ƒ Enter project name(ca.ab.cuug.xmleditor) →Next ƒ Choose UI and/or RCP →Next ƒ Choose template →Finish

Speaking Your Language | © 2012 Ian Graham Eclipse Java Editor – the richest example

ƒ Import Java Editor into your workspace to browse the code easily 1. File→Import→Plug-in Development →Plug-ins and Fragments →Next 2. Select Binary projects with linked content (minimizes space) 3. Filter by “jdt.ui” and add to “To Import” → Finish 4. Ctrl-T JavaEditor will find it.

ƒ Useful example is ToggleCommentAction.java ƒ similar implementation could be useful in many languages

ƒ Big and complex – not necessarily the easiest example to begin with!

Speaking Your Language | © 2012 Ian Graham Resources

ƒ Eclipse Samples as accessed from Welcome Page ƒ Simple java editor

ƒ Eclipse plug-in templates using File→New Project… ƒ Simple XML editor

ƒ Recipe editor source from EclipseCon 2006 tutorial ƒ http://www.eclipse.org/eclipse/platform-text/eclipseCon/2006/texteditorrecipes.zip

ƒ Eclipse FAQs - http://wiki.eclipse.org/The_Official_Eclipse_FAQs ƒ Numerous Eclipse articles, particularly corner at articles at ƒ http://eclipse.org/resources

Speaking Your Language | © 2012 Ian Graham Resources – Excellent Current Books

ƒ Eclipse Plug-ins (3rd Edition, Dec 2008) by Dan Rubel and Eric Clayberg ƒ Seminal book on developing Eclipse plug-ins, actively updated ƒ 4th Edition targeted for Nov 2012 ƒ Eclipse Rich Client Platform (2nd Edition 2010) by Jeff McAffer, Jean-Michel Lemieux, and Chris Aniszczyk ƒ Guide to developing non-IDE rich client applications on the Eclipse platform ƒ The Definitive ANTL Reference: Building Domain-Specific Languages by Terrence Parr ƒ Guide to developing parsers using ANTLR 3.0, progresses from introductory use for simpler languages to deeper discussions for trickier parsing challenges

Speaking Your Language | © 2012 Ian Graham Resources – Older Books

ƒ Contributing to Eclipse: Principles, Patterns, and Plug-Ins by Erich Gamma and Kent Beck ƒ My favourite Eclipse book, but old(2003) and not a reference ƒ People love it or hate it: if you don't enjoy philosophical aspects of software development, you'll probably hate it. If you want all the code itself to work as is ƒ Primarily walks you through a simple but broad-reaching tutorial using a test- driven development approach to develop tightly integrated TDD-supporting tools on top of JUnit. ƒ Presents an excellent set of Eclipse “House Rules” which you should absorb online if you don’t get the book ƒ Eclipse 3.0 FAQs by John Arthorne and Chris Laffra ƒ The book was a great reference but is now outdated and all the updated material is now available on-line.

Speaking Your Language | © 2012 Ian Graham Credits

ƒ NASA JPL Maestro screenshot taken from the Maestro Robot Interface Laboratory website ƒ http://www-robotics.jpl.nasa.gov

ƒ A number of much appreciated slides extracted from Text Editor Recipes by Tom Eicher presented at EclipseCon 2006. ƒ http://www.eclipse.org/eclipse/platform-text/development/dev.php

Speaking Your Language | © 2012 Ian Graham