Skip to content

Commit 1044fee

Browse files
committed
Allow block and line comments in JSON content
1 parent 27710e6 commit 1044fee

1 file changed

Lines changed: 99 additions & 2 deletions

File tree

  • StyleCop.Analyzers/StyleCop.Analyzers/LightJson/Serialization

StyleCop.Analyzers/StyleCop.Analyzers/LightJson/Serialization/TextScanner.cs

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,23 @@ public char Read()
9090
/// </summary>
9191
public void SkipWhitespace()
9292
{
93-
while (char.IsWhiteSpace(this.Peek()))
93+
while (true)
9494
{
95-
this.Read();
95+
char next = this.Peek();
96+
if (char.IsWhiteSpace(next))
97+
{
98+
this.Read();
99+
continue;
100+
}
101+
else if (next == '/')
102+
{
103+
this.SkipCommentOrInvalidSlash();
104+
continue;
105+
}
106+
else
107+
{
108+
break;
109+
}
96110
}
97111
}
98112

@@ -124,5 +138,88 @@ public void Assert(string next)
124138
this.Assert(next[i]);
125139
}
126140
}
141+
142+
private void SkipCommentOrInvalidSlash()
143+
{
144+
// First character is the a slash
145+
this.Read();
146+
switch (this.Peek())
147+
{
148+
case '/':
149+
this.SkipLineComment();
150+
return;
151+
152+
case '*':
153+
this.SkipBlockComment();
154+
return;
155+
156+
default:
157+
return;
158+
}
159+
}
160+
161+
private void SkipLineComment()
162+
{
163+
// First character is the second '/' of the opening '//'
164+
this.Read();
165+
166+
while (true)
167+
{
168+
switch (this.reader.Peek())
169+
{
170+
case '\n':
171+
// Reached the end of the line
172+
this.Read();
173+
return;
174+
175+
case -1:
176+
// Reached the end of the file
177+
return;
178+
179+
default:
180+
this.Read();
181+
continue;
182+
}
183+
}
184+
}
185+
186+
private void SkipBlockComment()
187+
{
188+
// First character is the '*' of the opening '/*'
189+
this.Read();
190+
191+
bool foundStar = false;
192+
while (true)
193+
{
194+
switch (this.reader.Peek())
195+
{
196+
case '*':
197+
this.Read();
198+
foundStar = true;
199+
continue;
200+
201+
case '/':
202+
this.Read();
203+
if (foundStar)
204+
{
205+
return;
206+
}
207+
else
208+
{
209+
foundStar = false;
210+
continue;
211+
}
212+
213+
case -1:
214+
// Reached the end of the file
215+
return;
216+
217+
default:
218+
this.Read();
219+
foundStar = false;
220+
continue;
221+
}
222+
}
223+
}
127224
}
128225
}

0 commit comments

Comments
 (0)