Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt

Use this file to discover all available pages before exploring further.

The parse command parses source files using a Tree-sitter parser. You can pass any number of file paths and glob patterns, or read from stdin if no paths are provided.
tree-sitter parse [OPTIONS] [PATHS]...
Aliases: p The command will exit with a non-zero status code if any parse errors occurred.

Input Sources

File Paths

Pass file paths or glob patterns directly:
tree-sitter parse file1.js file2.js
tree-sitter parse src/**/*.js

Paths File

Use --paths to provide a file containing paths:
tree-sitter parse --paths file-list.txt

Standard Input

If no paths are provided, input is read from stdin:
echo "console.log('hello')" | tree-sitter parse

Output Formats

Default (S-Expression)

By default, outputs the parse tree as an S-expression:
tree-sitter parse example.js

CST Format

Pretty-printed concrete syntax tree:
tree-sitter parse --cst example.js

XML Format

tree-sitter parse --xml example.js

Graphviz Dot

tree-sitter parse --dot example.js | dot -Tpng > tree.png

JSON Summary

tree-sitter parse --json-summary example.js

Options

Input Options

--paths
file
The path to a file that contains paths to source files to parse.
-p, --grammar-path
path
The path to the directory containing the grammar. Implies --rebuild.
-l, --lib-path
path
The path to the parser’s dynamic library. Used instead of the cached or automatically generated library.
--lang-name
string
If --lib-path is used, the name of the language used to extract the library’s language function.
--scope
scope
The language scope to use for parsing. Useful when the language is ambiguous.
--encoding
encoding
Set the encoding of the input file. One of utf8, utf16-le, utf16-be.By default, the CLI looks for a BOM to determine if the file is UTF-16BE or UTF-16LE. If no BOM is present, UTF-8 is assumed.
-n, --test-number
number
Parse a specific test in the corpus. The test number matches the output of tree-sitter test.

Output Options

--dot
flag
Output the parse tree with graphviz dot.
-x, --xml
flag
Output the parse tree in XML format.
-c, --cst
flag
Output the parse tree in a pretty-printed CST format.
-j, --json-summary
flag
Output parsing results in a JSON format.
-q, --quiet
flag
Suppress main output.
--no-ranges
flag
Omit the node’s ranges from the default parse output. Useful when copying S-Expressions to a test file.

Debug Options

-d, --debug
flag | type
Output parsing and lexing logs to stderr. Can optionally specify a debug type.
-D, --debug-graph
flag
Output logs of the graphs of the stack and parse trees during parsing. Graphs are constructed with graphviz dot, and output is written to log.html.
--open-log
flag
When using --debug-graph, open the log file in the default browser.
-0, --debug-build
flag
Compile the parser with debug flags enabled. Useful when debugging with gdb or lldb.

Performance Options

-s, --stat
flag
Show parsing statistics.
-t, --time
flag
Print the time taken to parse the file. If edits are provided, also prints time after each edit.
--timeout
microseconds
Set the timeout for parsing a single file, in microseconds.

Edit Options

--edits
edit
Apply edits after parsing the file. Edits are in the format: row,col|position delcount insert_text where row, col, and position are 0-indexed.Can be supplied multiple times.

Build Options

--wasm
flag
Compile and run the parser as a Wasm module (only if the CLI was built with --features=wasm).
-r, --rebuild
flag
Force a rebuild of the parser before parsing.
--config-path
path
The path to an alternative configuration (config.json) file. See init-config.

Examples

Parse a Single File

tree-sitter parse example.js

Parse Multiple Files with Statistics

tree-sitter parse --stat src/*.js

Parse with Debug Output

tree-sitter parse --debug-graph --open-log example.js

Parse and Measure Performance

tree-sitter parse --time --stat example.js

Parse with Edits

tree-sitter parse --edits "0,5|5 3 foo" example.js

Parse from Stdin

echo "const x = 42;" | tree-sitter parse --scope source.js