Casual Parsing in JavaScript

JavaScript, often abbreviated as JS, is a programming language that conforms to the ECMAScript specification.

There are lots of ways to write parsers (and lots of libraries for doing so), and many of them are going to be more sophisticated, more flexible, more performant etc than what I do here. This is just the way I've enjoyed doing things, and it strikes a good balance between flexibility, simplicity, and accessibility. I'm going to use TypeScript instead of plain JavaScript because I think it makes it easier to illustrate what's really going on, and because that's what I end up doing in my own projects. But you could just as easily remove all the type declarations from this code and run it as-is. 

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.

Full Version

JavaScript engines were originally used only in web browsers, but they are now core components of other software systems, most notably servers and a variety of applications.