node_modules/acorn/README.md

Acorn

A tiny, fast JavaScript parser written 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. For questions and discussion, please use the Tern discussion forum.

Installation

The easiest way to install acorn is from npm:

npm install acorn

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. The input parameter is a string, options can be undefined or 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"));

When encountering a syntax error, the parser will raise a SyntaxError 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 can be provided by passing a second argument, which should be an object containing any of these fields:

NOTE: Only 'stage 4' (finalized) ECMAScript features are being implemented by Acorn. Other proposed new features can be implemented through plugins.

If array is passed, each found token is pushed to it.

Note that you are not allowed to call the parser from the callback—that will corrupt its internal state.

When the locations options is on, the {line, column} locations of the comment’s start and end are passed as two additional parameters.

If array is passed for this option, each found comment is pushed to it as object in Esprima format:

javascript { "type": "Line" | "Block", "value": "comment text", "start": Number, "end": Number, // If `locations` option is on: "loc": { "start": {line: Number, column: Number} "end": {line: Number, column: Number} }, // If `ranges` option is on: "range": [Number, Number] }

Note that you are not allowed to call the parser from the callback—that will corrupt its internal state.

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 getToken method that can be called repeatedly to get the next token, a {start, end, type, value} object (with added loc property when the locations option is enabled and range property when the ranges option 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.

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(<bar/>)");

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

Plugins for ECMAScript proposals:



thisisnic/starrating documentation built on May 18, 2019, 1:32 p.m.