Ejemplo n.º 1
0
        private XmlToken TokenizeSimple(string text, XmlTokenKind kind, XmlTokenizerMode nextMode)
        {
            XmlToken token = new XmlToken(kind, text.Length);

            position += text.Length;
            mode      = nextMode;
            return(token);
        }
Ejemplo n.º 2
0
        private XmlToken TokenizeEntity()
        {
            Debug.Assert(mode == XmlTokenizerMode.OutsideElement);
            Debug.Assert(input[position] == '&');
            XmlToken token = new XmlToken(XmlTokenKind.Entity, input.IndexOf(';', position) - position);

            position += token.Length;
            return(token);
        }
Ejemplo n.º 3
0
        private XmlToken TokenizeAttributeValue()
        {
            Debug.Assert(mode == XmlTokenizerMode.AfterAttributeEquals);
            int      closePosition = input.IndexOf(input[position], position + 1);
            XmlToken token         = new XmlToken(XmlTokenKind.AttributeValue, closePosition + 1 - position);

            position = closePosition + 1;
            mode     = XmlTokenizerMode.InsideElement;
            return(token);
        }
Ejemplo n.º 4
0
        private XmlToken NextToken()
        {
            if (position >= input.Length)
            {
                return(new XmlToken(XmlTokenKind.EOF, 0));
            }

            XmlToken token;

            switch (mode)
            {
            case XmlTokenizerMode.AfterAttributeEquals:
                token = TokenizeAttributeValue();
                break;

            case XmlTokenizerMode.AfterAttributeName:
                token = TokenizeSimple("=", XmlTokenKind.Equals, XmlTokenizerMode.AfterAttributeEquals);
                break;

            case XmlTokenizerMode.AfterOpen:
                token = TokenizeName(XmlTokenKind.ElementName, XmlTokenizerMode.InsideElement);
                break;

            case XmlTokenizerMode.InsideCData:
                token = TokenizeInsideCData();
                break;

            case XmlTokenizerMode.InsideComment:
                token = TokenizeInsideComment();
                break;

            case XmlTokenizerMode.InsideElement:
                token = TokenizeInsideElement();
                break;

            case XmlTokenizerMode.InsideProcessingInstruction:
                token = TokenizeInsideProcessingInstruction();
                break;

            case XmlTokenizerMode.OutsideElement:
                token = TokenizeOutsideElement();
                break;

            default:
                token = new XmlToken(XmlTokenKind.EOF, 0);
                break;
            }
            return(token);
        }
Ejemplo n.º 5
0
        private XmlToken TokenizeElementWhitespace()
        {
            int i;

            for (i = position; i < input.Length; i++)
            {
                if (!char.IsWhiteSpace(input[i]))
                {
                    break;
                }
            }
            XmlToken token = new XmlToken(XmlTokenKind.ElementWhitespace, i - position);

            position = i;
            return(token);
        }
Ejemplo n.º 6
0
        private XmlToken TokenizeInsideComment()
        {
            Debug.Assert(mode == XmlTokenizerMode.InsideComment);
            int tokenend = input.IndexOf("-->", position);

            if (position == tokenend)
            {
                position += "-->".Length;
                mode      = XmlTokenizerMode.OutsideElement;
                return(new XmlToken(XmlTokenKind.CommentEnd, "-->".Length));
            }
            else
            {
                XmlToken token = new XmlToken(XmlTokenKind.CommentText, tokenend - position);
                position = tokenend;
                return(token);
            }
        }
Ejemplo n.º 7
0
        private XmlToken TokenizeInsideCData()
        {
            Debug.Assert(mode == XmlTokenizerMode.InsideCData);
            int tokenend = input.IndexOf("]]>", position);

            if (position == tokenend)
            {
                position += "]]>".Length;
                mode      = XmlTokenizerMode.OutsideElement;
                return(new XmlToken(XmlTokenKind.CDataEnd, "]]>".Length));
            }
            else
            {
                XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position);
                position = tokenend;
                return(token);
            }
        }
Ejemplo n.º 8
0
        private XmlToken TokenizeInsideProcessingInstruction()
        {
            Debug.Assert(mode == XmlTokenizerMode.InsideProcessingInstruction);
            int tokenend = input.IndexOf("?>", position);

            if (position == tokenend)
            {
                position += "?>".Length;
                mode      = XmlTokenizerMode.OutsideElement;
                return(new XmlToken(XmlTokenKind.CloseProcessingInstruction, "?>".Length));
            }
            else
            {
                XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position);
                position = tokenend;
                return(token);
            }
        }
Ejemplo n.º 9
0
        private XmlToken TokenizeName(XmlTokenKind kind, XmlTokenizerMode nextMode)
        {
            Debug.Assert(mode == XmlTokenizerMode.AfterOpen || mode == XmlTokenizerMode.InsideElement);
            int i;

            for (i = position; i < input.Length; i++)
            {
                if (!IsNameCharacter(input[i]))
                {
                    break;
                }
            }
            XmlToken token = new XmlToken(kind, i - position);

            mode     = nextMode;
            position = i;
            return(token);
        }
Ejemplo n.º 10
0
        //// We break on newlines because that makes it easier for us
        //// to ignore the space after comments
        //private Token TokenizeWhitespaceContent()
        //{
        //    Debug.Assert(char.IsWhiteSpace(input[position]));
        //    bool sawNewline = false;
        //    int i;
        //    for (i = position; i < input.Length; i++) {
        //        if (!char.IsWhiteSpace(input[i])) {
        //            break;
        //        } else if (input[i] == '\n' || input[i] == '\r') {
        //            sawNewline = true;
        //        } else if (sawNewline) {
        //            break;
        //        }
        //    }
        //    Token token = new Token(TokenKind.WhitespaceContent, i - position);
        //    position = i;
        //    return token;
        //}


        private XmlToken TokenizeText()
        {
            Debug.Assert(input[position] != '<');
            Debug.Assert(input[position] != '&');
            Debug.Assert(mode == XmlTokenizerMode.OutsideElement);
            int i;

            for (i = position; i < input.Length; i++)
            {
                if (input[i] == '<' || input[i] == '&')
                {
                    break;
                }
            }
            XmlToken token = new XmlToken(XmlTokenKind.TextContent, i - position);

            position = i;
            return(token);
        }
Ejemplo n.º 11
0
        private static Color ColorForToken(XmlToken token, string tokenText)
        {
            Color color = Colors.Black;

            switch (token.Kind)
            {
            case XmlTokenKind.Open:
            case XmlTokenKind.OpenClose:
            case XmlTokenKind.Close:
            case XmlTokenKind.SelfClose:
            case XmlTokenKind.CommentBegin:
            case XmlTokenKind.CommentEnd:
            case XmlTokenKind.CDataBegin:
            case XmlTokenKind.CDataEnd:
            case XmlTokenKind.Equals:
            case XmlTokenKind.OpenProcessingInstruction:
            case XmlTokenKind.CloseProcessingInstruction:
            case XmlTokenKind.AttributeValue:
                color = Colors.Blue;
                break;

            case XmlTokenKind.ElementName:
                color = Colors.Brown;
                break;

            case XmlTokenKind.TextContent:
                color = Colors.Black;
                break;

            case XmlTokenKind.AttributeName:
            case XmlTokenKind.Entity:
                color = Colors.Red;
                break;

            case XmlTokenKind.CommentText:
                color = Colors.Green;
                break;
            }

            return(color);
        }
Ejemplo n.º 12
0
 private XmlToken TokenizeInsideProcessingInstruction()
 {
     Debug.Assert(mode == XmlTokenizerMode.InsideProcessingInstruction);
     int tokenend = input.IndexOf("?>", position);
     if (position == tokenend) {
         position += "?>".Length;
         mode = XmlTokenizerMode.OutsideElement;
         return new XmlToken(XmlTokenKind.CloseProcessingInstruction, "?>".Length);
     } else {
         XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position);
         position = tokenend;
         return token;
     }
 }
Ejemplo n.º 13
0
 private XmlToken TokenizeName(XmlTokenKind kind, XmlTokenizerMode nextMode)
 {
     Debug.Assert(mode == XmlTokenizerMode.AfterOpen || mode == XmlTokenizerMode.InsideElement);
     int i;
     for (i = position; i < input.Length; i++) {
         if (!IsNameCharacter(input[i])) {
             break;
         }
     }
     XmlToken token = new XmlToken(kind, i - position);
     mode = nextMode;
     position = i;
     return token;
 }
Ejemplo n.º 14
0
 private XmlToken TokenizeInsideCData()
 {
     Debug.Assert(mode == XmlTokenizerMode.InsideCData);
     int tokenend = input.IndexOf("]]>", position);
     if (position == tokenend) {
         position += "]]>".Length;
         mode = XmlTokenizerMode.OutsideElement;
         return new XmlToken(XmlTokenKind.CDataEnd, "]]>".Length);
     } else {
         XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position);
         position = tokenend;
         return token;
     }
 }
Ejemplo n.º 15
0
 private XmlToken TokenizeInsideComment()
 {
     Debug.Assert(mode == XmlTokenizerMode.InsideComment);
     int tokenend = input.IndexOf("-->", position);
     if (position == tokenend) {
         position += "-->".Length;
         mode = XmlTokenizerMode.OutsideElement;
         return new XmlToken(XmlTokenKind.CommentEnd, "-->".Length);
     } else {
         XmlToken token = new XmlToken(XmlTokenKind.CommentText, tokenend - position);
         position = tokenend;
         return token;
     }
 }
Ejemplo n.º 16
0
 private XmlToken TokenizeEntity()
 {
     Debug.Assert(mode == XmlTokenizerMode.OutsideElement);
     Debug.Assert(input[position] == '&');
     XmlToken token = new XmlToken(XmlTokenKind.Entity, input.IndexOf(';', position) - position);
     position += token.Length;
     return token;
 }
Ejemplo n.º 17
0
 private XmlToken TokenizeElementWhitespace()
 {
     int i;
     for (i = position; i < input.Length; i++) {
         if (!char.IsWhiteSpace(input[i])) {
             break;
         }
     }
     XmlToken token = new XmlToken(XmlTokenKind.ElementWhitespace, i - position);
     position = i;
     return token;
 }
Ejemplo n.º 18
0
 private XmlToken TokenizeAttributeValue()
 {
     Debug.Assert(mode == XmlTokenizerMode.AfterAttributeEquals);
     int closePosition = input.IndexOf(input[position], position + 1);
     XmlToken token = new XmlToken(XmlTokenKind.AttributeValue, closePosition + 1 - position);
     position = closePosition + 1;
     mode = XmlTokenizerMode.InsideElement;
     return token;
 }
Ejemplo n.º 19
0
        private XmlToken NextToken()
        {
            if (position >= input.Length)
                return new XmlToken(XmlTokenKind.EOF, 0);

            XmlToken token;
            switch (mode) {
                case XmlTokenizerMode.AfterAttributeEquals:
                    token = TokenizeAttributeValue();
                    break;
                case XmlTokenizerMode.AfterAttributeName:
                    token = TokenizeSimple("=", XmlTokenKind.Equals, XmlTokenizerMode.AfterAttributeEquals);
                    break;
                case XmlTokenizerMode.AfterOpen:
                    token = TokenizeName(XmlTokenKind.ElementName, XmlTokenizerMode.InsideElement);
                    break;
                case XmlTokenizerMode.InsideCData:
                    token = TokenizeInsideCData();
                    break;
                case XmlTokenizerMode.InsideComment:
                    token = TokenizeInsideComment();
                    break;
                case XmlTokenizerMode.InsideElement:
                    token = TokenizeInsideElement();
                    break;
                case XmlTokenizerMode.InsideProcessingInstruction:
                    token = TokenizeInsideProcessingInstruction();
                    break;
                case XmlTokenizerMode.OutsideElement:
                    token = TokenizeOutsideElement();
                    break;
                default:
                    token = new XmlToken(XmlTokenKind.EOF, 0);
                    break;
            }
            return token;
        }
Ejemplo n.º 20
0
 private XmlToken TokenizeSimple(string text, XmlTokenKind kind, XmlTokenizerMode nextMode)
 {
     XmlToken token = new XmlToken(kind, text.Length);
     position += text.Length;
     mode = nextMode;
     return token;
 }
Ejemplo n.º 21
0
 //// We break on newlines because that makes it easier for us 
 //// to ignore the space after comments
 //private Token TokenizeWhitespaceContent()
 //{
 //    Debug.Assert(char.IsWhiteSpace(input[position]));
 //    bool sawNewline = false;
 //    int i;
 //    for (i = position; i < input.Length; i++) {
 //        if (!char.IsWhiteSpace(input[i])) {
 //            break;
 //        } else if (input[i] == '\n' || input[i] == '\r') {
 //            sawNewline = true;
 //        } else if (sawNewline) {
 //            break;
 //        }
 //    }
 //    Token token = new Token(TokenKind.WhitespaceContent, i - position);
 //    position = i;
 //    return token;
 //}
 private XmlToken TokenizeText()
 {
     Debug.Assert(input[position] != '<');
     Debug.Assert(input[position] != '&');
     Debug.Assert(mode == XmlTokenizerMode.OutsideElement);
     int i;
     for (i = position; i < input.Length; i++) {
         if (input[i] == '<' || input[i] == '&') {
             break;
         }
     }
     XmlToken token = new XmlToken(XmlTokenKind.TextContent, i - position);
     position = i;
     return token;
 }
Ejemplo n.º 22
0
        private static Color ColorForToken(XmlToken token, string tokenText)
        {
            Color color = Colors.Black;
            switch (token.Kind)
            {
                case XmlTokenKind.Open:
                case XmlTokenKind.OpenClose:
                case XmlTokenKind.Close:
                case XmlTokenKind.SelfClose:
                case XmlTokenKind.CommentBegin:
                case XmlTokenKind.CommentEnd:
                case XmlTokenKind.CDataBegin:
                case XmlTokenKind.CDataEnd:
                case XmlTokenKind.Equals:
                case XmlTokenKind.OpenProcessingInstruction:
                case XmlTokenKind.CloseProcessingInstruction:
                case XmlTokenKind.AttributeValue:
                    color = Colors.Blue;
                    break;
                case XmlTokenKind.ElementName:
                    color = Colors.Brown;
                    break;
                case XmlTokenKind.TextContent:
                    color = Colors.Black;
                    break;
                case XmlTokenKind.AttributeName:
                case XmlTokenKind.Entity:
                    color = Colors.Red;
                    break;
                case XmlTokenKind.CommentText:
                    color = Colors.Green;
                    break;
            }

            return color;
        }