-
Notifications
You must be signed in to change notification settings - Fork 18
/
EditAndContinueCapabilitiesParser.cs
63 lines (53 loc) · 2.18 KB
/
EditAndContinueCapabilitiesParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Microsoft.DotNet.HotReload.Utils.Generator;
public static class EditAndContinueCapabilitiesParser {
public readonly struct Token {
public readonly string Value {get; init;}
}
static public readonly Regex capabilitiesTokenizer = new (@"^\s*(?:(\S+)\s+)*(\S+)?$", RegexOptions.CultureInvariant);
public static IEnumerable<Token> Tokenize (string capabilities)
{
Match match = capabilitiesTokenizer.Match (capabilities);
if (!match.Success)
yield break;
foreach (Capture c in match.Groups[1].Captures) {
yield return new Token { Value = c.Value };
}
foreach (Capture c in match.Groups[2].Captures) {
yield return new Token { Value = c.Value };
}
}
public static IEnumerable<Token> Tokenize (IEnumerable<string> capabilities)
{
foreach (var cap in capabilities) {
foreach (var token in Tokenize(cap))
yield return token;
}
}
internal static (IEnumerable<EnC.EditAndContinueCapabilities> capabilities, IEnumerable<string> unknowns) Parse (IEnumerable<Token> tokens)
{
List<string> unknowns = new();
List<EnC.EditAndContinueCapabilities> capabilities = new();
foreach (var tok in tokens)
{
if (ParseToken (tok, out var cap)) {
capabilities.Add (cap);
} else {
unknowns.Add (tok.Value);
}
}
return (capabilities, unknowns);
}
internal static (IEnumerable<EnC.EditAndContinueCapabilities> capabilities, IEnumerable<string> unknowns) Parse (string capabilities)
{
return Parse(Tokenize(capabilities));
}
internal static (IEnumerable<EnC.EditAndContinueCapabilities> capabilities, IEnumerable<string> unknowns) Parse (IEnumerable<string> capabilities)
{
return Parse(Tokenize(capabilities));
}
internal static bool ParseToken (Token token, out EnC.EditAndContinueCapabilities res) =>
Enum.TryParse<EnC.EditAndContinueCapabilities>(token.Value, ignoreCase: true, out res);
}