Пример #1
0
        private void AddContainer(IFormulaToken token, int index)
        {
            _lastContainerAdded = true;

            if (index > CaretIndex)
            {
                index++;
            }

            var container = CreateContainer(token);

            if (container == null)
            {
                return;
            }
            Children.Insert(index, container);
            InitContainer(container);

            if (index <= CaretIndex)
            {
                CaretIndex++;
            }
            if (SelectionStart <= index && index < SelectionEnd)
            {
                SelectionLength++;
            }
        }
 public Grid CreateContainer(IFormulaToken token)
 {
     // DataContext is misused to hold the template
     var container = new Grid {HorizontalAlignment = HorizontalAlignment.Left, DataContext = this};
     container.Children.Add(new TextBlock {Text = FormulaSerializer.Serialize(token)});
     container.SizeChanged += (sender, e) => UpdateMargin(container);
     return container;
 }
Пример #3
0
        public static string Serialize(IFormulaToken token)
        {
            if (token == null)
            {
                return(string.Empty);
            }

            return(token.Serialize());
        }
 private IFormulaOperator InterpretMinusToken(IFormulaToken previousToken, FormulaNodeSubtract minusToken)
 {
     // change from subtract to negative sign token
     if (previousToken == null || previousToken is IFormulaOperator || previousToken is FormulaTokenParameterSeparator)
     {
         var negativeSignToken = FormulaTreeFactory.CreateNegativeSignNode(null);
         SetOrigin(negativeSignToken, minusToken);
         return(negativeSignToken);
     }
     return(minusToken);
 }
        public Grid CreateContainer(IFormulaToken token)
        {
            // DataContext is misused to hold the template
            var container = new Grid {
                HorizontalAlignment = HorizontalAlignment.Left, DataContext = this
            };

            container.Children.Add(new TextBlock {
                Text = FormulaSerializer.Serialize(token)
            });
            container.SizeChanged += (sender, e) => UpdateMargin(container);
            return(container);
        }
Пример #6
0
        private static Grid CreateContainer(IFormulaToken token)
        {
            // find template in FormulaTokenTemplates.xaml
            FormulaTokenTemplate template;

            if (!FormulaTokenTemplates.TryGetValue(token.GetType(), out template))
            {
                Debug.WriteLine("Please add template for \"" + token.GetType().Name +
                                "\" to FormulaTokenTemplates.xaml. ");
                return(null);
            }

            return(template.CreateContainer(token));
        }
        private void InterpretChildren(IFormulaToken commonToken, List<IFormulaToken> parenthesesTokens)
        {
            SetOrigin(commonToken, parenthesesTokens);

            // children are all tokens except the enclosing parentheses
            var children = parenthesesTokens.GetRange(1, parenthesesTokens.Count - 2);

            // interpret FormulaTokenParameter
            var parameterToken = commonToken as FormulaTokenParameter;
            if (parameterToken != null)
            {
                var unaryParameterToken = parameterToken as FormulaTokenUnaryParameter;
                var binaryParameterToken = parameterToken as FormulaTokenBinaryParameter;
                var parametersCount = unaryParameterToken != null ? 1 : 2;

                var interpretedChildren = children.AsEnumerable();
                interpretedChildren = InterpretNumbers(interpretedChildren, lookAhead: children);
                interpretedChildren = InterpretFunctions(interpretedChildren);
                interpretedChildren = InterpretMinusTokenForward(interpretedChildren);
                interpretedChildren = InterpretOperators(interpretedChildren, lookAhead: children);
                interpretedChildren = InterpretParameters(interpretedChildren);
                var parameters = interpretedChildren.Cast<FormulaTree>().Take(parametersCount).ToList();

                // valid parameters
                if (IsCancellationRequested) return;
                if (parameters.Count == 0)
                {
                    SetParsingError(
                        source: Range.Empty(GetOrigin(parenthesesTokens.First()).End),
                        message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_EmptyArgument"));
                    return;
                }
                if (parameters.Count < parametersCount)
                {
                    SetParsingError(
                        source: Range.Empty(GetOrigin(parenthesesTokens.Last()).Start),
                        message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_TooFewArguments"));
                    return;
                }
                var parametersRange = GetOrigin(parameters);
                if (parametersRange.Length < GetOrigin(children).Length)
                {
                    var nextIndex = parametersRange.End;
                    var nextToken = children.First(child => GetOrigin(child).Start == nextIndex);
                    if (nextToken is FormulaTokenParameterSeparator)
                    {
                        SetParsingError(
                            source: Range.FromLength(parametersRange.End, GetOrigin(children).Length - parametersRange.Length),
                            message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_TooManyArguments"));
                    }
                    else
                    {
                        SetParsingError(
                            source: Range.Empty(parametersRange.End),
                            message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_ArgumentDoubleValue"));
                    }
                    return;
                }

                // attach parameters
                if (unaryParameterToken != null)
                {
                    unaryParameterToken.Parameter = RemoveParentheses(parameters[0]);
                }
                if (binaryParameterToken != null)
                {
                    binaryParameterToken.FirstParameter = RemoveParentheses(parameters[0]);
                    binaryParameterToken.SecondParameter = RemoveParentheses(parameters[1]);
                }
            }

            // interpret FormulaNodeParentheses
            var parenthesesNode = commonToken as FormulaNodeParentheses;
            if (parenthesesNode != null)
            {
                var interpretedChildren = children.AsEnumerable();
                interpretedChildren = InterpretNonParameter(interpretedChildren);
                interpretedChildren = InterpretNumbers(interpretedChildren, lookAhead: children);
                interpretedChildren = InterpretFunctions(interpretedChildren);
                interpretedChildren = InterpretMinusTokenForward(interpretedChildren);
                interpretedChildren = InterpretOperators(interpretedChildren, lookAhead: children);
                var child = (FormulaTree) interpretedChildren.FirstOrDefault();

                // valid child
                if (IsCancellationRequested) return;
                if (child == null)
                {
                    SetParsingError(
                        source: Range.Empty(GetOrigin(parenthesesTokens.Last()).Start),
                        message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_EmptyParentheses"));
                    return;
                }
                var childRange = GetOrigin(child);
                if (childRange.Length < GetOrigin(children).Length)
                {
                    SetParsingError(
                        source: Range.Empty(childRange.End),
                        message: AppResourcesHelper.Get("FormulaInterpreter_DoubleValue"));
                    return;
                }

                // attach child
                parenthesesNode.Child = RemoveParentheses(child);
            }

        }
 private void CompleteChildren(IFormulaToken commonToken, List <IFormulaToken> parenthesesTokens)
 {
     SetOrigin(commonToken, parenthesesTokens);
 }
 private void SetParsingError(IFormulaToken source, string message)
 {
     SetParsingError(source == null ? Range.Empty(0) : GetOrigin(source), message);
 }
 private void SetOrigin(IFormulaToken token, IFormulaToken otherToken)
 {
     _origin[token] = GetOrigin(otherToken);
 }
 private Range GetOrigin(IFormulaToken from, IFormulaToken to)
 {
     return(Range.Combine(GetOrigin(from), GetOrigin(to)));
 }
Пример #12
0
 bool ITestEquatable <IFormulaToken> .TestEquals(IFormulaToken other)
 {
     return(other != null && other.GetType() == GetType() && TestEquals((FormulaToken)other));
 }
        private void AddContainer(IFormulaToken token, int index)
        {
            _lastContainerAdded = true;

            if (index > CaretIndex)
            {
                index++;
            }

            var container = CreateContainer(token);
            if (container == null) return;
            Children.Insert(index, container);
            InitContainer(container);

            if (index <= CaretIndex)
            {
                CaretIndex++;
            }
            if (SelectionStart <= index && index < SelectionEnd)
            {
                SelectionLength++;
            }
        }
        private static Grid CreateContainer(IFormulaToken token)
        {
            // find template in FormulaTokenTemplates.xaml
            FormulaTokenTemplate template;
            if (!FormulaTokenTemplates.TryGetValue(token.GetType(), out template))
            {
                Debug.WriteLine("Please add template for \"" + token.GetType().Name +
                                "\" to FormulaTokenTemplates.xaml. ");
                return null;
            }

            return template.CreateContainer(token);
        }
 private void CompleteChildren(IFormulaToken commonToken, List<IFormulaToken> parenthesesTokens)
 {
     SetOrigin(commonToken, parenthesesTokens);
 }
 private IFormulaOperator InterpretMinusToken(IFormulaToken previousToken, FormulaNodeSubtract minusToken)
 {
     // change from subtract to negative sign token
     if (previousToken == null || previousToken is IFormulaOperator || previousToken is FormulaTokenParameterSeparator)
     {
         var negativeSignToken = FormulaTreeFactory.CreateNegativeSignNode(null);
         SetOrigin(negativeSignToken, minusToken);
         return negativeSignToken;
     }
     return minusToken;
 }
 private Range GetOrigin(IFormulaToken token)
 {
     return _origin[token];
 }
 private Range GetOrigin(IFormulaToken token)
 {
     return(_origin[token]);
 }
 private Range GetOrigin(IFormulaToken from, IFormulaToken to)
 {
     return Range.Combine(GetOrigin(from), GetOrigin(to));
 }
 private void SetOrigin(IFormulaToken token, Range value)
 {
     _origin[token] = value;
 }
 private void SetOrigin(IFormulaToken token, Range value)
 {
     _origin[token] = value;
 }
 private void SetOrigin(IFormulaToken token, ICollection <IFormulaToken> tokens)
 {
     _origin[token] = GetOrigin(tokens);
 }
 private void SetOrigin(IFormulaToken token, IFormulaToken otherToken)
 {
     _origin[token] = GetOrigin(otherToken);
 }
        private void InterpretChildren(IFormulaToken commonToken, List <IFormulaToken> parenthesesTokens)
        {
            SetOrigin(commonToken, parenthesesTokens);

            // children are all tokens except the enclosing parentheses
            var children = parenthesesTokens.GetRange(1, parenthesesTokens.Count - 2);

            // interpret FormulaTokenParameter
            var parameterToken = commonToken as FormulaTokenParameter;

            if (parameterToken != null)
            {
                var unaryParameterToken  = parameterToken as FormulaTokenUnaryParameter;
                var binaryParameterToken = parameterToken as FormulaTokenBinaryParameter;
                var parametersCount      = unaryParameterToken != null ? 1 : 2;

                var interpretedChildren = children.AsEnumerable();
                interpretedChildren = InterpretNumbers(interpretedChildren, lookAhead: children);
                interpretedChildren = InterpretFunctions(interpretedChildren);
                interpretedChildren = InterpretMinusTokenForward(interpretedChildren);
                interpretedChildren = InterpretOperators(interpretedChildren, lookAhead: children);
                interpretedChildren = InterpretParameters(interpretedChildren);
                var parameters = interpretedChildren.Cast <FormulaTree>().Take(parametersCount).ToList();

                // valid parameters
                if (IsCancellationRequested)
                {
                    return;
                }
                if (parameters.Count == 0)
                {
                    SetParsingError(
                        source: Range.Empty(GetOrigin(parenthesesTokens.First()).End),
                        message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_EmptyArgument"));
                    return;
                }
                if (parameters.Count < parametersCount)
                {
                    SetParsingError(
                        source: Range.Empty(GetOrigin(parenthesesTokens.Last()).Start),
                        message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_TooFewArguments"));
                    return;
                }
                var parametersRange = GetOrigin(parameters);
                if (parametersRange.Length < GetOrigin(children).Length)
                {
                    var nextIndex = parametersRange.End;
                    var nextToken = children.First(child => GetOrigin(child).Start == nextIndex);
                    if (nextToken is FormulaTokenParameterSeparator)
                    {
                        SetParsingError(
                            source: Range.FromLength(parametersRange.End, GetOrigin(children).Length - parametersRange.Length),
                            message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_TooManyArguments"));
                    }
                    else
                    {
                        SetParsingError(
                            source: Range.Empty(parametersRange.End),
                            message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_ArgumentDoubleValue"));
                    }
                    return;
                }

                // attach parameters
                if (unaryParameterToken != null)
                {
                    unaryParameterToken.Parameter = RemoveParentheses(parameters[0]);
                }
                if (binaryParameterToken != null)
                {
                    binaryParameterToken.FirstParameter  = RemoveParentheses(parameters[0]);
                    binaryParameterToken.SecondParameter = RemoveParentheses(parameters[1]);
                }
            }

            // interpret FormulaNodeParentheses
            var parenthesesNode = commonToken as FormulaNodeParentheses;

            if (parenthesesNode != null)
            {
                var interpretedChildren = children.AsEnumerable();
                interpretedChildren = InterpretNonParameter(interpretedChildren);
                interpretedChildren = InterpretNumbers(interpretedChildren, lookAhead: children);
                interpretedChildren = InterpretFunctions(interpretedChildren);
                interpretedChildren = InterpretMinusTokenForward(interpretedChildren);
                interpretedChildren = InterpretOperators(interpretedChildren, lookAhead: children);
                var child = (FormulaTree)interpretedChildren.FirstOrDefault();

                // valid child
                if (IsCancellationRequested)
                {
                    return;
                }
                if (child == null)
                {
                    SetParsingError(
                        source: Range.Empty(GetOrigin(parenthesesTokens.Last()).Start),
                        message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_EmptyParentheses"));
                    return;
                }
                var childRange = GetOrigin(child);
                if (childRange.Length < GetOrigin(children).Length)
                {
                    SetParsingError(
                        source: Range.Empty(childRange.End),
                        message: AppResourcesHelper.Get("FormulaInterpreter_DoubleValue"));
                    return;
                }

                // attach child
                parenthesesNode.Child = RemoveParentheses(child);
            }
        }
 private void SetOrigin(IFormulaToken token, ICollection<IFormulaToken> tokens)
 {
     _origin[token] = GetOrigin(tokens);
 }
 private void SetParsingError(IFormulaToken source, string message)
 {
     SetParsingError(source == null ? Range.Empty(0) : GetOrigin(source), message);
 }
        public static string Serialize(IFormulaToken token)
        {
            if (token == null) return string.Empty;

            return token.Serialize();
        }