Skip to content

Commit da5466d

Browse files
committed
Cleaning up tokenizer
1 parent 0cf073c commit da5466d

File tree

5 files changed

+62
-61
lines changed

5 files changed

+62
-61
lines changed

StringMath/Tokenizer/ITokenizer.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace StringMath
2+
{
3+
/// <summary>Contract for tokenizers.</summary>
4+
internal interface ITokenizer
5+
{
6+
/// <summary>Reads the next token in the token stream.</summary>
7+
/// <returns>A token.</returns>
8+
Token ReadToken();
9+
}
10+
}

StringMath/Tokenizer/Token.cs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
namespace StringMath
22
{
3-
internal struct Token
3+
/// <summary>A token containing basic information about some text.</summary>
4+
internal readonly struct Token
45
{
5-
public int Position;
6-
public string Text;
7-
public TokenType Type;
6+
/// <summary>Initializes a new instance of a token.</summary>
7+
/// <param name="type">The token type.</param>
8+
/// <param name="text">The token value.</param>
9+
/// <param name="position">The token's position in the input string.</param>
10+
public Token(TokenType type, string text, int position)
11+
{
12+
Type = type;
13+
Text = text;
14+
Position = position;
15+
}
16+
17+
/// <summary>The token's position in the input string.</summary>
18+
public readonly int Position;
19+
20+
/// <summary>The token value.</summary>
21+
public readonly string Text;
22+
23+
/// <summary>The token type.</summary>
24+
public readonly TokenType Type;
825

26+
/// <inheritdoc />
927
public override string ToString()
1028
{
1129
return $"{Text} ({Type}):{Position}";

StringMath/Tokenizer/TokenType.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/// <summary>Available token types.</summary>
44
internal enum TokenType
55
{
6+
/// <summary>Unknown token.</summary>
7+
Unknown,
8+
69
/// <summary>\0</summary>
710
EndOfCode,
811

StringMath/Tokenizer/Tokenizer.Helpers.cs

+19-23
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ private string ReadIdentifier(ISourceText stream)
1818
}
1919
else
2020
{
21-
throw LangException.UnexpectedToken(new Token
22-
{
23-
Position = stream.Position,
24-
Text = stream.Current.ToString()
25-
}, TokenType.Identifier);
21+
Token token = new Token(TokenType.Unknown, stream.Current.ToString(), stream.Position);
22+
throw LangException.UnexpectedToken(token, TokenType.Identifier);
2623
}
2724

2825
while (stream.Current != identifierTerminator)
@@ -34,23 +31,22 @@ private string ReadIdentifier(ISourceText stream)
3431
}
3532
else
3633
{
37-
throw LangException.UnexpectedToken(new Token
38-
{
39-
Position = stream.Position,
40-
Text = stream.Current.ToString()
41-
}, identifierTerminator);
34+
Token token = new Token(TokenType.Unknown, stream.Current.ToString(), stream.Position);
35+
throw LangException.UnexpectedToken(token, identifierTerminator);
4236
}
4337
}
4438

4539
builder.Append(stream.Current);
4640
stream.MoveNext();
4741
string text = builder.ToString();
4842

49-
return text.Length == 2 ? throw LangException.UnexpectedToken(new Token
43+
if (text.Length == 2)
5044
{
51-
Position = stream.Position - 1,
52-
Text = identifierTerminator.ToString()
53-
}, identifierTerminator) : text;
45+
Token token = new Token(TokenType.Unknown, identifierTerminator.ToString(), stream.Position - 1);
46+
throw LangException.UnexpectedToken(token, identifierTerminator);
47+
}
48+
49+
return text;
5450
}
5551

5652
private string ReadOperator(ISourceText stream)
@@ -84,11 +80,8 @@ private string ReadNumber(ISourceText stream)
8480
}
8581
else
8682
{
87-
throw LangException.UnexpectedToken(new Token
88-
{
89-
Position = stream.Position,
90-
Text = stream.Current.ToString()
91-
}, TokenType.Number);
83+
Token token = new Token(TokenType.Unknown, stream.Current.ToString(), stream.Position);
84+
throw LangException.UnexpectedToken(token, TokenType.Number);
9285
}
9386
}
9487
else if (char.IsDigit(stream.Current))
@@ -103,11 +96,14 @@ private string ReadNumber(ISourceText stream)
10396
}
10497

10598
char peeked = stream.Peek(-1);
106-
return peeked == '.' ? throw LangException.UnexpectedToken(new Token
99+
100+
if (peeked == '.')
107101
{
108-
Position = stream.Position,
109-
Text = peeked.ToString()
110-
}, TokenType.Number) : builder.ToString();
102+
Token token = new Token(TokenType.Unknown, peeked.ToString(), stream.Position);
103+
throw LangException.UnexpectedToken(token, TokenType.Number);
104+
}
105+
106+
return builder.ToString();
111107
}
112108

113109
private void ReadWhiteSpace(ISourceText stream)

StringMath/Tokenizer/Tokenizer.cs

+8-34
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22

33
namespace StringMath
44
{
5-
/// <summary>Contract for tokenizers.</summary>
6-
internal interface ITokenizer
7-
{
8-
/// <summary>Reads the next token in the token stream.</summary>
9-
/// <returns>A token.</returns>
10-
Token ReadToken();
11-
}
12-
135
/// <inheritdoc />
146
internal sealed partial class Tokenizer : ITokenizer
157
{
@@ -27,7 +19,7 @@ public Tokenizer(ISourceText text)
2719
{
2820
_text = text;
2921
}
30-
22+
3123
/// <summary>Creates a new instance of the tokenizer.</summary>
3224
/// <param name="text">The text to tokenize.</param>
3325
public Tokenizer(string text) : this(new SourceText(text))
@@ -37,12 +29,6 @@ public Tokenizer(string text) : this(new SourceText(text))
3729
/// <inheritdoc />
3830
public Token ReadToken()
3931
{
40-
Token token = new Token
41-
{
42-
Text = $"{_text.Current}",
43-
Position = _text.Position
44-
};
45-
4632
switch (_text.Current)
4733
{
4834
case '.':
@@ -56,29 +42,22 @@ public Token ReadToken()
5642
case '7':
5743
case '8':
5844
case '9':
59-
token.Type = TokenType.Number;
60-
token.Text = ReadNumber(_text);
61-
break;
45+
return new Token(TokenType.Number, ReadNumber(_text), _text.Position);
6246

6347
case '(':
64-
token.Type = TokenType.OpenParen;
6548
_text.MoveNext();
66-
break;
49+
return new Token(TokenType.OpenParen, "(", _text.Position);
6750

6851
case ')':
69-
token.Type = TokenType.CloseParen;
7052
_text.MoveNext();
71-
break;
53+
return new Token(TokenType.CloseParen, ")", _text.Position);
7254

7355
case '{':
74-
token.Type = TokenType.Identifier;
75-
token.Text = ReadIdentifier(_text);
76-
break;
56+
return new Token(TokenType.Identifier, ReadIdentifier(_text), _text.Position);
7757

7858
case '!':
79-
token.Type = TokenType.Exclamation;
8059
_text.MoveNext();
81-
break;
60+
return new Token(TokenType.Exclamation, "!", _text.Position);
8261

8362
case ' ':
8463
case '\t':
@@ -91,17 +70,12 @@ public Token ReadToken()
9170
return ReadToken();
9271

9372
case '\0':
94-
token.Type = TokenType.EndOfCode;
95-
break;
73+
return new Token(TokenType.EndOfCode, "\0", _text.Position);
9674

9775
default:
9876
string op = ReadOperator(_text);
99-
token.Text = op;
100-
token.Type = TokenType.Operator;
101-
break;
77+
return new Token(TokenType.Operator, op, _text.Position);
10278
}
103-
104-
return token;
10579
}
10680

10781
/// <inheritdoc />

0 commit comments

Comments
 (0)