Abstract
This paper proposes a fluent API generator for Scala, Haskell, and C++. It receives a grammar definition and generates a code skeleton of the library in the host programming language. The generated library is accessed through a chain of method calls; this style of API is called a fluent API. The library uses the host-language type checker to detect an invalid chain of method calls. Each method call is regarded as a lexical token in the embedded domain specific language implemented by that library. A sequence of the lexical tokens is checked and, if the sequence is not acceptable by the grammar, a type error is reported during compilation time. A contribution of this paper is to present an algorithm for generating the code-skeleton for a fluent API that reports a type error when a chain of method calls to the library does not match the given LR grammar. Our algorithm works in Scala, Haskell, and C++. To encode LR parsing, it uses the method/function overloading available in those languages. It does not need an advanced type system, or exponential compilation time or memory consumption. This paper also presents our implementation of the proposed generator.
- Roland Bock. 2016. rbock/sqlpp11: A type safe SQL template library for C++. https://github.com/rbock/sqlpp11 .Google Scholar
- John Cocke. 1969. Programming Languages and Their Compilers: Preliminary Notes.Google Scholar
- Bruno Courcelle. 1977. On Jump-Deterministic Pushdown Automata. Mathematical systems theory (1977).Google Scholar
- Werner Dietl, Stephanie Dietzel, Michael D. Ernst, Kivanç Muşlu, and Todd W. Schiller. 2011. Building and Using Pluggable Type-checkers. In Proceedings of the 33rd International Conference on Software Engineering (ICSE ’11). ACM, 681–690. Google Scholar
Digital Library
- Apache Software Foundation. 2014. Apache Phoenix. https://phoenix.apache.org/ .Google Scholar
- Martin Fowler. 2005. FluentInterface. https://www.martinfowler.com/bliki/FluentInterface.html .Google Scholar
- Emden R. Gansner and Stephen C. North. 2000. An open graph visualization system and its applications to software engineering. SOFTWARE - PRACTICE AND EXPERIENCE 30, 11 (2000), 1203–1233.Google Scholar
Digital Library
- Jeremy Gibbons and Nicolas Wu. 2014. Folding domain-specific languages: Deep and shallow embeddings (functional Pearl). In Proceedings of the ACM SIGPLAN International Conference on Functional Programming.Google Scholar
Digital Library
- Joseph (Yossi) Gil and Keren Lenz. 2010. Simple and safe SQL queries with C++ templates. Science of Computer Programming 75, 7 (2010), 573 – 595. Generative Programming and Component Engineering (GPCE 2007).Google Scholar
Digital Library
- Yossi Gil and Tomer Levy. 2016. Formal Language Recognition with the Java Type Checker. In Proceedings of 30th European Conference on Object-Oriented Programming .Google Scholar
- Yossi Gil and Ori Roth. 2019. Fling - A Fluent API Generator. In 33rd European Conference on Object-Oriented Programming (ECOOP 2019) . Google Scholar
Cross Ref
- Jonathan Goldstine, John K. Price, and Detlef Wotschke. 1981. On reducing the number of states in a PDA. Mathematical systems theory 15, 1 (01 Dec 1981), 315–321. Google Scholar
Cross Ref
- Radu Grigore. 2017. Java Generics Are Turing Complete. In Proceedings of the 44th ACM SIGPLAN Symposium on Principles of Programming Languages .Google Scholar
Digital Library
- Stephan Heilbrunner. 1981. A parsing automata approach to LR theory. Theoretical Computer Science 15, 2 (1981), 117 – 157. Google Scholar
Cross Ref
- Kohei Honda. 1993. Types for dyadic interaction. In CONCUR’93, Eike Best (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg, 509–523.Google Scholar
- Paul Hudak. 1996. Building Domain-Specific Embedded Languages. Comput. Surveys (1996).Google Scholar
Digital Library
- Kazuhiro Ichikawa and Shigeru Chiba. 2017. User-Defined Operators Including Name Binding for New Language Constructs. The Art, Science, and Engineering of Programming 1, 2, Article 15 (2017), 15:1–15:25 pages.Google Scholar
- Google Inc. 2011. Improve your code with lint checks. https://developer.android.com/studio/write/lint .Google Scholar
- Tadao Kasami. 1965. An Efficient Recognition and Syntax-Analysis Algorithm for Context-Free Languages. Technical Report.Google Scholar
- Dimitrios Kouzapas, Ornela Dardha, Roly Perera, and Simon J. Gay. 2018. Typechecking protocols with Mungo and StMungo: A session type toolchain for Java. Science of Computer Programming 155 (2018), 52–75. Google Scholar
Cross Ref
- Stefan Krüger, Johannes Späth, Karim Ali, Eric Bodden, and Mira Mezini. 2018. CrySL: An Extensible Approach to Validating the Correct Usage of Cryptographic APIs. In European Conference on Object-Oriented Programming (ECOOP). 10:1–10:27.Google Scholar
- Tomer Levy. 2017. A Fluent API for Automatic Generation of Fluent APIs in Java.Google Scholar
- Harry G. Mairson. 1994. Generating Words in a Context-free Language Uniformly at Random. Inf. Process. Lett. (1994). Google Scholar
Digital Library
- Tomoki Nakamaru, Kazuhiro Ichikawa, Tetsuro Yamazaki, and Shigeru Chiba. 2017. Silverchain: a fluent API generator. In Proceedings of the 16th ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences .Google Scholar
Digital Library
- Hanne Riis Nielson and Flemming Nielson. 1992. Semantics with Applications: A Formal Introduction.Google Scholar
- Ligia Nistor, Darya Kurilova, Stephanie Balzer, Benjamin Chung, Alex Potanin, and Jonathan Aldrich. 2013. Wyvern: A Simple, Typed, and Pure Object-oriented Language. In Proceedings of the 5th Workshop on MechAnisms for SPEcialization, Generalization and inHerItance (MASPEGHI ’13) . ACM, 9–16.Google Scholar
Digital Library
- Joshua Sunshine, Karl Naden, Sven Stork, Jonathan Aldrich, and Éric Tanter. 2011. First-class State Change in Plaid. In Proceedings of the 2011 ACM International Conference on Object Oriented Programming Systems Languages and Applications (OOPSLA ’11) . ACM, 713–732. Google Scholar
Digital Library
- Peter Thiemann and Vasco T. Vasconcelos. 2016. Context-free Session Types. In Proceedings of the 21st ACM SIGPLAN International Conference on Functional Programming (ICFP 2016) . ACM, 462–475. Google Scholar
Digital Library
- Todd Veldhuizen. 2003. C++ Templates are Turing Complete. Technical Report. Indiana University Computer Science.Google Scholar
- Hao Xu. 2010. EriLex: An Embedded Domain Specific Language Generator.Google Scholar
- Daniel Younger. 1967. Recognition and parsing of context-free languages in time n 3 . Information and Control (1967).Google Scholar
Index Terms
Generating a fluent API with syntax checking from an LR grammar
Recommendations
Silverchain: a fluent API generator
GPCE 2017: Proceedings of the 16th ACM SIGPLAN International Conference on Generative Programming: Concepts and ExperiencesThis paper presents a tool named Silverchain, which generates class definitions for a fluent API from the grammar of the API. A fluent API is an API that is used by method chaining and its grammar is a BNF-like set of rules that defines method chains ...
Component-based LR parsing
A language implementation with proper compositionality enables a compiler developer to divide-and-conquer the complexity of building a large language by constructing a set of smaller languages. Ideally, these small language implementations should be ...
Generalized LR Parsing Algorithm for Grammars with One-Sided Contexts
The Generalized LR parsing algorithm for context-free grammars is notable for having a decent worst-case running time (cubic in the length of the input string, if implemented efficiently), as well as much better performance on "good" grammars. This ...






Comments