From 2067fe06fc188040f3d419f89ac2493557e1c6f7 Mon Sep 17 00:00:00 2001 From: Simon Diesenreiter Date: Fri, 13 Dec 2024 13:50:01 +0100 Subject: [PATCH] fix: remove duplicate TokenConverter definition, ref: NOISSUE --- TextParser.Tests/TextParserTests.cs | 30 +++- TextParser/TokenConverter.cs | 226 ---------------------------- 2 files changed, 29 insertions(+), 227 deletions(-) delete mode 100644 TextParser/TokenConverter.cs diff --git a/TextParser.Tests/TextParserTests.cs b/TextParser.Tests/TextParserTests.cs index 3490678..593bb4f 100644 --- a/TextParser.Tests/TextParserTests.cs +++ b/TextParser.Tests/TextParserTests.cs @@ -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(schema); + var numbers = parser + .SetInputText(testInput11) + .Parse() + .Filter(InputType.Integer) + .AsSingleStream(); + + 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]); + } } diff --git a/TextParser/TokenConverter.cs b/TextParser/TokenConverter.cs deleted file mode 100644 index 71588b5..0000000 --- a/TextParser/TokenConverter.cs +++ /dev/null @@ -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 ConvertData(this List tokenList, Func converter) where TTokenType : IValueToken - { - var newList = new List(); - foreach (var token in tokenList) - { - var typedToken = token as IValueToken; - if (typedToken == null) - { - throw new Exception("Invalid Token type encountered during value conversion"); - } - - newList.Add(converter(typedToken.GetValue())); - } - return newList; - } - - public static List ConvertData(this List tokenList, Func> converter) where TTokenType : IValueToken - { - var newList = new List(); - foreach (var token in tokenList) - { - var typedToken = token as IValueToken; - if (typedToken == null) - { - throw new Exception("Invalid Token type encountered during value conversion"); - } - - newList.AddRange(converter(typedToken.GetValue())); - } - return newList; - } - - public static List> ConvertData(this List> tokenListList, Func converter) where TTokenType : IValueToken - { - var newListList = new List>(); - foreach (var tokenList in tokenListList) - { - newListList.Add(tokenList.ConvertData(converter)); - } - return newListList; - } -} - -public static class DataManipulationHelpers -{ - public static TType ReduceData(this List data, Func 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(this List data, Func, TType> reducer) - { - return reducer(data); - } -} - -public class TokenConverter -{ - protected List> rawTokens = new List>(); - - public TokenConverter() - { - } - - private List AsGenericCollection() where T : ICollection, new() - { - List returnData = new List(); - 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? valueToken = token as IValueToken; - 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 AsSingleStream() - { - List returnData = new List(); - 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? valueToken = token as IValueToken; - if (valueToken == null) - { - throw new Exception("Provided token is not a ValueToken"); - } - returnData.Add(valueToken.GetValue()); - } - } - return returnData; - } - - public List AsRows() - { - var listRows = this.AsListRows(); - var newList = new List(); - - foreach (var rowList in listRows) - { - newList.Add(rowList.ToArray()); - } - - return newList; - } - - public List> AsListRows() - { - return this.AsGenericCollection, T>(); - } - - public List AsColumns() - { - var listColumns = this.AsListColumns(); - var newList = new List(); - - foreach (var columnList in listColumns) - { - newList.Add(columnList.ToArray()); - } - - return newList; - } - - public List> AsListColumns() - { - this.CheckConversionPrerequisites(); - var rows = AsListRows(); - - var columns = new List>(); - for (int i = 0; i < rows[0].Count; i++) - { - columns.Add(new List()); - } - - foreach (var row in rows) - { - for (int i = 0; i < row.Count; i++) - { - columns[i].Add(row[i]); - } - } - - return columns; - } - - public T[][] AsGrid() - { - this.CheckConversionPrerequisites(); - var rowsList = AsRows(); - return rowsList.ToArray(); - } - - public List> AsRawData() - { - return this.rawTokens; - } - - public List AsFragments() - { - var items = this.AsSingleStream(); - var newList = new List(); - - 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; - } -}