XSharp.VsParser.Helpers
24.25.1
dotnet add package XSharp.VsParser.Helpers --version 24.25.1
NuGet\Install-Package XSharp.VsParser.Helpers -Version 24.25.1
<PackageReference Include="XSharp.VsParser.Helpers" Version="24.25.1" />
paket add XSharp.VsParser.Helpers --version 24.25.1
#r "nuget: XSharp.VsParser.Helpers, 24.25.1"
// Install XSharp.VsParser.Helpers as a Cake Addin #addin nuget:?package=XSharp.VsParser.Helpers&version=24.25.1 // Install XSharp.VsParser.Helpers as a Cake Tool #tool nuget:?package=XSharp.VsParser.Helpers&version=24.25.1
XSharp.Parser.Helpers
Introduction
XSharp.VsParser.Helpers contains some helper classes, that simplify the parsing and rewriting of XSharp code files. It uses the VsParser.Dll of the XSharp project.
Requirements
In order to use this Nuget, you must install a version of the XSharp Compiler and accept the terms and conditions of the XSharp project.
Classes
ProjectHelper
- Get the Options for the parser initialisiation
- Get a list of all the files in the project
ParserHelper
- Parses a xsharp file and exposes it as tree
- Tree
- Can be enumerated
- Exposes a Rewriter to change the source code
- Can dump the tree as Xml or Yaml
- Comments
CacheHelper
- Caches serializable data objects for source files
- Is helpful to store calculated data to reduce the amount of files to parse
FileExtensionHelper
- Detects the most likely encoding of a file
Extension Methods
A series of IParseTree extension methods like
- WhereType or WhereTypeWithChildren to filter the tree
- FirstParentOrDefault to get a specific parent matching the type
- ToValues Methods for Class, Method, ... to give easy access to important values
Check the IParseTreeExtensions, ToIndexExtensions and ToValueExtensions files in the Parser folder for the complete list.
RewriteFor extension methods
A series of helper methods for code rewriting. Check the Rewriter folder for the complete list.
Examples
This is a simple example on how to use the ParserHelper to parse source code and extract information from the tree.
string sampleSourceCode = @"
// Example Class
class Example inherit BaseExample
// Dummy Method
method Dummy(value)
System.Console.WriteLine(value)
return
end class
";
// For this example, we initialize the parser with the default options. Normally, the options should be loaded usind the ProjectHelper
var parser = ParserHelper.BuildWithVoDefaultOptions();
// To get access to the tree, we have to parse the source code first
var result = parser.ParseText(sampleSourceCode, "dummy.prg");
if (!result.OK)
throw new Exception("File can not be parsed");
// Now we can access the tree. The first step is normaly to dump the tree to yaml to get a better understanding of the IParseTree items, that were created based on our source code
var dump = parser.Tree.DumpYaml();
Console.WriteLine(dump);
// in the dump we noticed, that the parser creates a class_context for every class. We can use that to find the first class_context.
var classContext = parser.Tree.FirstOrDefaultType<Class_Context>();
// from the class context we can extract the class name using the ToValue extension method
var className = classContext?.ToValues().Name;
Console.WriteLine(className);
// to get all the child nodes of the classContext, whe use AsEnumerable
var classChilds = classContext.AsEnumerable();
// now we want to count methods of the class. we use whereType and Count
var methodsCount = classChilds.WhereType<MethodContext>().Count();
Console.WriteLine(methodsCount);
Check the XSharp.VsParser.Helpers.Tests project for additional samples on how to use the parser helper
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- K4os.Hash.xxHash (>= 1.0.8)
- LiteDB (>= 5.0.17)
- UTF.Unknown (>= 2.5.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.