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
这是什么?Acorn 是一个体积很小、速度很快的 JavaScript 解析器,完全用 JavaScript 编写。它把 JS 源代码解析成抽象语法树(ESTree 兼容),之后各种工具就可以对这棵树进行分析或转换。
这是独立应用,还是我要加进项目的库?它是一个库,以三个包的形式发布:acorn(主解析器)、acorn-loose(容错解析器)和 acorn-walk(语法树遍历器)。
我要怎么把它加进项目?用 npm 把它安装为包,然后对一段 JS 源代码字符串调用它的 parse 函数,即可得到 AST;如果要构建仓库本身,运行 npm install。
使用前是否需要先安装什么特别的东西?只需要 Node.js 和 npm;这个包本身没有运行时依赖。
我能直接 fork Acorn 来支持自定义的 JS 方言吗?不必 fork。Acorn 支持插件系统:插件通过扩展 Parser 类来添加新语法,但插件 API 不够清晰,依赖内部实现细节,因此 Acorn 内部发生变化时,插件可能会失效。
谁会用到它?开发打包工具、代码检查工具、转译器或代码分析工具的开发者,这些工具需要把 JavaScript 解析成 AST。
能否同时组合多个插件,比如 JSX 和类型语法?可以。Parser.extend 接受多个插件函数作为参数,比如可以构建一个同时理解 JSX 标签和类型注解的 Parser 类。
插件到底怎么修改解析器?插件是一个函数,接收 Parser 类并返回一个扩展后的子类,通过重写 readToken 等方法来添加新的词法单元类型、分词器上下文或解析行为。
acorn-loose 是做什么用的?它是解析器的容错版本,即使输入的 JavaScript 存在语法错误,也会尽量生成一棵尽可能准确的 AST。
acorn-walk 是做什么的?它提供了一个语法树遍历器,用于遍历 acorn 生成的 AST,适合用于分析或转换处理。
Creator
acornjs
Size
6.4 MB
Stars
11.5K
Updated
CommitOct 14
Age
14y
Commits · 90 Days