Example #1
0
        public void Test_FailResult_SmallText()
        {
            string input  = "ab(cb";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsTrue(result.Equals("ab(cb"));
        }
Example #2
0
        public void Test_StartParenthesisWithoutEnd_SouldFailResult()
        {
            string input  = "this ( is";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsTrue(result.Equals("is ( is"));
        }
Example #3
0
        public void Test_FailResult_At_End_Of_Input()
        {
            string input  = "this i(";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsTrue(result.Equals("s i("));
        }
Example #4
0
        public void AssignmentTest_ShouldFailResult()
        {
            string input  = "this ( is [ a ) text ] where the parentheses are ) incorrect.";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsTrue(result.Equals(" a ) te"));
        }
Example #5
0
        public void Test_StartParenthesisWithoutEnd_SouldFail()
        {
            string input  = "this ( is";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsFalse(parsed);
        }
Example #6
0
        public void AssignmentTest_ShouldFail()
        {
            string input  = "this ( is [ a ) text ] where the parentheses are ) incorrect.";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsFalse(parsed);
        }
Example #7
0
        public void AssignmentTest_ShouldPass()
        {
            string input  = ":(( demo text ( [test] ( just a small ( text ( with all kind of { parentheses in {( different ( locations ) who ) then } also } with ) each other ) should ) match ) or ) not... :)";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsTrue(parsed);
        }
Example #8
0
        public void Test_Correct_Parentheses()
        {
            string input  = "([{}])";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsTrue(parsed);
        }
Example #9
0
        public void Test_No_Parentheses()
        {
            string input  = "some text";
            bool   parsed = ParenthesesParser.TryParse(input, out string result);

            Assert.IsTrue(parsed);
        }
Example #10
0
    internal static ComputationBody CreateBody(WorkingExpressionSet workingSet)
    {
        if (workingSet.CancellationToken.IsCancellationRequested)
        {
            return(ComputationBody.Empty);
        }

        foreach (Type extractorType in workingSet.Extractors.KeysByLevel.OrderBy(p => p.Key)
                 .SelectMany(p => p.Value).ToArray())
        {
            workingSet.Expression = workingSet.Extractors[extractorType].ExtractAllConstants(
                workingSet.Expression,
                workingSet.ConstantsTable,
                workingSet.ReverseConstantsTable,
                workingSet.Definition);

            if (workingSet.CancellationToken.IsCancellationRequested)
            {
                return(ComputationBody.Empty);
            }
        }

        workingSet.Expression = workingSet.Expression.Trim().Replace(
            " ",
            string.Empty);

        // Start preparing expression
        workingSet.SymbolTable.Add(
            string.Empty,
            ExpressionSymbol.GenerateSymbol(
                string.Empty,
                workingSet.Expression));

        // Prepares expression and takes care of operators to ensure that they are all OK and usable
        workingSet.Initialize();

        workingSet.SymbolTable[string.Empty].Expression = workingSet.Expression;

        if (workingSet.CancellationToken.IsCancellationRequested)
        {
            return(ComputationBody.Empty);
        }

        // Break expression based on function calls
        FunctionsExtractor.ReplaceFunctions(
            workingSet.Definition.Parentheses.Left,
            workingSet.Definition.Parentheses.Right,
            workingSet.Definition.ParameterSeparator,
            workingSet.ConstantsTable,
            workingSet.ReverseConstantsTable,
            workingSet.SymbolTable,
            workingSet.ReverseSymbolTable,
            workingSet.Interpreters,
            workingSet.ParameterRegistry,
            workingSet.SymbolTable[string.Empty].Expression,
            workingSet.AllSymbols);

        if (workingSet.CancellationToken.IsCancellationRequested)
        {
            return(ComputationBody.Empty);
        }

        // We save a split expression for determining parameter order
        var splitExpression = workingSet.Expression.Split(
            workingSet.AllSymbols.ToArray(),
            StringSplitOptions.RemoveEmptyEntries);

        // Break by parentheses
        ParenthesesParser.FormatParentheses(
            workingSet.Definition.Parentheses.Left,
            workingSet.Definition.Parentheses.Right,
            workingSet.Definition.ParameterSeparator,
            workingSet.AllOperatorsInOrder,
            workingSet.SymbolTable,
            workingSet.ReverseSymbolTable);

        if (workingSet.CancellationToken.IsCancellationRequested)
        {
            return(ComputationBody.Empty);
        }

        // Populating symbol tables
        foreach (var p in workingSet.SymbolTable.Where(p => !p.Value.IsFunctionCall)
                 .Select(p => p.Value.Expression))
        {
            TablePopulationGenerator.PopulateTables(
                p,
                workingSet.ConstantsTable,
                workingSet.ReverseConstantsTable,
                workingSet.SymbolTable,
                workingSet.ReverseSymbolTable,
                workingSet.ParameterRegistry,
                workingSet.Interpreters,
                workingSet.Expression,
                workingSet.Definition.Parentheses.Left,
                workingSet.AllSymbols);
        }

        // For each parameter from the table we've just populated, see where it's first used, and fill in that index as the order
        foreach (ParameterContext paramForOrdering in workingSet.ParameterRegistry.Dump())
        {
            paramForOrdering.Order = Array.IndexOf(
                splitExpression,
                paramForOrdering.Name);
        }

        if (workingSet.CancellationToken.IsCancellationRequested)
        {
            return(ComputationBody.Empty);
        }

        // Generate expressions
        NodeBase?body;

        try
        {
            body = GenerateExpression(
                workingSet.SymbolTable[string.Empty].Expression,
                workingSet);
        }
        catch
        {
            body = null;
        }

        if (body == null || workingSet.CancellationToken.IsCancellationRequested)
        {
            return(ComputationBody.Empty);
        }

        if (body is ConstantNodeBase && workingSet.ParameterRegistry.Populated)
        {
            return(ComputationBody.Empty);
        }

        workingSet.Success = true;

        return(new ComputationBody(
                   body,
                   workingSet.ParameterRegistry));
    }