acorn (original) (raw)

Acorn

A tiny, fast JavaScript parser written in JavaScript.

Community

Acorn is open source software released under anMIT license.

You are welcome toreport bugs or create pull requests on github.

Installation

The easiest way to install acorn is from npm:

Alternately, you can download the source and build acorn yourself:

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

Interface

parse(input, options) is the main interface to the library. Theinput parameter is a string, options must be an object setting some of the options listed below. The return value will be an abstract syntax tree object as specified by the ESTree spec.

let acorn = require("acorn"); console.log(acorn.parse("1 + 1", {ecmaVersion: 2020}));

When encountering a syntax error, the parser will raise aSyntaxError object with a meaningful message. The error object will have a pos property that indicates the string offset at which the error occurred, and a loc object that contains a {line, column}object referring to that same position.

Options are provided by in a second argument, which should be an object containing any of these fields (only ecmaVersion is required):

parseExpressionAt(input, offset, options) will parse a single expression in a string, and return its AST. It will not complain if there is more of the string left after the expression.

tokenizer(input, options) returns an object with a getTokenmethod that can be called repeatedly to get the next token, a {start, end, type, value} object (with added loc property when thelocations option is enabled and range property when the rangesoption is enabled). When the token's type is tokTypes.eof, you should stop calling the method, since it will keep returning that same token forever.

Note that tokenizing JavaScript without parsing it is, in modern versions of the language, not really possible due to the way syntax is overloaded in ways that can only be disambiguated by the parse context. This package applies a bunch of heuristics to try and do a reasonable job, but you are advised to use parse with the onTokenoption instead of this.

In ES6 environment, returned result can be used as any other protocol-compliant iterable:

for (let token of acorn.tokenizer(str)) { // iterate over the tokens }

// transform code to array of tokens: var tokens = [...acorn.tokenizer(str)];

tokTypes holds an object mapping names to the token type objects that end up in the type properties of tokens.

getLineInfo(input, offset) can be used to get a {line, column} object for a given program string and offset.

The Parser class

Instances of the Parser class contain all the state and logic that drives a parse. It has static methods parse,parseExpressionAt, and tokenizer that match the top-level functions by the same name.

When extending the parser with plugins, you need to call these methods on the extended version of the class. To extend a parser with plugins, you can use its static extend method.

var acorn = require("acorn"); var jsx = require("acorn-jsx"); var JSXParser = acorn.Parser.extend(jsx()); JSXParser.parse("foo()", {ecmaVersion: 2020});

The extend method takes any number of plugin values, and returns a new Parser class that includes the extra parser logic provided by the plugins.

Command line interface

The bin/acorn utility can be used to parse a file from the command line. It accepts as arguments its input file and the following options:

The utility spits out the syntax tree as JSON data.

Existing plugins