コード例 #1
0
        private static void CloseTag(string name, SymbolScanner scanner, Stack <XamlObjectNode> objStack)
        {
            XamlObjectNode objectNode = null;

            List <XamlObjectNode> children = new List <XamlObjectNode>();

            while (objStack.Count > 0)
            {
                objectNode = objStack.Peek();
                if (objectNode.Name == name)
                {
                    objectNode.LineNumberEnd = scanner.LineNumber + 1;
                    // it must be point to the next symbol so '-1'
                    objectNode.LinePositionEnd = scanner.CharPosition;
                    foreach (XamlObjectNode node in children)
                    {
                        objectNode.AddChild(node);
                    }

                    objectNode.SetState(XamlNodeBase.EState.Closed);

                    //remove found node from stack
                    objStack.Pop();
                    break;
                }

                children.Add(objectNode);
                objectNode = objStack.Pop();
            }
        }
コード例 #2
0
        public XamlMainObjectNode Parse(StreamReader fileStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException(nameof(fileStream));
            }

            Stack <XamlObjectNode> objStack    = new Stack <XamlObjectNode>();
            XamlObjectNode         currentNode = null;
            XamlMainObjectNode     mainObject  = null;

            SymbolScanner scanner = new SymbolScanner(fileStream);

            int lineNumberStart   = 0;
            int linePositionStart = 0;
            //long newLineStreamPosition = fileStream.BaseStream.Position;
            string ident;

            //bool tagBracketClosed = false;
            while (!fileStream.EndOfStream)
            {
                scanner.SkipSpaces();
                char ch = scanner.GetSymbol();
                if (ch == '\0')
                {
                    break;
                }

                if (ch == SymbolScanner.SymbolStartTag)
                {
                    //tagBracketClosed = false;
                    lineNumberStart = scanner.LineNumber;
                    //open tag symbol must be start not the next symbol
                    linePositionStart = scanner.CharPosition - 1;
                    char chNext = scanner.NextSymbol;
                    if (chNext == SymbolScanner.SymbolCloseTag)
                    {
                        scanner.SkipSymbol();
                        ident = scanner.GetName();
                        if (DoEndTag(ident, scanner))
                        {
                            CloseTag(ident, scanner, objStack);
                        }
                    }
                    else if (chNext == SymbolScanner.SymbolStartComment)
                    {
                        string comment = scanner.ReadComment();
                        Trace.WriteLine($"{lineNumberStart + 1}:{linePositionStart + 1} Comment:{comment}");
                        XamlCommentNode commentNode = new XamlCommentNode
                        {
                            LineNumberStart   = lineNumberStart + 1,
                            LinePositionStart = linePositionStart + 1,
                            Comment           = comment
                        };
                        commentNode.LineNumberEnd = scanner.LineNumber + 1;
                        //end position is outside comment tag
                        commentNode.LinePositionEnd = scanner.CharPosition;
                        objStack.Push(commentNode);
                    }
                    else
                    {
                        char breakSymbol;
                        ident       = scanner.GetName();
                        breakSymbol = scanner.NextSymbol;
                        //ident = GetIdent(fileStream, out breakSymbol);
                        Trace.WriteLine($"{lineNumberStart + 1}:{linePositionStart + 1} Start Tag:{ident}");
                        if (mainObject == null)
                        {
                            mainObject = new XamlMainObjectNode {
                                LineNumberStart = lineNumberStart + 1, LinePositionStart = linePositionStart + 1
                            };
                            mainObject.Name = ident;
                            currentNode     = mainObject;
                            //root.MainObject = mainObject;
                            objStack.Push(mainObject);
                        }
                        else
                        {
                            currentNode = new XamlObjectNode {
                                LineNumberStart = lineNumberStart + 1, LinePositionStart = linePositionStart + 1
                            };
                            currentNode.Name = ident;
                            objStack.Push(currentNode);
                        }

                        if (breakSymbol != SymbolScanner.SymbolEndTag)
                        {
                            scanner.SkipSpaces();
                            breakSymbol = scanner.NextSymbol;
                        }

                        if (breakSymbol == SymbolScanner.SymbolCloseTag)
                        {
                            //end of tag
                            if (DoEndTag(ident, scanner))
                            {
                                CloseTag(ident, scanner, objStack);
                            }
                        }
                        else if (breakSymbol != SymbolScanner.SymbolEndTag)
                        {
                            XamlObjectNode objectNode = null;
                            if (objStack.Count > 0)
                            {
                                objectNode = objStack.Peek();
                            }

                            bool isClosed = ReadAttributes(objectNode, scanner, breakSymbol);
                            if (isClosed)
                            {
                                //tagBracketClosed = true;
                                if (objectNode != null)
                                {
                                    objectNode.SetState(XamlNodeBase.EState.EndTagPresent);
                                    //objectNode.IsTagBracketClosed = true;
                                }

                                Trace.WriteLine("Tag end symbol for:" + ident);
                            }

                            if (scanner.NextSymbol == SymbolScanner.SymbolCloseTag)
                            {
                                //end of tag
                                if (DoEndTag(ident, scanner))
                                {
                                    CloseTag(ident, scanner, objStack);
                                }
                            }
                        }
                        else
                        {
                            // skip '>'
                            scanner.SkipSymbol();

                            XamlObjectNode objectNode = objStack.Peek();
                            objectNode.SetState(XamlNodeBase.EState.EndTagPresent);
                            Trace.WriteLine("Tag end symbol for:" + ident);
                        }

                        //chNext = scanner.NextSymbol;
                    }
                }
                else
                {
                    if (objStack.Count > 0)
                    {
                        XamlObjectNode objectNode = objStack.Peek();
                        if (objectNode.IsState(XamlNodeBase.EState.EndTagPresent))
                        {
                            lineNumberStart = scanner.LineNumber;
                            //text symbol must be current not the next symbol
                            linePositionStart = scanner.CharPosition - 1;

                            string text = scanner.ReadText(ch);
                            Trace.WriteLine("Text node:" + text);
                            XamlTextNode textNode = new XamlTextNode(text)
                            {
                                LineNumberStart = lineNumberStart + 1, LinePositionStart = linePositionStart + 1
                            };
                            textNode.LineNumberEnd = scanner.LastTextLineNumberEnd + 1;
                            // it must be the next symbol
                            textNode.LinePositionEnd = scanner.LastTextCharPositionEnd;
                            objectNode.SetState(XamlNodeBase.EState.TextNodePresent);
                            objectNode.AddChild(textNode);
                        }
                        else
                        {
                            //Trace.Write(ch);
                            _errorHandler?.Error($"Not expected symbol :'{ch}'", scanner);
                        }
                    }
                    else
                    {
                        //Trace.Write(ch);
                        _errorHandler?.Error($"Not expected symbol, no tag found :'{ch}'", scanner);
                    }
                }

                char chNext1 = scanner.CheckEndOfLine(scanner.NextSymbol);
            }

            if (objStack.Count > 0)
            {
                //ignore exception by non closed main state. Temporary
                if (mainObject != null && objStack.Count == 1)
                {
                    if (!mainObject.IsState(XamlNodeBase.EState.Closed))
                    {
                        return(mainObject);
                    }
                }
                _errorHandler?.Error($"unused {objStack.Count} nodes", scanner);
            }

            return(mainObject);
        }