generated from Templates/Dotnet_Library
227 lines
6.2 KiB
C#
227 lines
6.2 KiB
C#
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;
|
|
}
|
|
}
|