# acornjs/acorn architecture

> Architecture diagram of the GitHub repository acornjs/acorn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/acornjs/acorn
- Repository: https://github.com/acornjs/acorn

## Overview

Acorn is a JavaScript parsing toolkit with three product packages: the strict parser, an error-tolerant parser, and an ESTree syntax-tree walker. The main workflow accepts source text and options, tokenizes and parses it into an AST; callers can instead request tokens or parse an expression at an offset. The loose parser reuses Acorn’s tokenizer and produces ASTs despite malformed input. The walker traverses syntax trees. The graph includes documented package capabilities and central parser stages; some internal wiring is omitted where the excerpts do not show a calling relationship.

Main entry points: [`acorn/src/state.js`](https://github.com/acornjs/acorn/blob/master/acorn/src/state.js) (Parser state), [`acorn-loose/src/state.js`](https://github.com/acornjs/acorn/blob/master/acorn-loose/src/state.js) (Loose parser state), [`acorn/src/expression.js`](https://github.com/acornjs/acorn/blob/master/acorn/src/expression.js) (Expression parser), [`acorn/src/index.js`](https://github.com/acornjs/acorn/blob/master/acorn/src/index.js) (Parser API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_main["Acorn parser"]
  node_api["Parser API<br/>[index.js]"]
  node_parser["Parser state<br/>[state.js]"]
  node_tokenizer["Tokenizer<br/>[tokenize.js]"]
  node_token_context["Token context<br/>[tokencontext.js]"]
  node_expressions["Expression parser<br/>[expression.js]"]
  node_statements["Statement parser<br/>[statement.js]"]
end

subgraph group_loose["Loose parser"]
  node_loose_api["Loose parser API<br/>[index.js]"]
  node_loose_state["Loose parser state<br/>[state.js]"]
  node_loose_statements["Loose statements<br/>[statement.js]"]
  node_loose_expressions["Loose expressions<br/>[expression.js]"]
  node_loose_tokens["Loose token handling<br/>[tokenize.js]"]
  node_loose_util["Recovery utilities<br/>[parseutil.js]"]
end

subgraph group_walk["AST walker"]
  node_walker["Syntax tree walker<br/>[index.js]"]
end

subgraph group_support["Parser support"]
  node_token_types["Token types<br/>[tokentype.js]"]
  node_bindings["Bindings and scopes<br/>[lval.js]"]
  node_scope_rules["Scope rules<br/>[scope.js]"]
  node_parse_util["Parse utilities<br/>[parseutil.js]"]
  node_identifiers["Identifier rules<br/>[identifier.js]"]
  node_regexp["RegExp validation<br/>[regexp.js]"]
  node_locations["Source locations<br/>[locutil.js]"]
end

node_caller(("Library caller"))

node_caller -->|"submits source"| node_api
node_api -->|"parses with"| node_parser
node_parser -->|"parses expressions"| node_expressions
node_parser -->|"parses statements"| node_statements
node_parser -->|"tracks context"| node_token_context
node_token_context -->|"updates token context"| node_token_types
node_parser -->|"uses parsing helpers"| node_parse_util
node_parser -->|"checks identifiers"| node_identifiers
node_parser -->|"computes locations"| node_locations
node_expressions -->|"tests token types"| node_token_types
node_expressions -->|"checks assignment targets"| node_bindings
node_statements -->|"manages scopes"| node_scope_rules
node_loose_api -->|"creates parser"| node_loose_state
node_loose_state -->|"uses token handling"| node_loose_tokens
node_loose_state -->|"parses statements"| node_loose_statements
node_loose_state -->|"parses expressions"| node_loose_expressions
node_loose_state -->|"uses recovery helpers"| node_loose_util
node_loose_state -->|"uses base parser"| node_parser
node_caller -->|"walks syntax trees"| node_walker

click node_api "https://github.com/acornjs/acorn/blob/master/acorn/src/index.js"
click node_parser "https://github.com/acornjs/acorn/blob/master/acorn/src/state.js"
click node_tokenizer "https://github.com/acornjs/acorn/blob/master/acorn/src/tokenize.js"
click node_token_context "https://github.com/acornjs/acorn/blob/master/acorn/src/tokencontext.js"
click node_token_types "https://github.com/acornjs/acorn/blob/master/acorn/src/tokentype.js"
click node_expressions "https://github.com/acornjs/acorn/blob/master/acorn/src/expression.js"
click node_statements "https://github.com/acornjs/acorn/blob/master/acorn/src/statement.js"
click node_bindings "https://github.com/acornjs/acorn/blob/master/acorn/src/lval.js"
click node_scope_rules "https://github.com/acornjs/acorn/blob/master/acorn/src/scope.js"
click node_parse_util "https://github.com/acornjs/acorn/blob/master/acorn/src/parseutil.js"
click node_identifiers "https://github.com/acornjs/acorn/blob/master/acorn/src/identifier.js"
click node_regexp "https://github.com/acornjs/acorn/blob/master/acorn/src/regexp.js"
click node_locations "https://github.com/acornjs/acorn/blob/master/acorn/src/locutil.js"
click node_loose_api "https://github.com/acornjs/acorn/blob/master/acorn-loose/src/index.js"
click node_loose_state "https://github.com/acornjs/acorn/blob/master/acorn-loose/src/state.js"
click node_loose_statements "https://github.com/acornjs/acorn/blob/master/acorn-loose/src/statement.js"
click node_loose_expressions "https://github.com/acornjs/acorn/blob/master/acorn-loose/src/expression.js"
click node_loose_tokens "https://github.com/acornjs/acorn/blob/master/acorn-loose/src/tokenize.js"
click node_loose_util "https://github.com/acornjs/acorn/blob/master/acorn-loose/src/parseutil.js"
click node_walker "https://github.com/acornjs/acorn/blob/master/acorn-walk/src/index.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_parser,node_tokenizer,node_token_context,node_expressions,node_statements toneBlue
class node_loose_api,node_loose_state,node_loose_statements,node_loose_expressions,node_loose_tokens,node_loose_util toneAmber
class node_walker toneMint
class node_token_types,node_bindings,node_scope_rules,node_parse_util,node_identifiers,node_regexp,node_locations toneRose
class node_caller toneIndigo
```

## Components

### Acorn parser

- [Parser API](https://github.com/acornjs/acorn/blob/master/acorn/src/index.js): `acorn/src/index.js`
- [Parser state](https://github.com/acornjs/acorn/blob/master/acorn/src/state.js): `acorn/src/state.js`
- [Tokenizer](https://github.com/acornjs/acorn/blob/master/acorn/src/tokenize.js): `acorn/src/tokenize.js`
- [Token context](https://github.com/acornjs/acorn/blob/master/acorn/src/tokencontext.js): `acorn/src/tokencontext.js`
- [Expression parser](https://github.com/acornjs/acorn/blob/master/acorn/src/expression.js): `acorn/src/expression.js`
- [Statement parser](https://github.com/acornjs/acorn/blob/master/acorn/src/statement.js): `acorn/src/statement.js`

### Loose parser

- [Loose parser API](https://github.com/acornjs/acorn/blob/master/acorn-loose/src/index.js): `acorn-loose/src/index.js`
- [Loose parser state](https://github.com/acornjs/acorn/blob/master/acorn-loose/src/state.js): `acorn-loose/src/state.js`
- [Loose statements](https://github.com/acornjs/acorn/blob/master/acorn-loose/src/statement.js): `acorn-loose/src/statement.js`
- [Loose expressions](https://github.com/acornjs/acorn/blob/master/acorn-loose/src/expression.js): `acorn-loose/src/expression.js`
- [Loose token handling](https://github.com/acornjs/acorn/blob/master/acorn-loose/src/tokenize.js): `acorn-loose/src/tokenize.js`
- [Recovery utilities](https://github.com/acornjs/acorn/blob/master/acorn-loose/src/parseutil.js): `acorn-loose/src/parseutil.js`

### AST walker

- [Syntax tree walker](https://github.com/acornjs/acorn/blob/master/acorn-walk/src/index.js): `acorn-walk/src/index.js`

### Parser support

- [Token types](https://github.com/acornjs/acorn/blob/master/acorn/src/tokentype.js): `acorn/src/tokentype.js`
- [Bindings and scopes](https://github.com/acornjs/acorn/blob/master/acorn/src/lval.js): `acorn/src/lval.js`
- [Scope rules](https://github.com/acornjs/acorn/blob/master/acorn/src/scope.js): `acorn/src/scope.js`
- [Parse utilities](https://github.com/acornjs/acorn/blob/master/acorn/src/parseutil.js): `acorn/src/parseutil.js`
- [Identifier rules](https://github.com/acornjs/acorn/blob/master/acorn/src/identifier.js): `acorn/src/identifier.js`
- [RegExp validation](https://github.com/acornjs/acorn/blob/master/acorn/src/regexp.js): `acorn/src/regexp.js`
- [Source locations](https://github.com/acornjs/acorn/blob/master/acorn/src/locutil.js): `acorn/src/locutil.js`

### Other components

- **Library caller**

## Connections

- Library caller → Parser API: submits source
- Parser API → Parser state: parses with (evidence: `acorn/src/index.js`)
- Parser state → Expression parser: parses expressions (evidence: `acorn/src/expression.js`)
- Parser state → Statement parser: parses statements (evidence: `acorn/src/statement.js`)
- Parser state → Token context: tracks context (evidence: `acorn/src/tokencontext.js`)
- Token context → Token types: updates token context (evidence: `acorn/src/tokencontext.js`)
- Parser state → Parse utilities: uses parsing helpers (evidence: `acorn/src/parseutil.js`)
- Parser state → Identifier rules: checks identifiers (evidence: `acorn/src/state.js`)
- Parser state → Source locations: computes locations (evidence: `acorn/src/locutil.js`)
- Expression parser → Token types: tests token types (evidence: `acorn/src/expression.js`)
- Expression parser → Bindings and scopes: checks assignment targets (evidence: `acorn/src/lval.js`)
- Statement parser → Scope rules: manages scopes (evidence: `acorn/src/statement.js`)
- Loose parser API → Loose parser state: creates parser (evidence: `acorn-loose/src/index.js`)
- Loose parser state → Loose token handling: uses token handling (evidence: `acorn-loose/src/index.js`)
- Loose parser state → Loose statements: parses statements (evidence: `acorn-loose/src/index.js`)
- Loose parser state → Loose expressions: parses expressions (evidence: `acorn-loose/src/index.js`)
- Loose parser state → Recovery utilities: uses recovery helpers (evidence: `acorn-loose/src/state.js`)
- Loose parser state → Parser state: uses base parser (evidence: `acorn-loose/src/state.js`)
- Library caller → Syntax tree walker: walks syntax trees
