Exemplo n.º 1
0
        private void AddOperationToTree(CodeElement leftExprCode, CodeElement rightOpCode, CodeElement rightExprCode, List <TextElement> comments)
        {
            WordBinaryOperator leftOperator  = leftExprCode.ParserElement as WordBinaryOperator;
            WordBinaryOperator rightOpSymbol = rightOpCode.ParserElement as WordBinaryOperator;

            // is the insertpoint a value
            // Or the insertPoint is a completed expression (eg parentheses)
            if (leftOperator == null ||
                (_binaryOperators.Contains(leftOperator) && _completeOperations.Contains(leftExprCode)))
            {
                // insert
                TextElement parent = leftExprCode.Parent;
                rightOpCode.Add(leftExprCode);
                rightOpCode.Add(rightExprCode);
                parent.ReplaceSubElement(leftExprCode, rightOpCode);
                rightOpCode.AddRange(comments);
                return;
            }

            // if the insertPoint has higther precedence
            if (leftOperator.Precedence > rightOpSymbol.Precedence)
            {
                // insert
                TextElement parent = leftExprCode.Parent;
                rightOpCode.Add(leftExprCode);
                rightOpCode.Add(rightExprCode);
                parent.ReplaceSubElement(leftExprCode, rightOpCode);
                rightOpCode.AddRange(comments);
                return;
            }

            // if the insertPoint has lower precedence
            if (leftOperator.Precedence < rightOpSymbol.Precedence)
            {
                AddOperationToTree(leftExprCode.ChildNodes[1] as CodeElement, rightOpCode, rightExprCode, comments);
            }

            // if the insertPoint has same precedence and operator is rigth associative
            else if (rightOpSymbol.RightAssociative)
            {
                AddOperationToTree(leftExprCode.ChildNodes[1] as CodeElement, rightOpCode, rightExprCode, comments);
            }

            // if the insertPoint has same precedence and operator is left associative
            else
            {
                // insert
                TextElement parent = leftExprCode.Parent;
                rightOpCode.Add(leftExprCode);
                rightOpCode.Add(rightExprCode);
                parent.ReplaceSubElement(leftExprCode, rightOpCode);
                rightOpCode.AddRange(comments);
                return;
            }
        }
Exemplo n.º 2
0
        public override bool Load(List <TextElement> outElements, int level)
        {
            TextSubString subStr = new TextSubString(TextBuffer.PointerNextChar);

            if (Collapse)
            {
                if (!LoadSet(outElements, level))
                {
                    return(false);
                }
            }
            else
            {
                var         outSubNotes = new List <TextElement>();
                TextElement element;
                if (!LoadSet(outSubNotes, level))
                {
                    return(false);
                }

                subStr.To = TextBuffer.PointerNextChar;

                // _simplify is true when a rule just contains a single word
                // The word value is inserted directly (collapsed)
                if (Comment)
                {
                    TextBuffer.InsertComments(outElements);
                }
                if (_simplify)
                {
                    element = outSubNotes.FirstOrDefault(n => n is CodeElement);
                    if (element != null)
                    {
                        element.Name = Name;
                    }
                    else
                    {
                        element = new CodeElement(this, subStr);
                    }

                    outElements.Add(element);
                }
                else
                {
                    element = new CodeElement(this, subStr);
                    element.AddRange(outSubNotes);
                    outElements.Add(element);
                }
            }

            if (Comment)
            {
                TextBuffer.FindNextWord(outElements, true);
            }

            // If this is a 'division' set unambiguous and insert comments
            if (Trust && TextBuffer.PointerNextChar > subStr.From && outElements.Count > 0)
            {
                TextBuffer.Status.ThisIsUnambiguous(this, outElements[outElements.Count - 1]);
            }

            return(true);
        }