Skip to content

Commit 9b07170

Browse files
committed
Add line ending fix
1 parent 547d62a commit 9b07170

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

src/D.Core/Parsing/SourceReader.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public ReadOnlySpan<char> Consume(SearchValues<char> values)
4949

5050
_current = position < _text.Length ? _text[position] : '\0';
5151

52+
if (_current is '\n')
53+
{
54+
column = -1;
55+
56+
line++;
57+
}
58+
5259
return consumed;
5360
}
5461

@@ -111,11 +118,13 @@ public void Advance()
111118

112119
_current = _text[position];
113120

114-
if (_current == '\n')
121+
if (_current is '\n')
115122
{
116-
column = -2;
123+
column = -1;
117124

118125
line++;
126+
127+
return;
119128
}
120129

121130
column++;

src/D.Scripting.Tests/Tokenizer/GeneralTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
25

36
using E.Mathematics;
47

@@ -163,6 +166,46 @@ public void ReadPositions()
163166
Assert.Equal(new Location(3, 0, 38), tokens.Next().Start); // pipe
164167
}
165168

169+
[Fact]
170+
public void CanReadTwoStatements()
171+
{
172+
var tokenizer = new Tokenizer(
173+
"""
174+
image = 10
175+
b = 2
176+
""");
177+
178+
Assert.Equal(
179+
"""
180+
image | Identifier | 1 | 0
181+
= | Op | 1 | 6
182+
10 | Number | 1 | 8
183+
b | Identifier | 2 | 0
184+
= | Op | 2 | 2
185+
2 | Number | 2 | 4
186+
""".ReplaceLineEndings("\n"), tokenizer.Dump());
187+
}
188+
189+
[Fact]
190+
public void CanReadTwoStatements_Linux()
191+
{
192+
var tokenizer = new Tokenizer(
193+
"""
194+
image = 10
195+
b = 2
196+
""".ReplaceLineEndings("\n"));
197+
198+
Assert.Equal(
199+
"""
200+
image | Identifier | 1 | 0
201+
= | Op | 1 | 6
202+
10 | Number | 1 | 8
203+
b | Identifier | 2 | 0
204+
= | Op | 2 | 2
205+
2 | Number | 2 | 4
206+
""".ReplaceLineEndings("\n"), tokenizer.Dump());
207+
}
208+
166209
[Fact]
167210
public void Read2()
168211
{
@@ -233,6 +276,39 @@ Image.create 100px 100px #000
233276

234277
public static class TokenizerExtensions
235278
{
279+
public static string Dump(this Tokenizer tokenizer)
280+
{
281+
var tokens = new List<Token>();
282+
283+
Token token;
284+
285+
while ((token = tokenizer.Next()).Kind != TokenKind.EOF)
286+
{
287+
tokens.Add(token);
288+
}
289+
290+
291+
var sb = new StringBuilder();
292+
293+
int textWidth = Math.Max("Text".Length, tokens.Max(t => t.Text.Length));
294+
int kindWidth = Math.Max("Kind".Length, tokens.Max(t => t.Kind.ToString().Length));
295+
296+
int i = 0;
297+
298+
foreach (var t in tokens)
299+
{
300+
if (i > 0)
301+
{
302+
sb.Append('\n');
303+
}
304+
sb.AppendFormat("{0,-" + textWidth + "} | {1,-" + kindWidth + "} | {2,4} | {3,4}", t.Text, t.Kind, t.Start.Line, t.Start.Column);
305+
306+
i++;
307+
}
308+
309+
return sb.ToString();
310+
}
311+
236312
public static TokenKind ReadKind(this Tokenizer tokenizer) => tokenizer.Next().Kind;
237313

238314
public static string Read(this Tokenizer tokenizer, TokenKind kind)

0 commit comments

Comments
 (0)