Skip to content

Instantly share code, notes, and snippets.

@Pliner
Last active January 7, 2016 23:49
Show Gist options
  • Save Pliner/281558af47d29bfcaab1 to your computer and use it in GitHub Desktop.
Save Pliner/281558af47d29bfcaab1 to your computer and use it in GitHub Desktop.
Sprache example
using System;
using System.Linq;
using Sprache;
namespace Trash
{
public static class EntryPoint
{
public static void Main()
{
var typeCharParser = Parse.LetterOrDigit.Or(Parse.Char('.'));
var typenameParser = typeCharParser.AtLeastOnce().Text();
var commaDelimeterParser = from before in Parse.WhiteSpace.Many()
from delimeter in Parse.Char(',')
from after in Parse.WhiteSpace.Many()
select delimeter;
var simpleTypeParser = from typeName in typenameParser
from delimeter in commaDelimeterParser
from asseblyName in typenameParser
select typeName + delimeter + asseblyName;
Parser<string> typeParser = null;
var genericParameterParser =
from genericBegin in Parse.Char('[')
from type in Parse.Ref(() => typeParser)
from genericEnd in Parse.Char(']')
from delimeter in commaDelimeterParser.Optional()
select type;
var genericParametersParser = from genericsMarker in Parse.Char('`')
from genericsCount in Parse.Number.Token()
from genericsBegin in Parse.Char('[')
from typeParameters in genericParameterParser.Repeat(int.Parse(genericsCount))
from genericsEnd in Parse.Char(']')
select string.Join(",", typeParameters.Select(x => "(" + x + ")"));
var genericTypeParser = from typeName in typenameParser
from typeParameters in genericParametersParser
from delimeter in commaDelimeterParser
from asseblyName in typenameParser
select typeName + "(" + typeParameters + ")" + delimeter + asseblyName;
typeParser = simpleTypeParser.Or(genericTypeParser);
Console.WriteLine(typeParser.Parse("Shared.Command.ControlServer.Location.ProxyCommandPayload`1[[Shared.Command.Generic.Reflection.ReflectCommandPayload, Shared]], Shared"));
Console.WriteLine(typeParser.Parse("Shared.Command.ControlServer.Location.ProxyCommandPayload, Shared"));
Console.WriteLine(typeParser.Parse("System.Collections.Generic.Dictionary`2[[System.String, mscorlib], [System.Collections.Generic.List`1[[System.Int32, mscorlib]], mscorlib]], mscorlib"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment