-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathIdentifier.cs
More file actions
98 lines (89 loc) · 2.91 KB
/
Copy pathIdentifier.cs
File metadata and controls
98 lines (89 loc) · 2.91 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
using System;
using System.Linq;
using System.Text;
namespace LexicalAnalysis {
/// <summary>
/// If the identifier is found to be a keyword, then it will be a keyword
/// </summary>
public sealed class TokenIdentifier : Token {
public TokenIdentifier(String val) {
this.Val = val;
}
public override TokenKind Kind { get; } = TokenKind.IDENTIFIER;
public String Val { get; }
public override String ToString() {
return this.Kind + $" [{Line}:{Column}]: " + this.Val;
}
}
public sealed class FSAIdentifier : FSA {
private enum State {
START,
END,
ERROR,
ID
};
private State _state;
private StringBuilder _scanned;
public FSAIdentifier() {
this._state = State.START;
this._scanned = new StringBuilder();
}
public override void Reset() {
this._state = State.START;
this._scanned.Clear();
}
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() {
String name = this._scanned.ToString(0, this._scanned.Length - 1);
if (TokenKeyword.Keywords.ContainsKey(name)) {
return new TokenKeyword(TokenKeyword.Keywords[name]);
}
return new TokenIdentifier(name);
}
public override void ReadChar(Char ch) {
this._scanned = this._scanned.Append(ch);
switch (this._state) {
case State.END:
case State.ERROR:
this._state = State.ERROR;
break;
case State.START:
if (ch == '_' || Char.IsLetter(ch)) {
this._state = State.ID;
} else {
this._state = State.ERROR;
}
break;
case State.ID:
if (Char.IsLetterOrDigit(ch) || ch == '_') {
this._state = State.ID;
} else {
this._state = State.END;
}
break;
}
}
public override void ReadEOF() {
this._scanned = this._scanned.Append('0');
switch (this._state) {
case State.ID:
this._state = State.END;
break;
default:
this._state = State.ERROR;
break;
}
}
}
}