示例#1
0
            /// <summary>
            /// Creates a new sub-/superscript object.
            /// </summary>
            /// <param name="type">The type.</param>
            public void CreateSubAndSuperscript(CharType type)
            {
                if (SubAndSuperscriptExpr != null && (SubAndSuperscriptType == type || (type == CharType.Subscript && SubAndSuperscriptExpr.SubScript == null) || (type == CharType.Superscript && SubAndSuperscriptExpr.SuperScript == null)))
                {
                    // Append the expression.
                }
                else
                {
                    if (SubAndSuperscriptExpr != null)
                    {
                        CurrentExpr = null;
                    }

                    var script = new SubAndSuperscript();
                    script.Expression = CurrentExpr;

                    SubAndSuperscriptExpr = script;
                    CurrentExpr           = script;

                    // Update the result.
                    if (Result == null || !(Result is Block))
                    {
                        Result = CurrentExpr;
                    }
                    else
                    {
                        var block = (Block)Result;
                        block.Expressions[block.Expressions.Count - 1] = CurrentExpr;
                    }
                }

                SubAndSuperscriptType = type;
            }
示例#2
0
            /// <summary>
            /// Set the new expression.
            /// </summary>
            /// <param name="newExpr">The new expression.</param>
            public void SwitchCurrentExpression(ExpressionBase newExpr)
            {
                var updateResult = true;

                // Whitespace insertion control at operators.
                if (newExpr is Operator)
                {
                    var op = (Operator)newExpr;
                    // TODO @ Parser: Still errors in whitespace insertion...

                    if (CurrentExpr == null || (CurrentExpr is Operator && ((Operator)CurrentExpr).HasRightWhitespace))
                    {
                        op.HasLeftWhitespace  = false;
                        op.HasRightWhitespace = false;
                    }
                }

                // Control the behaviour of sub- and superscript.
                if (SubAndSuperscriptExpr != null)
                {
                    if (SubAndSuperscriptType == CharType.Subscript)
                    {
                        if (SubAndSuperscriptExpr.SubScript == null)
                        {
                            SubAndSuperscriptExpr.SubScript = newExpr;
                            return;
                        }
                        else
                        {
                            SubAndSuperscriptExpr = null;
                            SubAndSuperscriptType = CharType.Unknown;
                        }
                    }
                    else
                    {
                        if (SubAndSuperscriptExpr.SuperScript == null)
                        {
                            SubAndSuperscriptExpr.SuperScript = newExpr;
                            return;
                        }
                        else
                        {
                            SubAndSuperscriptExpr = null;
                            SubAndSuperscriptType = CharType.Unknown;
                        }
                    }
                }

                // Assign new expression to current expression.
                CurrentExpr = newExpr;

                // Handle command expressions. Stop adding expressions after the required count was reached.
                if (CommandExpr != null)
                {
                    updateResult = false;
                    CommandExpr.AddEpression(CurrentExpr);
                    CurrentExpr = CommandExpr;

                    if (CommandExpr.RequiredExpressionCount == 0)
                    {
                        // Handle fractions before they are finished.
                        if (CommandExpr is Fraction)
                        {
                            FractionStackCounter--;
                        }

                        // Set the previous command expression or null.
                        if (CommandExprStack.Count > 0)
                        {
                            CommandExpr = CommandExprStack.Pop();
                        }
                        else
                        {
                            CommandExpr = null;
                        }
                    }
                }

                // Check, whether this expression is a composite expression and therefore needs input blocks.
                if (CurrentExpr is CompositeExpression && (CurrentExpr != CommandExpr) && ((CompositeExpression)CurrentExpr).RequiredExpressionCount > 0)
                {
                    if (CommandExpr != null)
                    {
                        CommandExprStack.Push(CommandExpr);
                    }
                    CommandExpr = (CompositeExpression)CurrentExpr;

                    // Handle the fraction stack.
                    if (newExpr is Fraction)
                    {
                        FractionStackCounter++;
                        ((Fraction)newExpr).IsSubFraction = FractionStackCounter > 1;
                    }
                }

                // Break here, if needed.
                if (updateResult)
                {
                    // Set the new result:
                    // - If the last result was null, copy the new expression.
                    // - Otherwise, try to cast to a block or create a new one.
                    if (Result == null)
                    {
                        Result = CurrentExpr;
                    }
                    else
                    {
                        var block = (Result is Block) ? (Block)Result : new Block()
                        {
                            Expressions = { Result }
                        };
                        block.Expressions.Add(CurrentExpr);
                        Result = block;
                    }
                }
            }