Skip to content

Commit e45110c

Browse files
committed
Set up methods for skipping to a terminator, and added tests.
1 parent 57a8346 commit e45110c

12 files changed

Lines changed: 103 additions & 11 deletions

Schema Tests/text/reader/SchemaTextReaderNumberTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private void ReadAndAssert_<T>(
1212
SchemaTextReader tr,
1313
T expectedValue,
1414
Func<T> readHandler,
15-
Action<T> assertValue) {
15+
Action<T> assertValue) where T : notnull, IEquatable<T> {
1616
Asserts.Equal(expectedValue, readHandler());
1717
tr.Position = 0;
1818

Schema Tests/text/reader/TextReaderExtensionTests_Read.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ internal partial class TextReaderExtensionTests {
1010
[TestCase(" foo ", ExpectedResult = "foo")]
1111
[TestCase(" \n foo \t ", ExpectedResult = "foo")]
1212
[TestCase("foo bar", ExpectedResult = "foo")]
13-
[TestCase("`foo bar`", ExpectedResult = "`foo bar`")]
13+
/*[TestCase("`foo bar`", ExpectedResult = "`foo bar`")]
1414
[TestCase("'foo bar'", ExpectedResult = "'foo bar'")]
15-
[TestCase("\"foo bar\"", ExpectedResult = "\"foo bar\"")]
15+
[TestCase("\"foo bar\"", ExpectedResult = "\"foo bar\"")]*/
1616
public string TestReadWord(string text) {
1717
using var tr = TextSchemaTestUtil.CreateTextReader(text);
1818
return tr.ReadWord();

Schema Tests/text/reader/TextReaderExtensionTests_Skip.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,17 @@
44
namespace schema.text.reader;
55

66
internal partial class TextReaderExtensionTests {
7-
// TODO
7+
[Test]
8+
[TestCase("", ExpectedResult = "")]
9+
[TestCase(" ", ExpectedResult = "")]
10+
[TestCase(" foo ", ExpectedResult = "foo ")]
11+
[TestCase(" \n\t foo", ExpectedResult = "foo")]
12+
[TestCase(" // 123\n foo", ExpectedResult = "foo")]
13+
[TestCase(" // 123\n// abc\n foo", ExpectedResult = "foo")]
14+
[TestCase("/* 123 \n \n * */foo", ExpectedResult = "foo")]
15+
public string TestSkipCommentsAndWhitespace(string text) {
16+
using var tr = TextSchemaTestUtil.CreateTextReader(text);
17+
tr.SkipCommentsAndWhitespace();
18+
return tr.ReadRemainder();
19+
}
820
}

Schema/src/binary/parser/TypeInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ public ParseStatus ParseTypeSymbol(
225225
constraintType,
226226
isReadonly,
227227
out var constraintTypeInfo);
228-
Asserts.Equal(ParseStatus.SUCCESS, parseStatus);
228+
229+
if (parseStatus != ParseStatus.SUCCESS) {
230+
Asserts.Fail($"Expected {parseStatus} to equal {ParseStatus.SUCCESS}.");
231+
}
232+
229233
return constraintTypeInfo;
230234
})
231235
.ToArray();

Schema/src/text/reader/ITextReader_Char.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ public partial interface ITextReader {
66
string ReadUpToStartOfTerminator(char terminator);
77
string ReadUpToAndPastTerminator(char terminator);
88

9+
void SkipUpToStartOfTerminator(char terminator);
10+
void SkipUpToAndPastTerminator(char terminator);
11+
912
string ReadWhile(char match);
1013

1114
void SkipOnceIfPresent(char match);

Schema/src/text/reader/ITextReader_String.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using System;
2-
3-
namespace schema.text.reader;
1+
namespace schema.text.reader;
42

53
public partial interface ITextReader {
64
bool Matches(string match);
75

86
string ReadUpToStartOfTerminator(string terminator);
97
string ReadUpToAndPastTerminator(string terminator);
108

9+
void SkipUpToStartOfTerminator(string terminator);
10+
void SkipUpToAndPastTerminator(string terminator);
11+
1112
string ReadWhile(string match);
1213

1314
void SkipOnceIfPresent(string match);

Schema/src/text/reader/SchemaTextReader_MatchesChar.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ public string ReadUpToAndPastTerminator(char terminator) {
4242
return sb.ToString();
4343
}
4444

45+
public void SkipUpToStartOfTerminator(char terminator) {
46+
while (!this.Eof) {
47+
var originalLineNumber = this.LineNumber;
48+
var originalIndexInLine = this.IndexInLine;
49+
var originalPosition = this.PositionInternal_;
50+
51+
if (this.PeekCharAndProgressIfNotEqualTo_(terminator, out _)) {
52+
continue;
53+
}
54+
55+
this.LineNumber = originalLineNumber;
56+
this.IndexInLine = originalIndexInLine;
57+
this.PositionInternal_ = originalPosition;
58+
break;
59+
}
60+
}
61+
62+
public void SkipUpToAndPastTerminator(char terminator) {
63+
while (!this.Eof) {
64+
if (this.PeekCharAndProgressIfNotEqualTo_(terminator, out _)) {
65+
continue;
66+
}
67+
68+
this.ReadChar();
69+
break;
70+
}
71+
}
72+
4573
public string ReadWhile(char matches) {
4674
var sb = new StringBuilder();
4775

Schema/src/text/reader/SchemaTextReader_MatchesReadOnlySpanChar.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace schema.text.reader;
66

77
public sealed partial class SchemaTextReader {
88
public bool Matches(out char match, ReadOnlySpan<char> matches) {
9+
if (this.Eof) {
10+
match = default;
11+
return false;
12+
}
13+
914
foreach (var c in matches) {
1015
if (this.PeekCharAndProgressIfEqualTo_(c)) {
1116
match = c;

Schema/src/text/reader/SchemaTextReader_MatchesReadOnlySpanString.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace schema.text.reader;
66

77
public sealed partial class SchemaTextReader {
88
public bool Matches(out string text, ReadOnlySpan<string> matches) {
9+
if (this.Eof) {
10+
text = default;
11+
return false;
12+
}
13+
914
var originalLineNumber = this.LineNumber;
1015
var originalIndexInLine = this.IndexInLine;
1116
var originalPosition = this.PositionInternal_;

Schema/src/text/reader/SchemaTextReader_MatchesString.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ namespace schema.text.reader;
66

77
public sealed partial class SchemaTextReader {
88
public bool Matches(string match) {
9+
if (this.Eof) {
10+
return false;
11+
}
12+
913
var originalLineNumber = this.LineNumber;
1014
var originalIndexInLine = this.IndexInLine;
1115
var originalPosition = this.PositionInternal_;
@@ -66,6 +70,33 @@ public string ReadUpToAndPastTerminator(string terminator) {
6670
return sb.ToString();
6771
}
6872

73+
public void SkipUpToStartOfTerminator(string terminator) {
74+
while (!this.Eof) {
75+
var originalLineNumber = this.LineNumber;
76+
var originalIndexInLine = this.IndexInLine;
77+
var originalPosition = this.PositionInternal_;
78+
79+
if (this.Matches(terminator)) {
80+
this.LineNumber = originalLineNumber;
81+
this.IndexInLine = originalIndexInLine;
82+
this.PositionInternal_ = originalPosition;
83+
break;
84+
}
85+
86+
this.ReadChar();
87+
}
88+
}
89+
90+
public void SkipUpToAndPastTerminator(string terminator) {
91+
while (!this.Eof) {
92+
if (this.Matches(terminator)) {
93+
break;
94+
}
95+
96+
this.ReadChar();
97+
}
98+
}
99+
69100
public string ReadWhile(string match) {
70101
var sb = new StringBuilder();
71102

0 commit comments

Comments
 (0)