JavaScript Parser To Create Abstract Syntax Tree(AST): Acorn

0. Preface

JavaScript parser — Acorn and AST are useful things. They help us automatically and efficiently edit source code. This post shows you how to build and edit the AST of JavaScript code.

1. Content

2. Install the JavaScript Parser — Acorn

To quote from the github repository, Acorn is a tiny, fast JavaScript parser, written completely in JavaScript, released under a MIT license. Acorn can generate abstract syntax trees for JavaScript codes. It has 3 modules: the main JavaScript parser named “acorn”, the error-tolerant parser named “acorn-loose”, the syntax tree walker named “acorn-walk”. This post focuses on the main parser. In this section, we introduce its installation.

ANTLR: An Informal Introduction

In this article, I am going to introduce you to ANTLR's powerful framework. Equipped with this framework, we will also write a relatively simple language that coordinates the process of shearing a metal sheet (or any other sheet). The first steps in the language are going to be relatively simple, but in the next articles, more and more details will emerge. In the end, we will have something fully-fledged and effectively functioning. So, without further ado, let's take a closer look at how ANTLR works. 

What Is ANTLR? And How Can You Use It?

ANTLR (ANother Tool for Language Recognition), according to Terence Parr, "is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It’s widely used to build languages, tools, and frameworks. From grammar, ANTLR generates a parser that can build and walk parse trees."

JavaParser: Java Code Generation

In this article, I'm going to show you how to generate Java code using theJavaParser. I couldn't find much of the documentation available with regards to code generation in javaparser.org or the manual. So, I thought putting this out would help someone who would like to experiment with the Java parser.

In its simplest form, the JavaParser library allows you to interact with Java source code as a Java object representation in a Java environment. More formally, we refer to this object representation as an Abstract Syntax Tree (AST). Also, it has the ability to manipulate the underlying structure of the source code. This can then be written to a file, providing developers with the facility to build their own code generating software.