-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.cs
More file actions
executable file
·44 lines (43 loc) · 1.91 KB
/
Scanner.cs
File metadata and controls
executable file
·44 lines (43 loc) · 1.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FinalPA
{
using TokenStream = System.Collections.Generic.IEnumerator<Token>;
class Scanner
{
private TextReader reader;
public Scanner(TextReader reader) { this.reader = reader; }
public TokenStream Scan()
{
bool isValue = false;
List<char> specialChars = new List<char> { '{', '}', '(', ')', ',', '!', '=' };
string str = "";
while (reader.Peek() != -1) {
if (Char.IsWhiteSpace((char) reader.Peek())) reader.Read();
else {
str = str + (char) reader.Read();
switch (str) {
case "{": isValue = true; yield return new Token(Kind.LEFT_BRACE); break;
case "}": isValue = false; yield return new Token(Kind.RIGHT_BRACE); break;
case "(": yield return new Token(Kind.LEFT_PARENT); break;
case ")": yield return new Token(Kind.RIGHT_PARENT); break;
case ",": yield return new Token(Kind.COMMA); break;
case "!": yield return new Token(Kind.EXCLAMATION); break;
case "=": yield return new Token(Kind.ASSIGN); break;
default:
while (!Char.IsWhiteSpace((char)reader.Peek()) && !specialChars.Contains((char)reader.Peek()) && reader.Peek() != -1)
str = str + (char) reader.Read();
yield return new Token(str, isValue ? Kind.VALUE : Kind.VARIABLE);
break;
}
str = "";
}
}
yield return new Token(Kind.EOF);
}
}
}