99 lines
2.2 KiB
C#
99 lines
2.2 KiB
C#
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();
|
|
}
|
|
}
|