![]() |
VOOZH | about |
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
👁 Build Status
👁 DeepSource
👁 NuGet Version
👁 NuGet Downloads
C# library to build and manipulate abstract syntax trees when writing compilers.
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.
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.
dotnet add package MrKWatkins.Ast
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!
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
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 |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.9.135 | 639 | 3/10/2026 |
| 0.9.134 | 1,416 | 11/15/2025 |
| 0.9.133 | 676 | 9/8/2025 |
| 0.9.132 | 425 | 8/12/2025 |
| 0.9.131 | 352 | 8/10/2025 |
| 0.9.130 | 430 | 8/6/2025 |
| 0.9.129 | 2,548 | 1/29/2025 |
| 0.9.128 | 2,490 | 12/22/2024 |
| 0.9.127 | 276 | 12/17/2024 |
| 0.9.126 | 208 | 12/17/2024 |
| 0.9.125 | 212 | 12/17/2024 |
| 0.9.124 | 410 | 12/15/2024 |
| 0.9.123 | 506 | 12/8/2024 |
| 0.9.122 | 397 | 11/17/2024 |
| 0.9.121 | 598 | 11/1/2024 |
| 0.9.120 | 1,126 | 10/24/2024 |
| 0.9.119 | 1,081 | 10/21/2024 |
| 0.9.118 | 2,076 | 7/8/2024 |
| 0.9.117 | 1,163 | 3/27/2024 |
| 0.9.116 | 258 | 3/23/2024 |