MrKWatkins.Ast
0.9.135
Prefix Reserved
dotnet add package MrKWatkins.Ast --version 0.9.135
NuGet\Install-Package MrKWatkins.Ast -Version 0.9.135
<PackageReference Include="MrKWatkins.Ast" Version="0.9.135" />
<PackageVersion Include="MrKWatkins.Ast" Version="0.9.135" />Directory.Packages.props
<PackageReference Include="MrKWatkins.Ast" />Project file
paket add MrKWatkins.Ast --version 0.9.135
#r "nuget: MrKWatkins.Ast, 0.9.135"
#:package MrKWatkins.Ast@0.9.135
#addin nuget:?package=MrKWatkins.Ast&version=0.9.135Install as a Cake Addin
#tool nuget:?package=MrKWatkins.Ast&version=0.9.135Install as a Cake Tool
MrKWatkins.Ast
Build Status DeepSource NuGet Version NuGet Downloads
C# library to build and manipulate abstract syntax trees when writing compilers.
Background
As part of my Oakley project to create a compiler and it's associated OakAsm project to create an assembler (details coming soon) I needed to represent abstract syntax trees in C#. This library was created so I could share the code between those two projects.
Usage
Create a base node type for your abstract syntax tree:
public abstract class Expression : Node<Expression>
{
}
Create more specific nodes:
public sealed class ConstantNumber : Expression
{
public ConstantNumber(int value)
{
Value = value;
}
public int Value
{
get => Properties.GetOrThrow<int>(nameof(Value));
init => Properties.Set(nameof(Value), value);
}
}
public sealed class Addition : Expression
{
public Addition(ConstantNumber x, ConstantNumber y)
{
Children.Add(x);
Children.Add(y);
}
}
Walk the tree:
var fifty = new ConstantNumber(50);
var sixty = new ConstantNumber(60);
var expression = new Addition(fifty, sixty);
var allNodes = expression.ThisAndDescendents;
var fiftyAndParent = fifty.ThisAndAncestors;
var fiftyAndSixty = fifty.ThisAndNextSiblings;
var justSixty = sixty.PreviousSibling;
var result = expression.Children.OfType<ConstantNumber>().Sum(n => n.Value);
Mark nodes with errors, warnings and info messages:
sixty.AddError("Value must be less than 55.");
var expressionHasErrors = expression.HasErrors; // true.
Associate nodes with their position in source code during parsing:
var source = new TextFile(new FileInfo("MySource.code")); // Contains "50 + 60".
expression.SourcePosition = source.CreatePosition(0, 7, 0, 0); // startIndex, length, startLineIndex, startColumnIndex.
fifty.SourcePosition = source.CreatePosition(0, 2, 0, 0);
sixty.SourcePosition = source.CreatePosition(5, 2, 0, 5);
Output errors with highlighted source information:
var errors = MessageFormatter.FormatErrors(expression);
// MySource.code (1, 6): Error: Parent Value must be less than 55.
// 50 + 60
// --
Manipulate and copy the tree:
sixty.ReplaceWith(new ConstantNumber(55));
var copy = expression.Copy();
Full documentation will be available with version 1.0.x.
Install
dotnet add package MrKWatkins.Ast
Pull Requests
I'm not accepting pull requests at the current time; this project is tailored for some other projects of mine and I want to get them in a suitable state first.
Feel free to raise issues for bugs or suggestions, but I make no guarantees they will get looked at I'm afraid!
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages (15)
Showing the top 5 NuGet packages that depend on MrKWatkins.Ast:
| Package | Downloads |
|---|---|
|
MrKWatkins.OakAsm
Shared code for the OakAsm project. |
|
|
MrKWatkins.OakAsm.Z80
Library containing assembly definitions for the Z80 CPU, part of the OakAsm project. |
|
|
MrKWatkins.OakAsm.Formatting
Library for formatting assembly code, part of the OakAsm project. |
|
|
MrKWatkins.OakAsm.Parsing
Library for parsing assembly code, part of the OakAsm project. |
|
|
MrKWatkins.OakAsm.IO
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.9.135 | 700 | 3/10/2026 |
| 0.9.134 | 1,418 | 11/15/2025 |
| 0.9.133 | 678 | 9/8/2025 |
| 0.9.132 | 427 | 8/12/2025 |
| 0.9.131 | 356 | 8/10/2025 |
| 0.9.130 | 434 | 8/6/2025 |
| 0.9.129 | 2,550 | 1/29/2025 |
| 0.9.128 | 2,492 | 12/22/2024 |
| 0.9.127 | 279 | 12/17/2024 |
| 0.9.126 | 210 | 12/17/2024 |
| 0.9.125 | 216 | 12/17/2024 |
| 0.9.124 | 413 | 12/15/2024 |
| 0.9.123 | 509 | 12/8/2024 |
| 0.9.122 | 398 | 11/17/2024 |
| 0.9.121 | 601 | 11/1/2024 |
| 0.9.120 | 1,128 | 10/24/2024 |
| 0.9.119 | 1,085 | 10/21/2024 |
| 0.9.118 | 2,079 | 7/8/2024 |
| 0.9.117 | 1,167 | 3/27/2024 |
| 0.9.116 | 259 | 3/23/2024 |