Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ed103abbf | |||
| fab5d2eee7 | |||
| 7be09140e2 | |||
| 09bbba1293 |
@@ -17,23 +17,7 @@ on:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
linter:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
dotnet-version: [9.0.X]
|
||||
os: [ubuntu-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: ${{ matrix.dotnet-version }}
|
||||
- name: Run linter
|
||||
run: make lint
|
||||
|
||||
tests_linux:
|
||||
needs: linter
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
|
||||
17
HISTORY.md
17
HISTORY.md
@@ -4,6 +4,23 @@ Changelog
|
||||
|
||||
(unreleased)
|
||||
------------
|
||||
- Feat: enable named literals, ref: NOISSUE. [Simon Diesenreiter]
|
||||
|
||||
|
||||
0.5.1 (2024-12-03)
|
||||
------------------
|
||||
|
||||
Fix
|
||||
~~~
|
||||
- Some bugfixes with fragment parser logic, ref: NOISSUE. [Simon
|
||||
Diesenreiter]
|
||||
|
||||
Other
|
||||
~~~~~
|
||||
|
||||
|
||||
0.5.0 (2024-12-03)
|
||||
------------------
|
||||
|
||||
Fix
|
||||
~~~
|
||||
|
||||
@@ -18,8 +18,14 @@ public class TextParserTests
|
||||
private const string testInput5 = @"asdfnums(2,5,3)ght
|
||||
|
||||
cv strs(test) jh 4,3,2
|
||||
|
||||
|
||||
34,54,2nums(2,8) strs(aa,ab,ba,bb)aa,bb";
|
||||
private const string testInput6 = @"adfdf1()324ddf3()svsdvs
|
||||
davnsldkvjs2()m23423()
|
||||
mcsodkcn owdjnfj 1() asdfnad 23234 2() sdvsdv";
|
||||
private const string testInput7 = @"adfdf1()324ddf3()()()svsdvs
|
||||
davnsldkvjs2()()m23423()()()
|
||||
mcsodkcn owdjnfj 1() asdfnad 23234 2()() sdvsdv";
|
||||
|
||||
[Fact]
|
||||
public void LineParser_TestSimpleRepetition()
|
||||
@@ -230,7 +236,7 @@ public class TextParserTests
|
||||
.EndOptions()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser<FragmentSchemaContext>(schema);
|
||||
var parser = TextParser.Create(schema);
|
||||
var fragmentData = parser
|
||||
.SetInputText(testInput5)
|
||||
.Parse()
|
||||
@@ -257,4 +263,56 @@ public class TextParserTests
|
||||
Assert.Equal(12, convertedData[2]);
|
||||
Assert.Equal(4, convertedData[3]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FragmentParser_LiteralTest()
|
||||
{
|
||||
var schemaBuilder = new FragmentSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.StartOptions()
|
||||
.Option()
|
||||
.Expect("1()", "option1")
|
||||
.Option()
|
||||
.Expect("2()", "option2")
|
||||
.Option()
|
||||
.Expect("3()", "option3")
|
||||
.EndOptions()
|
||||
.Build();
|
||||
|
||||
var parser = TextParser.Create(schema);
|
||||
var fragmentData = parser
|
||||
.SetInputText(testInput6)
|
||||
.Parse()
|
||||
.AsFragments();
|
||||
|
||||
var convertedData = fragmentData
|
||||
.ConvertAll((Fragment f) =>
|
||||
{
|
||||
bool saw1 = f.ContainsKey("option1") ? f["option1"].Count > 0 : false;
|
||||
bool saw2 = f.ContainsKey("option2") ? f["option2"].Count() > 0 : false;
|
||||
bool saw3 = f.ContainsKey("option3") ? f["option3"].Count() > 0 : false;
|
||||
int indicator = 0;
|
||||
if (saw1)
|
||||
{
|
||||
indicator += 1;
|
||||
}
|
||||
if (saw2)
|
||||
{
|
||||
indicator += 2;
|
||||
}
|
||||
if (saw3)
|
||||
{
|
||||
indicator += 4;
|
||||
}
|
||||
return indicator;
|
||||
});
|
||||
|
||||
Assert.Equal(6, convertedData.Count);
|
||||
Assert.Equal(1, convertedData[0]);
|
||||
Assert.Equal(4, convertedData[1]);
|
||||
Assert.Equal(2, convertedData[2]);
|
||||
Assert.Equal(4, convertedData[3]);
|
||||
Assert.Equal(1, convertedData[4]);
|
||||
Assert.Equal(2, convertedData[5]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ class FixedRepetitionBlock : BuildingBlockBase
|
||||
this.context = this.inputSchema.CreateContext();
|
||||
}
|
||||
}
|
||||
return result.SingleOrDefault();
|
||||
return result.Single();
|
||||
}
|
||||
|
||||
public override bool CanParseWord(InputProvider inputs)
|
||||
|
||||
@@ -22,7 +22,7 @@ class GreedyRepetitionBlock : BuildingBlockBase
|
||||
{
|
||||
this.context = this.inputSchema.CreateContext();
|
||||
}
|
||||
return result.SingleOrDefault();
|
||||
return result.Single();
|
||||
}
|
||||
|
||||
public override bool CanParseWord(InputProvider inputs)
|
||||
|
||||
@@ -16,11 +16,13 @@ public class FragmentSchema : ISchema<FragmentSchemaContext>
|
||||
{
|
||||
private string fragmentRegex;
|
||||
private List<string> namedGroups = new List<string>();
|
||||
private List<string> namedLiterals = new List<string>();
|
||||
|
||||
public FragmentSchema(string fragmentRegex, List<string> namedGroups)
|
||||
public FragmentSchema(string fragmentRegex, List<string> namedGroups, List<string> namedLiterals)
|
||||
{
|
||||
this.fragmentRegex = fragmentRegex;
|
||||
this.namedGroups = namedGroups;
|
||||
this.namedLiterals = namedLiterals;
|
||||
}
|
||||
|
||||
public List<IToken> ProcessNextWord(FragmentSchemaContext currentContext, InputProvider inputs)
|
||||
@@ -42,6 +44,15 @@ public class FragmentSchema : ISchema<FragmentSchemaContext>
|
||||
}
|
||||
newToken.AddMatch(groupName, captureList);
|
||||
}
|
||||
foreach (var literalName in this.namedLiterals)
|
||||
{
|
||||
var captureList = new List<string>();
|
||||
if (match.Groups.Keys.Contains(literalName) && match.Groups[literalName].Length > 0)
|
||||
{
|
||||
captureList.Add(match.Groups[literalName].Length.ToString());
|
||||
}
|
||||
newToken.AddMatch(literalName, captureList);
|
||||
}
|
||||
tokenList.Add(newToken);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
|
||||
protected string fragmentRegex = @"";
|
||||
|
||||
private List<string> namedGroups = new List<string>();
|
||||
private List<string> namedLiterals = new List<string>();
|
||||
|
||||
public FragmentSchemaBuilder()
|
||||
{
|
||||
@@ -57,9 +58,15 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
|
||||
return this;
|
||||
}
|
||||
|
||||
public FragmentSchemaBuilder Expect(string literal)
|
||||
public FragmentSchemaBuilder Expect(string literal, string name = "")
|
||||
{
|
||||
this.fragmentRegex += Regex.Escape(literal);
|
||||
string groupNamePrefix = "";
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
groupNamePrefix = "?<" + name + ">";
|
||||
namedLiterals.Add(name);
|
||||
}
|
||||
this.fragmentRegex += "(" + groupNamePrefix + Regex.Escape(literal) + ")";
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -100,6 +107,10 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
|
||||
throw new Exception("Invalid repetition definitions!");
|
||||
}
|
||||
var oldSchemaBuilder = currentBuilder.UpperLayerBuilder;
|
||||
if (oldSchemaBuilder == null)
|
||||
{
|
||||
throw new Exception("Something went terribly wrong!");
|
||||
}
|
||||
|
||||
var currentRegex = "(" + currentBuilder.fragmentRegex + ")";
|
||||
switch (currentBuilder.RepetitionType)
|
||||
@@ -125,7 +136,7 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
|
||||
|
||||
public FragmentSchema Build()
|
||||
{
|
||||
var schema = new FragmentSchema(this.fragmentRegex, this.namedGroups);
|
||||
var schema = new FragmentSchema(this.fragmentRegex, this.namedGroups, this.namedLiterals);
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,22 +55,26 @@ public class InputSchemaBuilder : RepetitionSchemaBuilder<InputSchemaBuilder, In
|
||||
{
|
||||
throw new Exception("Invalid repetition definitions!");
|
||||
}
|
||||
var oldInputSchemaBuilder = currentBuilder.UpperLayerBuilder;
|
||||
var oldSchemaBuilder = currentBuilder.UpperLayerBuilder;
|
||||
if (oldSchemaBuilder == null)
|
||||
{
|
||||
throw new Exception("Something went terribly wrong!");
|
||||
}
|
||||
|
||||
var currentSchema = currentBuilder.Build();
|
||||
switch (currentBuilder.RepetitionType)
|
||||
{
|
||||
case RepetitionType.FixedRepetition:
|
||||
oldInputSchemaBuilder.schema.AddBuildingBlock(new FixedRepetitionBlock(currentSchema, currentBuilder.NumRepetition));
|
||||
oldSchemaBuilder.schema.AddBuildingBlock(new FixedRepetitionBlock(currentSchema, currentBuilder.NumRepetition));
|
||||
break;
|
||||
case RepetitionType.GreedyRepetition:
|
||||
oldInputSchemaBuilder.schema.AddBuildingBlock(new GreedyRepetitionBlock(currentSchema));
|
||||
oldSchemaBuilder.schema.AddBuildingBlock(new GreedyRepetitionBlock(currentSchema));
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Unrecognized RepetitionType");
|
||||
}
|
||||
|
||||
return oldInputSchemaBuilder;
|
||||
return oldSchemaBuilder;
|
||||
}
|
||||
|
||||
public InputSchema Build()
|
||||
|
||||
@@ -5,6 +5,14 @@ using System.Collections.Generic;
|
||||
using Parsing.Schema;
|
||||
using Parsing.Tokenization;
|
||||
|
||||
public static class TextParser
|
||||
{
|
||||
public static TextParser<TContext> Create<TContext>(ISchema<TContext> schema, string[]? delimiters = null, bool removeEmptyEntries = true) where TContext : ISchemaContext
|
||||
{
|
||||
return new TextParser<TContext>(schema, delimiters, removeEmptyEntries);
|
||||
}
|
||||
}
|
||||
|
||||
public class TextParser<T> : TokenConverter where T : ISchemaContext
|
||||
{
|
||||
private LineParser<T> lineParser;
|
||||
|
||||
@@ -56,7 +56,7 @@ public static class DataManipulationHelpers
|
||||
{
|
||||
if (data.Count < 2)
|
||||
{
|
||||
return data[0] ?? default(TType);
|
||||
return data[0];
|
||||
}
|
||||
TType result = data[0];
|
||||
for (int i = 1; i < data.Count; i++)
|
||||
|
||||
@@ -1 +1 @@
|
||||
0.5.0
|
||||
0.6.0
|
||||
|
||||
Reference in New Issue
Block a user