-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeStack.cs
More file actions
80 lines (72 loc) · 2.03 KB
/
ScopeStack.cs
File metadata and controls
80 lines (72 loc) · 2.03 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
using System;
using System.Collections.Generic;
namespace ServerCodeExciser
{
public class ScopeStack
{
private Dictionary<string, int> m_scopes = new Dictionary<string, int>();
private Stack<string> m_scope = new Stack<string>();
private int m_else = 0;
public bool Push(string name)
{
if (name.StartsWith("#ifdef "))
{
name = TrimAndStripComments(name.Substring(7));
}
else if (name.StartsWith("#if "))
{
name = TrimAndStripComments(name.Substring(4));
}
else if (name.StartsWith("#ifndef "))
{
name = "!" + TrimAndStripComments(name.Substring(8));
}
m_scope.Push(name);
if (m_scopes.ContainsKey(name) && m_scopes[name] > 0)
{
m_scopes[name] += 1;
return false;
}
else
{
m_scopes[name] = 1;
return true;
}
}
public void Else(out string name)
{
name = m_scope.Pop();
m_scopes[name] -= 1;
var o = (name[0] == '!') ? name.Substring(1) : ("!" + name);
Push(o);
}
public bool Pop(out string name)
{
if (m_scope.Count <= 0)
{
name = string.Empty;
return false;
}
name = m_scope.Pop();
m_scopes[name] -= 1;
return m_scopes[name] == 0;
}
public bool IsInScope(string name)
{
if (m_scopes.TryGetValue(name, out var count))
{
return count > 0;
}
return false;
}
private string TrimAndStripComments(string text)
{
int idx = text.IndexOf("//");
if (idx >= 0)
{
return text.Substring(0, idx).Trim();
}
return text.Trim();
}
}
}