> 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
