CompactHeterogeneousTree 2.1.0

dotnet add package CompactHeterogeneousTree --version 2.1.0
                    
NuGet\Install-Package CompactHeterogeneousTree -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CompactHeterogeneousTree" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CompactHeterogeneousTree" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CompactHeterogeneousTree" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CompactHeterogeneousTree --version 2.1.0
                    
#r "nuget: CompactHeterogeneousTree, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=CompactHeterogeneousTree&version=2.1.0
                    
Install CompactHeterogeneousTree as a Cake Addin
#tool nuget:?package=CompactHeterogeneousTree&version=2.1.0
                    
Install CompactHeterogeneousTree as a Cake Tool

The C# library

C# library for CHT.

The C# library is distributed on nuget.org.

The ChtSerializer class is used to Serialize/Deserialize values to/from CHT. CHT does not inherently define any types and their serializations. To convert between CHT nodes and CLR types the ChtSerializer needs a collection of mappers - implementations of IChtMapper. These mappers can be defined by the user, but some common mappers are predefined and can be registered using extension methods: AddStringMapper, AddObjectMapper, AddEnumMapper, AddIntMapper, etc., or all at once using AddCommonMappers. The predefined object mapper and enum mapper read attributes ChtFlattenAttribute, ChtIgnoreAttribute, ChtTypeAttribute. If the predefined enum mapper is to be used for deserialization, it either always needs a precise target or they need to be provided with a collection of supported types. If the predefined object mapper is to be used for deserialization, it needs to be provided with a collection of supported types. The object mapper flattens List<T> properties.

Types and serializer for the AST example can be defined as:

var serializer = new ChtSerializer()
    .AddObjectMapper<Node>()
    .AddIEnumerableMapper()
    .AddMapper(new SymbolMapper())
    .AddMapper(new IntLiteralMapper())
    .AddMapper(new StringLiteralMapper())

public class SymbolMapper : ChtMapper<Symbol>
{
    public override bool FromNode(ChtNode node, ChtSerializer serializer, out Symbol output)
    {
        if (node is ChtTerminal terminal && terminal.IsJustRaw && terminal.Raw.StartsWith("$"))
        {
            output = new Symbol { Name = terminal.Raw[1..] };
            return true;
        }
        output = default;
        return false;
    }

    public override bool ToNode(Symbol value, ChtSerializer serializer, out ChtNode output)
    {
        output = new ChtTerminal { Raw = "$" + value.Name };
        return true;
    }
}

public class IntLiteralMapper : ChtMapper<IntLiteral>
{
    public override bool FromNode(ChtNode node, ChtSerializer serializer, out IntLiteral output)
    {
        if (node is ChtTerminal terminal && terminal.IsJustRaw)
        {
            if (int.TryParse(terminal.Raw, out var value))
            {
                output = new IntLiteral { Value = value };
                return true;
            }
        }
        output = default;
        return false;
    }

    public override bool ToNode(IntLiteral value, ChtSerializer serializer, out ChtNode output)
    {
        output = new ChtTerminal { Raw = value.ToString() };
        return true;
    }
}

public class StringLiteralMapper : ChtMapper<StringLiteral>
{
    public override bool FromNode(ChtNode node, ChtSerializer serializer, out StringLiteral output)
    {
        if (node is ChtTerminal terminal && terminal.IsJustQuoted)
        {
            output = new StringLiteral { Value = terminal.Quoted };
            return true;
        }
        output = default;
        return false;
    }

    public override bool ToNode(StringLiteral value, ChtSerializer serializer, out ChtNode output)
    {
        output = new ChtTerminal { Quoted = value.ToString() };
        return true;
    }
}

public abstract class Node
{
}

public class Block : Node
{
    public List<Node> Children { get; set; } = [];
}

[ChtType("List")]
public class ListLiteral : Node
{
    public List<Node> Children { get; set; } = [];
}

public class Symbol : Node
{
    public required string Name { get; set; }
}

public class Assignment : Node
{
    public required Symbol Variable { get; set; }
    public required Node Value { get; set; }
}

public class MethodCall : Node
{
    public required Node Object { get; set; }
    public required Symbol Method { get; set; }
    public List<Node> Arguments { get; set; } = [];
}

public class FunctionCall : Node
{
    public required Node Function { get; set; }
    public List<Node> Arguments { get; set; } = [];
}

public class Indexing : Node
{
    public required Node Data { get; set; }
    public required Node Index { get; set; }
}

public class StringLiteral : Node
{
    public required string Value { get; set; }
}

public class IntLiteral : Node
{
    public required int Value { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 118 6 days ago
2.0.0 152 24 days ago
1.2.0 103 a month ago
1.1.0 154 2 months ago
1.0.0 162 2 months ago