-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathString.cs
More file actions
164 lines (148 loc) · 5.15 KB
/
Copy pathString.cs
File metadata and controls
164 lines (148 loc) · 5.15 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
using System;
using System.Text;
namespace LexicalAnalysis {
/// <summary>
/// String literal
/// </summary>
public sealed class TokenString : Token {
public TokenString(String val, String raw) {
if (val == null) {
throw new ArgumentNullException(nameof(val));
}
this.Val = val;
this.Raw = raw;
}
public override TokenKind Kind { get; } = TokenKind.STRING;
public String Raw { get; }
public String Val { get; }
public override String ToString() =>
$"{this.Kind} [{Line}:{Column}]: \"{this.Raw}\"\n\"{this.Val}\"";
}
public sealed class TokenUnicodeString : Token
{
public TokenUnicodeString(String val, String raw)
{
if (val == null)
{
throw new ArgumentNullException(nameof(val));
}
this.Val = val;
this.Raw = raw;
}
public override TokenKind Kind { get; } = TokenKind.STRING;
public String Raw { get; }
public String Val { get; }
public override String ToString() =>
$"{this.Kind} [{Line}:{Column}]: L\"{this.Raw}\"\n\"{this.Val}\"";
}
public sealed class FSAString : FSA {
private enum State {
START,
END,
ERROR,
L,
Q,
QQ
};
private State _state;
private readonly FSAChar _fsachar;
private StringBuilder _val;
private StringBuilder _raw;
private bool unicode = false;
public FSAString() {
this._state = State.START;
this._fsachar = new FSAChar('\"');
this._raw = new StringBuilder();
this._val = new StringBuilder();
}
public override void Reset() {
this._state = State.START;
this._fsachar.Reset();
this._raw.Clear();
this._val.Clear();
unicode = false;
}
public override FSAStatus GetStatus() {
if (this._state == State.START) {
return FSAStatus.NONE;
}
if (this._state == State.END) {
return FSAStatus.END;
}
if (this._state == State.ERROR) {
return FSAStatus.ERROR;
}
return FSAStatus.RUNNING;
}
public override Token RetrieveToken() {
if (unicode) return new TokenUnicodeString(this._val.ToString(), this._raw.ToString());
return new TokenString(this._val.ToString(), this._raw.ToString());
}
public override void ReadChar(Char ch) {
switch (this._state) {
case State.END:
case State.ERROR:
this._state = State.ERROR;
break;
case State.START:
switch (ch) {
case 'L':
unicode = true;
this._state = State.L;
break;
case '\"':
this._state = State.Q;
this._fsachar.Reset();
break;
default:
this._state = State.ERROR;
break;
}
break;
case State.L:
if (ch == '\"') {
this._state = State.Q;
this._fsachar.Reset();
} else {
this._state = State.ERROR;
}
break;
case State.Q:
if (this._fsachar.GetStatus() == FSAStatus.NONE && ch == '\"') {
this._state = State.QQ;
this._fsachar.Reset();
} else {
this._fsachar.ReadChar(ch);
switch (this._fsachar.GetStatus()) {
case FSAStatus.END:
this._state = State.Q;
this._val.Append(this._fsachar.RetrieveChar());
this._raw.Append(this._fsachar.RetrieveRaw());
this._fsachar.Reset();
ReadChar(ch);
break;
case FSAStatus.ERROR:
this._state = State.ERROR;
break;
default:
break;
}
}
break;
case State.QQ:
this._state = State.END;
break;
default:
this._state = State.ERROR;
break;
}
}
public override void ReadEOF() {
if (this._state == State.QQ) {
this._state = State.END;
} else {
this._state = State.ERROR;
}
}
}
}