Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f533c2018 | |||
| 2067fe06fc | |||
| cc0f0a24d9 | |||
| c41d665ab8 | |||
| 2fbdafa0e9 | |||
| f942954678 | |||
| 4c67e8efb0 | |||
| e83e99758a | |||
| 81ac797b4c | |||
| e9aa60524c | |||
| 7e5ab9f799 | |||
| fc137ebd03 |
57
HISTORY.md
57
HISTORY.md
@@ -5,11 +5,68 @@ Changelog
|
||||
(unreleased)
|
||||
------------
|
||||
|
||||
Fix
|
||||
~~~
|
||||
- Remove duplicate TokenConverter definition, ref: NOISSUE. [Simon
|
||||
Diesenreiter]
|
||||
|
||||
|
||||
0.9.2 (2024-12-13)
|
||||
------------------
|
||||
|
||||
Fix
|
||||
~~~
|
||||
- More bugfixes, ref: NOISSUE. [Simon Diesenreiter]
|
||||
|
||||
Other
|
||||
~~~~~
|
||||
|
||||
|
||||
0.9.1 (2024-12-13)
|
||||
------------------
|
||||
|
||||
Fix
|
||||
~~~
|
||||
- Fix build issues, ref: NOISSUE. [Simon Diesenreiter]
|
||||
|
||||
Other
|
||||
~~~~~
|
||||
|
||||
|
||||
0.9.0 (2024-12-13)
|
||||
------------------
|
||||
- Feat: add filter option to TokenConverter, ref: NOISSUE. [Simon
|
||||
Diesenreiter]
|
||||
|
||||
|
||||
0.8.0 (2024-12-12)
|
||||
------------------
|
||||
- Feat: adding sensible index constructors refs: NOISSUE. [Simon
|
||||
Diesenreiter]
|
||||
|
||||
|
||||
0.7.2 (2024-12-05)
|
||||
------------------
|
||||
|
||||
Fix
|
||||
~~~
|
||||
- Add some missing API methods, ref: NOISSUE. [Simon Diesenreiter]
|
||||
|
||||
Other
|
||||
~~~~~
|
||||
|
||||
|
||||
0.7.1 (2024-12-05)
|
||||
------------------
|
||||
|
||||
Fix
|
||||
~~~
|
||||
- Allow for parsing single chars as input, ref: NOISSUE. [Simon
|
||||
Diesenreiter]
|
||||
|
||||
Other
|
||||
~~~~~
|
||||
|
||||
|
||||
0.7.0 (2024-12-05)
|
||||
------------------
|
||||
|
||||
@@ -33,6 +33,7 @@ public class TextParserTests
|
||||
private const string testInput10 = @"abc
|
||||
bca
|
||||
cab";
|
||||
private const string testInput11 = @"2 x y 4 x y 6 x y 4 x y 1 x y";
|
||||
|
||||
[Fact]
|
||||
public void LineParser_TestSimpleRepetition()
|
||||
@@ -394,7 +395,7 @@ public class TextParserTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TextPArser_TestReadingChars()
|
||||
public void TextParser_TestReadingChars()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
@@ -415,4 +416,31 @@ public class TextParserTests
|
||||
Assert.Equal(3, row[1].Count);
|
||||
Assert.Equal(3, row[2].Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TextParser_TestFilter()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.Repeat()
|
||||
.Expect(InputType.Integer)
|
||||
.Expect(InputType.Char)
|
||||
.Expect(InputType.Char)
|
||||
.EndRepetition()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser<InputSchemaContext>(schema);
|
||||
var numbers = parser
|
||||
.SetInputText(testInput11)
|
||||
.Parse()
|
||||
.Filter(InputType.Integer)
|
||||
.AsSingleStream<int>();
|
||||
|
||||
Assert.Equal(5, numbers.Count);
|
||||
Assert.Equal(2, numbers[0]);
|
||||
Assert.Equal(4, numbers[1]);
|
||||
Assert.Equal(6, numbers[2]);
|
||||
Assert.Equal(4, numbers[3]);
|
||||
Assert.Equal(1, numbers[4]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,24 @@ public static class DataManipulationHelpers
|
||||
{
|
||||
return reducer(data);
|
||||
}
|
||||
|
||||
public static List<TNewType> TransformData<TType, TNewType>(this List<TType> data, Func<TType, TNewType> transformer)
|
||||
{
|
||||
var newList = new List<TNewType>();
|
||||
foreach (TType dataItem in data)
|
||||
{
|
||||
newList.Add(transformer(dataItem));
|
||||
}
|
||||
return newList;
|
||||
}
|
||||
|
||||
public static List<TNewType> TransformData<TType, TNewType>(this List<List<TType>> data, Func<List<TType>, TNewType> transformer)
|
||||
{
|
||||
var newList = new List<TNewType>();
|
||||
foreach (List<TType> dataItemList in data)
|
||||
{
|
||||
newList.Add(transformer(dataItemList));
|
||||
}
|
||||
return newList;
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,22 @@ namespace Parsing.Data;
|
||||
|
||||
public class SearchResult<TIndexType>
|
||||
{
|
||||
public SearchResult(IDataIndex<TIndexType> dataIndex)
|
||||
{
|
||||
this.DataIndex = dataIndex;
|
||||
}
|
||||
|
||||
public IDataIndex<TIndexType>? DataIndex { get; set; }
|
||||
}
|
||||
|
||||
public class DirectionalSearchResult<TIndexType> : SearchResult<TIndexType>
|
||||
{
|
||||
public DirectionalSearchResult(IDataIndex<TIndexType> dataIndex, Direction direction, int length): base(dataIndex)
|
||||
{
|
||||
this.Direction = direction;
|
||||
this.Length = length;
|
||||
}
|
||||
|
||||
public Direction Direction { get; set; }
|
||||
public int Length { get; set; }
|
||||
}
|
||||
@@ -111,10 +122,7 @@ public abstract class DataSetManipulatorBase<TCollectedType, TDataType, TIndexTy
|
||||
}
|
||||
if (searchIndex == data.Count)
|
||||
{
|
||||
var result = new DirectionalSearchResult<TIndexType>();
|
||||
result.DataIndex = currentPosition;
|
||||
result.Direction = direction;
|
||||
result.Length = searchIndex;
|
||||
var result = new DirectionalSearchResult<TIndexType>(currentPosition, direction, searchIndex);
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
@@ -123,7 +131,7 @@ public abstract class DataSetManipulatorBase<TCollectedType, TDataType, TIndexTy
|
||||
return results;
|
||||
}
|
||||
|
||||
public List<DirectionalSearchResult<TIndexType>> FindInSet(List<TDataType> data)
|
||||
public List<DirectionalSearchResult<TIndexType>> FindInSet(List<TDataType> data, Direction directions)
|
||||
{
|
||||
var result = new List<DirectionalSearchResult<TIndexType>>();
|
||||
|
||||
@@ -131,11 +139,16 @@ public abstract class DataSetManipulatorBase<TCollectedType, TDataType, TIndexTy
|
||||
var startingPoints = this.FindInSet(data[0]);
|
||||
foreach (var startingPoint in startingPoints)
|
||||
{
|
||||
foreach (var results in this.FindAtPosition(startingPoint.DataIndex, data))
|
||||
foreach (var results in this.FindAtPosition(startingPoint.DataIndex, data, directions))
|
||||
{
|
||||
result.AddRange(results);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<DirectionalSearchResult<TIndexType>> FindInSet(List<TDataType> data)
|
||||
{
|
||||
return this.FindInSet(data, this.ValidDirections());
|
||||
}
|
||||
}
|
||||
@@ -49,8 +49,7 @@ public class DefaultOneDimensionalManipulator<TDataType> : DataSetManipulatorBas
|
||||
{
|
||||
if (EqualityComparer<TDataType>.Default.Equals(this.dataSet[i], data))
|
||||
{
|
||||
var singleResult = new SearchResult<int>();
|
||||
singleResult.DataIndex = new DefaultPositionalDataIndex(i);
|
||||
var singleResult = new SearchResult<int>(new DefaultPositionalDataIndex(i));
|
||||
results.Add(singleResult);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,8 +71,7 @@ public class DefaultTwoDimensionalManipulator<TDataType> : DataSetManipulatorBas
|
||||
{
|
||||
if (EqualityComparer<TDataType>.Default.Equals(this.dataSet[this.dataSet.Count - y - 1][x], data))
|
||||
{
|
||||
var singleResult = new SearchResult<int>();
|
||||
singleResult.DataIndex = new DefaultPositionalDataIndex(x, y);
|
||||
var singleResult = new SearchResult<int>(new DefaultPositionalDataIndex(x, y));
|
||||
results.Add(singleResult);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,4 +156,26 @@ public class TokenConverter
|
||||
|
||||
return newList;
|
||||
}
|
||||
|
||||
public TokenConverter Filter(params InputType[] inputTypes)
|
||||
{
|
||||
var newTokenListList = new List<List<IToken>>();
|
||||
|
||||
foreach(var tokenList in rawTokens)
|
||||
{
|
||||
var newTokenList = new List<IToken>();
|
||||
foreach(var token in tokenList)
|
||||
{
|
||||
if(inputTypes.Contains(token.GetInputType()))
|
||||
{
|
||||
newTokenList.Add(token);
|
||||
}
|
||||
}
|
||||
newTokenListList.Add(newTokenList);
|
||||
}
|
||||
|
||||
this.rawTokens = newTokenListList;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,226 +0,0 @@
|
||||
namespace Parsing;
|
||||
|
||||
using System;
|
||||
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>>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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>();
|
||||
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>()
|
||||
{
|
||||
this.CheckConversionPrerequisites();
|
||||
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>()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
0.7.1
|
||||
0.9.3
|
||||
|
||||
Reference in New Issue
Block a user