@arrmee/tree-sitter-vba
v1.0.0
Published
Tree-sitter grammar for Visual Basic for Applications (VBA) — case-insensitive, with full statement coverage
Maintainers
Readme
tree-sitter-vba
A Tree-sitter grammar for Visual Basic for Applications (VBA).
Fills the gap of the long-missing tree-sitter VBA parser — the only production-ready open-source VBA grammar for tree-sitter.
Features
- ✅ Case-insensitive keywords — VBA is case-insensitive by nature;
Sub,sub, andSUBall match correctly - ✅ Line continuation — supports VBA's
_line continuation character - ✅ Full VBA statement coverage —
Sub/Function/Property,If/ElseIf/Else,For/For Each/While/Doloops,Select Case,With,Dim,Const,Set,GoTo, error handling, file I/O, and more - ✅ Expression hierarchy — proper operator precedence for arithmetic, comparison, logical, and concatenation operators
- ✅ Pre-built DLL — compiled for Windows with MSVC ABI compatibility (via Zig)
Project Structure
tree-sitter-vba/
├── grammar.js # Tree-sitter grammar definition
├── src/
│ ├── parser.c # Generated C parser (1.2M)
│ ├── node-types.json # AST node type definitions
│ └── grammar.json # Grammar metadata
├── tree_sitter_vba.dll # Pre-built Windows DLL (MSVC compatible)
├── sgconfig.yml # ast-grep custom language config
├── rules/ # ast-grep search rules
│ └── find_subs.yml # Example rule: find all Sub procedures
├── test/
│ ├── sample.bas # Sample VBA test file
│ └── simple.bas # Simple VBA test file
└── tree-sitter.json # Tree-sitter configurationUsage
As a Tree-sitter grammar
# Install the grammar
npm install tree-sitter-vba
# Or generate from source
npx tree-sitter generate
npx tree-sitter buildWith Python
import ctypes
from tree_sitter import Language, Parser
lib = ctypes.CDLL('./tree_sitter_vba.dll')
lib.tree_sitter_vba.restype = ctypes.c_void_p
lang = Language(lib.tree_sitter_vba())
parser = Parser(lang)
tree = parser.parse(bytes(code, 'utf-8'))With C/C++
#include "tree_sitter/api.h"
TSLanguage *tree_sitter_vba();
TSLanguage *vba = tree_sitter_vba();
TSParser *parser = ts_parser_new();
ts_parser_set_language(parser, vba);With ast-grep (custom language)
# sgconfig.yml
customLanguages:
vba:
libraryPath: ./tree_sitter_vba.dll
extensions:
- .bas
- .cls
- .frmBuilding from Source
Prerequisites
- Tree-sitter CLI
- A C compiler (GCC/MinGW, MSVC, or Zig)
Generate & Build
# Generate the C parser from grammar.js
npx tree-sitter generate
# Compile the shared library with Zig (MSVC compatible)
zig cc -shared -O3 -I src src/parser.c -o tree_sitter_vba.dll -target x86_64-windows-msvcLicense
MIT. This grammar is an independent implementation based on the publicly available Microsoft VBA Language Specification (MS-VBAL). It does not derive from any GPL-licensed ANTLR4 grammar or other third-party VBA parser implementation.
Why another VBA parser?
Existing tree-sitter VBA grammars were either:
- Incomplete — only parsing a subset of VBScript (
tree-sitter-vbscript) - Wrong language — targeting VB.NET (
tree-sitter-vb-dotnet) which has significant syntax differences from VBA - Abandoned — started but never reached production quality
This grammar is implemented from scratch based on the Microsoft VBA Language Specification. It was developed using AI-assisted translation with manual validation and extensive testing against real-world VBA codebases.
