{"id":16513751,"url":"https://github.com/nitin42/creating-a-pretty-printer","last_synced_at":"2026-02-19T14:05:48.462Z","repository":{"id":87330112,"uuid":"123112831","full_name":"nitin42/Creating-a-pretty-printer","owner":"nitin42","description":"This is a tutorial for creating a pretty printer in JavaScript","archived":false,"fork":false,"pushed_at":"2018-02-27T14:56:57.000Z","size":23,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-07T19:26:53.444Z","etag":null,"topics":["academic","compilers","compuer-science","parsers","prettier","programming"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nitin42.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-27T10:26:44.000Z","updated_at":"2023-03-14T23:41:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"3b4effef-311f-4886-8a8e-0f947172d661","html_url":"https://github.com/nitin42/Creating-a-pretty-printer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nitin42/Creating-a-pretty-printer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin42%2FCreating-a-pretty-printer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin42%2FCreating-a-pretty-printer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin42%2FCreating-a-pretty-printer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin42%2FCreating-a-pretty-printer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nitin42","download_url":"https://codeload.github.com/nitin42/Creating-a-pretty-printer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin42%2FCreating-a-pretty-printer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29616960,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T13:04:20.082Z","status":"ssl_error","status_checked_at":"2026-02-19T13:03:33.775Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["academic","compilers","compuer-science","parsers","prettier","programming"],"created_at":"2024-10-11T16:10:10.292Z","updated_at":"2026-02-19T14:05:48.440Z","avatar_url":"https://github.com/nitin42.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Creating a pretty printer\n\n\u003e This is a tutorial for creating a pretty printer in JavaScript\n\n## Introduction\n\nThis 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.\n\nSo 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.\n\n## Prerequisite\n\nFor 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](https://medium.com/@NTulswani/embracing-functional-style-within-object-oriented-paradigm-3e5e0fe5ccf).\n\n## Tools and Language\n\nWe will be using JavaScript.\n\n## Who should read this tutorial ?\n\nCasual programmers!\n\n## What we will build ?\n\nWe will create a basic printer to pretty print each node in the syntax tree in **Lisp style**.\n\nConsidering that we have this syntax tree -\n\n```\n{\n  Expression: {\n    BinaryExpression: {\n      left: {\n        LiteralExpression: {\n          value: 12\n        }\n      },\n      operator: '*',\n      right: {\n        BinaryExpression: {\n          left: {\n            LiteralExpression: {\n              value: 10\n            }\n          },\n          operator: '-',\n          right: {\n            LiteralExpression: {\n              value: 20\n            }\n          }\n        }\n      }\n    }\n  }\n}\n```\n\nThe printer will output the below code -\n\n```lisp\n(* 12 (- 10 20))\n```\n\n## Overview\n\nWe will be covering:\n\n* [Defining grammar](./grammar.md)\n\n* [Creating visitor interface](./visitor.md)\n\n* [Defining expression class for each node in our tree](./expression.md)\n\n* [Creating a helper class using inheritance to traverse each node in our tree and pretty print the node](./printer.md)\n\nI 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.\n\n## Why should I care ?\n\nHmm... may be you are interested in understanding the magic behind the tools that make your code look pretty.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnitin42%2Fcreating-a-pretty-printer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnitin42%2Fcreating-a-pretty-printer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnitin42%2Fcreating-a-pretty-printer/lists"}