Exemplo n.º 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);
        }