acorn README

The project's README from github.com/acornjs/acorn. As one markdown file: README.md.

Acorn Logo Acorn

Build Status NPM version CDNJS

A tiny, fast JavaScript parser, written completely in JavaScript.

Community

Acorn is open source software released under an MIT license.

You are welcome to report bugs or create pull requests on github.

Packages

This repository holds three packages:

To build the content of the repository, run npm install.

git clone https://github.com/acornjs/acorn.git
cd acorn
npm install

Plugin developments

Acorn is designed to support plugins which can, within reasonable bounds, redefine the way the parser works. Plugins can add new token types and new tokenizer contexts (if necessary), and extend methods in the parser object. This is not a clean, elegant API—using it requires an understanding of Acorn's internals, and plugins are likely to break whenever those internals are significantly changed. But still, it is possible, in this way, to create parsers for JavaScript dialects without forking all of Acorn. And in principle it is even possible to combine such plugins, so that if you have, for example, a plugin for parsing types and a plugin for parsing JSX-style XML literals, you could load them both and parse code with both JSX tags and types.

A plugin is a function from a parser class to an extended parser class. Plugins can be used by simply applying them to the Parser class (or a version of that already extended by another plugin). But because that gets a little awkward, syntactically, when you are using multiple plugins, the static method Parser.extend can be called with any number of plugin values as arguments to create a Parser class extended by all those plugins. You'll usually want to create such an extended class only once, and then repeatedly call parse on it, to avoid needlessly confusing the JavaScript engine's optimizer.

const {Parser} = require("acorn")

const MyParser = Parser.extend(
  require("acorn-jsx")(),
  require("acorn-bigint")
)
console.log(MyParser.parse("// Some bigint + JSX code"))

Plugins override methods in their new parser class to implement additional functionality. It is recommended for a plugin package to export its plugin function as its default value or, if it takes configuration parameters, to export a constructor function that creates the plugin function.

This is what a trivial plugin, which adds a bit of code to the readToken method, might look like:

module.exports = function noisyReadToken(Parser) {
  return class extends Parser {
    readToken(code) {
      console.log("Reading a token!")
      super.readToken(code)
    }
  }
}

acorn (acornjs/acorn)

Searching Database…

11.5K ·GitHub
What is this?Acorn is a tiny, fast JavaScript parser written entirely in JavaScript. It parses JS source code into an abstract syntax tree (ESTree-compatible), which tools can then analyze or transform.
Is this a standalone app or a library I add to my project?It is a library, distributed as three packages: acorn (the main parser), acorn-loose (an error-tolerant parser), and acorn-walk (a syntax tree walker).
How do I add it to a project?Install it as an npm package and call its parse function on a string of JS source to get back the AST; to build the repo itself, run npm install.
Do I need anything special installed first?Just Node.js and npm; the package itself has no runtime dependencies.
Can I just fork Acorn to support a custom JS dialect?Instead of forking, Acorn supports a plugin system: plugins extend the Parser class to add new syntax, but the plugin API isn't clean and relies on internal details, so plugins can break when Acorn's internals change.
Who would use this?Developers building tools like bundlers, linters, transpilers, or code analyzers that need to parse JavaScript into an AST.
Can I combine multiple plugins at once, like JSX and type syntax?Yes, Parser.extend accepts multiple plugin functions as arguments, letting you build a single Parser class that understands both JSX tags and type annotations, for example.
How do plugins actually modify the parser?A plugin is a function that takes the Parser class and returns an extended subclass, overriding methods (like readToken) to add new token types, tokenizer contexts, or parsing behavior.
What is acorn-loose for?It is the error-tolerant variant of the parser, meant to produce a best-effort AST even when the input JavaScript has syntax errors.
What does acorn-walk do?It provides a syntax tree walker for traversing the AST produced by acorn, useful for analysis or transformation passes.
Creator
acornjs
Size
6.4 MB
Stars
11.5K
Updated
CommitOct 14
Age
14y
Commits · 90 Days