Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75af8ebc69 | ||
|
|
6d4b9315f4 | ||
|
|
263be78318 | ||
|
|
b74a8f5212 | ||
| 8707e0da3a |
@@ -46,7 +46,7 @@ create_file() {
|
||||
}
|
||||
|
||||
get_commit_range() {
|
||||
rm $TEMP_FILE_PATH/messages.txt
|
||||
rm -f $TEMP_FILE_PATH/messages.txt
|
||||
if [[ $LAST_TAG =~ $PATTERN ]]; then
|
||||
create_file true
|
||||
else
|
||||
@@ -86,8 +86,8 @@ start() {
|
||||
echo "New version: $new_version"
|
||||
|
||||
gitchangelog | grep -v "[rR]elease:" > HISTORY.md
|
||||
git add DotnetTestLib/VERSION HISTORY.md
|
||||
echo $new_version > DotnetTestLib/VERSION
|
||||
git add TextParser/VERSION HISTORY.md
|
||||
echo $new_version > TextParser/VERSION
|
||||
git commit -m "release: version $new_version 🚀"
|
||||
echo "creating git tag : $new_version"
|
||||
git tag $new_version
|
||||
|
||||
11
HISTORY.md
11
HISTORY.md
@@ -1,11 +0,0 @@
|
||||
Changelog
|
||||
=========
|
||||
|
||||
|
||||
(unreleased)
|
||||
------------
|
||||
- More bugfixes. [Simon Diesenreiter]
|
||||
- Fix linting errors. [Simon Diesenreiter]
|
||||
- Initial commit. [Simon Diesenreiter]
|
||||
|
||||
|
||||
|
||||
12
Makefile
12
Makefile
@@ -2,7 +2,7 @@
|
||||
|
||||
.PHONY: issetup
|
||||
issetup:
|
||||
@[ -f .git/hooks/commit-msg ] || [ -v SKIP_MAKE_SETUP_CHECK ] || (echo "You must run 'make setup' first to initialize the repo!" && exit 1)
|
||||
@[ -f .git/hooks/commit-msg ] || [ -n SKIP_MAKE_SETUP_CHECK ] || (echo "You must run 'make setup' first to initialize the repo!" && exit 1)
|
||||
|
||||
.PHONY: setup
|
||||
setup:
|
||||
@@ -45,15 +45,7 @@ clean: issetup ## Clean unused files.
|
||||
|
||||
.PHONY: release
|
||||
release: issetup ## Create a new tag for release.
|
||||
@echo "WARNING: This operation will create a version tag and push to gitea"
|
||||
@read -p "Version? (provide the next x.y.z semver) : " TAG; echo "$$TAG" > TextParser/VERSION
|
||||
@gitchangelog > HISTORY.md
|
||||
@git add TextParser/VERSION HISTORY.md
|
||||
@git commit -m "release: version $$(cat TextParser/VERSION) 🚀"
|
||||
@echo "creating git tag : $$(cat TextParser/VERSION)"
|
||||
@git tag $$(cat TextParser/VERSION)
|
||||
@git push -u origin HEAD --tags
|
||||
@echo "Gitea Actions will detect the new tag and release the new version."
|
||||
@./.gitea/conventional_commits/generate-version.sh
|
||||
|
||||
.PHONY: docs
|
||||
docs: issetup ## Build the documentation.
|
||||
|
||||
@@ -9,9 +9,12 @@ public class TextParserTests
|
||||
{
|
||||
private const string testInput1 = "2 4 6 8";
|
||||
private const string testInput2 = "2 ab ba 8 cd dc";
|
||||
private const string testInput3 = @"2 4 6 1
|
||||
3 5 7 2
|
||||
4 6 8 3";
|
||||
|
||||
[Fact]
|
||||
public void TestSimpleRepetition()
|
||||
public void LineParser_TestSimpleRepetition()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
@@ -20,7 +23,7 @@ public class TextParserTests
|
||||
.EndRepetition()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(schema);
|
||||
var parser = new LineParser(schema);
|
||||
var tokens = parser.ParseLine(testInput1);
|
||||
|
||||
Assert.Equal(4, tokens.Count);
|
||||
@@ -35,7 +38,7 @@ public class TextParserTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestSimpleInput()
|
||||
public void LineParser_TestSimpleInput()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
@@ -45,7 +48,7 @@ public class TextParserTests
|
||||
.Expect(InputType.Integer)
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(schema);
|
||||
var parser = new LineParser(schema);
|
||||
var tokens = parser.ParseLine(testInput1);
|
||||
|
||||
Assert.Equal(4, tokens.Count);
|
||||
@@ -57,11 +60,11 @@ public class TextParserTests
|
||||
Assert.Equal(4, (tokens[1] as IntegerToken)?.GetValue());
|
||||
Assert.Equal(6, (tokens[2] as IntegerToken)?.GetValue());
|
||||
Assert.Equal(8, (tokens[3] as IntegerToken)?.GetValue());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void TestNestedRepetition()
|
||||
public void LineParser_TestNestedRepetition()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
@@ -73,7 +76,7 @@ public class TextParserTests
|
||||
.EndRepetition()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(schema);
|
||||
var parser = new LineParser(schema);
|
||||
var tokens = parser.ParseLine(testInput2);
|
||||
|
||||
Assert.Equal(6, tokens.Count);
|
||||
@@ -90,4 +93,68 @@ public class TextParserTests
|
||||
Assert.Equal("cd", (tokens[4] as StringToken)?.GetValue());
|
||||
Assert.Equal("dc", (tokens[5] as StringToken)?.GetValue());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TextParser_TestRepetitionAsRows()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.Repeat(4)
|
||||
.Expect(InputType.Integer)
|
||||
.EndRepetition()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(schema);
|
||||
var rows = parser
|
||||
.SetInputText(testInput3)
|
||||
.Parse()
|
||||
.AsRows<int>();
|
||||
|
||||
Assert.Equal(3, rows.Count);
|
||||
Assert.Equal(4, rows[0].Length);
|
||||
Assert.Equal(2, rows[0][0]);
|
||||
Assert.Equal(4, rows[0][1]);
|
||||
Assert.Equal(6, rows[0][2]);
|
||||
Assert.Equal(1, rows[0][3]);
|
||||
Assert.Equal(3, rows[1][0]);
|
||||
Assert.Equal(5, rows[1][1]);
|
||||
Assert.Equal(7, rows[1][2]);
|
||||
Assert.Equal(2, rows[1][3]);
|
||||
Assert.Equal(4, rows[2][0]);
|
||||
Assert.Equal(6, rows[2][1]);
|
||||
Assert.Equal(8, rows[2][2]);
|
||||
Assert.Equal(3, rows[2][3]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TextParser_TestRepetitionAsColumns()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.Repeat(4)
|
||||
.Expect(InputType.Integer)
|
||||
.EndRepetition()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(schema);
|
||||
var columns = parser
|
||||
.SetInputText(testInput3)
|
||||
.Parse()
|
||||
.AsColumns<int>();
|
||||
|
||||
Assert.Equal(4, columns.Count);
|
||||
Assert.Equal(3, columns[0].Length);
|
||||
Assert.Equal(2, columns[0][0]);
|
||||
Assert.Equal(3, columns[0][1]);
|
||||
Assert.Equal(4, columns[0][2]);
|
||||
Assert.Equal(4, columns[1][0]);
|
||||
Assert.Equal(5, columns[1][1]);
|
||||
Assert.Equal(6, columns[1][2]);
|
||||
Assert.Equal(6, columns[2][0]);
|
||||
Assert.Equal(7, columns[2][1]);
|
||||
Assert.Equal(8, columns[2][2]);
|
||||
Assert.Equal(1, columns[3][0]);
|
||||
Assert.Equal(2, columns[3][1]);
|
||||
Assert.Equal(3, columns[3][2]);
|
||||
}
|
||||
}
|
||||
|
||||
37
TextParser/LineParser.cs
Normal file
37
TextParser/LineParser.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace Parsing;
|
||||
|
||||
using Parsing.Schema;
|
||||
using Parsing.Tokenization;
|
||||
|
||||
public class LineParser
|
||||
{
|
||||
private string[] delimiters;
|
||||
private bool removeEmptyEntries = false;
|
||||
private InputSchema schema;
|
||||
private InputSchemaContext context;
|
||||
|
||||
public LineParser(InputSchema schema, string[]? delimiters = null, bool removeEmptyEntries = true)
|
||||
{
|
||||
this.delimiters = delimiters ?? new string[] { " " };
|
||||
this.removeEmptyEntries = removeEmptyEntries;
|
||||
this.schema = schema;
|
||||
this.context = this.schema.CreateContext();
|
||||
}
|
||||
|
||||
private string[] ParseLineIntoWords(string line)
|
||||
{
|
||||
var options = StringSplitOptions.TrimEntries;
|
||||
if (this.removeEmptyEntries)
|
||||
{
|
||||
options = options | StringSplitOptions.RemoveEmptyEntries;
|
||||
}
|
||||
return line.Split(this.delimiters, options);
|
||||
}
|
||||
|
||||
public List<IToken> ParseLine(string line)
|
||||
{
|
||||
this.context = this.schema.CreateContext();
|
||||
var words = this.ParseLineIntoWords(line);
|
||||
return this.schema.ProcessWordList(words);
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public class InputSchema
|
||||
}
|
||||
}
|
||||
|
||||
public IList<IToken> ProcessWordList(string[] words)
|
||||
public List<IToken> ProcessWordList(string[] words)
|
||||
{
|
||||
List<IToken> tokens = new List<IToken>();
|
||||
InputProvider inputs = new InputProvider(words);
|
||||
|
||||
@@ -1,36 +1,40 @@
|
||||
namespace Parsing;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Parsing.Schema;
|
||||
using Parsing.Tokenization;
|
||||
|
||||
public class TextParser
|
||||
public class TextParser : TokenConverter
|
||||
{
|
||||
private string[] delimiters;
|
||||
private bool removeEmptyEntries = false;
|
||||
private InputSchema schema;
|
||||
private InputSchemaContext context;
|
||||
private LineParser lineParser;
|
||||
private string[] lines;
|
||||
private bool removeEmptyEntries;
|
||||
|
||||
public TextParser(InputSchema schema, string[]? delimiters = null, bool removeEmptyEntries = true)
|
||||
public TextParser(InputSchema schema, string[]? delimiters = null, bool removeEmptyEntries = true) : base()
|
||||
{
|
||||
this.delimiters = delimiters ?? new string[] { " " };
|
||||
this.lineParser = new LineParser(schema, delimiters, removeEmptyEntries);
|
||||
this.lines = new string[] { };
|
||||
this.removeEmptyEntries = removeEmptyEntries;
|
||||
this.schema = schema;
|
||||
this.context = this.schema.CreateContext();
|
||||
}
|
||||
|
||||
private string[] ParseLineIntoWords(string line)
|
||||
public TextParser SetInputText(string text)
|
||||
{
|
||||
var options = StringSplitOptions.TrimEntries;
|
||||
if (this.removeEmptyEntries)
|
||||
if (removeEmptyEntries)
|
||||
{
|
||||
options = options | StringSplitOptions.RemoveEmptyEntries;
|
||||
}
|
||||
return line.Split(this.delimiters, options);
|
||||
this.lines = text.Split("\n", options);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IList<IToken> ParseLine(string line)
|
||||
public TextParser Parse()
|
||||
{
|
||||
var words = this.ParseLineIntoWords(line);
|
||||
return this.schema.ProcessWordList(words);
|
||||
foreach (var line in this.lines)
|
||||
{
|
||||
this.rawTokens.Add(this.lineParser.ParseLine(line));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
98
TextParser/TokenConverter.cs
Normal file
98
TextParser/TokenConverter.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
namespace Parsing;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Parsing.Schema;
|
||||
using Parsing.Tokenization;
|
||||
|
||||
public class TokenConverter
|
||||
{
|
||||
protected List<List<IToken>> rawTokens = new List<List<IToken>>();
|
||||
|
||||
public TokenConverter()
|
||||
{
|
||||
}
|
||||
|
||||
private List<T> AsGenericCollection<T, U>() where T : ICollection<U>, new()
|
||||
{
|
||||
List<T> returnData = new List<T>();
|
||||
foreach (var tokenRow in this.rawTokens)
|
||||
{
|
||||
T newRow = new T();
|
||||
foreach (IToken token in tokenRow)
|
||||
{
|
||||
if (token == null)
|
||||
{
|
||||
throw new Exception("No token was provided, but token was expected!");
|
||||
}
|
||||
IValueToken<U>? valueToken = token as IValueToken<U>;
|
||||
if (valueToken == null)
|
||||
{
|
||||
throw new Exception("Provided token is not a ValueToken");
|
||||
}
|
||||
newRow.Add(valueToken.GetValue());
|
||||
}
|
||||
|
||||
returnData.Add(newRow);
|
||||
}
|
||||
return returnData;
|
||||
}
|
||||
|
||||
public List<T[]> AsRows<T>()
|
||||
{
|
||||
var listRows = this.AsListRows<T>();
|
||||
var newList = new List<T[]>();
|
||||
|
||||
foreach (var rowList in listRows)
|
||||
{
|
||||
newList.Add(rowList.ToArray());
|
||||
}
|
||||
|
||||
return newList;
|
||||
}
|
||||
|
||||
public List<List<T>> AsListRows<T>()
|
||||
{
|
||||
return this.AsGenericCollection<List<T>, T>();
|
||||
}
|
||||
|
||||
public List<T[]> AsColumns<T>()
|
||||
{
|
||||
var listColumns = this.AsListColumns<T>();
|
||||
var newList = new List<T[]>();
|
||||
|
||||
foreach (var columnList in listColumns)
|
||||
{
|
||||
newList.Add(columnList.ToArray());
|
||||
}
|
||||
|
||||
return newList;
|
||||
}
|
||||
|
||||
public List<List<T>> AsListColumns<T>()
|
||||
{
|
||||
var rows = AsListRows<T>();
|
||||
|
||||
var columns = new List<List<T>>();
|
||||
for (int i = 0; i < rows[0].Count; i++)
|
||||
{
|
||||
columns.Add(new List<T>());
|
||||
}
|
||||
|
||||
foreach (var row in rows)
|
||||
{
|
||||
for (int i = 0; i < row.Count; i++)
|
||||
{
|
||||
columns[i].Add(row[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
|
||||
public T[][] AsGrid<T>()
|
||||
{
|
||||
var rowsList = AsRows<T>();
|
||||
return rowsList.ToArray();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace Parsing.Tokenization;
|
||||
using System;
|
||||
|
||||
namespace Parsing.Tokenization;
|
||||
|
||||
public interface IValueToken<T> : IToken
|
||||
{
|
||||
|
||||
@@ -39,11 +39,6 @@ public class InputProvider
|
||||
|
||||
public string YieldWord()
|
||||
{
|
||||
Console.WriteLine("current words:");
|
||||
foreach (var word in words)
|
||||
{
|
||||
Console.WriteLine(word);
|
||||
}
|
||||
if (this.CurrentPosition > this.words.Length)
|
||||
{
|
||||
return string.Empty;
|
||||
|
||||
@@ -1 +1 @@
|
||||
0.1.1
|
||||
0.2.0
|
||||
|
||||
Reference in New Issue
Block a user