Skip to content

Commit 48134ed

Browse files
committed
Added Pattern Fragment matching. Removed pattern merging. Added option to ignore whitespace in EBNF conversion.
1 parent 956dfc0 commit 48134ed

37 files changed

+3437
-2273
lines changed

docs/Matcher/Language-Matcher-Schema.md

Lines changed: 581 additions & 48 deletions
Large diffs are not rendered by default.

docs/Matcher/LanguageMatcherDefinition.schema.json

Lines changed: 343 additions & 43 deletions
Large diffs are not rendered by default.

src/Matcher.CLI/Matcher.CLI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
<AssemblyName>matchercli</AssemblyName>
77
<RootNamespace>Synfron.Staxe.Matcher.CLI</RootNamespace>
88
<Authors>Daquanne Dwight</Authors>

src/Matcher.Interop.Ebnf/CharacterClassMatchEngine.cs

Lines changed: 253 additions & 497 deletions
Large diffs are not rendered by default.

src/Matcher.Interop.Ebnf/EbnfConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ public LanguageMatcher Convert(string name, string ebnfText)
6565

6666
AddRules((FragmentMatchData)matcherResult.MatchData);
6767

68-
_languageMatcher.IndexingMode = IndexingMode.Eager;
68+
if (IgnoreWhitespace)
69+
{
70+
_languageMatcher.IndexingMode = IndexingMode.Eager;
71+
}
6972
_languageMatcher.StartingFragment = _languageMatcher.Fragments.FirstOrDefault();
7073

7174
return _languageMatcher;

src/Matcher.Interop.Ebnf/EbnfMatchEngine.cs

Lines changed: 1412 additions & 1079 deletions
Large diffs are not rendered by default.

src/Matcher.Interop.Ebnf/Matcher.Interop.Ebnf.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>Synfron.Staxe.Matcher.Interop.Bnf</RootNamespace>
66
<AssemblyName>Synfron.Staxe.Matcher.Interop.Bnf</AssemblyName>
7-
<Version>2.0.0</Version>
7+
<Version>2.0.1</Version>
88
<Authors>Daquanne Dwight</Authors>
99
<Company>Synfron</Company>
1010
<Copyright>© Daquanne Dwight</Copyright>

src/Matcher.Interop.Model/DefinitionConverter.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private static IList<string> ProcessMatcherActions(LanguageMatcherDefinition lan
5050
Dictionary<string, CreateVariableMatcherAction> createVariableMap = new Dictionary<string, CreateVariableMatcherAction>();
5151
foreach (MatcherActionDefinition actionDefinition in languageMatcherModel.Actions)
5252
{
53-
if (actionDefinition.ActionType == MatcherActionType.CreateVariable)
53+
if (actionDefinition.Action == MatcherActionType.CreateVariable)
5454
{
5555
CreateVariableMatcherAction action = new CreateVariableMatcherAction
5656
{
@@ -68,7 +68,7 @@ private static IList<string> ProcessMatcherActions(LanguageMatcherDefinition lan
6868
foreach (MatcherActionDefinition actionDefinition in languageMatcherModel.Actions)
6969
{
7070
MatcherAction action = null;
71-
switch (actionDefinition.ActionType)
71+
switch (actionDefinition.Action)
7272
{
7373
case MatcherActionType.Assert:
7474
{
@@ -97,7 +97,7 @@ private static IList<string> ProcessMatcherActions(LanguageMatcherDefinition lan
9797
case MatcherActionType.CreateVariable:
9898
break;
9999
default:
100-
throw new InvalidOperationException($"Action type {actionDefinition.ActionType} is not supported");
100+
throw new InvalidOperationException($"Action type {actionDefinition.Action} is not supported");
101101
}
102102
}
103103
}
@@ -117,7 +117,6 @@ private static List<PatternMatcher> ProcessPatternMatchers(LanguageMatcherDefini
117117
{
118118
PatternMatcher patternMatcher = isEager ? PatternReader.Parse(patternIndex + 1, model.Name, model.Pattern) : PatternReader.LazyParse(patternIndex + 1, model.Name, model.Pattern);
119119
patternMatcher.IsNoise = model.IsNoise;
120-
patternMatcher.Mergable = model.Mergable;
121120
patternMatcherMap.Add(patternMatcher.Name, patternMatcher);
122121
if (!model.IsAuxiliary)
123122
{
@@ -245,7 +244,6 @@ PatternMatcherDefinition ConvertPattern(PatternMatcher patternMatcher, bool isAu
245244
{
246245
IsAuxiliary = isAuxilary,
247246
IsNoise = patternMatcher.IsNoise,
248-
Mergable = patternMatcher.Mergable,
249247
Name = patternMatcher.Name,
250248
Pattern = patternMatcher is GroupPatternMatcher groupPatternMatcher ? groupPatternMatcher.ToString(true) : patternMatcher.ToString()
251249
};
@@ -258,7 +256,7 @@ MatcherActionDefinition ConvertMatcherAction(MatcherAction matcherAction)
258256
case UpdateVariableMatcherAction updateVariableMatcherAction:
259257
return new MatcherActionDefinition
260258
{
261-
ActionType = updateVariableMatcherAction.ActionType,
259+
Action = updateVariableMatcherAction.Action,
262260
Name = updateVariableMatcherAction.Name,
263261
Change = updateVariableMatcherAction.Change,
264262
FirstVariableName = languageMatcher.Blobs[updateVariableMatcherAction.TargetBlobId],
@@ -267,7 +265,7 @@ MatcherActionDefinition ConvertMatcherAction(MatcherAction matcherAction)
267265
case CreateVariableMatcherAction createVariableMatcherAction:
268266
return new MatcherActionDefinition
269267
{
270-
ActionType = createVariableMatcherAction.ActionType,
268+
Action = createVariableMatcherAction.Action,
271269
Name = createVariableMatcherAction.Name,
272270
Source = createVariableMatcherAction.Source,
273271
FirstVariableName = languageMatcher.Blobs[createVariableMatcherAction.BlobId],
@@ -276,7 +274,7 @@ MatcherActionDefinition ConvertMatcherAction(MatcherAction matcherAction)
276274
case AssertMatcherAction assertMatcherAction:
277275
return new MatcherActionDefinition
278276
{
279-
ActionType = assertMatcherAction.ActionType,
277+
Action = assertMatcherAction.Action,
280278
Name = assertMatcherAction.Name,
281279
Assert = assertMatcherAction.Assert,
282280
FirstVariableName = languageMatcher.Blobs[assertMatcherAction.FirstBlobId],

src/Matcher.Interop.Model/Matcher.Interop.Model.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>Synfron.Staxe.Matcher.Interop.Model</RootNamespace>
66
<AssemblyName>Synfron.Staxe.Matcher.Interop.Model</AssemblyName>
7-
<Version>2.0.0</Version>
7+
<Version>3.0.0</Version>
88
<Authors>Daquanne Dwight</Authors>
99
<Company>Synfron</Company>
1010
<Copyright>© Daquanne Dwight</Copyright>

src/Matcher.Interop.Model/MatcherActionDefinition.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using Synfron.Staxe.Matcher.Input.Actions;
2+
using System;
23

34
namespace Synfron.Staxe.Matcher.Interop.Model
45
{
56
public class MatcherActionDefinition
67
{
78
public string Name { get; set; }
89

9-
public MatcherActionType ActionType { get; set; }
10+
public MatcherActionType Action { get; set; }
1011

1112
public string FirstVariableName { get; set; }
1213

@@ -18,6 +19,6 @@ public class MatcherActionDefinition
1819

1920
public AssertType Assert { get; set; }
2021

21-
public object Value { get; set; }
22+
public IConvertible Value { get; set; }
2223
}
2324
}

src/Matcher.Interop.Model/PatternMatcherDefinition.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ public class PatternMatcherDefinition
88

99
public bool IsNoise { get; set; }
1010

11-
public bool Mergable { get; set; }
12-
1311
public string Fragment { get; set; }
1412

1513
public bool IsAuxiliary { get; set; }

src/Matcher/AbstractLanguageMatchEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public abstract class AbstractLanguageMatchEngine : ILanguageMatchEngine
1313
protected ref struct State
1414
{
1515
public int CurrentIndex;
16-
public int MaxIndex;
1716
public List<StringMatchData> DistinctStringMatches;
1817
public int Id;
1918
public StringBuilder MatchLogBuilder;

src/Matcher/Data/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Synfron.Staxe.Matcher.Data
66
{
7-
internal static class Extensions
7+
public static class Extensions
88
{
99

1010
public static int GetEndIndex(this IMatchData matchData)

src/Matcher/Data/StringMatchData.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ public class StringMatchData : IMatchData
1212

1313
public string Name { get; set; }
1414

15-
public bool IsNoise { get; set; }
16-
17-
public bool Mergable { get; set; }
18-
1915
public int Id { get; set; }
2016

2117
public string ToXml()

src/Matcher/Input/Actions/AssertMatcherAction.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class AssertMatcherAction : MatcherAction
1515

1616
public AssertType Assert { get; set; }
1717

18-
public override MatcherActionType ActionType => MatcherActionType.Assert;
18+
public override MatcherActionType Action => MatcherActionType.Assert;
1919

2020
public override bool Perform(Span<BlobData> blobDatas, IList<IMatchData> matchDatas)
2121
{
@@ -27,26 +27,26 @@ public override bool Perform(Span<BlobData> blobDatas, IList<IMatchData> matchDa
2727
return !Equals(blobDatas[FirstBlobId].Value?.ToString(), blobDatas[SecondBlobId].Value?.ToString());
2828
case AssertType.GreaterThan:
2929
{
30-
return double.TryParse(blobDatas[FirstBlobId].Value.ToString(), out double num1)
31-
&& double.TryParse(blobDatas[SecondBlobId].Value.ToString(), out double num2) ?
30+
return double.TryParse(blobDatas[FirstBlobId].Value?.ToString(), out double num1)
31+
&& double.TryParse(blobDatas[SecondBlobId].Value?.ToString(), out double num2) ?
3232
num1 > num2 : false;
3333
}
3434
case AssertType.GreaterThanOrEquals:
3535
{
36-
return double.TryParse(blobDatas[FirstBlobId].Value.ToString(), out double num1)
37-
&& double.TryParse(blobDatas[SecondBlobId].Value.ToString(), out double num2) ?
36+
return double.TryParse(blobDatas[FirstBlobId].Value?.ToString(), out double num1)
37+
&& double.TryParse(blobDatas[SecondBlobId].Value?.ToString(), out double num2) ?
3838
num1 >= num2 : false;
3939
}
4040
case AssertType.LessThan:
4141
{
42-
return double.TryParse(blobDatas[FirstBlobId].Value.ToString(), out double num1)
43-
&& double.TryParse(blobDatas[SecondBlobId].Value.ToString(), out double num2) ?
42+
return double.TryParse(blobDatas[FirstBlobId].Value?.ToString(), out double num1)
43+
&& double.TryParse(blobDatas[SecondBlobId].Value?.ToString(), out double num2) ?
4444
num1 < num2 : false;
4545
}
4646
case AssertType.LessThanOrEquals:
4747
{
48-
return double.TryParse(blobDatas[FirstBlobId].Value.ToString(), out double num1)
49-
&& double.TryParse(blobDatas[SecondBlobId].Value.ToString(), out double num2) ?
48+
return double.TryParse(blobDatas[FirstBlobId].Value?.ToString(), out double num1)
49+
&& double.TryParse(blobDatas[SecondBlobId].Value?.ToString(), out double num2) ?
5050
num1 <= num2 : false;
5151
}
5252
case AssertType.Contains:
@@ -71,10 +71,10 @@ internal override string Generate(MatcherEngineGenerator generator)
7171
string code = $@"private bool {methodName}(Span<BlobData> blobDatas, IList<IMatchData> matchDatas)
7272
{{
7373
{(Assert == AssertType.Equals ? $@"
74-
return Equals(blobDatas[{FirstBlobId}]?.Value, blobDatas[{SecondBlobId}]?.Value);
74+
return Equals(blobDatas[{FirstBlobId}].Value?.ToString(), blobDatas[{SecondBlobId}].Value?.ToString());
7575
" : null)}
7676
{(Assert == AssertType.NotEquals ? $@"
77-
return !Equals(blobDatas[{FirstBlobId}]?.Value, blobDatas[{SecondBlobId}]?.Value);
77+
return !Equals(blobDatas[{FirstBlobId}].Value?.ToString(), blobDatas[{SecondBlobId}].Value?.ToString());
7878
" : null)}
7979
{(Assert == AssertType.GreaterThan ? $@"
8080
return double.TryParse(blobDatas[{FirstBlobId}].Value?.ToString(), out double num1)

src/Matcher/Input/Actions/CreateVariableMatcherAction.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Synfron.Staxe.Matcher.Data;
22
using System;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Text;
56

67
namespace Synfron.Staxe.Matcher.Input.Actions
@@ -11,9 +12,9 @@ public class CreateVariableMatcherAction : MatcherAction
1112

1213
public VariableValueSource Source { get; set; }
1314

14-
public object Value { get; set; }
15+
public IConvertible Value { get; set; }
1516

16-
public override MatcherActionType ActionType => MatcherActionType.CreateVariable;
17+
public override MatcherActionType Action => MatcherActionType.CreateVariable;
1718

1819
public override bool Perform(Span<BlobData> blobDatas, IList<IMatchData> matchDatas)
1920
{
@@ -25,10 +26,10 @@ public override bool Perform(Span<BlobData> blobDatas, IList<IMatchData> matchDa
2526
Value = Value
2627
};
2728
break;
28-
case VariableValueSource.PartsText:
29+
case VariableValueSource.PartsXml:
2930
blobDatas[BlobId] = new BlobData
3031
{
31-
Value = matchDatas.GetText(true)
32+
Value = string.Join("", matchDatas.Select(matchData => matchData.ToXml()))
3233
};
3334
break;
3435
case VariableValueSource.PartsLength:
@@ -71,13 +72,13 @@ internal override string Generate(MatcherEngineGenerator generator)
7172
{(Source == VariableValueSource.Value ? $@"
7273
blobDatas[{BlobId}] = new BlobData
7374
{{
74-
Value = Value
75+
Value = {(Value is string str ? $"\"{str}\"" : Value)}
7576
}};
7677
" : null)}
77-
{(Source == VariableValueSource.PartsText ? $@"
78+
{(Source == VariableValueSource.PartsXml ? $@"
7879
blobDatas[{BlobId}] = new BlobData
7980
{{
80-
Value = GetText(matchDatas)
81+
Value = string.Join("", matchDatas.Select(matchData => matchData.ToXml()))
8182
}};
8283
" : null)}
8384
{(Source == VariableValueSource.PartsLength ? $@"

src/Matcher/Input/Actions/MatcherAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public abstract class MatcherAction
1010
{
1111
public string Name { get; set; }
1212

13-
public abstract MatcherActionType ActionType { get; }
13+
public abstract MatcherActionType Action { get; }
1414

1515
public abstract bool Perform(Span<BlobData> blobDatas, IList<IMatchData> matchDatas);
1616

src/Matcher/Input/Actions/UpdateVariableMatcherAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class UpdateVariableMatcherAction : MatcherAction
1313

1414
public int SourceBlobId { get; set; }
1515

16-
public override MatcherActionType ActionType => MatcherActionType.UpdateVariable;
16+
public override MatcherActionType Action => MatcherActionType.UpdateVariable;
1717

1818
public override bool Perform(Span<BlobData> blobDatas, IList<IMatchData> matchDatas)
1919
{

src/Matcher/Input/Actions/VariableValueSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public enum VariableValueSource
88
{
99
Value,
1010
PartsCount,
11-
PartsText,
11+
PartsXml,
1212
PartsLength,
1313
StringPartsText,
1414
StringPartsLength

src/Matcher/Input/FallThroughMode.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ public enum FallThroughMode
1111
One = 1,
1212
All = int.MaxValue
1313
}
14+
15+
public static class FallThroughModeExtensions
16+
{
17+
public static bool IsCountBased(this FallThroughMode mode)
18+
{
19+
return FallThroughMode.None < mode && mode < FallThroughMode.All;
20+
}
21+
}
1422
}

src/Matcher/Input/Patterns/PatternMatcher.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ public abstract class PatternMatcher : IMatcher
1212

1313
public bool IsNoise { get; set; }
1414

15-
public bool Mergable { get; set; }
16-
1715
public bool IsStandard { get; set; } = true;
1816

1917
public abstract (bool success, int offset) IsMatch(string text, int startOffset = 0);

0 commit comments

Comments
 (0)