> For the complete documentation index, see [llms.txt](https://gofast.disasm.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gofast.disasm.dev/basics/parsing-javascript-files.md).

# Parsing JavaScript Files

## Parsing JavaScript Files

The **parser** module in `go-fAST` allows you to turn JavaScript source code into a rich, structured Abstract Syntax Tree (AST). This is the foundation for any static analysis, transformation, or introspection you want to perform.

***

### ✨ Basic Usage

To parse a JavaScript string into an AST, use the `parser.ParseFile()` function:

```go
prog, err := parser.ParseFile(src)
if err != nil {
    panic("error parsing src script")
}
```

If successful, `prog` will contain an `*ast.Program` node — the root of your AST.

***

### 📂 Example: Parsing a File

```go
package main

import (
	"fmt"
	"os"

	"github.com/t14raptor/go-fast/parser"
)

func main() {
	data, err := os.ReadFile("in.js")
	if err != nil {
		fmt.Println("Failed to read file:", err)
		return
	}

	ast, err := parser.ParseFile(string(data))
	if err != nil {
		fmt.Println("Parsing failed:", err)
		return
	}

	fmt.Println("Successfully parsed AST root:", ast.Type())
	// You can now traverse or transform the AST
}
```

***

### 📦 What You Get

The result of `parser.ParseFile()` is an `*ast.Program`, which contains a `Body` field consisting of a list of statements. This structure mirrors ECMAScript’s source grammar:

```go
type Program struct {
	Body ast.Statements // List of top-level statements
}
```

Each node in the tree implements the `VisitableNode` interface, allowing you to use the Visitor API for traversal and analysis.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gofast.disasm.dev/basics/parsing-javascript-files.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
