Skip to content

Commit 42462b9

Browse files
refined parse errors
1 parent 4e6dae0 commit 42462b9

15 files changed

+193
-118
lines changed

EsotericDevZone.RuleBasedParser.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@
5151
<Reference Include="System.Xml" />
5252
</ItemGroup>
5353
<ItemGroup>
54+
<Compile Include="ParseError.cs" />
5455
<Compile Include="Presets\AtomBuilders.cs" />
5556
<Compile Include="IgnoreMatch.cs" />
5657
<Compile Include="NoTokensProvidedException.cs" />
5758
<Compile Include="ParseException.cs" />
5859
<Compile Include="CommentStyle.cs" />
60+
<Compile Include="Presets\AtomBuildException.cs" />
5961
<Compile Include="Presets\CommentStyles.cs" />
6062
<Compile Include="Helpers\CommentsStyleHelper.cs" />
6163
<Compile Include="Helpers\TokenSplitHelper.cs" />

IgnoreMatch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ internal class IgnoreMatch : IParseRulePatternItemMatch
66
public int Position { get; }
77

88
public int TokensCount { get; } = 1;
9+
public double Similarity { get; set; }
910

1011
public IgnoreMatch(int position, int tokensCount=1)
1112
{

ParseError.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using EsotericDevZone.RuleBasedParser.ParseRulePatterns;
2+
3+
namespace EsotericDevZone.RuleBasedParser
4+
{
5+
internal class ParseError : IParseRulePatternItemMatch
6+
{
7+
public string Message { get; }
8+
public int Position { get; }
9+
10+
public int TokensCount { get; } = 0;
11+
12+
public double Similarity { get; set; } = 0;
13+
14+
public ParseError(ParseError error, double similarity)
15+
{
16+
Message = error.Message;
17+
Position = error.Position;
18+
Similarity = similarity;
19+
}
20+
21+
public ParseError(string message, int position)
22+
{
23+
Message = message;
24+
Position = position;
25+
}
26+
27+
public override string ToString() => $"ParseError: \"{Message}\" at token index {Position} [S={Similarity}]";
28+
}
29+
}

ParseRecord.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ class ParseRecord : IParseRulePatternItemMatch
1010
public ParseResult Result { get; }
1111
public int Position { get; }
1212
public int TokensCount { get; }
13+
public double Similarity { get; set; }
14+
public ParseError Error { get; }
15+
16+
public ParseRecord(ParseError error)
17+
{
18+
Error = error;
19+
}
20+
1321
public ParseRecord(string ruleKey, ParseResult value, int position, int tokensCount)
1422
{
1523
RuleKey = ruleKey;
1624
Result = value;
1725
Position = position;
1826
TokensCount = tokensCount;
27+
Error = null;
1928
}
2029

2130
public override string ToString() => $"Record(Key='{RuleKey}', Position={Position}, TokensCount={TokensCount})";

ParseRulePatterns/AtomPatternItem.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ namespace EsotericDevZone.RuleBasedParser.ParseRulePatterns
99
internal class AtomPatternItem : IParseRulePatternItem
1010
{
1111
public string Name { get; }
12-
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position)
12+
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position, int stack = 0)
1313
{
1414
try
1515
{
1616
var resObj = parser.GetAtomBuilder(Name)(tokens[position].Value);
1717
var result = new ParseResult(tokens[position], resObj);
18-
return new ParseRecord(Name, result, position, 1);
19-
}
20-
catch(ParseException e)
21-
{
22-
throw new ParseException((ParseException)e);
18+
return new ParseRecord(Name, result, position, 1) { Similarity = 1 };
2319
}
2420
catch(Exception e)
2521
{
26-
throw new ParseException(tokens[position], e.Message);
22+
return new ParseError(e.Message, position);
2723
}
2824
}
2925

ParseRulePatterns/IParseRulePatternItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace EsotericDevZone.RuleBasedParser.ParseRulePatterns
44
{
55
internal interface IParseRulePatternItem
66
{
7-
IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position);
7+
IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position, int stack=0);
88
}
99
}

ParseRulePatterns/IParseRulePatternItemMatch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ internal interface IParseRulePatternItemMatch
44
{
55
int Position { get; }
66
int TokensCount { get; }
7+
double Similarity { get; set; }
78
}
89
}

ParseRulePatterns/LiteralPatternItem.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ namespace EsotericDevZone.RuleBasedParser.ParseRulePatterns
99
internal class LiteralPatternItem : IParseRulePatternItem
1010
{
1111
public string Name { get; }
12-
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position)
12+
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position, int stack = 0)
1313
{
1414
if (tokens[position].Value != Name)
15-
throw new ParseException(tokens[position], $"Expected '{Name}', got '{tokens[position].Value}'");
16-
return new IgnoreMatch(position);
15+
{
16+
return new ParseError($"Expected '{Name}', got '{tokens[position].Value}'", position);
17+
}
18+
return new IgnoreMatch(position) { Similarity = 1 };
1719
}
1820

1921
public LiteralPatternItem(string name)

ParseRulePatterns/RepeatableTailItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace EsotericDevZone.RuleBasedParser.ParseRulePatterns
44
{
55
internal class RepeatableTailItem : IParseRulePatternItem
66
{
7-
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position)
7+
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position, int stack = 0)
88
{
9-
return new IgnoreMatch(position, 0);
9+
return new IgnoreMatch(position, 0) { Similarity = 1 };
1010
}
1111

1212
public override string ToString() => "??";

ParseRulePatterns/RuleKeyPatternItem.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
62

73
namespace EsotericDevZone.RuleBasedParser.ParseRulePatterns
84
{
95
internal class RuleKeyPatternItem : IParseRulePatternItem
106
{
117
public string RuleKey { get; }
128

13-
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position)
9+
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position, int stack = 0)
1410
{
15-
try
11+
var rec = parser.LookFor(RuleKey, tokens, position, stack + 1);
12+
if (rec.Error != null)
1613
{
17-
var rec = parser.LookFor(RuleKey, tokens, position);
18-
return rec;
19-
}
20-
catch(ParseException e)
21-
{
22-
throw new ParseException((ParseException)e);
23-
}
24-
catch (Exception e)
25-
{
26-
throw new ParseException(tokens[position], e.Message);
14+
return new ParseError(rec.Error, rec.Error.Similarity);
2715
}
16+
return rec;
2817
}
2918

3019
public RuleKeyPatternItem(string ruleKey)

0 commit comments

Comments
 (0)