In layout-sensitive languages, the indentation of an expression or statement can influence how a program is parsed. While some layout-sensitive languages (e.g., Python) have wide adoption in practice, there is little support for software language engineers in building tools for layout-sensitive languages. As a result, parsers, pretty-printers, program analyses, or refactorings often need to be handwritten, which decreases the maintainability and extensibility of these tools. Even state-of-the-art language workbenches have yet little support for layout-sensitive languages, restricting the development and prototyping of such languages.

To ease dealing with layout-sensitive languages in practice, we introduce a novel approach to support declarative specifications of layout-sensitive languages using layout declarations. Layout declarations are high-level specifications of indentation rules that occur in layout-sensitive languages, abstracting from low-level technicalities. We show how to automatically derive an efficient layout-sensitive generalized parser and a corresponding pretty-printer from a language specification with layout declarations. We validate our approach in a case-study using a syntax definition for the Haskell programming language, investigating the performance of the generated parser and the correctness of the generated pretty-printer when considering a corpus of 22191 Haskell files.