forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectiveLineProcessor.cs
More file actions
61 lines (50 loc) · 1.86 KB
/
DirectiveLineProcessor.cs
File metadata and controls
61 lines (50 loc) · 1.86 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
using ScriptCs.Contracts.Exceptions;
namespace ScriptCs.Contracts
{
public abstract class DirectiveLineProcessor : IDirectiveLineProcessor
{
protected virtual BehaviorAfterCode BehaviorAfterCode
{
get { return BehaviorAfterCode.Ignore; }
}
protected abstract string DirectiveName { get; }
private string DirectiveString
{
get { return string.Format("#{0}", DirectiveName); }
}
public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
{
if (!Matches(line))
{
return false;
}
if (!isBeforeCode)
{
if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Throw)
{
throw new InvalidDirectiveUseException(string.Format("Encountered directive '{0}' after the start of code. Please move this directive to the beginning of the file.", DirectiveString));
}
else if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Ignore)
{
return true;
}
}
return ProcessLine(parser, context, line);
}
protected string GetDirectiveArgument(string line)
{
Guard.AgainstNullArgument("line", line);
return line.Replace(DirectiveString, string.Empty)
.Trim()
.Replace("\"", string.Empty)
.Replace(";", string.Empty);
}
protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);
public bool Matches(string line)
{
Guard.AgainstNullArgument("line", line);
var tokens = line.Split();
return tokens[0] == DirectiveString;
}
}
}