-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
245 lines (227 loc) · 11.3 KB
/
Program.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using Fclp;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
namespace AntlrGen
{
public enum GenerateStatus
{
NotGenerated,
Generated,
Error,
}
/// <summary>
/// Generate parser files if grammar has been changed.
/// Grammar changing date (.g4) stored to the header of generating file (.cs).
/// If dates are not equal then files will be regenerated.
/// </summary>
class Program
{
static void Main(string[] args)
{
var argsWithUsualSlashes = args.Select(arg => arg.Replace('/', '\\')).ToArray(); // TODO: bug in FluentCommandLineParser.
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var cmdParser = new FluentCommandLineParser();
string lexerFile = null;
string parserFile = null;
string lexerSuperClass = null;
string parserSuperClass = null;
string packageName = null;
bool listener = false;
string output = null;
bool standard = false;
cmdParser.Setup<string>("lexer").Callback(lexer => lexerFile = NormDirSeparator(lexer));
cmdParser.Setup<string>("parser").Callback(parser => parserFile = NormDirSeparator(parser));
cmdParser.Setup<string>("package").Callback(package => packageName = package);
cmdParser.Setup<bool>("listener").Callback(l => listener = l);
cmdParser.Setup<string>("output").Callback(o => output = NormDirSeparator(o));
cmdParser.Setup<string>("lexerSuperClass").Callback(param => lexerSuperClass = param);
cmdParser.Setup<string>("parserSuperClass").Callback(param => parserSuperClass = param);
cmdParser.Setup<bool>("standard").Callback(param => standard = param);
var result = cmdParser.Parse(argsWithUsualSlashes);
if (!result.HasErrors)
{
GenerateStatus generateStatus = GenerateStatus.NotGenerated;
if (!string.IsNullOrEmpty(lexerFile))
{
generateStatus = GenerateCode(lexerFile, packageName, true, listener, output, lexerSuperClass, standard);
}
if (generateStatus == GenerateStatus.Error)
{
Console.Error.WriteLine("Code generation error");
}
if (!string.IsNullOrEmpty(parserFile))
{
generateStatus = GenerateCode(parserFile, packageName, false, listener, output, parserSuperClass, standard);
}
if (generateStatus == GenerateStatus.Error)
{
Console.Error.WriteLine("Code generation error");
}
}
else
{
var errorMessage = "Command line arguments processing error: " + result.ErrorText;
Console.Error.WriteLine(errorMessage);
}
}
private static GenerateStatus GenerateCode(string grammarFileName, string packageName, bool lexer, bool listener, string output, string superClass, bool standard)
{
if (!Path.IsPathRooted(grammarFileName))
{
grammarFileName = Path.Combine(Environment.CurrentDirectory, grammarFileName);
}
DateTime grammarModifyDate = File.GetLastWriteTime(grammarFileName);
grammarModifyDate = DateTime.Parse(grammarModifyDate.ToString());
grammarModifyDate = new DateTime(
grammarModifyDate.Year, grammarModifyDate.Month, grammarModifyDate.Day,
grammarModifyDate.Hour, grammarModifyDate.Minute, grammarModifyDate.Second);
DateTime maxModifyDate = grammarModifyDate;
string shortGrammarFileName = Path.GetFileName(grammarFileName);
string grammarFileDir = Path.GetDirectoryName(grammarFileName);
string outputDirectory = output ?? Path.Combine(grammarFileDir, "Generated");
if (!Path.IsPathRooted(outputDirectory))
{
outputDirectory = Path.Combine(Environment.CurrentDirectory, outputDirectory);
}
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
string generatedFileName = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(grammarFileName));
if (!lexer && !Path.GetFileNameWithoutExtension(grammarFileName).EndsWith("Parser"))
{
generatedFileName += "Parser";
}
generatedFileName += ".cs";
bool generate = false;
var importedGrammarFileNames = new List<string>() { shortGrammarFileName };
if (File.Exists(generatedFileName))
{
string line1 = "";
using (StreamReader reader = new StreamReader(generatedFileName))
{
line1 = reader.ReadLine();
}
try
{
// Get stored date from comment in generated file.
var dateString = line1.Substring(line1.LastIndexOf(' ', line1.LastIndexOf(' ') - 1) + 1);
DateTime modifyDateInGenerated = DateTime.Parse(dateString);
if (grammarModifyDate > modifyDateInGenerated)
{
generate = true;
}
string grammarData = File.ReadAllText(grammarFileName);
MatchCollection vocabMatches = Regex.Matches(grammarData, @"tokenVocab\s*=\s*([^;]+);");
MatchCollection importMatches = Regex.Matches(grammarData, @"\r\nimport\s+([^;]+);\r\n");
// Detect file dependencies: lexer or import files.
var imports = new HashSet<string>();
foreach (Match match in vocabMatches)
{
string[] importFileNames = match.Groups[1].Value.Split(',');
foreach (string importFileName in importFileNames)
{
imports.Add(importFileName);
}
}
foreach (Match match in importMatches)
{
string[] importFileNames = match.Groups[1].Value.Split(',');
foreach (var importFileName in importFileNames)
{
imports.Add(importFileName);
}
}
foreach (string import in imports)
{
importedGrammarFileNames.Insert(0, import + ".g4");
DateTime lastWriteTime = File.GetLastWriteTime(Path.Combine(grammarFileDir, import + ".g4"));
lastWriteTime = new DateTime(lastWriteTime.Year, lastWriteTime.Month, lastWriteTime.Day,
lastWriteTime.Hour, lastWriteTime.Minute, lastWriteTime.Second);
if (lastWriteTime != modifyDateInGenerated)
{
generate = true;
if (lastWriteTime > maxModifyDate)
{
maxModifyDate = lastWriteTime;
}
}
}
}
catch
{
generate = true;
}
}
else
{
// If generating file does not exist then generate new file.
generate = true;
}
// Regenerated files if required.
GenerateStatus result = GenerateStatus.NotGenerated;
if (generate)
{
if (!ProcessUtils.IsProcessCanBeExecuted("java"))
{
Console.WriteLine("java is not installed or java path is not specified.");
return GenerateStatus.Error;
}
string antlrJarName = standard ? "antlr-4.7.1-standard.jar" : "antlr-4.6.4-optimized.jar";
string antlrFullJarFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), antlrJarName);
string lexerParser = lexer ? "Lexer" : "Parser";
Console.WriteLine($"{lexerParser} for {shortGrammarFileName} generation...");
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "java";
string visitorListenerStr = (listener ? "-listener " : "-no-listener ") + "-visitor";
string superClassParam = string.IsNullOrEmpty(superClass) ? "" : $"-DsuperClass={superClass}";
string packageParam = string.IsNullOrEmpty(packageName) ? "" : $"-package {packageName}";
process.StartInfo.Arguments = $@"-jar ""{antlrFullJarFileName}"" -o ""{outputDirectory}"" ""{grammarFileName}"" -Dlanguage={(standard ? "CSharp" : "CSharp_v4_5")} {visitorListenerStr} {superClassParam} -Werror {packageParam}";
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit(7500);
string outputText = process.StandardOutput.ReadToEnd();
if (!string.IsNullOrWhiteSpace(outputText))
{
Console.Write(outputText);
}
string errorText = process.StandardError.ReadToEnd();
if (string.IsNullOrWhiteSpace(errorText) || errorText.Contains("Picked up _JAVA_OPTIONS")) // TODO: fix workaround
{
string grammarsString = string.Join(", ", importedGrammarFileNames);
string modifyDateString = $"// {grammarsString} date: {maxModifyDate}";
string generateCode = File.ReadAllText(generatedFileName);
string resultCode = modifyDateString + Environment.NewLine + generateCode;
File.WriteAllText(generatedFileName, resultCode);
result = GenerateStatus.Generated;
Console.WriteLine($"{lexerParser} for {shortGrammarFileName} has been generated.");
}
else
{
Console.WriteLine($"Arguments: {process.StartInfo.Arguments}");
Console.WriteLine($"Error: {errorText}");
File.Delete(generatedFileName);
result = GenerateStatus.Error;
Console.WriteLine($"{lexerParser} for {shortGrammarFileName} generation error.");
}
}
else
{
Console.WriteLine($"{shortGrammarFileName} has not been changed. Parser has not been generated.");
}
return result;
}
private static string NormDirSeparator(string path)
{
return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
}
}
}