forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilePreProcessor.cs
More file actions
171 lines (133 loc) · 5.49 KB
/
FilePreProcessor.cs
File metadata and controls
171 lines (133 loc) · 5.49 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Common.Logging;
namespace ScriptCs
{
public class FilePreProcessor : IFilePreProcessor
{
private readonly ILog _logger;
private readonly IFileSystem _fileSystem;
public FilePreProcessor(IFileSystem fileSystem, ILog logger)
{
_fileSystem = fileSystem;
_logger = logger;
}
public FilePreProcessorResult ProcessFile(string path)
{
return Parse(context => ParseFile(path, context));
}
public FilePreProcessorResult ProcessScript(string script)
{
var scriptLines = _fileSystem.SplitLines(script).ToList();
return Parse(context => ParseScript(scriptLines, context));
}
private FilePreProcessorResult Parse(Action<FilePreProcessorContext> parseAction)
{
var context = new FilePreProcessorContext();
_logger.DebugFormat("Starting pre-processing");
parseAction(context);
var code = GenerateCode(context);
_logger.DebugFormat("Pre-processing finished successfully");
return new FilePreProcessorResult
{
UsingStatements = context.UsingStatements,
LoadedScripts = context.LoadedScripts,
References = context.References,
Code = code
};
}
private string GenerateCode(FilePreProcessorContext context)
{
var stringBuilder = new StringBuilder();
var usingLines = context.UsingStatements
.Select(item => string.Format("using {0};", item))
.ToList();
if (usingLines.Count > 0)
{
stringBuilder.AppendLine(string.Join(_fileSystem.NewLine, usingLines));
stringBuilder.AppendLine(); // Insert a blank separator line
}
stringBuilder.Append(string.Join(_fileSystem.NewLine, context.Body));
return stringBuilder.ToString();
}
private void ParseFile(string path, FilePreProcessorContext context)
{
_logger.DebugFormat("Processing {0}...", Path.GetFileName(path));
var scriptLines = _fileSystem.ReadFileLines(path).ToList();
ParseScript(scriptLines, context, path);
}
private void ParseScript(List<string> scriptLines, FilePreProcessorContext context, string path = null)
{
// Insert line directive if there's a path
if (path != null) InsertLineDirective(path, scriptLines);
var codeIndex = scriptLines.FindIndex(PreProcessorUtil.IsNonDirectiveLine);
for (var index = 0; index < scriptLines.Count; index++)
{
ProcessLine(context, scriptLines[index], index < codeIndex || codeIndex < 0);
}
if (path != null) context.LoadedScripts.Add(path);
}
private static void InsertLineDirective(string path, List<string> fileLines)
{
var bodyIndex = fileLines.FindIndex(line => PreProcessorUtil.IsNonDirectiveLine(line) && !PreProcessorUtil.IsUsingLine(line));
if (bodyIndex == -1) return;
var directiveLine = string.Format("#line {0} \"{1}\"", bodyIndex + 1, path);
fileLines.Insert(bodyIndex, directiveLine);
}
private void ProcessLine(FilePreProcessorContext context, string line, bool isBeforeCode)
{
if (PreProcessorUtil.IsUsingLine(line))
{
var @using = PreProcessorUtil.GetPath(PreProcessorUtil.UsingString, line);
if (!context.UsingStatements.Contains(@using))
{
context.UsingStatements.Add(@using);
}
return;
}
if (PreProcessorUtil.IsRLine(line))
{
if (isBeforeCode)
{
var reference = PreProcessorUtil.GetPath(PreProcessorUtil.RString, line);
if (!string.IsNullOrWhiteSpace(reference) && !context.References.Contains(reference))
{
context.References.Add(reference);
}
}
return;
}
if (PreProcessorUtil.IsLoadLine(line))
{
if (isBeforeCode)
{
var filePath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, line);
if (!string.IsNullOrWhiteSpace(filePath) && !context.LoadedScripts.Contains(filePath))
{
ParseFile(filePath, context);
}
}
return;
}
// If we've reached this, the line is part of the body...
context.Body.Add(line);
}
private class FilePreProcessorContext
{
public FilePreProcessorContext()
{
UsingStatements = new List<string>();
References = new List<string>();
LoadedScripts = new List<string>();
Body = new List<string>();
}
public List<string> UsingStatements { get; private set; }
public List<string> References { get; private set; }
public List<string> LoadedScripts { get; private set; }
public List<string> Body { get; private set; }
}
}
}