-
-
Notifications
You must be signed in to change notification settings - Fork 520
/
Copy pathTemplateParser.cs
146 lines (123 loc) · 4.89 KB
/
TemplateParser.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
namespace Spectre.Console.Cli;
internal static class TemplateParser
{
public sealed class ArgumentResult
{
public string Value { get; set; }
public bool Required { get; set; }
public ArgumentResult(string value, bool required)
{
Value = value;
Required = required;
}
}
public sealed class OptionResult
{
public List<string> LongNames { get; set; }
public List<string> ShortNames { get; set; }
public string? Value { get; set; }
public bool ValueIsOptional { get; set; }
public OptionResult()
{
ShortNames = new List<string>();
LongNames = new List<string>();
}
}
public static ArgumentResult ParseArgumentTemplate(string template)
{
var valueName = default(string);
var required = false;
foreach (var token in TemplateTokenizer.Tokenize(template))
{
if (token.TokenKind == TemplateToken.Kind.ShortName ||
token.TokenKind == TemplateToken.Kind.LongName)
{
throw CommandTemplateException.ArgumentCannotContainOptions(template, token);
}
if (token.TokenKind == TemplateToken.Kind.OptionalValue ||
token.TokenKind == TemplateToken.Kind.RequiredValue)
{
if (!string.IsNullOrWhiteSpace(valueName))
{
throw CommandTemplateException.MultipleValuesAreNotSupported(template, token);
}
if (string.IsNullOrWhiteSpace(token.Value))
{
throw CommandTemplateException.ValuesMustHaveName(template, token);
}
valueName = token.Value;
required = token.TokenKind == TemplateToken.Kind.RequiredValue;
}
}
if (valueName == null)
{
throw CommandTemplateException.ArgumentsMustHaveValueName(template);
}
return new ArgumentResult(valueName, required);
}
public static OptionResult ParseOptionTemplate(string template)
{
var result = new OptionResult();
foreach (var token in TemplateTokenizer.Tokenize(template))
{
if (token.TokenKind == TemplateToken.Kind.LongName || token.TokenKind == TemplateToken.Kind.ShortName)
{
if (string.IsNullOrWhiteSpace(token.Value))
{
throw CommandTemplateException.OptionsMustHaveName(template, token);
}
if (char.IsDigit(token.Value[0]))
{
throw CommandTemplateException.OptionNamesCannotStartWithDigit(template, token);
}
foreach (var character in token.Value)
{
if (!char.IsLetterOrDigit(character) && character != '-' && character != '_' && character != '?')
{
throw CommandTemplateException.InvalidCharacterInOptionName(template, token, character);
}
}
}
if (token.TokenKind == TemplateToken.Kind.LongName)
{
if (token.Value.Length == 1)
{
throw CommandTemplateException.LongOptionMustHaveMoreThanOneCharacter(template, token);
}
result.LongNames.Add(token.Value);
}
if (token.TokenKind == TemplateToken.Kind.ShortName)
{
if (token.Value.Length > 1)
{
throw CommandTemplateException.ShortOptionMustOnlyBeOneCharacter(template, token);
}
result.ShortNames.Add(token.Value);
}
if (token.TokenKind == TemplateToken.Kind.RequiredValue ||
token.TokenKind == TemplateToken.Kind.OptionalValue)
{
if (!string.IsNullOrWhiteSpace(result.Value))
{
throw CommandTemplateException.MultipleOptionValuesAreNotSupported(template, token);
}
foreach (var character in token.Value)
{
if (!char.IsLetterOrDigit(character) &&
character != '=' && character != '-' && character != '_' && character != '|')
{
throw CommandTemplateException.InvalidCharacterInValueName(template, token, character);
}
}
result.Value = token.Value.ToUpperInvariant();
result.ValueIsOptional = token.TokenKind == TemplateToken.Kind.OptionalValue;
}
}
if (result.LongNames.Count == 0 &&
result.ShortNames.Count == 0)
{
throw CommandTemplateException.MissingLongAndShortName(template, null);
}
return result;
}
}