Exemplo n.º 1
0
 public override IEnumerable <IFormulaToken> Tokenize()
 {
     return(Enumerable.Repeat(CreateToken(), 1)
            .Concat(Enumerable.Repeat(FormulaTokenFactory.CreateParenthesisToken(true), 1))
            .Concat(Child == null ? FormulaTokenizer.EmptyChild : Child.Tokenize())
            .Concat(Enumerable.Repeat(FormulaTokenFactory.CreateParenthesisToken(false), 1)));
 }
        public bool HandleKey(FormulaEditorKey key, LocalVariable localVariable = null, GlobalVariable globalVariable = null)
        {
            if (key == FormulaEditorKey.Delete)
            {
                PushUndo();
                return(Delete());
            }

            PushUndo();
            var token = CreateToken(key, localVariable, globalVariable);

            return(Insert((token is IFormulaFunction)
                ? new[] { token, FormulaTokenFactory.CreateParenthesisToken(true) }
                : new[] { token }));
        }
        private void InitInstanceSpecificTokenMappings()
        {
            _instanceSpecificTokenMappings = new Dictionary <string, Func <IFormulaToken> >();

            // variables
            foreach (var variable in _globalVariables.Select(entry => entry.Value))
            {
                var variable1 = variable;
                AddTokenMapping(_instanceSpecificTokenMappings, () => FormulaTokenFactory.CreateGlobalVariableToken(variable1));
            }
            foreach (var variable in _localVariables.Select(entry => entry.Value))
            {
                var variable1 = variable;
                AddTokenMapping(_instanceSpecificTokenMappings, () => FormulaTokenFactory.CreateLocalVariableToken(variable1));
            }
        }
        // Tokenize\(.*, (FormulaTokenFactory.\w+), .*

        private static void InitInvariantTokenMappings()
        {
            _invariantTokenMappings = new Dictionary <string, Func <IFormulaToken> >();

            // constants
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreatePiToken);

            // operators
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreatePlusToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateMinusToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateMultiplyToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateDivideToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateCaretToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateEqualsToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateNotEqualsToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateLessToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateLessEqualToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateGreaterToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateGreaterEqualToken);


            // functions
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateExpToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateLogToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateLnToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateSinToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateCosToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateTanToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateArcsinToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateArccosToken);
            AddTokenMapping(_invariantTokenMappings, FormulaTokenFactory.CreateArctanToken);

            // brackets
            AddTokenMapping(_invariantTokenMappings, () => FormulaTokenFactory.CreateParenthesisToken(true));
            AddTokenMapping(_invariantTokenMappings, () => FormulaTokenFactory.CreateParenthesisToken(false));
        }
Exemplo n.º 5
0
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateTransparencyToken());
 }
Exemplo n.º 6
0
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateBrightnessToken());
 }
Exemplo n.º 7
0
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateRotationToken());
 }
        /// <summary>
        /// Maps all opening and closing parentheses and packs them with their interpreted children into <see cref="FormulaTokenParameter"/>.
        /// </summary>
        private IEnumerable <IFormulaToken> InterpretBrackets(IEnumerable <IFormulaToken> tokens)
        {
            var parenthesesTokens = new Stack <List <IFormulaToken> >();
            var parentheses       = new Stack <IFormulaToken>();

            foreach (var context in tokens.WithContext())
            {
                if (IsCancellationRequested)
                {
                    yield break;
                }
                var previousToken = context[0];
                var token         = context[1];

                var parenthesisToken = token as FormulaTokenParenthesis;
                if (parenthesisToken != null)
                {
                    // handle opening parenthesis
                    if (parenthesisToken.IsOpening)
                    {
                        parenthesesTokens.Push(new List <IFormulaToken>());
                        parenthesesTokens.Peek().Add(token);
                        if (previousToken is FormulaNodeUnaryFunction)
                        {
                            parentheses.Push(FormulaTokenFactory.CreateUnaryParameterToken(null));
                        }
                        else if (previousToken is FormulaNodeBinaryFunction)
                        {
                            parentheses.Push(FormulaTokenFactory.CreateBinaryParameterToken(null, null));
                        }
                        else
                        {
                            parentheses.Push(FormulaTreeFactory.CreateParenthesesNode(null));
                        }
                    }

                    // handle closing parenthesis
                    else
                    {
                        if (parenthesesTokens.Count == 0)
                        {
                            if (previousToken is IFormulaFunction)
                            {
                                SetParsingError(Range.Empty(GetOrigin(parenthesisToken).Start), AppResourcesHelper.Get("FormulaInterpreter_Function_Empty"));
                            }
                            else
                            {
                                SetParsingError(parenthesisToken, AppResourcesHelper.Get("FormulaInterpreter_Brackets_UnmatchedClosingParenthesis"));
                            }
                            yield break;
                        }
                        parenthesesTokens.Peek().Add(token);

                        // interpret parentheses
                        var commonToken = parentheses.Pop();
                        InterpretChildren(commonToken, parenthesesTokens.Pop());
                        if (IsCancellationRequested)
                        {
                            yield break;
                        }
                        if (parenthesesTokens.Count != 0)
                        {
                            parenthesesTokens.Peek().Add(commonToken);
                        }
                        else
                        {
                            yield return(commonToken);
                        }
                    }
                    continue;
                }

                // last token
                if (token == null)
                {
                    if (parenthesesTokens.Count != 0)
                    {
                        SetParsingError(
                            source: Range.Empty(GetOrigin(parenthesesTokens.Peek().Last()).End),
                            message: AppResourcesHelper.Get("FormulaInterpreter_Brackets_UnmatchedOpeningParenthesis"));
                        yield break;
                    }
                    continue;
                }

                // stash tokens inside parentheses
                if (parenthesesTokens.Count != 0)
                {
                    parenthesesTokens.Peek().Add(token);
                    continue;
                }

                // yield any token outside parentheses
                yield return(token);
            }
        }
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateAccelerationZToken());
 }
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateInclinationYToken());
 }
Exemplo n.º 11
0
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateMultiplyToken());
 }
        private static IFormulaToken CreateToken(FormulaEditorKey key, LocalVariable localVariable, GlobalVariable globalVariable)
        {
            switch (key)
            {
            // Constants
            case FormulaEditorKey.D0: return(FormulaTokenFactory.CreateDigitToken(0));

            case FormulaEditorKey.D1: return(FormulaTokenFactory.CreateDigitToken(1));

            case FormulaEditorKey.D2: return(FormulaTokenFactory.CreateDigitToken(2));

            case FormulaEditorKey.D3: return(FormulaTokenFactory.CreateDigitToken(3));

            case FormulaEditorKey.D4: return(FormulaTokenFactory.CreateDigitToken(4));

            case FormulaEditorKey.D5: return(FormulaTokenFactory.CreateDigitToken(5));

            case FormulaEditorKey.D6: return(FormulaTokenFactory.CreateDigitToken(6));

            case FormulaEditorKey.D7: return(FormulaTokenFactory.CreateDigitToken(7));

            case FormulaEditorKey.D8: return(FormulaTokenFactory.CreateDigitToken(8));

            case FormulaEditorKey.D9: return(FormulaTokenFactory.CreateDigitToken(9));

            case FormulaEditorKey.DecimalSeparator: return(FormulaTokenFactory.CreateDecimalSeparatorToken());

            case FormulaEditorKey.ParameterSeparator: return(FormulaTokenFactory.CreateParameterSeparatorToken());

            case FormulaEditorKey.Pi: return(FormulaTokenFactory.CreatePiToken());

            case FormulaEditorKey.True: return(FormulaTokenFactory.CreateTrueToken());

            case FormulaEditorKey.False: return(FormulaTokenFactory.CreateFalseToken());

            // Operators
            case FormulaEditorKey.Plus: return(FormulaTokenFactory.CreatePlusToken());

            case FormulaEditorKey.Minus: return(FormulaTokenFactory.CreateMinusToken());

            case FormulaEditorKey.Multiply: return(FormulaTokenFactory.CreateMultiplyToken());

            case FormulaEditorKey.Divide: return(FormulaTokenFactory.CreateDivideToken());

            case FormulaEditorKey.Caret: return(FormulaTokenFactory.CreateCaretToken());

            case FormulaEditorKey.Equals: return(FormulaTokenFactory.CreateEqualsToken());

            case FormulaEditorKey.NotEquals: return(FormulaTokenFactory.CreateNotEqualsToken());

            case FormulaEditorKey.Greater: return(FormulaTokenFactory.CreateGreaterToken());

            case FormulaEditorKey.GreaterEqual: return(FormulaTokenFactory.CreateGreaterEqualToken());

            case FormulaEditorKey.Less: return(FormulaTokenFactory.CreateLessToken());

            case FormulaEditorKey.LessEqual: return(FormulaTokenFactory.CreateLessEqualToken());

            case FormulaEditorKey.And: return(FormulaTokenFactory.CreateAndToken());

            case FormulaEditorKey.Or: return(FormulaTokenFactory.CreateOrToken());

            case FormulaEditorKey.Not: return(FormulaTokenFactory.CreateNotToken());

            case FormulaEditorKey.Mod: return(FormulaTokenFactory.CreateModToken());

            // Functions
            case FormulaEditorKey.Exp: return(FormulaTokenFactory.CreateExpToken());

            case FormulaEditorKey.Log: return(FormulaTokenFactory.CreateLogToken());

            case FormulaEditorKey.Ln: return(FormulaTokenFactory.CreateLnToken());

            case FormulaEditorKey.Min: return(FormulaTokenFactory.CreateMinToken());

            case FormulaEditorKey.Max: return(FormulaTokenFactory.CreateMaxToken());

            case FormulaEditorKey.Sin: return(FormulaTokenFactory.CreateSinToken());

            case FormulaEditorKey.Cos: return(FormulaTokenFactory.CreateCosToken());

            case FormulaEditorKey.Tan: return(FormulaTokenFactory.CreateTanToken());

            case FormulaEditorKey.Arcsin: return(FormulaTokenFactory.CreateArcsinToken());

            case FormulaEditorKey.Arccos: return(FormulaTokenFactory.CreateArccosToken());

            case FormulaEditorKey.Arctan: return(FormulaTokenFactory.CreateArctanToken());

            case FormulaEditorKey.Sqrt: return(FormulaTokenFactory.CreateSqrtToken());

            case FormulaEditorKey.Abs: return(FormulaTokenFactory.CreateAbsToken());

            case FormulaEditorKey.Round: return(FormulaTokenFactory.CreateRoundToken());

            case FormulaEditorKey.Random: return(FormulaTokenFactory.CreateRandomToken());

            // Sensors
            case FormulaEditorKey.AccelerationX: return(FormulaTokenFactory.CreateAccelerationXToken());

            case FormulaEditorKey.AccelerationY: return(FormulaTokenFactory.CreateAccelerationYToken());

            case FormulaEditorKey.AccelerationZ: return(FormulaTokenFactory.CreateAccelerationZToken());

            case FormulaEditorKey.Compass: return(FormulaTokenFactory.CreateCompassToken());

            case FormulaEditorKey.InclinationX: return(FormulaTokenFactory.CreateInclinationXToken());

            case FormulaEditorKey.InclinationY: return(FormulaTokenFactory.CreateInclinationYToken());

            case FormulaEditorKey.Loudness: return(FormulaTokenFactory.CreateLoudnessToken());

            // Properties
            case FormulaEditorKey.Brightness: return(FormulaTokenFactory.CreateBrightnessToken());

            case FormulaEditorKey.Layer: return(FormulaTokenFactory.CreateLayerToken());

            case FormulaEditorKey.Transparency: return(FormulaTokenFactory.CreateTransparencyToken());

            case FormulaEditorKey.PositionX: return(FormulaTokenFactory.CreatePositionXToken());

            case FormulaEditorKey.PositionY: return(FormulaTokenFactory.CreatePositionYToken());

            case FormulaEditorKey.Rotation: return(FormulaTokenFactory.CreateRotationToken());

            case FormulaEditorKey.Size: return(FormulaTokenFactory.CreateSizeToken());

            // Variables
            case FormulaEditorKey.LocalVariable: return(FormulaTokenFactory.CreateLocalVariableToken(localVariable));

            case FormulaEditorKey.GlobalVariable: return(FormulaTokenFactory.CreateGlobalVariableToken(globalVariable));

            // brackets
            case FormulaEditorKey.OpeningParenthesis: return(FormulaTokenFactory.CreateParenthesisToken(true));

            case FormulaEditorKey.ClosingParenthesis: return(FormulaTokenFactory.CreateParenthesisToken(false));

            default: throw new ArgumentOutOfRangeException("key");
            }
        }
Exemplo n.º 13
0
 protected override IFormulaToken CreateToken(bool isOpening)
 {
     return(FormulaTokenFactory.CreateParenthesisToken(isOpening));
 }
        private static void InitCultureSpecificTokenMappings(CultureInfo culture)
        {
            _cultureSpecificTokenMappings = new Dictionary <string, Func <IFormulaToken> >();

            // constants
            AddTokenMapping(_cultureSpecificTokenMappings, FormulaTokenFactory.CreateDecimalSeparatorToken);
            AddTokenMapping(_cultureSpecificTokenMappings, culture.NumberFormat.NegativeSign, FormulaTokenFactory.CreateMinusToken);
            foreach (var digit in Enumerable.Range(0, 10))
            {
                // access to foreach variable in closure
                var value = digit;
                AddTokenMapping(_cultureSpecificTokenMappings, digit.ToString(culture), () => FormulaTokenFactory.CreateDigitToken(value));
            }

            // functions
            var parameterSeparatorValue = culture.NumberFormat.NumberDecimalSeparator == "," ? ", " : ",";

            AddTokenMapping(_cultureSpecificTokenMappings, parameterSeparatorValue, FormulaTokenFactory.CreateParameterSeparatorToken);

            _cultureSpecificTokenMappingsCulture = culture;
        }
Exemplo n.º 15
0
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateLessEqualToken());
 }
Exemplo n.º 16
0
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreatePositionXToken());
 }
Exemplo n.º 17
0
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateFalseToken());
 }
 protected override IFormulaToken CreateToken()
 {
     return(FormulaTokenFactory.CreateGlobalVariableToken(Variable));
 }