Skip to content

Commit acc73c3

Browse files
committed
More parser fixes for #357
1 parent bd55c02 commit acc73c3

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/ServiceInsight.Desktop/CodeParser/BaseParser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Particular.ServiceInsight.Desktop.CodeParser
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
56
using System.Text;
@@ -43,9 +44,9 @@ protected void TryExtract(ref SourcePart text, string lex)
4344
}
4445
}
4546

46-
protected bool TryExtract(List<CodeLexem> res, ref SourcePart text, string lex, LexemType type)
47+
protected bool TryExtract(List<CodeLexem> res, ref SourcePart text, string lex, LexemType type, StringComparison comparisonType = StringComparison.Ordinal)
4748
{
48-
if (text.StartsWith(lex))
49+
if (text.StartsWith(lex, comparisonType))
4950
{
5051
res.Add(new CodeLexem(type, CutString(ref text, lex.Length)));
5152
return true;

src/ServiceInsight.Desktop/CodeParser/JsonParser.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ protected override List<CodeLexem> Parse(SourcePart text)
2727

2828
while (reader.Read())
2929
{
30+
var length = text.Length;
31+
3032
switch (reader.TokenType)
3133
{
3234
case JsonToken.StartArray: Extract(list, ref text, "[", LexemType.Symbol); break;
@@ -55,6 +57,14 @@ protected override List<CodeLexem> Parse(SourcePart text)
5557

5658
default: list.Add(new CodeLexem(LexemType.Error, reader.TokenType.ToString())); break;
5759
}
60+
61+
if (length == text.Length)
62+
{
63+
// Parser chocked on something, dump the rest of the
64+
// string and exit.
65+
list.Add(new CodeLexem(CutString(ref text, text.Length)));
66+
return list;
67+
}
5868
}
5969
}
6070

@@ -80,7 +90,7 @@ protected override List<CodeLexem> Parse(SourcePart text)
8090

8191
void Extract(List<CodeLexem> res, ref SourcePart text, string lex, LexemType type)
8292
{
83-
while (!TryExtract(res, ref text, lex, type))
93+
while (!TryExtract(res, ref text, lex, type, StringComparison.OrdinalIgnoreCase))
8494
{
8595
var length = text.Length;
8696

src/ServiceInsight.Tests/JsonParserTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,21 @@ public void should_parse_complex_json_graphs()
9393
lexemes.Count(lx => lx.Type == LexemType.Value).ShouldBe(8);
9494
lexemes.Count(lx => lx.Type == LexemType.LineBreak).ShouldBe(11);
9595
}
96+
97+
[Test]
98+
public void issue357()
99+
{
100+
const string JsonGraph = "{ \"foo\":true, \"bar\":false }";
101+
102+
var lexemes = new CodeLexem(JsonGraph).Parse(CodeLanguage.Json);
103+
104+
lexemes.Count().ShouldBe(17);
105+
lexemes.Count(lx => lx.Type == LexemType.Property).ShouldBe(2);
106+
lexemes.Count(lx => lx.Type == LexemType.Value).ShouldBe(2);
107+
lexemes.Count(lx => lx.Type == LexemType.Quotes).ShouldBe(4);
108+
lexemes.Count(lx => lx.Type == LexemType.Space).ShouldBe(3);
109+
lexemes.Count(lx => lx.Type == LexemType.Symbol).ShouldBe(5);
110+
lexemes.Count(lx => lx.Type == LexemType.Complex).ShouldBe(0);
111+
}
96112
}
97113
}

0 commit comments

Comments
 (0)