Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

acorn

> 编程语言
Open source

A small, fast, JavaScript-based JavaScript parser

11.4K stars0 likes5 views
WebsiteGitHub

About

A small, fast, JavaScript-based JavaScript parser

Acorn

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:

  • acorn: The main parser
  • acorn-loose: The error-tolerant parser
  • acorn-walk: The syntax tree walker

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

bash
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.

javascript
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:

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

Issues· 14 open

View all issuesOpen on GitHub
  • #1461

    currentVarScope/currentThisScope walk the scope stack per statement, so deeply nested blocks parse in quadratic time

    Updated Sep 27, 2026
  • #1445

    Quadratic regexp validation for ES2025 duplicate named capture groups

    Updated Jul 2, 2026
  • #1398

    "Runtime Errors for Function Call Assignment Targets" Test262 tests fail

    Updated May 26, 2026
  • #1260

    Latest release is missing type definitions

    Updated Sep 15, 2025
  • #1342

    Replace keyword Regexps with Sets for improved performance

    Updated May 12, 2025
  • #199

    Loose parser fails to parse ArrayExpression on multiple lines

    Updated Sep 17, 2024
  • #1285

    [acorn] Missing `value` property in `Token` (TypeScript)

    Updated Apr 18, 2024
  • #346

    Allow singling out a particular test

    enhancementUpdated Dec 30, 2023
  • #1228

    Add support for spec-compliant decorators

    Updated Dec 11, 2023
  • #989

    tokenizer fails on an await expression containing a regexp

    Updated Oct 11, 2020

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言