Casual Parsing in JavaScript August 16, 2021

submited by
Style Pass
2021-08-17 07:00:08

Over the last year and a half I've gotten really into writing parsers and parser-adjacent things like interpreters, transpilers, etc. I've done most of these projects in JavaScript, and I've settled into a nice little pattern that I re-use across projects. I wanted to share it because I think it's neat, and it's brought me joy, and it could be an interesting or entertaining thing for others to follow along with!

This doesn't aim to be a comprehensive tutorial on parsing (for that I recommend the excellent Crafting Interpreters, which is how I learned!), but I want to make it accessible to people who may be new to this world, so I'm going to give a quick introduction. Feel free to skip the next section if you already know how recursive-descent parsing works in general.

A parser is a program that takes a (code) string as input, and outputs a structured representation of that code to some other logic, usually in the form of an "Abstract Syntax Tree" (or AST). An AST is a tree-like structure where each node in the tree represents some syntax concept found in the source code- maybe a particular if statement, or a function call, or a class definition. It's a tree because many nodes "contain" other nodes: an if statement will contain its condition and the block of statements associated with that condition.

Leave a Comment