-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathRenameFileParser.cs
161 lines (141 loc) · 5.79 KB
/
RenameFileParser.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//-----------------------------------------------------------------------
// <copyright file="RenameFileParser.cs" company="(none)">
// Copyright © 2013 John Gietzen and the WebGit .NET Authors. All rights reserved.
// </copyright>
// <author>John Gietzen</author>
//-----------------------------------------------------------------------
namespace WebGitNet
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public static class RenameFileParser
{
private static readonly Dictionary<Terminal, string> terminals = new Dictionary<Terminal, string>
{
{ Terminal.Field, "name|email" },
{ Terminal.SearchType, "[=%~]=" },
{ Terminal.Assignment, "=" },
{ Terminal.Seperator, "," },
{ Terminal.Definition, ":" },
{ Terminal.String, @"""((?:[^""]|"""")+)""" },
{ Terminal.Whitespace, @"\s+" },
{ Terminal.EndOfLine, @"$" },
};
private enum Terminal
{
Field,
SearchType,
Assignment,
Seperator,
Definition,
String,
Whitespace,
EndOfLine,
}
public static List<RenameEntry> Parse(string[] lines)
{
return lines.Select(l => Parse(l)).Where(l => l != null).ToList();
}
private static string Accept(Terminal terminal, ref string subject)
{
var regex = terminals[terminal];
var match = Regex.Match(subject, "^" + regex, RegexOptions.IgnoreCase);
if (match.Success)
{
subject = subject.Substring(match.Length);
return match.Value;
}
else
{
return null;
}
}
private static RenameEntry Parse(string line)
{
if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#"))
{
return null;
}
RenameField sourceField = RenameField.None;
string operation = null;
string match = null;
RenameField destinationField1 = RenameField.None;
string destinationValue1 = null;
RenameField destinationField2 = RenameField.None;
string destinationValue2 = null;
Accept(Terminal.Whitespace, ref line);
sourceField = (RenameField)Enum.Parse(typeof(RenameField), Require(Terminal.Field, ref line), true);
Accept(Terminal.Whitespace, ref line);
operation = Require(Terminal.SearchType, ref line);
Accept(Terminal.Whitespace, ref line);
match = Require(Terminal.String, ref line);
match = match.Substring(1, match.Length - 2).Replace("\"\"", "\"");
Accept(Terminal.Whitespace, ref line);
Require(Terminal.Definition, ref line);
Accept(Terminal.Whitespace, ref line);
destinationField1 = (RenameField)Enum.Parse(typeof(RenameField), Require(Terminal.Field, ref line), true);
Accept(Terminal.Whitespace, ref line);
Require(Terminal.Assignment, ref line);
Accept(Terminal.Whitespace, ref line);
destinationValue1 = Require(Terminal.String, ref line);
destinationValue1 = destinationValue1.Substring(1, destinationValue1.Length - 2).Replace("\"\"", "\"");
Accept(Terminal.Whitespace, ref line);
if (Accept(Terminal.Seperator, ref line) != null)
{
Accept(Terminal.Whitespace, ref line);
destinationField2 = (RenameField)Enum.Parse(typeof(RenameField), Require(Terminal.Field, ref line), true);
Accept(Terminal.Whitespace, ref line);
Require(Terminal.Assignment, ref line);
Accept(Terminal.Whitespace, ref line);
destinationValue2 = Require(Terminal.String, ref line);
destinationValue2 = destinationValue2.Substring(1, destinationValue2.Length - 2).Replace("\"\"", "\"");
}
Accept(Terminal.Whitespace, ref line);
Require(Terminal.EndOfLine, ref line);
if (destinationField1 == destinationField2)
{
throw new RenameFileSyntaxException("Duplicate destinations are invalid.");
}
RenameEntry.Destination[] destinations;
if (destinationField2 == RenameField.None)
{
destinations = new[]
{
new RenameEntry.Destination { Field = destinationField1, Replacement = destinationValue1 },
};
}
else
{
destinations = new[]
{
new RenameEntry.Destination { Field = destinationField1, Replacement = destinationValue1 },
new RenameEntry.Destination { Field = destinationField2, Replacement = destinationValue2 },
};
}
RenameStyle style = RenameStyle.Exact;
switch (operation)
{
case "~=": style = RenameStyle.CaseInsensitive; break;
case "%=": style = RenameStyle.Regex; break;
}
return new RenameEntry
{
RenameStyle = style,
SourceField = sourceField,
Match = match,
Destinations = destinations,
};
}
private static string Require(Terminal terminal, ref string subject)
{
var match = Accept(terminal, ref subject);
if (match == null)
{
throw new RenameFileSyntaxException("Expected " + terminal + ".");
}
return match;
}
}
}