Пример #1
0
        private string FormatLogicalExpression(IFilterTree tree, bool isolate)
        {
            var operatorText = TokenMappings.TokenTypeToDefaultValueMap[tree.Token.Type];

            string resultExpression;

            if (TokenMappings.IsBinaryLogicalOperation(tree.Token))
            {
                var operandTexts = new List <string>();
                foreach (var childItem in tree.GetChildren())
                {
                    operandTexts.Add(FormatExpression(childItem, true));
                }

                resultExpression = string.Join($" {operatorText} ", operandTexts);
            }
            else if (TokenMappings.IsUnaryLogicalOperation(tree.Token))
            {
                var childNode   = tree.GetChild(0);
                var operandText = FormatExpression(childNode, false);

                resultExpression = TokenMappings.IsBinaryLogicalOperation(childNode.Token)
                                        ? $"{operatorText} ({operandText})"
                                        : $"{operatorText} {operandText}";
            }
            else
            {
                throw new FormaterException($"Encountered unexpected node of type {tree.Token.Type}.");
            }

            return(isolate
                                ? $"({resultExpression})"
                                : resultExpression);
        }
        private string FormatComparisonExpression(IFilterTree tree)
        {
            var attributeText = tree.GetChild(0).Token.Value;
            var operatorText  = TokenMappings.TokenTypeToLegacyValueMap[tree.Token.Type];

            var contentText = TokenMappings.IsListOperation(tree.Token)
                                ? FormatValueList(tree.GetChild(1))
                                : FormatValue(tree.GetChild(1));

            return($"{attributeText} {operatorText} [{contentText}]");
        }
        private string FormatExpression(IFilterTree tree)
        {
            if (TokenMappings.IsLogicalOperation(tree.Token))
            {
                return(FormatLogicalExpression(tree));
            }

            if (TokenMappings.IsComparisonOperation(tree.Token))
            {
                return(FormatComparisonExpression(tree));
            }

            throw new FormaterException($"Encountered unexpected node of type {tree.Token.Type}.");
        }