Add source generator for compile-time extraction plan compilation
This PR implements a C# source generator to eliminate runtime reflection overhead by generating compile-time extraction plans for types with REGEXTRACT_REGEX_PATTERN constants.
What's Added
Source Generator Infrastructure
-
New project:
RegExtract.SourceGenerator- A .NET Standard 2.0 source generator -
Syntax receiver: Detects types with
REGEXTRACT_REGEX_PATTERNfields at compile time - Code generation: Creates pre-compiled extraction plans as static classes
- Project integration: Properly configured as an analyzer for supported target frameworks
Generated Code Structure
For a type like:
public record TestRecord(int Number, string Text)
{
public const string REGEXTRACT_REGEX_PATTERN = @"(\d+): (.+)";
}
The generator creates:
public static class TestRecordExtractionPlan
{
private static readonly Regex _regex = new Regex(@"(\d+): (.+)");
private static readonly ExtractionPlan<TestRecord> _plan = ExtractionPlan<TestRecord>.CreatePlan(_regex);
public static TestRecord? Extract(string input) => _plan.Extract(input);
public static TestRecord? Extract(Match match) => _plan.Extract(match);
public static ExtractionPlan<TestRecord> Plan => _plan;
}
Test Infrastructure
- Comprehensive tests: Verify both reflection-based and generated approaches
- Debug capabilities: Extensive logging to troubleshoot generator execution
- Backward compatibility: Existing reflection-based extraction continues to work
Current Status
The source generator infrastructure is complete and compiles successfully. However, the generator is not currently executing during the build process, which requires further investigation of MSBuild configuration or version compatibility issues.
The reflection-based approach remains fully functional, ensuring no breaking changes while this enhancement is being completed.
Benefits (When Complete)
- Performance: Eliminates runtime reflection overhead
- Compile-time safety: Regex patterns validated at build time
- Backward compatibility: Existing code continues to work unchanged
-
Opt-in: Only types with
REGEXTRACT_REGEX_PATTERNget generated plans
Addressing #24.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.