Пример #1
0
        private void ParseXmlEndTag(XmlElement xmlElement)
        {
            // XmlEndTag -> "<" "/" XmlName ">".

            if (_scanner.EOF)
            {
                return;
            }

            MatchToken(TokenType.XmlTagStart);
            MatchToken(TokenType.XmlForwardSlash);

            //TODO use ParseXmlName instead of just getting the image
            if (CurrentTokenType == TokenType.XmlTagName)
            {
                if (xmlElement.Name != _scanner.CurrentToken.Image)
                {
                    AddError("Mismatched element start and end tags");
                }
                _scanner.GetToken();
            }

            if (CurrentTokenType == TokenType.XmlTagEnd)
            {
                xmlElement.Position.End = GetCurrentTokenPosition();
                xmlElement.EndTagPosition = GetCurrentTokenPosition();
                xmlElement.IsComplete = true;
                _scanner.GetToken();
            }
            else
            {
                AddError("Expected '>'");
            }
        }
Пример #2
0
        private void ParseXmlRestElement(XmlElement xmlElement)
        {
            // XmlRestElement -> "/" ">"
            //                -> ">" { Content } XmlEndTag.

            if (CurrentTokenType == TokenType.XmlForwardSlash)
            {
                xmlElement.IsSelfClosing = true;

                MatchToken(TokenType.XmlForwardSlash);
                if (CurrentTokenType == TokenType.XmlTagEnd)
                {
                    xmlElement.Position.End = GetCurrentTokenPosition();
                    xmlElement.IsComplete = true;

                    _scanner.GetToken();
                }
            }
            else if (CurrentTokenType == TokenType.XmlTagEnd)
            {
                MatchToken(TokenType.XmlTagEnd);

                xmlElement.Position.End = GetCurrentTokenPosition();

                while (!(CurrentTokenType == TokenType.XmlTagStart &&
                    _scanner.PeekToken(1) != null && _scanner.PeekToken(1).Type == TokenType.XmlForwardSlash))
                {
                    List<AstNode> content = ParseContent();
                    xmlElement.Content.AddRange(content);

                    // Set the parent property on the XmlElement
                    foreach (AstNode astNode in content)
                    {
                        if (astNode is XmlElement)
                        {
                            ((XmlElement)astNode).Parent = xmlElement;
                        }
                    }

                    //TODO write some comments
                    if (content.Count == 0 && !_scanner.EOF)
                    {
                        AddError("The parser has been pre-emptively terminated because it appears " +
                            "as if the parser is stuck. [In ParseXmlRestElement()]");
                        break;
                    }

                    if (_scanner.EOF)
                    {
                        AddError(string.Format("Expected closing tag for opening tag '{0}'.", xmlElement.Name));
                        break;
                    }
                }

                ParseXmlEndTag(xmlElement);
            }
            else
            {
                AddError("Expected end of XML element");
            }
        }
Пример #3
0
        private XmlElement ParseXmlElement()
        {
            // XmlElement -> "<" XmlName { XmlAttribute | NVStatement } XmlRestElement.

            Position startPos = GetCurrentTokenPosition();

            MatchToken(TokenType.XmlTagStart);

            string elementName;
            if (CurrentTokenType == TokenType.XmlTagName)
            {
                elementName = _scanner.CurrentToken.Image;
                _scanner.GetToken();
            }
            else
            {
                AddError("Expected XML tag name.");
                XmlElement emptyXmlElement = new XmlElement("");
                emptyXmlElement.Position = startPos;

                // Consume TokenType.XmlAttributeMemberSelect tokens because these separate this partial element and the next node
                while (CurrentTokenType == TokenType.XmlAttributeMemberSelect)
                {
                    _scanner.GetToken();
                }

                return emptyXmlElement;
            }

            XmlElement xmlElement = new XmlElement(elementName);
            startPos.End = GetCurrentTokenPosition();
            xmlElement.Position = startPos;

            if (CurrentTokenIn(TokenType.NVDirectiveHash, TokenType.NVDollar))
            {
                AstNode statement = ParseNVStatement();
                if (statement != null)
                {
                    xmlElement.Attributes.Add(statement);
                }
            }

            // Consume TokenType.XmlAttributeMemberSelect tokens
            while (CurrentTokenType == TokenType.XmlAttributeMemberSelect)
            {
                // Do this before getting the next token because the next token may not belong to this XML element
                xmlElement.Position.End = GetCurrentTokenPosition().Start;

                _scanner.GetToken();
            }

            while (CurrentTokenType == TokenType.XmlAttributeName)
            {
                XmlAttribute xmlAttribute = ParseXmlAttribute();
                if (xmlAttribute != null)
                {
                    //TODO xmlAttribute.Position = GetCurrentTokenPosition();
                    xmlElement.Attributes.Add(xmlAttribute);
                }
                else if (!_scanner.EOF)
                {
                    //TODO write some comments
                    AddError("The parser has been pre-emptively terminated because it appears " +
                        "as if the parser is stuck. [In ParseXmlElement()]");
                    break;
                }

                xmlElement.Position.End = GetCurrentTokenPosition().Start;

                // Consume TokenType.XmlAttributeMemberSelect tokens
                while (CurrentTokenType == TokenType.XmlAttributeMemberSelect)
                {
                    // Do this before getting the next token because the next token may not belong to this XML element
                    xmlElement.Position.End = GetCurrentTokenPosition().Start;

                    _scanner.GetToken();
                }
            }

            if (_scanner.EOF)
            {
                // Include the characters right to the end
                xmlElement.Position.End = GetCurrentTokenPosition();
            }

            ParseXmlRestElement(xmlElement);

            return xmlElement;
        }