14 Commits
0.2.2 ... 0.6.0

Author SHA1 Message Date
2ed103abbf release: version 0.6.0 🚀
Some checks failed
Upload Python Package / Create Release (push) Successful in 14s
Upload Python Package / deploy (push) Failing after 1m24s
2024-12-03 23:09:15 +01:00
fab5d2eee7 feat: enable named literals, ref: NOISSUE 2024-12-03 23:09:08 +01:00
7be09140e2 release: version 0.5.1 🚀
Some checks failed
Upload Python Package / Create Release (push) Successful in 14s
Upload Python Package / deploy (push) Failing after 4m28s
2024-12-03 21:32:05 +01:00
09bbba1293 fix: some bugfixes with fragment parser logic, ref: NOISSUE 2024-12-03 21:32:02 +01:00
c1705d9f96 release: version 0.5.0 🚀
Some checks failed
Upload Python Package / Create Release (push) Successful in 14s
Upload Python Package / deploy (push) Failing after 4m29s
2024-12-03 21:06:09 +01:00
62092d0380 fix: fix bugs with fragment parsing support, refs: NOISSUE 2024-12-03 21:06:00 +01:00
Simon Diesenreiter
29f7aa37da feat: initial support for fragment parsing, ref: NOISSUE 2024-12-03 18:07:55 +01:00
Simon Diesenreiter
5f07f16f1f release: version 0.4.0 🚀
Some checks failed
Upload Python Package / Create Release (push) Successful in 17s
Upload Python Package / deploy (push) Failing after 4m29s
2024-12-02 15:33:30 +01:00
Simon Diesenreiter
d5cfe494d8 style: fix linting errors, ref: NOISSUE 2024-12-02 15:33:11 +01:00
Simon Diesenreiter
0d85132a32 feat: implement greedy repetition, ref: A24-13 2024-12-02 15:30:07 +01:00
Simon Diesenreiter
e15190ecd6 release: version 0.3.0 🚀
Some checks failed
Upload Python Package / Create Release (push) Successful in 17s
Upload Python Package / deploy (push) Failing after 5m59s
2024-12-02 11:54:41 +01:00
Simon Diesenreiter
0dc5fb14c5 feat: ci pipeline fix for releases, ref NOISSUE 2024-12-02 11:54:35 +01:00
Simon Diesenreiter
308489fd01 release: version 0.2.3 🚀
Some checks failed
Upload Python Package / Create Release (push) Successful in 17s
Upload Python Package / deploy (push) Failing after 3m4s
2024-12-02 11:47:43 +01:00
Simon Diesenreiter
6a0addf7a3 fix: more release script fixes,ref: NOISSUE 2024-12-02 11:34:33 +01:00
27 changed files with 879 additions and 64 deletions

View File

@@ -66,17 +66,14 @@ start() {
echo $message
if echo $message | grep -Pq '(feat|style)(\([\w]+\))?!:([a-zA-Z0-9-_!\&\.\%\(\)\=\w\s]+)\s?(,?\s?)((ref(s?):?\s?)(([A-Z0-9]+\-[0-9]+)|(NOISSUE)))'; then
increment_type="major"
echo "a"
break
elif echo $message | grep -Pq '(feat|style)(\([\w]+\))?:([a-zA-Z0-9-_!\&\.\%\(\)\=\w\s]+)\s?(,?\s?)((ref(s?):?\s?)(([A-Z0-9]+\-[0-9]+)|(NOISSUE)))'; then
if [ -z "$increment_type" ] || [ "$increment_type" == "patch" ]; then
increment_type="minor"
echo "b"
fi
elif echo $message | grep -Pq '(build|fix|perf|refactor|revert)(\(.+\))?:\s([a-zA-Z0-9-_!\&\.\%\(\)\=\w\s]+)\s?(,?\s?)((ref(s?):?\s?)(([A-Z0-9]+\-[0-9]+)|(NOISSUE)))'; then
if [ -z "$increment_type" ]; then
increment_type="patch"
echo "c"
fi
fi
done < $TEMP_FILE_PATH/messages.txt
@@ -86,8 +83,9 @@ start() {
echo "New version: $new_version"
gitchangelog | grep -v "[rR]elease:" > HISTORY.md
git add TextParser/VERSION HISTORY.md
echo $new_version > TextParser/VERSION
git add TextParser/VERSION
git add HISTORY.md
git commit -m "release: version $new_version 🚀"
echo "creating git tag : $new_version"
git tag $new_version

View File

@@ -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:

View File

@@ -64,5 +64,5 @@ jobs:
QUOTED_REPOSITORY_SOURCE_NAME=$(dotnet nuget config get all | grep "/packages/${REPOSITORY_OWNER}/nuget/index.json" | awk '{print $2}' | awk -F= '{print $2}')
REPOSITORY_SOURCE_NAME=${QUOTED_REPOSITORY_SOURCE_NAME:1:-1}
fi
dotnet pack --include-symbols --include-source -p:PackageVersion=$(cat $REPOSITORY_NAME/VERSION) DotnetTestLib.sln
dotnet pack --include-symbols --include-source -p:PackageVersion=$(cat $REPOSITORY_NAME/VERSION) TextParser.sln
dotnet nuget push -k $GITEA_PAT --source $REPOSITORY_SOURCE_NAME $REPOSITORY_NAME/bin/Release/$REPOSITORY_NAME.$(cat $REPOSITORY_NAME/VERSION).symbols.nupkg

View File

@@ -4,11 +4,67 @@ 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
~~~
- Fix bugs with fragment parsing support, refs: NOISSUE. [Simon
Diesenreiter]
Other
~~~~~
- Feat: initial support for fragment parsing, ref: NOISSUE. [Simon
Diesenreiter]
0.4.0 (2024-12-02)
------------------
- Style: fix linting errors, ref: NOISSUE. [Simon Diesenreiter]
- Feat: implement greedy repetition, ref: A24-13. [Simon Diesenreiter]
0.3.0 (2024-12-02)
------------------
- Feat: ci pipeline fix for releases, ref NOISSUE. [Simon Diesenreiter]
0.2.3 (2024-12-02)
------------------
Fix
~~~
- More release script fixes,ref: NOISSUE. [Simon Diesenreiter]
Other
~~~~~
0.2.2 (2024-12-02)
------------------
Fix
~~~
- Increment version,refs:NOISSUE. [Simon Diesenreiter]
Other
~~~~~
0.2.1 (2024-12-02)
------------------

View File

@@ -12,6 +12,20 @@ public class TextParserTests
private const string testInput3 = @"2 4 6 1
3 5 7 2
4 6 8 3";
private const string testInput4 = @"2 ab ba fd er sd
8 cd dc
7 uh 6 yp rt";
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()
@@ -23,7 +37,7 @@ public class TextParserTests
.EndRepetition()
.Build();
var parser = new LineParser(schema);
var parser = new LineParser<InputSchemaContext>(schema);
var tokens = parser.ParseLine(testInput1);
Assert.Equal(4, tokens.Count);
@@ -48,7 +62,7 @@ public class TextParserTests
.Expect(InputType.Integer)
.Build();
var parser = new LineParser(schema);
var parser = new LineParser<InputSchemaContext>(schema);
var tokens = parser.ParseLine(testInput1);
Assert.Equal(4, tokens.Count);
@@ -76,7 +90,7 @@ public class TextParserTests
.EndRepetition()
.Build();
var parser = new LineParser(schema);
var parser = new LineParser<InputSchemaContext>(schema);
var tokens = parser.ParseLine(testInput2);
Assert.Equal(6, tokens.Count);
@@ -104,7 +118,7 @@ public class TextParserTests
.EndRepetition()
.Build();
var parser = new TextParser(schema);
var parser = new TextParser<InputSchemaContext>(schema);
var rows = parser
.SetInputText(testInput3)
.Parse()
@@ -136,7 +150,7 @@ public class TextParserTests
.EndRepetition()
.Build();
var parser = new TextParser(schema);
var parser = new TextParser<InputSchemaContext>(schema);
var columns = parser
.SetInputText(testInput3)
.Parse()
@@ -157,4 +171,148 @@ public class TextParserTests
Assert.Equal(2, columns[3][1]);
Assert.Equal(3, columns[3][2]);
}
[Fact]
public void TextParser_TestGreedyRepetitionAsRows()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
.Repeat()
.Expect(InputType.Integer)
.Repeat()
.Expect(InputType.String)
.EndRepetition()
.EndRepetition()
.Build();
var parser = new TextParser<InputSchemaContext>(schema);
var rows = parser
.SetInputText(testInput4)
.Parse()
.AsRawData();
Assert.Equal(3, rows.Count);
Assert.Equal(6, rows[0].Count);
Assert.Equal(3, rows[1].Count);
Assert.Equal(5, rows[2].Count);
Assert.Equal(InputType.Integer, rows[0][0].GetInputType());
Assert.Equal(InputType.String, rows[0][1].GetInputType());
Assert.Equal(InputType.String, rows[0][2].GetInputType());
Assert.Equal(InputType.String, rows[0][3].GetInputType());
Assert.Equal(InputType.String, rows[0][4].GetInputType());
Assert.Equal(InputType.String, rows[0][5].GetInputType());
Assert.Equal(InputType.Integer, rows[1][0].GetInputType());
Assert.Equal(InputType.String, rows[1][1].GetInputType());
Assert.Equal(InputType.String, rows[1][2].GetInputType());
Assert.Equal(InputType.Integer, rows[2][0].GetInputType());
Assert.Equal(InputType.String, rows[2][1].GetInputType());
Assert.Equal(InputType.Integer, rows[2][2].GetInputType());
Assert.Equal(InputType.String, rows[2][3].GetInputType());
Assert.Equal(InputType.String, rows[2][4].GetInputType());
}
[Fact]
public void FragmentParser_SimpleTest()
{
var schemaBuilder = new FragmentSchemaBuilder();
var schema = schemaBuilder
.StartOptions()
.Option()
.Expect("nums(")
.Expect(InputType.Integer, "num")
.Repeat()
.Expect(",")
.Expect(InputType.Integer, "num")
.EndRepetition()
.Expect(")")
.Option()
.Expect("strs(")
.Expect(InputType.String, "str")
.Repeat()
.Expect(",")
.Expect(InputType.String, "str")
.EndRepetition()
.Expect(")")
.EndOptions()
.Build();
var parser = TextParser.Create(schema);
var fragmentData = parser
.SetInputText(testInput5)
.Parse()
.AsFragments();
var convertedData = fragmentData
.ConvertAll((Fragment f) =>
{
int numSum = 0;
foreach (var numString in f["num"])
{
numSum += int.Parse(numString);
}
return f["num"].Count + f["str"].Count + numSum;
});
Assert.Equal(4, fragmentData.Count);
Assert.Equal(3, fragmentData[0]["num"].Count);
Assert.Single(fragmentData[1]["str"]);
Assert.Equal(2, fragmentData[2]["num"].Count);
Assert.Equal(4, fragmentData[3]["str"].Count);
Assert.Equal(13, convertedData[0]);
Assert.Equal(1, convertedData[1]);
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]);
}
}

View File

@@ -3,14 +3,14 @@ namespace Parsing;
using Parsing.Schema;
using Parsing.Tokenization;
public class LineParser
public class LineParser<T> where T : ISchemaContext
{
private string[] delimiters;
private bool removeEmptyEntries = false;
private InputSchema schema;
private InputSchemaContext context;
private ISchema<T> schema;
private T context;
public LineParser(InputSchema schema, string[]? delimiters = null, bool removeEmptyEntries = true)
public LineParser(ISchema<T> schema, string[]? delimiters = null, bool removeEmptyEntries = true)
{
this.delimiters = delimiters ?? new string[] { " " };
this.removeEmptyEntries = removeEmptyEntries;

View File

@@ -1,6 +1,13 @@
namespace Parsing.Schema;
[Flags]
public enum BlockType
{
Integer, String, FixedRepetition, GreedyRepetition
Integer = 1,
String = 2,
// technically not a block type but keeping it here for consistency/having all types in one place
Fragment = 4,
FixedRepetition = 8,
GreedyRepetition = 16,
NonZeroRepetition = 32,
}

View File

@@ -12,6 +12,8 @@ abstract class BuildingBlockBase : IBuildingBlock
public abstract bool CanParseWord(InputProvider inputs);
public abstract bool CanParseWord(string word);
public abstract BlockType GetBlockType();
public virtual bool IsRepetitionType()
@@ -19,7 +21,7 @@ abstract class BuildingBlockBase : IBuildingBlock
return false;
}
public virtual bool CheckIsDoneParsingAndReset()
public virtual bool CheckIsDoneParsingAndReset(InputProvider inputs)
{
// most blocks are always done parsing after consuming a token
// repetition blocks can consume multiple tokens

View File

@@ -1,6 +1,7 @@
namespace Parsing.Schema.BuildingBlocks;
using System.IO.Pipelines;
using System.Linq;
using Parsing.Tokenization;
class FixedRepetitionBlock : BuildingBlockBase
@@ -30,7 +31,7 @@ class FixedRepetitionBlock : BuildingBlockBase
this.context = this.inputSchema.CreateContext();
}
}
return result;
return result.Single();
}
public override bool CanParseWord(InputProvider inputs)
@@ -48,6 +49,21 @@ class FixedRepetitionBlock : BuildingBlockBase
return result;
}
public override bool CanParseWord(string word)
{
bool result;
if (this.repetitionCount == 0)
{
result = false;
}
else
{
result = inputSchema.CanProcessNextWord(context, word);
}
return result;
}
public override BlockType GetBlockType()
{
return BlockType.FixedRepetition;
@@ -58,7 +74,7 @@ class FixedRepetitionBlock : BuildingBlockBase
return true;
}
public override bool CheckIsDoneParsingAndReset()
public override bool CheckIsDoneParsingAndReset(InputProvider inputs)
{
// we are done parsing once all repetitions are exhausted
var result = this.repetitionCount == 0;

View File

@@ -0,0 +1,58 @@
namespace Parsing.Schema.BuildingBlocks;
using System.IO.Pipelines;
using System.Linq;
using Parsing.Tokenization;
class GreedyRepetitionBlock : BuildingBlockBase
{
private InputSchema inputSchema;
private InputSchemaContext context;
public GreedyRepetitionBlock(InputSchema inputSchema)
{
this.inputSchema = inputSchema;
this.context = this.inputSchema.CreateContext();
}
public override IToken ParseWord(InputProvider inputs)
{
var result = inputSchema.ProcessNextWord(context, inputs);
if (!this.CanParseWord(inputs))
{
this.context = this.inputSchema.CreateContext();
}
return result.Single();
}
public override bool CanParseWord(InputProvider inputs)
{
return inputSchema.CanProcessNextWord(context, inputs) && inputs.CanYieldWord();
}
public override bool CanParseWord(string word)
{
return inputSchema.CanProcessNextWord(context, word);
}
public override BlockType GetBlockType()
{
return BlockType.GreedyRepetition;
}
public override bool IsRepetitionType()
{
return true;
}
public override bool CheckIsDoneParsingAndReset(InputProvider inputs)
{
// we are done parsing greedily once the next token doesn't match anymore
var result = !this.CanParseWord(inputs);
if (result)
{
this.context = this.inputSchema.CreateContext();
}
return result;
}
}

View File

@@ -8,9 +8,11 @@ public interface IBuildingBlock
public bool CanParseWord(InputProvider inputs);
public bool CanParseWord(string word);
public BlockType GetBlockType();
public bool IsRepetitionType();
public bool CheckIsDoneParsingAndReset();
public bool CheckIsDoneParsingAndReset(InputProvider inputs);
}

View File

@@ -18,12 +18,16 @@ class IntegerBlock : BuildingBlockBase
{
using (inputs.GetLookaheadContext())
{
int number = 0;
var success = int.TryParse(inputs.YieldWord(), out number);
return success;
return this.CanParseWord(inputs.YieldWord());
}
}
public override bool CanParseWord(string word)
{
int number = 0;
return int.TryParse(word, out number);
}
public override BlockType GetBlockType()
{
return BlockType.Integer;

View File

@@ -16,6 +16,24 @@ class StringBlock : BuildingBlockBase
public override bool CanParseWord(InputProvider inputs)
{
string word = string.Empty;
using (inputs.GetLookaheadContext())
{
word = inputs.YieldWord();
}
return this.CanParseWord(word);
}
public override bool CanParseWord(string word)
{
// Here we need to ensure we are not matching any non-string tokens, since string can match pretty much anything
IntegerBlock intBlock = new IntegerBlock();
if (intBlock.CanParseWord(word))
{
return false;
}
return true;
}

View File

@@ -0,0 +1,112 @@
namespace Parsing.Schema;
using Parsing.Schema;
using Parsing.Schema.BuildingBlocks;
using Parsing.Tokenization;
using System.Collections;
using System.Text.RegularExpressions;
public class FragmentSchemaContext : ISchemaContext
{
public int lastProcessedBlockIndex { get; set; } = 0;
public bool HasFinished { get; set; } = false;
}
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, List<string> namedLiterals)
{
this.fragmentRegex = fragmentRegex;
this.namedGroups = namedGroups;
this.namedLiterals = namedLiterals;
}
public List<IToken> ProcessNextWord(FragmentSchemaContext currentContext, InputProvider inputs)
{
Regex r = new Regex(this.fragmentRegex);
var tokenList = new List<IToken>();
// one token per match
foreach (Match match in r.Matches(inputs.YieldWord()))
{
var newToken = new FragmentToken(match.Value);
// token contains data from all included matches
foreach (var groupName in this.namedGroups)
{
var captureList = new List<string>();
foreach (Capture capture in match.Groups[groupName].Captures)
{
captureList.Add(capture.Value);
}
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);
}
if (!inputs.CanYieldWord())
{
currentContext.HasFinished = true;
}
return tokenList;
}
public bool CanProcessNextWord(FragmentSchemaContext currentContext, InputProvider inputs)
{
using (inputs.GetLookaheadContext())
{
return this.CanProcessNextWord(currentContext, inputs.YieldWord());
}
}
public bool CanProcessNextWord(FragmentSchemaContext currentContext, string word)
{
if (currentContext.HasFinished)
{
return false;
}
Regex r = new Regex(this.fragmentRegex);
return r.Match(word).Success;
}
public List<IToken> ProcessWordList(string[] words)
{
List<IToken> tokens = new List<IToken>();
InputProvider inputs = new InputProvider(words);
var overallContext = this.CreateContext();
while (!overallContext.HasFinished && inputs.CanYieldWord())
{
if (this.CanProcessNextWord(overallContext, inputs))
{
tokens.AddRange(this.ProcessNextWord(overallContext, inputs));
}
else
{
inputs.SkipCurrentWord();
}
}
overallContext.HasFinished = true;
return tokens;
}
public FragmentSchemaContext CreateContext()
{
return new FragmentSchemaContext();
}
}

View File

@@ -0,0 +1,142 @@
namespace Parsing.Schema;
using Parsing.Schema.BuildingBlocks;
using System.Text.RegularExpressions;
public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuilder, FragmentSchema, FragmentSchemaContext>, ISchemaBuilder<FragmentSchema, FragmentSchemaContext>
{
protected string fragmentRegex = @"";
private List<string> namedGroups = new List<string>();
private List<string> namedLiterals = new List<string>();
public FragmentSchemaBuilder()
{
}
public FragmentSchemaBuilder StartOptions()
{
this.fragmentRegex += "((";
return this;
}
public FragmentSchemaBuilder EndOptions()
{
this.fragmentRegex += "))";
return this;
}
public FragmentSchemaBuilder Option()
{
// if we just started an options group there is no need to add an option separator
if (!this.fragmentRegex.EndsWith("(") || this.fragmentRegex.EndsWith("\\("))
{
this.fragmentRegex += ")|(";
}
return this;
}
public FragmentSchemaBuilder Expect(InputType type, string name = "")
{
string groupNamePrefix = "";
if (!string.IsNullOrEmpty(name))
{
groupNamePrefix = "?<" + name + ">";
namedGroups.Add(name);
}
switch (type)
{
case InputType.String:
this.fragmentRegex += "(" + groupNamePrefix + "\\w+)";
break;
case InputType.Integer:
this.fragmentRegex += "(" + groupNamePrefix + "\\d+)";
break;
default:
throw new Exception("Unrecognized InputType");
}
return this;
}
public FragmentSchemaBuilder Expect(string literal, string name = "")
{
string groupNamePrefix = "";
if (!string.IsNullOrEmpty(name))
{
groupNamePrefix = "?<" + name + ">";
namedLiterals.Add(name);
}
this.fragmentRegex += "(" + groupNamePrefix + Regex.Escape(literal) + ")";
return this;
}
public FragmentSchemaBuilder Repeat(int repetitionCount)
{
// add another layer of parsing
var newSchemaBuilder = this.GetNewRepetitionSchemaBuilder(this);
newSchemaBuilder.NumRepetition = repetitionCount;
newSchemaBuilder.RepetitionType = RepetitionType.FixedRepetition;
return newSchemaBuilder;
}
public FragmentSchemaBuilder Repeat()
{
// add another layer of parsing
var newSchemaBuilder = this.GetNewRepetitionSchemaBuilder(this);
newSchemaBuilder.RepetitionType = RepetitionType.GreedyRepetition;
return newSchemaBuilder;
}
public FragmentSchemaBuilder Repeat(RepetitionType repetitionType)
{
// add another layer of parsing
var newSchemaBuilder = this.GetNewRepetitionSchemaBuilder(this);
newSchemaBuilder.RepetitionType = repetitionType;
return newSchemaBuilder;
}
public FragmentSchemaBuilder EndRepetition()
{
// return back to upper layer of parsing
var currentBuilder = this as FragmentSchemaBuilder;
if (currentBuilder == null)
{
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)
{
case RepetitionType.FixedRepetition:
currentRegex += "{" + this.NumRepetition.ToString() + "}";
break;
case RepetitionType.GreedyRepetition:
currentRegex += "*";
break;
case RepetitionType.NonZeroRepetition:
case RepetitionType.NonZeroRepetition | RepetitionType.GreedyRepetition:
currentRegex += "+";
break;
default:
throw new Exception("Unrecognized RepetitionType");
}
oldSchemaBuilder.fragmentRegex += "(" + currentRegex + ")";
return oldSchemaBuilder;
}
public FragmentSchema Build()
{
var schema = new FragmentSchema(this.fragmentRegex, this.namedGroups, this.namedLiterals);
return schema;
}
}

View File

@@ -0,0 +1,25 @@
namespace Parsing.Schema;
using Parsing.Schema;
using Parsing.Schema.BuildingBlocks;
using Parsing.Tokenization;
using System.Collections;
public interface ISchemaContext
{
public int lastProcessedBlockIndex { get; set; }
public bool HasFinished { get; set; }
}
public interface ISchema<T> where T : ISchemaContext
{
public List<IToken> ProcessNextWord(T currentContext, InputProvider inputs);
public bool CanProcessNextWord(T currentContext, InputProvider inputs);
public bool CanProcessNextWord(T currentContext, string word);
public List<IToken> ProcessWordList(string[] words);
public T CreateContext();
}

View File

@@ -0,0 +1,8 @@
namespace Parsing.Schema;
using Parsing.Schema.BuildingBlocks;
public interface ISchemaBuilder<T, U> where T : ISchema<U> where U : ISchemaContext
{
public T Build();
}

View File

@@ -5,13 +5,13 @@ using Parsing.Schema.BuildingBlocks;
using Parsing.Tokenization;
using System.Collections;
public class InputSchemaContext
public class InputSchemaContext : ISchemaContext
{
public int lastProcessedBlockIndex { get; set; } = 0;
public bool HasFinished { get; set; } = false;
}
public class InputSchema
public class InputSchema : ISchema<InputSchemaContext>
{
private List<IBuildingBlock> buildingBlocks;
@@ -25,16 +25,18 @@ public class InputSchema
this.buildingBlocks.Add(buildingBlock);
}
public IToken ProcessNextWord(InputSchemaContext currentContext, InputProvider inputs)
public List<IToken> ProcessNextWord(InputSchemaContext currentContext, InputProvider inputs)
{
var nextBlock = this.buildingBlocks[currentContext.lastProcessedBlockIndex];
var token = nextBlock.ParseWord(inputs);
if (!nextBlock.IsRepetitionType() || nextBlock.CheckIsDoneParsingAndReset())
if (!nextBlock.IsRepetitionType() || nextBlock.CheckIsDoneParsingAndReset(inputs))
{
currentContext.lastProcessedBlockIndex++;
currentContext.HasFinished = currentContext.lastProcessedBlockIndex >= this.buildingBlocks.Count;
}
return token;
var newTokenList = new List<IToken>();
newTokenList.Add(token);
return newTokenList;
}
public bool CanProcessNextWord(InputSchemaContext currentContext, InputProvider inputs)
@@ -50,6 +52,16 @@ public class InputSchema
}
}
public bool CanProcessNextWord(InputSchemaContext currentContext, string word)
{
if (currentContext.HasFinished)
{
return false;
}
var nextBlock = this.buildingBlocks[currentContext.lastProcessedBlockIndex];
return nextBlock.CanParseWord(word);
}
public List<IToken> ProcessWordList(string[] words)
{
List<IToken> tokens = new List<IToken>();
@@ -58,7 +70,7 @@ public class InputSchema
while (this.CanProcessNextWord(overallContext, inputs))
{
tokens.Add(this.ProcessNextWord(overallContext, inputs));
tokens.AddRange(this.ProcessNextWord(overallContext, inputs));
}
return tokens;

View File

@@ -2,7 +2,7 @@
using Parsing.Schema.BuildingBlocks;
public class InputSchemaBuilder
public class InputSchemaBuilder : RepetitionSchemaBuilder<InputSchemaBuilder, InputSchema, InputSchemaContext>, ISchemaBuilder<InputSchema, InputSchemaContext>
{
private InputSchema schema = new InputSchema();
@@ -31,34 +31,50 @@ public class InputSchemaBuilder
public InputSchemaBuilder Repeat(int repetitionCount)
{
// add another layer of parsing
var newInputSchemaBuilder = new RepetitionSchemaBuilder(this);
var newInputSchemaBuilder = this.GetNewRepetitionSchemaBuilder(this);
newInputSchemaBuilder.NumRepetition = repetitionCount;
newInputSchemaBuilder.RepetitionType = RepetitionType.FixedRepetition;
return newInputSchemaBuilder;
}
public InputSchemaBuilder Repeat()
{
// add another layer of parsing
var newInputSchemaBuilder = this.GetNewRepetitionSchemaBuilder(this);
newInputSchemaBuilder.RepetitionType = RepetitionType.GreedyRepetition;
return newInputSchemaBuilder;
}
public InputSchemaBuilder EndRepetition()
{
// return back to upper layer of parsing
var currentBuilder = this as RepetitionSchemaBuilder;
var currentBuilder = this as InputSchemaBuilder;
if (currentBuilder == null)
{
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:
oldSchemaBuilder.schema.AddBuildingBlock(new GreedyRepetitionBlock(currentSchema));
break;
default:
throw new Exception("Unrecognized RepetitionType");
}
return oldInputSchemaBuilder;
return oldSchemaBuilder;
}
public InputSchema Build()

View File

@@ -1,7 +1,9 @@
namespace Parsing.Schema;
[Flags]
public enum InputType
{
Integer = BlockType.Integer,
String = BlockType.String
String = BlockType.String,
Fragment = BlockType.Fragment,
}

View File

@@ -1,13 +1,15 @@
namespace Parsing.Schema;
public class RepetitionSchemaBuilder : InputSchemaBuilder
public abstract class RepetitionSchemaBuilder<S, T, U> where S : RepetitionSchemaBuilder<S, T, U>, ISchemaBuilder<T, U>, new() where T : ISchema<U> where U : ISchemaContext
{
public RepetitionSchemaBuilder(InputSchemaBuilder upperLayerBuilder)
public S GetNewRepetitionSchemaBuilder(S upperLayerBuilder)
{
this.UpperLayerBuilder = upperLayerBuilder;
var newBuilder = new S();
newBuilder.UpperLayerBuilder = upperLayerBuilder;
return newBuilder;
}
public InputSchemaBuilder UpperLayerBuilder { get; set; }
public S? UpperLayerBuilder { get; set; }
public int NumRepetition { get; set; }

View File

@@ -1,7 +1,9 @@
namespace Parsing.Schema;
[Flags]
public enum RepetitionType
{
FixedRepetition = BlockType.FixedRepetition,
GreedyRepetition = BlockType.GreedyRepetition
GreedyRepetition = BlockType.GreedyRepetition,
NonZeroRepetition = BlockType.NonZeroRepetition,
}

View File

@@ -5,20 +5,28 @@ using System.Collections.Generic;
using Parsing.Schema;
using Parsing.Tokenization;
public class TextParser : TokenConverter
public static class TextParser
{
private LineParser lineParser;
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;
private string[] lines;
private bool removeEmptyEntries;
public TextParser(InputSchema schema, string[]? delimiters = null, bool removeEmptyEntries = true) : base()
public TextParser(ISchema<T> schema, string[]? delimiters = null, bool removeEmptyEntries = true) : base()
{
this.lineParser = new LineParser(schema, delimiters, removeEmptyEntries);
this.lineParser = new LineParser<T>(schema, delimiters, removeEmptyEntries);
this.lines = new string[] { };
this.removeEmptyEntries = removeEmptyEntries;
}
public TextParser SetInputText(string text)
public TextParser<T> SetInputText(string text)
{
var options = StringSplitOptions.TrimEntries;
if (removeEmptyEntries)
@@ -29,7 +37,7 @@ public class TextParser : TokenConverter
return this;
}
public TextParser Parse()
public TextParser<T> Parse()
{
foreach (var line in this.lines)
{

View File

@@ -5,6 +5,73 @@ using System.Collections.Generic;
using Parsing.Schema;
using Parsing.Tokenization;
public static class DataConversionHelpers
{
public static List<TNewType> ConvertData<TTokenType, TNewType, TOldType>(this List<IToken> tokenList, Func<TOldType, TNewType> converter) where TTokenType : IValueToken<TOldType>
{
var newList = new List<TNewType>();
foreach (var token in tokenList)
{
var typedToken = token as IValueToken<TOldType>;
if (typedToken == null)
{
throw new Exception("Invalid Token type encountered during value conversion");
}
newList.Add(converter(typedToken.GetValue()));
}
return newList;
}
public static List<TNewType> ConvertData<TTokenType, TNewType, TOldType>(this List<IToken> tokenList, Func<TOldType, List<TNewType>> converter) where TTokenType : IValueToken<TOldType>
{
var newList = new List<TNewType>();
foreach (var token in tokenList)
{
var typedToken = token as IValueToken<TOldType>;
if (typedToken == null)
{
throw new Exception("Invalid Token type encountered during value conversion");
}
newList.AddRange(converter(typedToken.GetValue()));
}
return newList;
}
public static List<List<TNewType>> ConvertData<TTokenType, TNewType, TOldType>(this List<List<IToken>> tokenListList, Func<TOldType, TNewType> converter) where TTokenType : IValueToken<TOldType>
{
var newListList = new List<List<TNewType>>();
foreach (var tokenList in tokenListList)
{
newListList.Add(tokenList.ConvertData<TTokenType, TNewType, TOldType>(converter));
}
return newListList;
}
}
public static class DataManipulationHelpers
{
public static TType ReduceData<TType>(this List<TType> data, Func<TType, TType, TType> reducer)
{
if (data.Count < 2)
{
return data[0];
}
TType result = data[0];
for (int i = 1; i < data.Count; i++)
{
result = reducer(result, data[i]);
}
return result;
}
public static TType ReduceData<TType>(this List<TType> data, Func<List<TType>, TType> reducer)
{
return reducer(data);
}
}
public class TokenConverter
{
protected List<List<IToken>> rawTokens = new List<List<IToken>>();
@@ -38,6 +105,42 @@ public class TokenConverter
return returnData;
}
private void CheckConversionPrerequisites()
{
// in order to convert rows to columns or grid we require every row to have the same length
int rowLength = this.rawTokens[0].Count;
foreach (var tokenRow in this.rawTokens)
{
if (tokenRow.Count != rowLength)
{
throw new Exception("Attempted to convert token dataset that is not able to be converted!");
}
}
}
public List<T> AsSingleStream<T>()
{
List<T> returnData = new List<T>();
foreach (var tokenRow in this.rawTokens)
{
foreach (IToken token in tokenRow)
{
if (token == null)
{
throw new Exception("No token was provided, but token was expected!");
}
IValueToken<T>? valueToken = token as IValueToken<T>;
if (valueToken == null)
{
throw new Exception("Provided token is not a ValueToken");
}
returnData.Add(valueToken.GetValue());
}
}
return returnData;
}
public List<T[]> AsRows<T>()
{
var listRows = this.AsListRows<T>();
@@ -71,6 +174,7 @@ public class TokenConverter
public List<List<T>> AsListColumns<T>()
{
this.CheckConversionPrerequisites();
var rows = AsListRows<T>();
var columns = new List<List<T>>();
@@ -92,7 +196,31 @@ public class TokenConverter
public T[][] AsGrid<T>()
{
this.CheckConversionPrerequisites();
var rowsList = AsRows<T>();
return rowsList.ToArray();
}
public List<List<IToken>> AsRawData()
{
return this.rawTokens;
}
public List<Fragment> AsFragments()
{
var items = this.AsSingleStream<Fragment>();
var newList = new List<Fragment>();
foreach (var item in items)
{
var typedItem = item as Fragment;
if (typedItem == null)
{
throw new Exception("Invalid token type encountered");
}
newList.Add(typedItem);
}
return newList;
}
}

View File

@@ -0,0 +1,39 @@
namespace Parsing.Tokenization;
using System.Runtime.CompilerServices;
using Parsing.Schema;
public class Fragment : Dictionary<string, List<string>>
{
}
public class FragmentToken : IValueToken<Fragment>
{
private string word;
private Fragment matches = new Fragment();
public FragmentToken(string word)
{
this.word = word;
}
public string GetText()
{
return word;
}
public void AddMatch(string name, List<string> values)
{
this.matches.Add(name, values);
}
public Fragment GetValue()
{
return this.matches;
}
public InputType GetInputType()
{
return InputType.Fragment;
}
}

View File

@@ -32,14 +32,30 @@ public class InputProvider
this.CurrentPosition = 0;
}
public InputProvider(string text)
{
this.words = text.Split("\n");
this.CurrentPosition = 0;
}
public InputProvider.LookaheadContext GetLookaheadContext()
{
return new InputProvider.LookaheadContext(this);
}
public bool CanYieldWord()
{
return this.CurrentPosition < this.words.Length;
}
public void SkipCurrentWord()
{
this.CurrentPosition += 1;
}
public string YieldWord()
{
if (this.CurrentPosition > this.words.Length)
if (!this.CanYieldWord())
{
return string.Empty;
}

View File

@@ -1 +1 @@
0.2.1
0.6.0