Git Product home page Git Product logo

creating-a-pretty-printer's Introduction

Creating a pretty printer

This is a tutorial for creating a pretty printer in JavaScript

Introduction

This tutorial is about creating a pretty printer in JavaScript to pretty print a very small subset of a language grammar, which we will define in later parts. But first of all, what is pretty printing ? I won't throw a very academic definition at you because I want to keep the things simple.

So before I define what is pretty printing, first let's understand what does a parser do ? A parser takes a string of source code and then uses it to construct an abstract syntax tree. Pretty printing is totally opposite of this process (approximately). It takes the abtract syntax tree and then produces a string of text from it which validates the syntax of our language.

Prerequisite

For this tutorial, we will be using a design pattern called visitor pattern because we will be dealing with each node in our tree and this pattern will be really helpful. I assume you know what is visitor pattern and how to use it. If not, then I've also written an article about it which you can find here.

Tools and Language

We will be using JavaScript.

Who should read this tutorial ?

Casual programmers!

What we will build ?

We will create a basic printer to pretty print each node in the syntax tree in Lisp style.

Considering that we have this syntax tree -

{
  Expression: {
    BinaryExpression: {
      left: {
        LiteralExpression: {
          value: 12
        }
      },
      operator: '*',
      right: {
        BinaryExpression: {
          left: {
            LiteralExpression: {
              value: 10
            }
          },
          operator: '-',
          right: {
            LiteralExpression: {
              value: 20
            }
          }
        }
      }
    }
  }
}

The printer will output the below code -

(* 12 (- 10 20))

Overview

We will be covering:

I promise not to bore you with the language theory around formal grammars, Chomsky hierarchy, and other academic material. I am sure you will enjoy reading this tutorial.

Why should I care ?

Hmm... may be you are interested in understanding the magic behind the tools that make your code look pretty.

creating-a-pretty-printer's People

Contributors

nitin42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.