# Where to Find the Tree-Sitter Bash Grammar in Kimi-Code

> Locate the tree-sitter-bash grammar in Kimi-Code at packages/tree-sitter-bash/src/grammar.ts. Access the source code for the @moonshot-ai/tree-sitter-bash package and understand its structure.

- Repository: [Moonshot AI/kimi-code](https://github.com/MoonshotAI/kimi-code)
- Tags: how-to-guide
- Published: 2026-08-14

---

**The Tree-Sitter Bash grammar in Kimi-Code is located at [`packages/tree-sitter-bash/src/grammar.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/tree-sitter-bash/src/grammar.ts) within the `@moonshot-ai/tree-sitter-bash` package.**

Kimi-Code, MoonshotAI's AI coding assistant, implements a pure-TypeScript Tree-Sitter parser for Bash to power shell script analysis and understanding. This grammar file defines all node types, precedence rules, and production rules that match the upstream Tree-Sitter Bash grammar at version 0.25.0.

## Primary Grammar File Location

The definitive source for the **Tree-Sitter Bash grammar** lives in a single TypeScript file:

- **[`packages/tree-sitter-bash/src/grammar.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/tree-sitter-bash/src/grammar.ts)** — Contains complete grammar rules, node type mappings, and precedence declarations.

This file serves as the source of truth for how Bash scripts are parsed throughout the Kimi-Code monorepo. Unlike traditional Tree-Sitter grammars that use JavaScript grammar files compiled to C, Kimi-Code's implementation keeps everything in TypeScript for direct consumption by the parser.

## Supporting Parser Implementation Files

The grammar connects to three core implementation files that handle the actual parsing pipeline:

| File | Responsibility |
|------|---------------|
| [`packages/tree-sitter-bash/src/lexer.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/tree-sitter-bash/src/lexer.ts) | Tokenizes raw Bash source code according to lexical conventions |
| [`packages/tree-sitter-bash/src/parser.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/tree-sitter-bash/src/parser.ts) | Recursive-descent parser that walks grammar rules to build syntax trees |
| [`packages/tree-sitter-bash/src/index.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/tree-sitter-bash/src/index.ts) | Public API entry point exposing the `parse()` function with budget controls |

The lexer and parser work in tandem: the lexer produces a token stream, and the parser consumes that stream while applying the grammar rules from [`grammar.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/grammar.ts) to construct a concrete syntax tree.

## How the Grammar Is Used Across Kimi-Code

The **Tree-Sitter Bash grammar** integrates into higher-level systems through adapter services and UI components:

### Service Layer Integration

In [`packages/agent-core-v2/src/app/bashParser/bashParserService.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/agent-core-v2/src/app/bashParser/bashParserService.ts), a thin service wraps the parser to expose Bash parsing capabilities to the agent core:

```typescript
// Conceptual usage pattern from bashParserService.ts
import { parse, ParseOptions } from '@moonshot-ai/tree-sitter-bash';

// The service forwards parsing requests with configurable resource limits
const result = parse(scriptContent, {
  timeoutMs: 1000,
  maxNodes: 50_000
});

```

This service layer lets other Kimi-Code components request Bash ASTs without directly importing the parser package.

### Developer Tooling Integration

For debugging and inspection, [`apps/kimi-inspect/src/components/BashParserView.tsx`](https://github.com/MoonshotAI/kimi-code/blob/main/apps/kimi-inspect/src/components/BashParserView.tsx) renders parsed Bash trees visually:

```tsx
// React component consuming the Tree-Sitter Bash parser
import { parse } from '@moonshot-ai/tree-sitter-bash';
import type { SyntaxNode } from '@moonshot-ai/tree-sitter-bash';

function BashTreeView({ script }: { script: string }) {
  const { tree, hasError } = parse(script, { maxNodes: 10_000 });
  
  if (hasError) {
    return <div className="parse-error">Partial parse due to errors</div>;
  }
  
  return <SyntaxTreeRenderer root={tree} />;
}

```

## Practical Parsing Example

Here's how to use the **Tree-Sitter Bash grammar** implementation directly:

```typescript
import { parse } from '@moonshot-ai/tree-sitter-bash';

const bashScript = `#!/usr/bin/env bash
set -euo pipefail

# Extract filename without extension

filename="${'$'}{1%.*}"
echo "Processing: $filename"
`;

// Parse with optional safety limits
const result = parse(bashScript, {
  timeoutMs: 500,
  maxNodes: 5_000
});

// Inspect the resulting syntax tree
if (result.tree) {
  console.log(result.tree.type); // "program"
  
  // Walk named children (shebang, command, assignment, etc.)
  const nodeTypes = result.tree.namedChildren.map(n => n.type);
  console.log(nodeTypes);
  // ["shebang", "set_command", "comment", "variable_assignment", "command"]
}

```

The parser returns a **concrete syntax tree** whose node types directly correspond to the rules defined in [`grammar.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/grammar.ts) — enabling precise analysis of shell script structure.

## Grammar Documentation and Known Differences

The [`packages/tree-sitter-bash/README.md`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/tree-sitter-bash/README.md) file documents:

- Design rationale for the pure-TypeScript implementation
- Specific divergences from the upstream Tree-Sitter Bash grammar
- Performance characteristics and recommended usage patterns
- Version compatibility matrix with the upstream C-based grammar

Check this README when migrating code between Kimi-Code's parser and standard Tree-Sitter Bash implementations.

## Summary

- The **Tree-Sitter Bash grammar** source is at [`packages/tree-sitter-bash/src/grammar.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/packages/tree-sitter-bash/src/grammar.ts)
- The `@moonshot-ai/tree-sitter-bash` package provides a complete pure-TypeScript parser matching upstream v0.25.0
- Lexer ([`lexer.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/lexer.ts)) and parser ([`parser.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/parser.ts)) implement the grammar's parsing logic
- Service adapters in `agent-core-v2` and UI components in `kimi-inspect` consume parsed output
- Use `parse()` with optional `timeoutMs` and `maxNodes` budgets for safe parsing of untrusted input

## Frequently Asked Questions

### What version of the Tree-Sitter Bash grammar does Kimi-Code use?

Kimi-Code implements the Tree-Sitter Bash grammar at **version 0.25.0**. The [`grammar.ts`](https://github.com/MoonshotAI/kimi-code/blob/main/grammar.ts) file mirrors the upstream rule definitions while translating them into TypeScript-idiomatic structures for the pure-JavaScript parser.

### Why does Kimi-Code use a custom TypeScript parser instead of the official Tree-Sitter?

The `@moonshot-ai/tree-sitter-bash` package eliminates native compilation dependencies. Standard Tree-Sitter requires compiling C or WebAssembly grammars, which complicates distribution and startup. The TypeScript implementation enables immediate parsing in any JavaScript environment without platform-specific binaries.

### How do I handle parse errors or incomplete trees?

The `parse()` function returns a `hasError` flag and still provides a partial tree when errors occur. Always check `result.hasError` before assuming the tree is complete, and consider using the `timeoutMs` and `maxNodes` options to prevent resource exhaustion on malformed input.

### Can I use this parser outside of Kimi-Code?

The `@moonshot-ai/tree-sitter-bash` package is publishable and self-contained. Import it directly, pass Bash source strings to `parse()`, and work with the resulting `SyntaxNode` trees. The API intentionally mimics standard Tree-Sitter conventions for easy migration.