Repository navigation
Getting Started
David Sisco edited this page Jan 11, 2026
·
4 revisions
Install TinyTokenizer via NuGet:
dotnet add package TinyAstOr add a reference to the TinyTokenizer project in your solution.
The primary API is SyntaxTree.Parse() which gives you a queryable, editable syntax tree:
using TinyTokenizer.Ast;
// Parse source into a syntax tree
var tree = SyntaxTree.Parse("function foo() { return 1; }");
// Access root and children
var root = tree.Root;
Console.WriteLine($"Root has {root.Children.Count()} children");
// Find all identifiers
var idents = tree.Leaves.Where(l => l.Kind == NodeKind.Ident);
foreach (var ident in idents)
{
Console.WriteLine(ident.Text);
}Use the Query static class for CSS-like selectors:
var tree = SyntaxTree.Parse("foo(x) + bar(y)");
// Find specific identifiers
var fooNodes = tree.Select(Query.Ident("foo")); // [Ident("foo")]
// Find all identifiers
var allIdents = tree.Select(Query.AnyIdent); // [Ident("foo"), Ident("x"), Ident("bar"), Ident("y")]
// Find function calls (ident followed by parens)
var funcCalls = tree.Select(Query.AnyIdent.FollowedBy(Query.ParenBlock)); // [Ident("foo"), Ident("bar")]Make batched mutations with undo/redo support:
var tree = SyntaxTree.Parse("hello world");
tree.CreateEditor()
.Replace(Query.Ident("hello"), "goodbye")
.Replace(Query.Ident("world"), "universe")
.Commit();
Console.WriteLine(tree.Root.ToFullString()); // "goodbye universe"
tree.Undo();
Console.WriteLine(tree.Root.ToFullString()); // "hello world"Use a Schema to enable syntax node matching:
var tree = SyntaxTree.Parse("obj.method(x)", Schema.Default);
// Match structural patterns
var methodCalls = tree.Match<MethodCallSyntax>().ToList(); // [MethodCallSyntax { Object="obj", Method="method" }]For scenarios where you don't need a syntax tree, use direct tokenization:
using TinyTokenizer;
var tokens = "func(a, b)".TokenizeToTokens();
// tokens contains:
// - IdentToken("func")
// - BlockToken("(a, b)") with children:
// - IdentToken("a")
// - SymbolToken(",")
// - WhitespaceToken(" ")
// - IdentToken("b")Configure tokenization behavior using TokenizerOptions:
var options = TokenizerOptions.Default
.WithCommentStyles(CommentStyle.CStyleSingleLine, CommentStyle.CStyleMultiLine)
.WithOperators(CommonOperators.CFamily);
var tokens = "x = 42; // comment".TokenizeToTokens(options);- TinyAst Guide — Deep dive into the syntax tree API
- Query API — Master the query combinator system
- SyntaxEditor — Learn batch editing and undo/redo
- Schema — Configure parsing and syntax definitions