-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokeniser.cs
More file actions
173 lines (149 loc) · 4.58 KB
/
Tokeniser.cs
File metadata and controls
173 lines (149 loc) · 4.58 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
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ExpressionParser
{
public class Tokeniser
{
private readonly string text;
private int pos;
public Token CurrentToken { get; private set; }
public Tokeniser(string code)
{
this.text = code;
this.pos = 0;
CurrentToken = new Token(TokenType.End,""); // Added to remove warning about non-initialised property
NextToken();
}
// Return the current character or the null character if at the end.
private char CurrentChar => pos < text.Length ? text[pos] : '\0';
public void NextToken()
{
SkipWhitespace();
if (pos >= text.Length)
{
CurrentToken = new Token(TokenType.End, "");
return;
}
char ch = CurrentChar;
if (ch == '+')
{
pos++;
CurrentToken = new Token(TokenType.Plus, "+");
return;
}
if (ch == '-')
{
pos++;
CurrentToken = new Token(TokenType.Minus, "-");
return;
}
if (ch == '*')
{
pos++;
CurrentToken = new Token(TokenType.Multiply, "*");
return;
}
if (ch == '/')
{
pos++;
CurrentToken = new Token(TokenType.Divide, "/");
return;
}
if (ch == '(')
{
pos++;
CurrentToken = new Token(TokenType.LeftParen, "(");
return;
}
if (ch == ')')
{
pos++;
CurrentToken = new Token(TokenType.RightParen, ")");
return;
}
if (ch == ',')
{
pos++;
CurrentToken = new Token(TokenType.Comma, ",");
return;
}
// Variable names are enclosed in { and }.
if (ch == '{')
{
pos++; // consume '{'
int start = pos;
while (pos < text.Length && text[pos] != '}')
{
if (!char.IsLetterOrDigit(text[pos]) && ch != '.')
throw new Exception("Invalid character in variable name");
pos++;
}
if (pos >= text.Length)
throw new Exception("Unterminated variable token");
string varName = text.Substring(start, pos - start).Trim();
pos++; // consume '}'
CurrentToken = new Token(TokenType.Variable, varName);
return;
}
// String literal: accepts both '...' and "..."
if (ch == '\'' || ch == '\"')
{
char quote = ch;
pos++; // skip opening quote
int start = pos;
while (pos < text.Length && text[pos] != quote)
{
pos++;
}
if (pos >= text.Length)
throw new Exception("Unterminated string literal");
string strVal = text.Substring(start, pos - start);
pos++; // skip closing quote
CurrentToken = new Token(TokenType.String, strVal);
return;
}
// Numeric literal.
if (char.IsDigit(ch) || (ch == '.' && pos + 1 < text.Length && char.IsDigit(text[pos + 1])))
{
int start = pos;
bool foundDot = false;
while (pos < text.Length && (char.IsDigit(text[pos]) || text[pos] == '.'))
{
if (text[pos] == '.')
{
if (foundDot) break;
foundDot = true;
}
pos++;
}
string numStr = text.Substring(start, pos - start);
CurrentToken = new Token(TokenType.Number, numStr);
return;
}
// Identifier: letter followed by letters/digits or underscore. (Used for function names)
if (char.IsLetter(ch))
{
int start = pos;
while (pos < text.Length && (char.IsLetterOrDigit(text[pos]) || text[pos] == '_'))
{
pos++;
}
string id = text.Substring(start, pos - start);
CurrentToken = new Token(TokenType.Identifier, id);
return;
}
throw new Exception($"Unexpected character: {ch}");
}
private void SkipWhitespace()
{
while (pos < text.Length && (char.IsWhiteSpace(text[pos]) || text[pos] == '\0'))
{
pos++;
}
}
}
}