The Evolution of the StringTemplate Engine Presented to the UC Berkeley Harmonia group 15 December 2004 Terence Parr University of San Francisco

Slides:



Advertisements
Similar presentations
Copyright 2001, ActiveState. XSLT and Scripting Languages or…XSLT: what is everyone so hot and bothered about?
Advertisements

Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
The Role Of Template Engines in Code Generation Terence Parr University of San Francisco
1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
Database Systems: Design, Implementation, and Management Tenth Edition
Describing Process Specifications and Structured Decisions Systems Analysis and Design, 7e Kendall & Kendall 9 © 2008 Pearson Prentice Hall.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Using the Semantic Web to Construct an Ontology- Based Repository for Software Patterns Scott Henninger Computer Science and Engineering University of.
Copyright  2005 Symbian Software Ltd. 1 Lars Kurth Technology Architect, Core Toolchain The Template Engine CDT Developer Conference, Oct 2005.
Object-Oriented PHP (1)
Visual Web Information Extraction With Lixto Robert Baumgartner Sergio Flesca Georg Gottlob.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Thayer School of Engineering Dartmouth Lecture 2 Overview Web Services concept XML introduction Visual Studio.net.
Scripting Languages CS351 – Programming Paradigms.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
UNIT-V The MVC architecture and Struts Framework.
CASE Tools And Their Effect On Software Quality Peter Geddis – pxg07u.
StringTemplate Terence Parr University of San Francisco
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
JSP Standard Tag Library
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Imperative Programming
Another PillowTalk Presentation  2004 Dynamic Systems, Inc. Introduction to XML for SOA Lee H. Burstein,
Getting Started with ANTLR Chapter 1. Domain Specific Languages DSLs are high-level languages designed for specific tasks DSLs include data formats, configuration.
Chapter 1 Understanding the Web Design Environment Principles of Web Design, 4 th Edition.
Week 1 Understanding the Web Design Environment. 1-2 HTML: Then and Now HTML is an application of the Standard Generalized Markup Language Intended to.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
JSP Java Server Pages Softsmith Infotech.
Parser-Driven Games Tool programming © Allan C. Milne Abertay University v
Database Systems: Design, Implementation, and Management Ninth Edition
Introduction to MDA (Model Driven Architecture) CYT.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
How Java becomes agile riding Rhino Xavier Casellato VP of engineering, Liligo.com.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
SE: CHAPTER 7 Writing The Program
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
NJIT UML Class Diagrams Chapter 16 Applying UML and Patterns Craig Larman.
Enforcing Strict Model- View Separation in Template Engines Terence Parr University of San Francisco
1 Chapter 1 Introduction. 2 Outlines 1.1 Overview and History 1.2 What Do Compilers Do? 1.3 The Structure of a Compiler 1.4 The Syntax and Semantics of.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
SWEN 5130Requirements Engineering Algebraic Specification Slide 1 Algebraic Specification u Specifying abstract types in terms of relationships between.
User Profiling using Semantic Web Group members: Ashwin Somaiah Asha Stephen Charlie Sudharshan Reddy.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
SDPL 2002Notes 4: Intro to Style Sheets1 4. Introduction to Style Sheets n Discussed recently: –Programmatic manipulation of documents n Now a more human-oriented.
Object Oriented Programming
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Concepts and Realization of a Diagram Editor Generator Based on Hypergraph Transformation Author: Mark Minas Presenter: Song Gu.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
Web Design Principles 5 th Edition Chapter 3 Writing HTML for the Modern Web.
Design Evaluation Overview Introduction Model for Interface Design Evaluation Types of Evaluation –Conceptual Design –Usability –Learning Outcome.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 6 - Database Implementation and Use
Introduction to Parsing (adapted from CS 164 at Berkeley)
More Object-Oriented Programming
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Review CSE116 2/21/2019 B.Ramamurthy.
Object-Oriented PHP (1)
More XML XML schema, XPATH, XSLT
Presentation transcript:

The Evolution of the StringTemplate Engine Presented to the UC Berkeley Harmonia group 15 December 2004 Terence Parr University of San Francisco

Overview Introduction Introduction what is StringTemplate? what is StringTemplate? why did I build it? why did I build it? the nature of text generation the nature of text generation Design goals Design goals The evolution; 4 phases The evolution; 4 phases Experience Experience Lessons Lessons

What is StringTemplate Template engine designed with Tom Burns (CEO, jGuru.com) while building commercial sites over many years in response to JSP Template engine designed with Tom Burns (CEO, jGuru.com) while building commercial sites over many years in response to JSP small: 170k uncompressed binary (w/o test rig) small: 170k uncompressed binary (w/o test rig) Evolved from simple “document with holes” into a functional language capable of generating large class of languages Evolved from simple “document with holes” into a functional language capable of generating large class of languages Well suited to generative programming as well as dynamic page generation; being used in new ANTLR parser generator for code generation Well suited to generative programming as well as dynamic page generation; being used in new ANTLR parser generator for code generation Distinguishing characteristic: strictly enforces separation of model and view Distinguishing characteristic: strictly enforces separation of model and view

Canonical Operations Attribute reference: Attribute reference: Template references (possibly recursive): Template references (possibly recursive): Apply template to multi-valued attribute: or ;}> Apply template to multi-valued attribute: or ;}> Conditional include: extends Name: Guest Conditional include: extends Name: Guest

Example* Translated “Program Manipulation via Interactive Transformations” example to actual StringTemplate template definition: Translated “Program Manipulation via Interactive Transformations” example to actual StringTemplate template definition: Integration with existing code clean, easy Integration with existing code clean, easy generateMemSize(node) ::= << int mem_size() { int result = 0;.mem_size();}> return result; >> *shameless StringTemplate plug

Semantics Side-effect free expressions Side-effect free expressions No “state” No “state” No defined order of execution No defined order of execution Lazy evaluation Lazy evaluation Template inheritance group to subgroup Template inheritance group to subgroup Dynamically scoped; values inherited Dynamically scoped; values inherited Recursive template instantiation Recursive template instantiation

Design Goals Optimized for enforcement of separation not for Turing completeness nor expressive “one-liners” Optimized for enforcement of separation not for Turing completeness nor expressive “one-liners” Conceptual integrity Conceptual integrity single-minded fanaticism against entanglement single-minded fanaticism against entanglement stick to a few concepts: attributes and templates stick to a few concepts: attributes and templates consistency; no special cases; driven by design not implementation details consistency; no special cases; driven by design not implementation details proper language formalisms, semantics, sane syntax proper language formalisms, semantics, sane syntax Simple as possible Simple as possible useful to nonprogrammers useful to nonprogrammers makes it fast, easy to build/maintain makes it fast, easy to build/maintain Powerful as possible Powerful as possible

Phase I, “Document with holes”; only tag refs “Document with holes”; only tag refs Could nest templates by setting a tag value to template; dynamic scoping (“value inheritance”) Could nest templates by setting a tag value to template; dynamic scoping (“value inheritance”) Not intended for full page generation Not intended for full page generation File of template definitions loaded into hash table; code manually created template instances File of template definitions loaded into hash table; code manually created template instances INSERT INTO Topics VALUES (, ' ' );

Phase II, Rewrite of jGuru.com; learned entanglement allowed by JSP is horrible! Rewrite of jGuru.com; learned entanglement allowed by JSP is horrible! Decided to enhance StringTemplate, letting needs dictate feature set; would not be Turing- complete! Decided to enhance StringTemplate, letting needs dictate feature set; would not be Turing- complete! Moved to single template per file format with $$…$$ indicating attribute ref Moved to single template per file format with $$…$$ indicating attribute ref Needed “include” Needed “include” added template reference rather than #include, macro added template reference rather than #include, macro subconciously supported recursion subconciously supported recursion

Phase II, (Cont’d) Needed to walk multi-valued attributes; added “apply” syntax: Needed to walk multi-valued attributes; added “apply” syntax: $names:bold(item=names)$ $names:bold(item=names)$ $names:bold(item=names, separator=“, “)$ $names:bold(item=names, separator=“, “)$ Allowed anonymous templates Allowed anonymous templates $names:” $names$ ”$ $names:” $names$ ”$ Iterated value naming convention evolved Iterated value naming convention evolved $names:bold(item=names[i])$ $names:bold(item=names[i])$ Needed multiple array walk for relational DB Needed multiple array walk for relational DB $a,b:foo(x=a[i], y=b[i])$ $a,b:foo(x=a[i], y=b[i])$ Finally, needed conditional inclusion Finally, needed conditional inclusion $if(userName)$ Name: $userName$ $endif(userName)$

Phase III, 2003-Summer 2004 Wanted to say $names:bold()$ w/o arg Wanted to say $names:bold()$ w/o arg defined default attribute attr defined default attribute attr Cleaned up IF: $if(foo)$…$endif$ Cleaned up IF: $if(foo)$…$endif$ Nested anonymous blocks: $names:{…}$ Nested anonymous blocks: $names:{…}$ Round-robin template application Round-robin template application $users: rowOdd(), rowEven()$ $users: rowOdd(), rowEven()$ Properties: $user.name$ calls user.getName() Properties: $user.name$ calls user.getName() Indirect template ref: $(tname)()$ ; added in context of “immediate evaluation” Indirect template ref: $(tname)()$ ; added in context of “immediate evaluation” Template inheritance via StringTemplateGroup Template inheritance via StringTemplateGroup

Phase IV, 2004 Added group file format: mutually- referential templates with formal arguments Added group file format: mutually- referential templates with formal arguments method(type,name,args,body) ::= << public ( ) { } >> assign(lhs,expr) ::= “ = ;” …

Future Named args for anonymous blocks ala Smalltalk Named args for anonymous blocks ala Smalltalk $list:{x | $x$ }$ $list:{x | $x$ }$.mem_size();}> iterator could also choose single argument from named templates iterator could also choose single argument from named templates Add parallel array walking back in Add parallel array walking back in Syntax for super group? Syntax for super group? group sub : super; group sub : super; Lots of little clean up; e.g., whitespace, … Lots of little clean up; e.g., whitespace, …

Experience (ANTLR v3.0) Tree walker (controller) collects data from AST (model), pushes data into templates (view) Tree walker (controller) collects data from AST (model), pushes data into templates (view) Decouples order of computation from order of output (this is huge) Decouples order of computation from order of output (this is huge) Enforced separation guarantees easy retargeting, no code duplication, … Enforced separation guarantees easy retargeting, no code duplication, … no code in template no code in template no output strings in code generator no output strings in code generator Previous code generator hopelessly entangled Previous code generator hopelessly entangled Group file format (output grammar) is great! “Executable documentation” Group file format (output grammar) is great! “Executable documentation”

Practical Lessons Separate pattern matching from templates! Separate pattern matching from templates! don’t make templates also analyze model don’t make templates also analyze model lazy evaluation essential; decouple order of computation from order of output; build in any order lazy evaluation essential; decouple order of computation from order of output; build in any order Isolates templates (“user code”) from model changes, which will surely evolve Isolates templates (“user code”) from model changes, which will surely evolve Templates may be reusable w/o embedded logic Templates may be reusable w/o embedded logic Recursion required for nested structures Recursion required for nested structures Attributes Attributes dynamic scoping really handy dynamic scoping really handy push don’t pull attributes; attributes are inert push don’t pull attributes; attributes are inert Simple language is fast, easy to learn, easy to use, promotes retargetability Simple language is fast, easy to learn, easy to use, promotes retargetability

Philosophical Lessons Say what you mean! e.g., no FOR-loops Say what you mean! e.g., no FOR-loops Focus on principles, conceptual integrity Focus on principles, conceptual integrity focus on what not how focus on what not how other tools either doc with holes or Turing complete other tools either doc with holes or Turing complete Difficult to stick to your principles and still create usable tool; grey areas appear Difficult to stick to your principles and still create usable tool; grey areas appear Language design is hard; implementation is relatively easy Language design is hard; implementation is relatively easy Selection pressure results in StringTemplate Selection pressure results in StringTemplate output conforms to a language, hence, a grammar output conforms to a language, hence, a grammar strict enforcement of separation leads to grammar strict enforcement of separation leads to grammar

Demo Generating Java / XML with same model Generating Java / XML with same model