How to use acorn

Comprehensive acorn code examples:

How to use acorn.TokContext:

121
122
123
124
125
126
127
128
129
130
131
const {TokenType, TokContext, tokTypes, tokContexts, isNewLine, isIdentifierStart, isIdentifierChar} = require("acorn");
const {idents} = require('./util.js');
const arrowjsType = require('./arrowjsType.js');


const proc_tc = {
  p_expr: new TokContext("proc", true),
  p_cmd: new TokContext("cmd"),
  p_do: new TokContext("do{}"),
};

How to use acorn.TokenType:

131
132
133
134
135
136
137
138
139
140
141
const beforeExpr = {beforeExpr: true};


// new tokens (only valid within specific context)
const proc_tok = {
  parrow: new TokenType('->', beforeExpr),
  tail: new TokenType('-<', beforeExpr),
  tailUnit: new TokenType('-<|', beforeExpr),
  bind: new TokenType('<-', beforeExpr),
  form_start: new TokenType('(|', beforeExpr),
  form_end: new TokenType('|)'),

How to use acorn.Node:

37
38
39
40
41
42
43
44
45
46
 * @constructor
 * @param {!acorn.Parser} parser
 * @param {?} pos
 * @param {?} loc
 */
acorn.Node = acorn.Node;

/**
 * @constructor
 * @param {!Object} options Parse options.

How to use acorn.parseExpressionAt:

7
8
9
10
11
12
13
14
15
16

  throw new Error(`Unsupported Syntax ${node.type} at Location ${node.start}:${node.end}`);
}

function customerEval(code, env = {}) {
  const node = acorn.parseExpressionAt(code, 0, {
    ecmaVersion: 6
  })
  return evaluate(node, env)
}

How to use acorn.isIdentifierStart:

1
2
3
4
5
6
7
8
9
10
11
12
13


"use strict"


var acorn = require("acorn")
var tt = acorn.tokTypes
var isIdentifierStart = acorn.isIdentifierStart


module.exports = function(Parser) {
  return /*@__PURE__*/(function (Parser) {
    function anonymous () {

How to use acorn.Parser:

45
46
47
48
49
50
51
52
53
54
 * @constructor
 * @param {!Object} options Parse options.
 * @param {string} input The text to be parsed.
 * @param {number=} startPos Character offset to start parsing at.
 */
acorn.Parser = acorn.Parser;

/**
 * @return {!Node}
 */

How to use acorn.tokTypes:

31
32
33
34
35
36
37
38
39
40
41
_acorn.tokTypes._import.startsExpr = true;


function parseDynamicImport() {
  var node = this.startNode();
  this.next();
  if (this.type !== _acorn.tokTypes.parenL) {
    this.unexpected();
  }
  return this.finishNode(node, DynamicImportKey);
}

How to use acorn.parse:

4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622


function generateBinding (exp) {
  if (exp && typeof exp === 'string') {
    var ast = null;
    try {
      ast = acorn.parse(("(" + exp + ")"));
    } catch (e) {
      // warn(`Failed to parse the expression: "${exp}"`)
      return ''
    }