acorn

Acorn

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

Installation

The easiest way to install acorn is with npm.

npm install acorn

Alternately, download the source.

git clone https://github.com/marijnh/acorn.git

Components

When run in a CommonJS (node.js) or AMD environment, exported values
appear in the interfaces exposed by the individual files, as usual.
When loaded in the browser (Acorn works in any JS-enabled browser more
recent than IE5) without any kind of module management, a single
global object acorn will be defined, and all the exported properties
will be added to that.

acorn.js

This file contains the actual parser (and is what you get when you
require("acorn") in node.js).

parse(input, options) is used to parse a JavaScript program.
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
Mozilla Parser API.

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 character offset at which the
error occurred, and a loc object that contains a {line, column}
object referring to that same position.

  • ecmaVersion: Indicates the ECMAScript version to parse. Must be
    either 3, 5, or 6. This influences support for strict mode, the set
    of reserved words, and support for new syntax features. Default is 5.

  • strictSemicolons: If true, prevents the parser from doing
    automatic semicolon insertion, and statements that do not end with
    a semicolon will generate an error. Defaults to false.

  • allowTrailingCommas: If false, the parser will not allow
    trailing commas in array and object literals. Default is true.

  • forbidReserved: If true, using a reserved word will generate
    an error. Defaults to false. When given the value "everywhere",
    reserved words and keywords can also not be used as property names
    (as in Internet Explorer's old parser).

  • allowReturnOutsideFunction: By default, a return statement at
    the top level raises an error. Set this to true to accept such
    code.

  • locations: When true, each node has a loc object attached
    with start and end subobjects, each of which contains the
    one-based line and zero-based column numbers in {line, column}
    form. Default is false.

  • onToken: If a function is passed for this option, each found
    token will be passed in same format as tokenize() returns.

    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.

  • onComment: If a function is passed for this option, whenever a
    comment is encountered the function will be called with the
    following parameters:

    • block: true if the comment is a block comment, false if it
      is a line comment.
    • text: The content of the comment.
    • start: Character offset of the start of the comment.
    • end: Character offset of the end of the comment.

    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:

    {
      "type": "Line" | "Block",
      "value": "comment text",
      "range": ...,
      "loc": ...
    }
    

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

  • ranges: Nodes have their start and end characters offsets
    recorded in start and end properties (directly on the node,
    rather than the loc object, which holds line/column data. To also
    add a semi-standardized "range" property holding a
    [start, end] array with the same numbers, set the ranges option
    to true.

  • program: It is possible to parse multiple files into a single
    AST by passing the tree produced by parsing the first file as the
    program option in subsequent parses. This will add the toplevel
    forms of the parsed file to the "Program" (top) node of an existing
    parse tree.

  • sourceFile: When the locations option is true, you can pass
    this option to add a source attribute in every node’s loc
    object. Note that the contents of this option are not examined or
    processed in any way; you are free to use whatever format you
    choose.

  • directSourceFile: Like sourceFile, but a sourceFile property
    will be added directly to the nodes, rather than the loc object.

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.

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

tokenize(input, options) exports a primitive interface to
Acorn's tokenizer. The function takes an input string and options
similar to parse (though only some options are meaningful here), and
returns a function that can be called repeatedly to read a single
token, and returns 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).

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

Note on using with Escodegen

Escodegen supports generating comments from AST, attached in
Esprima-specific format. In order to simulate same format in
Acorn, consider following example:

var comments = [], tokens = [];

var ast = acorn.parse('var x = 42; // answer', {
    // collect ranges for each node
    ranges: true,
    // collect comments in Esprima's format
    onComment: comments,
    // collect token ranges
    onToken: tokens
});

// attach comments using collected information
escodegen.attachComments(ast, comments, tokens);

// generate code
console.log(escodegen.generate(ast, {comment: true}));
// > 'var x = 42;    // answer'

Using Acorn in an environment with a Content Security Policy

Some contexts, such as Chrome Web Apps, disallow run-time code evaluation.
Acorn uses new Function to generate fast functions that test whether
a word is in a given set, and will trigger a security error when used
in a context with such a
Content Security Policy
(see #90 and
#123).

The bin/without_eval script can be used to generate a version of
acorn.js that has the generated code inlined, and can thus run
without evaluating anything. In versions of this library downloaded
from NPM, this script will be available as acorn_csp.js.

acorn_loose.js

This file implements an error-tolerant parser. It exposes a single
function.

parse_dammit(input, options) takes the same arguments and
returns the same syntax tree as the parse function in acorn.js,
but never raises an error, and will do its best to parse syntactically
invalid code in as meaningful a way as it can. It'll insert identifier
nodes with name "✖" as placeholders in places where it can't make
sense of the input. Depends on acorn.js, because it uses the same
tokenizer.

util/walk.js

Implements an abstract syntax tree walker. Will store its interface in
acorn.walk when used without a module system.

simple(node, visitors, base, state) does a 'simple' walk over
a tree. node should be the AST node to walk, and visitors an
object with properties whose names correspond to node types in the
Mozilla Parser API. The properties should contain functions
that will be called with the node object and, if applicable the state
at that point. The last two arguments are optional. base is a walker
algorithm, and state is a start state. The default walker will
simply visit all statements and expressions and not produce a
meaningful state. (An example of a use of state it to track scope at
each point in the tree.)

ancestor(node, visitors, base, state) does a 'simple' walk over
a tree, building up an array of ancestor nodes (including the current node)
and passing the array to callbacks in the state parameter.

recursive(node, state, functions, base) does a 'recursive'
walk, where the walker functions are responsible for continuing the
walk on the child nodes of their target node. state is the start
state, and functions should contain an object that maps node types
to walker functions. Such functions are called with (node, state, c)
arguments, and can cause the walk to continue on a sub-node by calling
the c argument on it with (node, state) arguments. The optional
base argument provides the fallback walker functions for node types
that aren't handled in the functions object. If not given, the
default walkers will be used.

make(functions, base) builds a new walker object by using the
walker functions in functions and filling in the missing ones by
taking defaults from base.

findNodeAt(node, start, end, test, base, state) tries to
locate a node in a tree at the given start and/or end offsets, which
satisfies the predicate test. start end end can be either null
(as wildcard) or a number. test may be a string (indicating a node
type) or a function that takes (nodeType, node) arguments and
returns a boolean indicating whether this node is interesting. base
and state are optional, and can be used to specify a custom walker.
Nodes are tested from inner to outer, so if two nodes match the
boundaries, the inner one will be preferred.

findNodeAround(node, pos, test, base, state) is a lot like
findNodeAt, but will match any node that exists 'around' (spanning)
the given position.

findNodeAfter(node, pos, test, base, state) is similar to
findNodeAround, but will match all nodes after the given position
(testing outer nodes before inner nodes).

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:

  • --ecma3|--ecma5|--ecma6: Sets the ECMAScript version to parse. Default is
    version 5.

  • --strictSemicolons: Prevents the parser from doing automatic
    semicolon insertion. Statements that do not end in semicolons will
    generate an error.

  • --locations: Attaches a "loc" object to each node with "start" and
    "end" subobjects, each of which contains the one-based line and
    zero-based column numbers in {line, column} form.

  • --compact: No whitespace is used in the AST output.

  • --silent: Do not output the AST, just return the exit status.

  • --help: Print the usage information and quit.

The utility spits out the syntax tree as JSON data.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容