Skip to content

Commit 5c30a7e

Browse files
improved performance
1 parent 42462b9 commit 5c30a7e

17 files changed

+140
-121
lines changed

AtomResult.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace EsotericDevZone.RuleBasedParser
8+
{
9+
public class AtomResult
10+
{
11+
public object Value { get; }
12+
public string ErrorMessage { get; }
13+
14+
public bool Failed => ErrorMessage != null;
15+
16+
private AtomResult(object value, string errorMessage)
17+
{
18+
Value = value;
19+
ErrorMessage = errorMessage;
20+
}
21+
22+
public static AtomResult Atom(object value)
23+
{
24+
return new AtomResult(value, null);
25+
}
26+
public static AtomResult Error(string message)
27+
{
28+
return new AtomResult(null, message);
29+
}
30+
31+
public override string ToString() => Failed ? $"AtomResult Error={ErrorMessage}"
32+
: $"AtomResult Value={Value}";
33+
34+
}
35+
}

EsotericDevZone.RuleBasedParser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Reference Include="System.Xml" />
5252
</ItemGroup>
5353
<ItemGroup>
54+
<Compile Include="AtomResult.cs" />
5455
<Compile Include="ParseError.cs" />
5556
<Compile Include="Presets\AtomBuilders.cs" />
5657
<Compile Include="IgnoreMatch.cs" />

IgnoreMatch.cs

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

8-
public int TokensCount { get; } = 1;
9-
public double Similarity { get; set; }
8+
public int TokensCount { get; } = 1;
109

1110
public IgnoreMatch(int position, int tokensCount=1)
1211
{

ParseError.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ internal class ParseError : IParseRulePatternItemMatch
99

1010
public int TokensCount { get; } = 0;
1111

12-
public double Similarity { get; set; } = 0;
12+
public double Relevance { get; set; } = 0;
1313

1414
public ParseError(ParseError error, double similarity)
1515
{
1616
Message = error.Message;
1717
Position = error.Position;
18-
Similarity = similarity;
18+
Relevance = similarity;
1919
}
2020

2121
public ParseError(string message, int position)
@@ -24,6 +24,6 @@ public ParseError(string message, int position)
2424
Position = position;
2525
}
2626

27-
public override string ToString() => $"ParseError: \"{Message}\" at token index {Position} [S={Similarity}]";
27+
public override string ToString() => $"ParseError: \"{Message}\" at token index {Position} [S={Relevance}]";
2828
}
2929
}

ParseException.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ namespace EsotericDevZone.RuleBasedParser
88
{
99
public class ParseException : Exception
1010
{
11-
public Token ReferencedToken { get; } = null;
11+
public Token ReferencedToken { get; } = null;
1212

1313
public ParseException() : base() { }
1414
public ParseException(string message) : base(message) { }
1515
public ParseException(Token token, string message) : base(message) { ReferencedToken = token; }
1616
public ParseException(Exception exception) : base(exception.Message, exception) { }
1717
public ParseException(ParseException exception) : this(exception.ReferencedToken, exception.Message) { }
1818

19-
public override string ToString()
19+
public override string Message
2020
{
21-
if (ReferencedToken == null)
22-
return $"Parse error: {Message}";
23-
else
24-
return $"{ReferencedToken.Line}:{ReferencedToken.Column} Parse error: {Message}";
25-
}
21+
get
22+
{
23+
if (ReferencedToken == null)
24+
return $"Parse error: {base.Message}";
25+
else
26+
return $"{ReferencedToken.Line}:{ReferencedToken.Column} Parse error: {base.Message}";
27+
}
28+
}
29+
30+
public override string ToString() => Message;
2631
}
2732
}

ParseRecord.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ class ParseRecord : IParseRulePatternItemMatch
99
public string RuleKey { get; }
1010
public ParseResult Result { get; }
1111
public int Position { get; }
12-
public int TokensCount { get; }
13-
public double Similarity { get; set; }
12+
public int TokensCount { get; }
1413
public ParseError Error { get; }
1514

1615
public ParseRecord(ParseError error)

ParseRulePatterns/AtomPatternItem.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +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 AtomPatternItem : IParseRulePatternItem
106
{
117
public string Name { get; }
12-
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position, int stack = 0)
13-
{
14-
try
15-
{
16-
var resObj = parser.GetAtomBuilder(Name)(tokens[position].Value);
17-
var result = new ParseResult(tokens[position], resObj);
18-
return new ParseRecord(Name, result, position, 1) { Similarity = 1 };
19-
}
20-
catch(Exception e)
8+
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position)
9+
{
10+
var atom = parser.GetAtomBuilder(Name)(tokens[position].Value);
11+
if (atom.Failed)
2112
{
22-
return new ParseError(e.Message, position);
13+
return new ParseError(atom.ErrorMessage, position);
2314
}
15+
var result = new ParseResult(tokens[position], atom.Value);
16+
return new ParseRecord(Name, result, position, 1);
2417
}
2518

2619
public AtomPatternItem(string name)

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, int stack=0);
7+
IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position);
88
}
99
}

ParseRulePatterns/IParseRulePatternItemMatch.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
internal interface IParseRulePatternItemMatch
44
{
55
int Position { get; }
6-
int TokensCount { get; }
7-
double Similarity { get; set; }
6+
int TokensCount { get; }
87
}
98
}

ParseRulePatterns/LiteralPatternItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +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, int stack = 0)
12+
public IParseRulePatternItemMatch Match(Parser parser, List<Token> tokens, int position)
1313
{
1414
if (tokens[position].Value != Name)
1515
{
1616
return new ParseError($"Expected '{Name}', got '{tokens[position].Value}'", position);
1717
}
18-
return new IgnoreMatch(position) { Similarity = 1 };
18+
return new IgnoreMatch(position);
1919
}
2020

2121
public LiteralPatternItem(string name)

0 commit comments

Comments
 (0)