internal static void AddExpressionNode (char c, IXmlParserContext context)
		{
			Debug.Assert (c != '@' && c!= '-', "AspNetExpressionState should not be passed a directive or comment");

			switch (c) {
			//DATABINDING EXPRESSION <%#
			case '#':
				context.Nodes.Push (new WebFormsBindingExpression (context.LocationMinus (3)));
				break;
			//RESOURCE EXPRESSION <%$
			case '$':
				context.Nodes.Push (new WebFormsResourceExpression (context.LocationMinus (3)));
				break;
			//RENDER EXPRESSION <%=
			case '=':
				context.Nodes.Push (new WebFormsRenderExpression (context.LocationMinus (3)));
				break;
			//HTML ENCODED EXPRESSION <%:
			case ':':
				context.Nodes.Push (new WebFormsHtmlEncodedExpression (context.LocationMinus (3)));
				break;
			// RENDER BLOCK
			default:
				context.Nodes.Push (new WebFormsRenderBlock (context.LocationMinus (3)));
				break;
			}
		}
        internal static void AddExpressionNode(char c, IXmlParserContext context)
        {
            Debug.Assert(c != '@' && c != '-', "AspNetExpressionState should not be passed a directive or comment");

            switch (c)
            {
            //DATABINDING EXPRESSION <%#
            case '#':
                context.Nodes.Push(new WebFormsBindingExpression(context.LocationMinus(3)));
                break;

            //RESOURCE EXPRESSION <%$
            case '$':
                context.Nodes.Push(new WebFormsResourceExpression(context.LocationMinus(3)));
                break;

            //RENDER EXPRESSION <%=
            case '=':
                context.Nodes.Push(new WebFormsRenderExpression(context.LocationMinus(3)));
                break;

            //HTML ENCODED EXPRESSION <%:
            case ':':
                context.Nodes.Push(new WebFormsHtmlEncodedExpression(context.LocationMinus(3)));
                break;

            // RENDER BLOCK
            default:
                context.Nodes.Push(new WebFormsRenderBlock(context.LocationMinus(3)));
                break;
            }
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 1) {
				switch (c) {
				case '(':
					context.StateTag = NONE_EXPLICIT;
					context.Nodes.Push (new RazorExplicitExpression (context.LocationMinus (2)));
					return null;

				default:
					context.StateTag = NONE_IMPLICIT;
					if (!(context.PreviousState is RazorSpeculativeState))
						context.Nodes.Push (new RazorImplicitExpression (context.LocationMinus (2)));
					break;
				}
			}

			switch (c)
			{
				case '(':
					if (context.StateTag == NONE_EXPLICIT)
						context.StateTag = INSIDE_BRACKET_EXPLICIT;
					else if (context.StateTag == NONE_IMPLICIT)
						context.StateTag = INSIDE_BRACKET_IMPLICIT;
					context.KeywordBuilder.Append (c);
					break;

				case ')':
					if (context.StateTag == NONE_EXPLICIT) {
						StateEngineService.EndCodeFragment<RazorExplicitExpression> (context);
						return Parent;
					}
					else if (context.StateTag != NONE_IMPLICIT) {
						if (context.KeywordBuilder.Length > 0)
							context.KeywordBuilder.Remove (0, 1);
						if (context.KeywordBuilder.Length == 0)
							context.StateTag = (context.StateTag == INSIDE_BRACKET_IMPLICIT ? NONE_IMPLICIT : NONE_EXPLICIT);
					}
					break;

				default:
					// Space between function's name and open bracket not allowed in razor, e.g. @Html.Raw (foo) 
					if (!(Char.IsLetterOrDigit (c) || allowedChars.Any (ch => ch == c))
							&& context.StateTag == NONE_IMPLICIT) {
						StateEngineService.EndCodeFragment<RazorImplicitExpression> (context, 1);
						rollback = String.Empty;
						if (Parent is RazorSpeculativeState)
							return Parent.Parent;
						return Parent;
					}
					break;
			}

			return null;
		}
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 1)
            {
                context.Nodes.Push(new RazorComment(context.LocationMinus(2)));
                return(null);
            }

            switch (context.StateTag)
            {
            case NOMATCH:
                if (c == '*')
                {
                    context.StateTag = STAR;
                }
                break;

            case STAR:
                if (c == '@')
                {
                    StateEngineService.EndCodeFragment <RazorComment> (context);
                    return(Parent);
                }
                else
                {
                    context.StateTag = NOMATCH;
                }
                break;
            }

            return(null);
        }
Example #5
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 1)
            {
                bracketsBuilder.Clear();
                var block = context.Nodes.FirstOrDefault(n => n is RazorCodeBlock);
                if (block == null)
                {
                    var razorBlock = new RazorCodeBlock(context.LocationMinus(2));
                    context.Nodes.Push(razorBlock);
                    CorrespondingBlock = razorBlock;
                }
                else
                {
                    CorrespondingBlock = block as RazorCodeFragment;
                }
            }

            switch (c)
            {
            case '{':
                return(ParseOpeningBracket(c, context));

            case '}':
                return(ParseClosingBracket <RazorCodeBlock> (c, context, Parent));
            }

            return(base.PushChar(c, context, ref rollback));
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 1) {
				context.Nodes.Push (new XCData (context.LocationMinus ("<![CDATA[".Length + 1)));
			}
			
			if (c == ']') {
				//make sure we know when there are two ']' chars together
				if (context.StateTag == NOMATCH)
					context.StateTag = SINGLE_BRACKET;
				else
					context.StateTag = DOUBLE_BRACKET;
				
			} else if (c == '>' && context.StateTag == DOUBLE_BRACKET) {
				// if the ']]' is followed by a '>', the state has ended
				// so attach a node to the DOM and end the state
				var cdata = (XCData) context.Nodes.Pop ();
				
				if (context.BuildTree) {
					cdata.End (context.Location);
					((XContainer) context.Nodes.Peek ()).AddChildNode (cdata); 
				}
				return Parent;
			} else {
				// not any part of a ']]>', so make sure matching is reset
				context.StateTag = NOMATCH;
			}
			
			return null;
		}
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 1) {
				context.Nodes.Push (new XProcessingInstruction (context.LocationMinus ("<?".Length)));
			}
			
			if (c == '?') {
				if (context.StateTag == NOMATCH) {
					context.StateTag = QUESTION;
					return null;
				}
			} else if (c == '>' && context.StateTag == QUESTION) {
				// if the '?' is followed by a '>', the state has ended
				// so attach a node to the DOM and end the state
				var xpi = (XProcessingInstruction) context.Nodes.Pop ();
				
				if (context.BuildTree) {
					xpi.End (context.Location);
					((XContainer) context.Nodes.Peek ()).AddChildNode (xpi); 
				}
				return Parent;
			} else {
				context.StateTag = NOMATCH;
			}
			
			return null;
		}
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 1)
            {
                context.Nodes.Push(new XProcessingInstruction(context.LocationMinus("<?".Length)));
            }

            if (c == '?')
            {
                if (context.StateTag == NOMATCH)
                {
                    context.StateTag = QUESTION;
                    return(null);
                }
            }
            else if (c == '>' && context.StateTag == QUESTION)
            {
                // if the '?' is followed by a '>', the state has ended
                // so attach a node to the DOM and end the state
                var xpi = (XProcessingInstruction)context.Nodes.Pop();

                if (context.BuildTree)
                {
                    xpi.End(context.Location);
                    ((XContainer)context.Nodes.Peek()).AddChildNode(xpi);
                }
                return(Parent);
            }
            else
            {
                context.StateTag = NOMATCH;
            }

            return(null);
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 0)
				context.StateTag = 0;
			
			if (c == '<') {
				if (context.StateTag == 0) {
					context.StateTag++;
					return null;
				}
			}
			if (context.StateTag > 0) {
				if (CLOSE[context.StateTag] == c) {
					context.StateTag++;
					if (context.StateTag == CLOSE.Length) {
						var el = (XElement) context.Nodes.Pop ();
						var closing = new XClosingTag (new XName ("script"), context.LocationMinus (CLOSE.Length));
						closing.End (context.Location);
						el.Close (closing);
						rollback = string.Empty;
						return Parent;
					}
				} else {
					context.StateTag = 0;
				}
			}
			return null;
		}
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 1)
            {
                context.Nodes.Push(new WebFormsServerComment(context.LocationMinus(context.CurrentStateLength + "<%--".Length)));
            }

            switch (context.StateTag)
            {
            case NOMATCH:
                if (c == '-')
                {
                    context.StateTag = SINGLE_DASH;
                }
                break;

            case SINGLE_DASH:
                if (c == '-')
                {
                    context.StateTag = DOUBLE_DASH;
                }
                else
                {
                    context.StateTag = NOMATCH;
                }
                break;

            case DOUBLE_DASH:
                if (c == '%')
                {
                    context.StateTag = PERCENT;
                }
                else
                {
                    context.StateTag = NOMATCH;
                }
                break;

            case PERCENT:
                if (c == '>')
                {
                    var comment = (WebFormsServerComment)context.Nodes.Pop();
                    comment.End(context.Location);
                    if (context.BuildTree)
                    {
                        XObject ob = context.Nodes.Peek();
                        if (ob is XContainer)
                        {
                            ((XContainer)ob).AddChildNode(comment);
                        }
                        //FIXME: add to other kinds of node, e.g. if used within a tag
                    }
                    return(Parent);
                }
                break;
            }

            return(null);
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			var directive = context.Nodes.Peek () as WebFormsDirective;
			
			if (directive == null || directive.IsComplete) {
				directive = new WebFormsDirective (context.LocationMinus (4)); // 4 == <%@ + current char
				context.Nodes.Push (directive);
			}
			
			if (c == '<') {
				context.LogError ("Unexpected '<' in directive.");
				rollback = string.Empty;
				return Parent;
			}
			
			Debug.Assert (!directive.IsComplete);
			
			if (context.StateTag != ENDING && c == '%') {
				context.StateTag = ENDING;
				return null;
			}
			
			
			if (context.StateTag == ENDING) {
				if (c == '>') {
					//have already checked that directive is not null, i.e. top of stack is our directive
					context.Nodes.Pop ();
					
					if (!directive.IsNamed) {
						context.LogError ("Directive closed prematurely.");
					} else {
						directive.End (context.Location);
						if (context.BuildTree) {
							var container = (XContainer)context.Nodes.Peek ();
							container.AddChildNode (directive);
						}
					}
					return Parent;
				}
				//ending but not '>'? Error; go to end.
			} else if (char.IsLetter (c)) {
				rollback = string.Empty;
				if (!directive.IsNamed) {
					return NameState;
				} else {
					return AttributeState;
				}
			} else if (char.IsWhiteSpace (c))
				return null;
			
			rollback = string.Empty;
			context.LogError ("Unexpected character '" + c + "' in tag.");
			return Parent;
		}
Example #12
0
        XmlParserState SwitchToStatement(IXmlParserContext context, ref string rollback)
        {
            string key = context.KeywordBuilder.ToString();
            var    stm = new RazorStatement(context.LocationMinus(key.Length + 2))
            {
                Name = key.Trim()
            };

            context.Nodes.Push(stm);
            rollback = String.Empty;
            return(EnsureSetAndAdopted <RazorStatementState> (ref statementState));
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			string key;

			switch (context.StateTag) {
			case UNKNOWN:
				context.KeywordBuilder.Append (c);
				key = context.KeywordBuilder.ToString ();
				if (!RazorSymbols.CanBeStatementOrDirective (key)) {
					context.Nodes.Push (new RazorImplicitExpression (context.LocationMinus (key.Length + 1)));
					rollback = String.Empty;
					return EnsureSetAndAdopted<RazorExpressionState> (ref expressionState);
				}
				if (key == "using")
					context.StateTag = USING;
				else if (RazorSymbols.IsDirective (key))
					context.StateTag = POSSIBLE_DIRECTIVE;
				else if (RazorSymbols.IsStatement (key))
					context.StateTag = POSSIBLE_STATEMENT;

				break;

			// Using can be either statement: @using (resource) {}, or directive: @using System.IO
			case USING:
				if (c == '(' || c == '\n')
					return SwitchToStatement (context, ref rollback);
				else if (Char.IsLetterOrDigit(c))
					return SwitchToDirective (context, ref rollback);

				context.KeywordBuilder.Append (c);
				break;

			case POSSIBLE_STATEMENT:
				if (Char.IsWhiteSpace (c) || c == '{' || c == '(')
					return SwitchToStatement(context, ref rollback);

				context.KeywordBuilder.Append (c);
				context.StateTag = UNKNOWN;
				break;

			case POSSIBLE_DIRECTIVE:
				if (Char.IsWhiteSpace (c) || c == '{')
					return SwitchToDirective (context, ref rollback);

				context.KeywordBuilder.Append (c);
				context.StateTag = UNKNOWN;
				break;
			}

			return null;
		}
Example #14
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 1)
            {
                context.Nodes.Push(new XComment(context.LocationMinus("<!--".Length + 1)));
            }

            if (c == '-')
            {
                //make sure we know when there are two '-' chars together
                if (context.StateTag == NOMATCH)
                {
                    context.StateTag = SINGLE_DASH;
                }
                else
                {
                    context.StateTag = DOUBLE_DASH;
                }
            }
            else if (context.StateTag == DOUBLE_DASH)
            {
                if (c == '>')
                {
                    // if the '--' is followed by a '>', the state has ended
                    // so attach a node to the DOM and end the state
                    var comment = (XComment)context.Nodes.Pop();

                    if (context.BuildTree)
                    {
                        comment.End(context.Location);
                        ((XContainer)context.Nodes.Peek()).AddChildNode(comment);
                    }

                    rollback = string.Empty;
                    return(Parent);
                }
                else
                {
                    context.LogWarning("The string '--' should not appear within comments.");
                    context.StateTag = NOMATCH;
                }
            }
            else
            {
                // not any part of a '-->', so make sure matching is reset
                context.StateTag = NOMATCH;
            }

            return(null);
        }
Example #15
0
        XmlParserState SwitchToDirective(IXmlParserContext context, ref string rollback)
        {
            string key  = context.KeywordBuilder.ToString();
            string name = key.Trim();
            var    dir  = new RazorDirective(context.LocationMinus(key.Length + 2))
            {
                Name = name,
                IsSimpleDirective = RazorSymbols.IsSimpleDirective(name)
            };

            context.Nodes.Push(dir);
            rollback = String.Empty;
            return(EnsureSetAndAdopted <RazorDirectiveState> (ref directiveState));
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 1) {
				context.Nodes.Push (new WebFormsServerComment (context.LocationMinus (context.CurrentStateLength + "<%--".Length)));
			}
			
			switch (context.StateTag) {
			case NOMATCH:
				if (c == '-')
					context.StateTag = SINGLE_DASH;
				break;
				
			case SINGLE_DASH:
				if (c == '-')
					context.StateTag = DOUBLE_DASH;
				else
					context.StateTag = NOMATCH;
				break;
				
			case DOUBLE_DASH:
				if (c == '%')
					context.StateTag = PERCENT;
				else
					context.StateTag = NOMATCH;
				break;
				
			case PERCENT:
				if (c == '>') {
					var comment = (WebFormsServerComment) context.Nodes.Pop ();
					comment.End (context.Location);
					if (context.BuildTree) {
						XObject ob = context.Nodes.Peek ();
						if (ob is XContainer) {
							((XContainer)ob).AddChildNode (comment);
						}
					 	//FIXME: add to other kinds of node, e.g. if used within a tag
					}
					return Parent;
				}
				break;
			}
			
			return null;
		}
        XmlParserState SwitchToContinuationStatement(IXmlParserContext context, string key)
        {
            string name   = key.Trim();
            int    length = key.Length;

            if (name == "else if")
            {
                length = key.Length - 1;
            }
            else if (name == "else")
            {
                length = key.Length + 1;
            }
            var stm = new RazorStatement(context.LocationMinus(length))
            {
                Name = name
            };

            context.Nodes.Push(stm);
            return(EnsureSetAndAdopted <RazorStatementState> (ref statementState));
        }
Example #18
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 1)
            {
                context.Nodes.Push(new XCData(context.LocationMinus("<![CDATA[".Length + 1)));
            }

            if (c == ']')
            {
                //make sure we know when there are two ']' chars together
                if (context.StateTag == NOMATCH)
                {
                    context.StateTag = SINGLE_BRACKET;
                }
                else
                {
                    context.StateTag = DOUBLE_BRACKET;
                }
            }
            else if (c == '>' && context.StateTag == DOUBLE_BRACKET)
            {
                // if the ']]' is followed by a '>', the state has ended
                // so attach a node to the DOM and end the state
                var cdata = (XCData)context.Nodes.Pop();

                if (context.BuildTree)
                {
                    cdata.End(context.Location);
                    ((XContainer)context.Nodes.Peek()).AddChildNode(cdata);
                }
                return(Parent);
            }
            else
            {
                // not any part of a ']]>', so make sure matching is reset
                context.StateTag = NOMATCH;
            }

            return(null);
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 1) {
				bracketsBuilder.Clear ();
				var block = context.Nodes.FirstOrDefault (n => n is RazorCodeBlock);
				if (block == null) {
					var razorBlock = new RazorCodeBlock (context.LocationMinus (2));
					context.Nodes.Push (razorBlock);
					CorrespondingBlock = razorBlock;
				} else
					CorrespondingBlock = block as RazorCodeFragment;
			}

			switch (c) {
			case '{':
				return ParseOpeningBracket (c, context);

			case '}':
				return ParseClosingBracket<RazorCodeBlock> (c, context, Parent);
			}

			return base.PushChar (c, context, ref rollback);
		}
Example #20
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 0)
            {
                context.StateTag = 0;
            }

            if (c == '<')
            {
                if (context.StateTag == 0)
                {
                    context.StateTag++;
                    return(null);
                }
            }
            if (context.StateTag > 0)
            {
                if (CLOSE[context.StateTag] == c)
                {
                    context.StateTag++;
                    if (context.StateTag == CLOSE.Length)
                    {
                        var el      = (XElement)context.Nodes.Pop();
                        var closing = new XClosingTag(new XName("script"), context.LocationMinus(CLOSE.Length));
                        closing.End(context.Location);
                        el.Close(closing);
                        rollback = string.Empty;
                        return(Parent);
                    }
                }
                else
                {
                    context.StateTag = 0;
                }
            }
            return(null);
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 1) {
				context.Nodes.Push (new XComment (context.LocationMinus ("<!--".Length + 1)));
			}
			
			if (c == '-') {
				//make sure we know when there are two '-' chars together
				if (context.StateTag == NOMATCH)
					context.StateTag = SINGLE_DASH;
				else
					context.StateTag = DOUBLE_DASH;
				
			} else if (context.StateTag == DOUBLE_DASH) {
				if (c == '>') {
					// if the '--' is followed by a '>', the state has ended
					// so attach a node to the DOM and end the state
					var comment = (XComment) context.Nodes.Pop ();
					
					if (context.BuildTree) {
						comment.End (context.Location);
						((XContainer) context.Nodes.Peek ()).AddChildNode (comment);
					}
					
					rollback = string.Empty;
					return Parent;
				} else {
					context.LogWarning ("The string '--' should not appear within comments.");
					context.StateTag = NOMATCH;
				}
			} else {
				// not any part of a '-->', so make sure matching is reset
				context.StateTag = NOMATCH;
			}
			
			return null;
		}
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (context.CurrentStateLength == 1) {
				context.Nodes.Push (new RazorComment (context.LocationMinus (2)));
				return null;
			}

			switch (context.StateTag) {
			case NOMATCH:
				if (c == '*')
					context.StateTag = STAR;
				break;

			case STAR:
				if (c == '@') {
					StateEngineService.EndCodeFragment<RazorComment> (context);
					return Parent;
				} else
					context.StateTag = NOMATCH;
				break;
			}

			return null;
		}
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			var ct = context.Nodes.Peek () as XClosingTag;
			
			if (ct == null) {
				Debug.Assert (context.CurrentStateLength == 1,
					"IncompleteNode must not be an XClosingTag when CurrentStateLength is 1");
				
				ct = new XClosingTag (context.LocationMinus (3)); //3 = </ and the current char
				context.Nodes.Push (ct);
			}
			
			//if tag closed
			if (c == '>') {
				context.Nodes.Pop ();
				
				if (ct.IsNamed) {
					ct.End (context.Location);
					
					// walk up tree of parents looking for matching tag
					int popCount = 0;
					bool found = false;
					foreach (XObject node in context.Nodes) {
						popCount++;
						XElement element = node as XElement;
						if (element != null && element.Name == ct.Name) {
							found = true;
							break;
						}
					}
					if (!found)
						popCount = 0;
					
					//clear the stack of intermediate unclosed tags
					while (popCount > 1) {
						XElement el = context.Nodes.Pop () as XElement;
						if (el != null)
							context.LogError (string.Format (
								"Unclosed tag '{0}' at line {1}, column {2}.", el.Name.FullName, el.Region.BeginLine, el.Region.BeginColumn),
								ct.Region);
						popCount--;
					}
					
					//close the start tag, if we found it
					if (popCount > 0) {
						if (context.BuildTree)
							((XElement) context.Nodes.Pop ()).Close (ct);
						else
							context.Nodes.Pop ();
					} else {
						context.LogError (
							"Closing tag '" + ct.Name.FullName + "' does not match any currently open tag.",
							ct.Region
						);
					}
				} else {
					context.LogError ("Closing tag ended prematurely.");
				}
				return Parent;
			}
			
			if (c == '<') {
				context.LogError ("Unexpected '<' in tag.", context.LocationMinus (1));
				context.Nodes.Pop ();
				rollback = string.Empty;
				return Parent;
			}

			if (XmlChar.IsWhitespace (c)) {
				return null;
			}
			
			if (!ct.IsNamed && (char.IsLetter (c) || c == '_')) {
				rollback = string.Empty;
				return NameState;
			}
			
			rollback = string.Empty;
			context.LogError ("Unexpected character '" + c + "' in closing tag.", context.LocationMinus (1));
			context.Nodes.Pop ();
			return Parent;
		}
Example #24
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            string key;

            switch (context.StateTag)
            {
            case UNKNOWN:
                context.KeywordBuilder.Append(c);
                key = context.KeywordBuilder.ToString();
                if (!RazorSymbols.CanBeStatementOrDirective(key))
                {
                    context.Nodes.Push(new RazorImplicitExpression(context.LocationMinus(key.Length + 1)));
                    rollback = String.Empty;
                    return(EnsureSetAndAdopted <RazorExpressionState> (ref expressionState));
                }
                if (key == "using")
                {
                    context.StateTag = USING;
                }
                else if (RazorSymbols.IsDirective(key))
                {
                    context.StateTag = POSSIBLE_DIRECTIVE;
                }
                else if (RazorSymbols.IsStatement(key))
                {
                    context.StateTag = POSSIBLE_STATEMENT;
                }

                break;

            // Using can be either statement: @using (resource) {}, or directive: @using System.IO
            case USING:
                if (c == '(' || c == '\n')
                {
                    return(SwitchToStatement(context, ref rollback));
                }
                else if (Char.IsLetterOrDigit(c))
                {
                    return(SwitchToDirective(context, ref rollback));
                }

                context.KeywordBuilder.Append(c);
                break;

            case POSSIBLE_STATEMENT:
                if (Char.IsWhiteSpace(c) || c == '{' || c == '(')
                {
                    return(SwitchToStatement(context, ref rollback));
                }

                context.KeywordBuilder.Append(c);
                context.StateTag = UNKNOWN;
                break;

            case POSSIBLE_DIRECTIVE:
                if (Char.IsWhiteSpace(c) || c == '{')
                {
                    return(SwitchToDirective(context, ref rollback));
                }

                context.KeywordBuilder.Append(c);
                context.StateTag = UNKNOWN;
                break;
            }

            return(null);
        }
Example #25
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            XElement element = context.Nodes.Peek() as XElement;

            if (element == null || element.IsComplete)
            {
                var parent = element;
                element        = new XElement(context.LocationMinus(2));            // 2 == < + current char
                element.Parent = parent;
                context.Nodes.Push(element);
            }

            if (c == '<')
            {
                if (element.IsNamed)
                {
                    context.LogError("Unexpected '<' in tag '" + element.Name.FullName + "'.");
                    Close(element, context, context.LocationMinus(1));
                }
                else
                {
                    context.LogError("Unexpected '<' in unnamed tag.");
                }

                rollback = string.Empty;
                return(Parent);
            }

            Debug.Assert(!element.IsComplete);

            if (element.IsClosed && c != '>')
            {
                if (char.IsWhiteSpace(c))
                {
                    context.LogWarning("Unexpected whitespace after '/' in self-closing tag.");
                    return(null);
                }
                context.LogError("Unexpected character '" + c + "' after '/' in self-closing tag.");
                context.Nodes.Pop();
                return(Parent);
            }

            //if tag closed
            if (c == '>')
            {
                if (context.StateTag == MAYBE_SELF_CLOSING)
                {
                    element.Close(element);
                }
                if (!element.IsNamed)
                {
                    context.LogError("Tag closed prematurely.");
                }
                else
                {
                    Close(element, context, context.Location);
                }
                return(Parent);
            }

            if (c == '/')
            {
                context.StateTag = MAYBE_SELF_CLOSING;
                return(null);
            }

            if (context.StateTag == ATTEMPT_RECOVERY)
            {
                if (XmlChar.IsWhitespace(c))
                {
                    context.StateTag = RECOVERY_FOUND_WHITESPACE;
                }
                return(null);
            }

            if (context.StateTag == RECOVERY_FOUND_WHITESPACE)
            {
                if (!XmlChar.IsFirstNameChar(c))
                {
                    return(null);
                }
            }

            context.StateTag = OK;

            if (!element.IsNamed && XmlChar.IsFirstNameChar(c))
            {
                rollback = string.Empty;
                return(NameState);
            }

            if (context.CurrentStateLength > 1 && XmlChar.IsFirstNameChar(c))
            {
                rollback = string.Empty;
                return(AttributeState);
            }

            if (XmlChar.IsWhitespace(c))
            {
                return(null);
            }

            context.LogError("Unexpected character '" + c + "' in tag.", context.LocationMinus(1));
            context.StateTag = ATTEMPT_RECOVERY;
            return(null);
        }
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            var ct = context.Nodes.Peek() as XClosingTag;

            if (ct == null)
            {
                Debug.Assert(context.CurrentStateLength == 1,
                             "IncompleteNode must not be an XClosingTag when CurrentStateLength is 1");

                ct = new XClosingTag(context.LocationMinus(3));                   //3 = </ and the current char
                context.Nodes.Push(ct);
            }

            //if tag closed
            if (c == '>')
            {
                context.Nodes.Pop();

                if (ct.IsNamed)
                {
                    ct.End(context.Location);

                    // walk up tree of parents looking for matching tag
                    int  popCount = 0;
                    bool found    = false;
                    foreach (XObject node in context.Nodes)
                    {
                        popCount++;
                        XElement element = node as XElement;
                        if (element != null && element.Name == ct.Name)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        popCount = 0;
                    }

                    //clear the stack of intermediate unclosed tags
                    while (popCount > 1)
                    {
                        XElement el = context.Nodes.Pop() as XElement;
                        if (el != null)
                        {
                            context.LogError(string.Format(
                                                 "Unclosed tag '{0}' at line {1}, column {2}.", el.Name.FullName, el.Region.BeginLine, el.Region.BeginColumn),
                                             ct.Region);
                        }
                        popCount--;
                    }

                    //close the start tag, if we found it
                    if (popCount > 0)
                    {
                        if (context.BuildTree)
                        {
                            ((XElement)context.Nodes.Pop()).Close(ct);
                        }
                        else
                        {
                            context.Nodes.Pop();
                        }
                    }
                    else
                    {
                        context.LogError(
                            "Closing tag '" + ct.Name.FullName + "' does not match any currently open tag.",
                            ct.Region
                            );
                    }
                }
                else
                {
                    context.LogError("Closing tag ended prematurely.");
                }
                return(Parent);
            }

            if (c == '<')
            {
                context.LogError("Unexpected '<' in tag.");
                rollback = string.Empty;
                return(Parent);
            }

            if (!ct.IsNamed && (char.IsLetter(c) || c == '_'))
            {
                rollback = string.Empty;
                return(NameState);
            }

            rollback = string.Empty;
            context.LogError("Unexpected character '" + c + "' in closing tag.");
            return(Parent);
        }
Example #27
0
 public static void EndCodeFragment <T> (IXmlParserContext context, int minus = 0) where T : XNode
 {
     EndCodeFragment <T> (context, context.LocationMinus(minus));
 }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			var att = context.Nodes.Peek () as XAttribute;

			//state has just been entered
			if (context.CurrentStateLength == 1)  {
				if (context.PreviousState is XmlNameState) {
					//error parsing name
					if (!att.IsNamed) {
						context.Nodes.Pop ();
						rollback = string.Empty;
						return Parent;
					}
					context.StateTag = GETTINGEQ;
				}
				else if (context.PreviousState is XmlAttributeValueState) {
					//Got value, so end attribute
					context.Nodes.Pop ();
					att.End (context.LocationMinus (1));
					IAttributedXObject element = (IAttributedXObject) context.Nodes.Peek ();
					element.Attributes.AddAttribute (att);
					rollback = string.Empty;
					return Parent;
				}
				else {
					//starting a new attribute
					Debug.Assert (att == null);
					Debug.Assert (context.StateTag == NAMING);
					att = new XAttribute (context.LocationMinus (1));
					context.Nodes.Push (att);
					rollback = string.Empty;
					return XmlNameState;
				}
			}

			if (context.StateTag == GETTINGEQ) {
				if (char.IsWhiteSpace (c)) {
					return null;
				}
				if (c == '=') {
					context.StateTag = GETTINGVAL;
					return null;
				}
				context.LogError ("Expecting = in attribute, got '" + c + "'.");
			} else if (context.StateTag == GETTINGVAL) {
				if (char.IsWhiteSpace (c)) {
					return null;
				}
				rollback = string.Empty;
				return AttributeValueState;
			} else if (c != '<') {
				//parent handles message for '<'
				context.LogError ("Unexpected character '" + c + "' in attribute.");
			}

			if (att != null)
				context.Nodes.Pop ();
			
			rollback = string.Empty;
			return Parent;
		}
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			var doc = context.Nodes.Peek () as XDocType;
			if (doc == null) {
				doc = new XDocType (context.LocationMinus ("<!DOCTYPE".Length + 1));
				context.Nodes.Push (doc);
			}
			
			if (!doc.RootElement.IsValid) {
				if (XmlChar.IsWhitespace (c))
					return null;
				else if (XmlChar.IsFirstNameChar (c)) {
					rollback = "";
					return nameState;
				}
			}
			else if (doc.PublicFpi == null) {
				if (context.StateTag == 0) {
					if (c == 's' || c == 'S') {
						context.StateTag = 1;
						return null;
					} else if (c == 'p' || c == 'P') {
						context.StateTag = -1;
						return null;
					} if (XmlChar.IsWhitespace (c)) {
						return null;
					}
				} else if (Math.Abs (context.StateTag) < 6) {
					if (context.StateTag > 0) {
						if ("YSTEM"[context.StateTag - 1] == c || "ystem"[context.StateTag - 1] == c) {
							context.StateTag++;
							if (context.StateTag == 6) {
								context.StateTag = 0;
								doc.PublicFpi = "";
							}
							return null;
						}
					} else {
						int absState = Math.Abs (context.StateTag) - 1;
						if ("UBLIC"[absState] == c || "ublic"[absState] == c) {
							context.StateTag--;
							return null;
						}
					}
				} else {
					if (context.KeywordBuilder.Length == 0) {
						if (XmlChar.IsWhitespace (c))
							return null;
						else if (c == '"') {
							context.KeywordBuilder.Append (c);
							return null;
						}
					} else {
						if (c == '"') {
							context.KeywordBuilder.Remove (0,1);
							doc.PublicFpi = context.KeywordBuilder.ToString ();
							context.KeywordBuilder.Length = 0;
							context.StateTag = 0;
						} else {
							context.KeywordBuilder.Append (c);
						}
						return null;
					}
				}
			}
			else if (doc.Uri == null) {
				if (context.KeywordBuilder.Length == 0) {
					if (XmlChar.IsWhitespace (c))
						return null;
					else if (c == '"') {
						context.KeywordBuilder.Append (c);
						return null;
					}
				} else {
					if (c == '"') {
						context.KeywordBuilder.Remove (0,1);
						doc.Uri = context.KeywordBuilder.ToString ();
						context.KeywordBuilder.Length = 0;
					} else {
						context.KeywordBuilder.Append (c);
					}
					return null;
				}
			}
			else if (doc.InternalDeclarationRegion.EndLine <= 0) {
				if (XmlChar.IsWhitespace (c))
						return null;
				switch (context.StateTag) {
				case 0:
					if (c == '[') {
						doc.InternalDeclarationRegion = new DocumentRegion (context.Location, DocumentLocation.Empty);
						context.StateTag = 1;
						return null;
					}
					break;
				case 1:
					if (c == '<') {
						context.StateTag = 2;
						return null;
					} else if (c == ']') {
						context.StateTag = 0;
						doc.InternalDeclarationRegion = new DocumentRegion (doc.InternalDeclarationRegion.Begin, context.Location);
						return null;
					}
					break;
				case 2:
					if (c == '>') {
						context.StateTag = 1;
					}
					return null;
				default:
					throw new InvalidOperationException ();
				}
			}
			
			doc = (XDocType)context.Nodes.Pop ();
			if (c == '<') {
				rollback = string.Empty;
				context.LogError ("Doctype ended prematurely.");
			} else if (c != '>') {
				context.LogError ("Unexpected character '" + c +"' in doctype.");
			}
			
			if (context.BuildTree) {
				doc.End (context.Location);
				((XContainer) context.Nodes.Peek ()).AddChildNode (doc); 
			}
			return Parent;
		}
Example #30
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            var att = context.Nodes.Peek() as XAttribute;

            //state has just been entered
            if (context.CurrentStateLength == 1)
            {
                if (context.PreviousState is XmlNameState)
                {
                    //error parsing name
                    if (!att.IsNamed)
                    {
                        context.Nodes.Pop();
                        rollback = string.Empty;
                        return(Parent);
                    }
                    context.StateTag = GETTINGEQ;
                }
                else if (context.PreviousState is XmlAttributeValueState)
                {
                    //Got value, so end attribute
                    context.Nodes.Pop();
                    att.End(context.LocationMinus(1));
                    IAttributedXObject element = (IAttributedXObject)context.Nodes.Peek();
                    if (element.Attributes.Get(att.Name, false) != null)
                    {
                        context.LogError("'" + att.Name + "' is a duplicate attribute name.", att.Region);
                    }
                    element.Attributes.AddAttribute(att);
                    rollback = string.Empty;
                    return(Parent);
                }
                else
                {
                    //starting a new attribute
                    Debug.Assert(att == null);
                    Debug.Assert(context.StateTag == NAMING);
                    att = new XAttribute(context.LocationMinus(1));
                    context.Nodes.Push(att);
                    rollback = string.Empty;
                    return(XmlNameState);
                }
            }

            if (context.StateTag == GETTINGEQ)
            {
                if (char.IsWhiteSpace(c))
                {
                    return(null);
                }
                if (c == '=')
                {
                    context.StateTag = GETTINGVAL;
                    return(null);
                }
                context.LogError("Expecting = in attribute, got '" + c + "'.");
            }
            else if (context.StateTag == GETTINGVAL)
            {
                if (char.IsWhiteSpace(c))
                {
                    return(null);
                }
                rollback = string.Empty;
                return(AttributeValueState);
            }
            else if (c != '<')
            {
                //parent handles message for '<'
                context.LogError("Unexpected character '" + c + "' in attribute.");
            }

            if (att != null)
            {
                context.Nodes.Pop();
            }

            rollback = string.Empty;
            return(Parent);
        }
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (context.CurrentStateLength == 1)
            {
                switch (c)
                {
                case '(':
                    context.StateTag = NONE_EXPLICIT;
                    context.Nodes.Push(new RazorExplicitExpression(context.LocationMinus(2)));
                    return(null);

                default:
                    context.StateTag = NONE_IMPLICIT;
                    if (!(context.PreviousState is RazorSpeculativeState))
                    {
                        context.Nodes.Push(new RazorImplicitExpression(context.LocationMinus(2)));
                    }
                    break;
                }
            }

            switch (c)
            {
            case '(':
                if (context.StateTag == NONE_EXPLICIT)
                {
                    context.StateTag = INSIDE_BRACKET_EXPLICIT;
                }
                else if (context.StateTag == NONE_IMPLICIT)
                {
                    context.StateTag = INSIDE_BRACKET_IMPLICIT;
                }
                context.KeywordBuilder.Append(c);
                break;

            case ')':
                if (context.StateTag == NONE_EXPLICIT)
                {
                    StateEngineService.EndCodeFragment <RazorExplicitExpression> (context);
                    return(Parent);
                }
                else if (context.StateTag != NONE_IMPLICIT)
                {
                    if (context.KeywordBuilder.Length > 0)
                    {
                        context.KeywordBuilder.Remove(0, 1);
                    }
                    if (context.KeywordBuilder.Length == 0)
                    {
                        context.StateTag = (context.StateTag == INSIDE_BRACKET_IMPLICIT ? NONE_IMPLICIT : NONE_EXPLICIT);
                    }
                }
                break;

            default:
                // Space between function's name and open bracket not allowed in razor, e.g. @Html.Raw (foo)
                if (!(Char.IsLetterOrDigit(c) || allowedChars.Any(ch => ch == c)) &&
                    context.StateTag == NONE_IMPLICIT)
                {
                    StateEngineService.EndCodeFragment <RazorImplicitExpression> (context, 1);
                    rollback = String.Empty;
                    if (Parent is RazorSpeculativeState)
                    {
                        return(Parent.Parent);
                    }
                    return(Parent);
                }
                break;
            }

            return(null);
        }
Example #32
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            var maskedTag = (AttState)((context.StateTag & TagMask) >> XmlAttributeValueState.TagShift);

            switch (maskedTag)
            {
            case AttState.Incoming:
                if (c == '<')
                {
                    SetTag(context, AttState.Bracket);
                    return(null);
                }
                return(base.PushChar(c, context, ref rollback));

            case AttState.Bracket:
                if (c == '%')
                {
                    SetTag(context, AttState.Percent);
                    return(null);
                }
                rollback = "<";
                return(Parent);

            case AttState.Percent:
                if (c == '-')
                {
                    SetTag(context, AttState.PercentDash);
                    return(null);
                }
                if (c == '@')
                {
                    context.LogError(GettextCatalog.GetString("Invalid directive location"));
                    rollback = "<%";
                    return(Parent);
                }
                WebFormsExpressionState.AddExpressionNode(c, context);
                SetTag(context, AttState.Expression);
                return(null);

            case AttState.PercentDash:
                if (c == '-')
                {
                    context.Nodes.Push(new WebFormsServerComment(context.LocationMinus(4)));
                    SetTag(context, AttState.Comment);
                    return(null);
                }
                context.LogError(GettextCatalog.GetString("Malformed server comment"));
                rollback = "<%-";
                return(Parent);

            case AttState.Expression:
                if (c == '%')
                {
                    SetTag(context, AttState.EndPercent);
                }
                return(null);

            case AttState.EndPercent:
                if (c == '>')
                {
                    //TODO: attach nodes
                    var n = context.Nodes.Pop();
                    n.End(context.Location);
                    SetTag(context, AttState.Incoming);
                    //ensure attribute get closed if value is unquoted
                    var baseState = (context.StateTag & XmlAttributeValueState.TagMask);
                    if (baseState == FREE || baseState == UNQUOTED)
                    {
                        var att = (XAttribute)context.Nodes.Peek();
                        att.Value = "";
                        return(Parent);
                    }
                    return(null);
                }
                SetTag(context, AttState.Expression);
                return(null);

            case AttState.Comment:
                if (c == '-')
                {
                    SetTag(context, AttState.EndDash);
                }
                return(null);

            case AttState.EndDash:
                if (c == '-')
                {
                    SetTag(context, AttState.EndDashDash);
                }
                else
                {
                    SetTag(context, AttState.Comment);
                }
                return(null);

            case AttState.EndDashDash:
                if (c == '%')
                {
                    SetTag(context, AttState.EndDashDashPercent);
                }
                else if (c != '-')
                {
                    SetTag(context, AttState.Comment);
                }
                return(null);

            case AttState.EndDashDashPercent:
                if (c == '>')
                {
                    //TODO: attach nodes
                    var n = context.Nodes.Pop();
                    n.End(context.Location);
                    SetTag(context, AttState.Incoming);
                    return(null);
                }
                SetTag(context, AttState.Comment);
                return(null);

            default:
                return(base.PushChar(c, context, ref rollback));
            }
        }
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			var maskedTag = (AttState) ((context.StateTag & TagMask) >> XmlAttributeValueState.TagShift);
			switch (maskedTag) {
			case AttState.Incoming:
				if (c == '<') {
					SetTag (context, AttState.Bracket);
					return null;
				}
				return base.PushChar (c, context, ref rollback);
			case AttState.Bracket:
				if (c == '%') {
					SetTag (context, AttState.Percent);
					return null;
				}
				rollback = "<";
				return Parent;
			case AttState.Percent:
				if (c == '-') {
					SetTag (context, AttState.PercentDash);
					return null;
				}
				if (c == '@') {
					context.LogError (GettextCatalog.GetString ("Invalid directive location"));
					rollback = "<%";
					return Parent;
				}
				WebFormsExpressionState.AddExpressionNode (c, context);
				SetTag (context, AttState.Expression);
				return null;
			case AttState.PercentDash:
				if (c == '-') {
					context.Nodes.Push (new WebFormsServerComment (context.LocationMinus (4)));
					SetTag (context, AttState.Comment);
					return null;
				}
				context.LogError (GettextCatalog.GetString ("Malformed server comment"));
				rollback = "<%-";
				return Parent;
			case AttState.Expression:
				if (c == '%')
					SetTag (context, AttState.EndPercent);
				return null;
			case AttState.EndPercent:
				if (c == '>') {
					//TODO: attach nodes
					var n = context.Nodes.Pop ();
					n.End (context.Location);
					SetTag (context, AttState.Incoming);
					//ensure attribute get closed if value is unquoted
					var baseState = (context.StateTag & XmlAttributeValueState.TagMask);
					if (baseState == FREE || baseState == UNQUOTED) {
						var att = (XAttribute)context.Nodes.Peek ();
						att.Value = "";
						return Parent;
					}
					return null;
				}
				SetTag (context, AttState.Expression);
				return null;
			case AttState.Comment:
				if (c == '-')
					SetTag (context, AttState.EndDash);
				return null;
			case AttState.EndDash:
				if (c == '-')
					SetTag (context, AttState.EndDashDash);
				else
					SetTag (context, AttState.Comment);
				return null;
			case AttState.EndDashDash:
				if (c == '%')
					SetTag (context, AttState.EndDashDashPercent);
				else if (c != '-')
					SetTag (context, AttState.Comment);
				return null;
			case AttState.EndDashDashPercent:
				if (c == '>') {
					//TODO: attach nodes
					var n = context.Nodes.Pop ();
					n.End (context.Location);
					SetTag (context, AttState.Incoming);
					return null;
				}
				SetTag (context, AttState.Comment);
				return null;
			default:
				return base.PushChar (c, context, ref rollback);
			}
		}
Example #34
0
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			XElement element = context.Nodes.Peek () as XElement;
			
			if (element == null || element.IsComplete) {
				element = new XElement (context.LocationMinus (2)); // 2 == < + current char
				context.Nodes.Push (element);
			}
			
			if (c == '<') {
				if (element.IsNamed) {
					context.LogError ("Unexpected '<' in tag '" + element.Name.FullName + "'.");
					Close (element, context, context.LocationMinus (1));
				} else {
					context.LogError ("Unexpected '<' in unnamed tag.");
				}
				
				rollback = string.Empty;
				return Parent;
			}
			
			Debug.Assert (!element.IsComplete);
			
			if (element.IsClosed && c != '>') {
				if (char.IsWhiteSpace (c)) {
					context.LogWarning ("Unexpected whitespace after '/' in self-closing tag.");
					return null;
				}
				context.LogError ("Unexpected character '" + c + "' after '/' in self-closing tag.");
				context.Nodes.Pop ();
				return Parent;
			}
			
			//if tag closed
			if (c == '>') {
				if (context.StateTag == MAYBE_SELF_CLOSING) {
					element.Close (element);
				}
				if (!element.IsNamed) {
					context.LogError ("Tag closed prematurely.");
				} else {
					Close (element, context, context.Location);
				}
				return Parent;
			}

			if (c == '/') {
				context.StateTag = MAYBE_SELF_CLOSING;
				return null;
			}

			if (context.StateTag == ATTEMPT_RECOVERY) {
				if (XmlChar.IsWhitespace (c)) {
					context.StateTag = RECOVERY_FOUND_WHITESPACE;
				}
				return null;
			}

			if (context.StateTag == RECOVERY_FOUND_WHITESPACE) {
				if (!XmlChar.IsFirstNameChar (c))
					return null;
			}

			context.StateTag = OK;

			if (!element.IsNamed && XmlChar.IsFirstNameChar (c)) {
				rollback = string.Empty;
				return NameState;
			}

			if (context.CurrentStateLength > 1 && XmlChar.IsFirstNameChar (c)) {
				rollback = string.Empty;
				return AttributeState;
			}
			
			if (XmlChar.IsWhitespace (c))
				return null;

			context.LogError ("Unexpected character '" + c + "' in tag.", context.LocationMinus (1));
			context.StateTag = ATTEMPT_RECOVERY;
			return null;
		}
Example #35
0
        public virtual XmlParserState ParseClosingBracket <T> (char c, IXmlParserContext context, XmlParserState stateToReturn) where T : XNode
        {
            bool isEnding  = false;
            var  rootState = RootState as RazorRootState;

            if (rootState.UseSimplifiedBracketTracker)
            {
                if (bracketsBuilder.Length > 0)
                {
                    bracketsBuilder.Remove(0, 1);
                }
                if (bracketsBuilder.Length == 0)
                {
                    isEnding = true;
                }
            }
            else if (!rootState.UseSimplifiedBracketTracker && CorrespondingBlock.IsEndingBracket(context.LocationMinus(1)))
            {
                isEnding = true;
            }

            if (isEnding)
            {
                StateEngineService.EndCodeFragment <T> (context);
                return(stateToReturn);
            }

            return(null);
        }
		XmlParserState SwitchToDirective (IXmlParserContext context, ref string rollback)
		{
			string key = context.KeywordBuilder.ToString ();
			string name = key.Trim ();
			var dir = new RazorDirective (context.LocationMinus (key.Length + 2)) {
				Name = name,
				IsSimpleDirective = RazorSymbols.IsSimpleDirective (name)
			};
			context.Nodes.Push (dir);
			rollback = String.Empty;
			return EnsureSetAndAdopted<RazorDirectiveState> (ref directiveState);
		}
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            var directive = context.Nodes.Peek() as WebFormsDirective;

            if (directive == null || directive.IsComplete)
            {
                directive = new WebFormsDirective(context.LocationMinus(4));                   // 4 == <%@ + current char
                context.Nodes.Push(directive);
            }

            if (c == '<')
            {
                context.LogError("Unexpected '<' in directive.");
                rollback = string.Empty;
                return(Parent);
            }

            Debug.Assert(!directive.IsComplete);

            if (context.StateTag != ENDING && c == '%')
            {
                context.StateTag = ENDING;
                return(null);
            }


            if (context.StateTag == ENDING)
            {
                if (c == '>')
                {
                    //have already checked that directive is not null, i.e. top of stack is our directive
                    context.Nodes.Pop();

                    if (!directive.IsNamed)
                    {
                        context.LogError("Directive closed prematurely.");
                    }
                    else
                    {
                        directive.End(context.Location);
                        if (context.BuildTree)
                        {
                            var container = (XContainer)context.Nodes.Peek();
                            container.AddChildNode(directive);
                        }
                    }
                    return(Parent);
                }
                //ending but not '>'? Error; go to end.
            }
            else if (char.IsLetter(c))
            {
                rollback = string.Empty;
                if (!directive.IsNamed)
                {
                    return(NameState);
                }
                else
                {
                    return(AttributeState);
                }
            }
            else if (char.IsWhiteSpace(c))
            {
                return(null);
            }

            rollback = string.Empty;
            context.LogError("Unexpected character '" + c + "' in tag.");
            return(Parent);
        }
Example #38
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            var doc = context.Nodes.Peek() as XDocType;

            if (doc == null)
            {
                doc = new XDocType(context.LocationMinus("<!DOCTYPE".Length + 1));
                context.Nodes.Push(doc);
            }

            if (!doc.RootElement.IsValid)
            {
                if (XmlChar.IsWhitespace(c))
                {
                    return(null);
                }
                else if (XmlChar.IsFirstNameChar(c))
                {
                    rollback = "";
                    return(nameState);
                }
            }
            else if (doc.PublicFpi == null)
            {
                if (context.StateTag == 0)
                {
                    if (c == 's' || c == 'S')
                    {
                        context.StateTag = 1;
                        return(null);
                    }
                    else if (c == 'p' || c == 'P')
                    {
                        context.StateTag = -1;
                        return(null);
                    }
                    if (XmlChar.IsWhitespace(c))
                    {
                        return(null);
                    }
                }
                else if (Math.Abs(context.StateTag) < 6)
                {
                    if (context.StateTag > 0)
                    {
                        if ("YSTEM"[context.StateTag - 1] == c || "ystem"[context.StateTag - 1] == c)
                        {
                            context.StateTag++;
                            if (context.StateTag == 6)
                            {
                                context.StateTag = 0;
                                doc.PublicFpi    = "";
                            }
                            return(null);
                        }
                    }
                    else
                    {
                        int absState = Math.Abs(context.StateTag) - 1;
                        if ("UBLIC"[absState] == c || "ublic"[absState] == c)
                        {
                            context.StateTag--;
                            return(null);
                        }
                    }
                }
                else
                {
                    if (context.KeywordBuilder.Length == 0)
                    {
                        if (XmlChar.IsWhitespace(c))
                        {
                            return(null);
                        }
                        else if (c == '"')
                        {
                            context.KeywordBuilder.Append(c);
                            return(null);
                        }
                    }
                    else
                    {
                        if (c == '"')
                        {
                            context.KeywordBuilder.Remove(0, 1);
                            doc.PublicFpi = context.KeywordBuilder.ToString();
                            context.KeywordBuilder.Length = 0;
                            context.StateTag = 0;
                        }
                        else
                        {
                            context.KeywordBuilder.Append(c);
                        }
                        return(null);
                    }
                }
            }
            else if (doc.Uri == null)
            {
                if (context.KeywordBuilder.Length == 0)
                {
                    if (XmlChar.IsWhitespace(c))
                    {
                        return(null);
                    }
                    else if (c == '"')
                    {
                        context.KeywordBuilder.Append(c);
                        return(null);
                    }
                }
                else
                {
                    if (c == '"')
                    {
                        context.KeywordBuilder.Remove(0, 1);
                        doc.Uri = context.KeywordBuilder.ToString();
                        context.KeywordBuilder.Length = 0;
                    }
                    else
                    {
                        context.KeywordBuilder.Append(c);
                    }
                    return(null);
                }
            }
            else if (doc.InternalDeclarationRegion.EndLine <= 0)
            {
                if (XmlChar.IsWhitespace(c))
                {
                    return(null);
                }
                switch (context.StateTag)
                {
                case 0:
                    if (c == '[')
                    {
                        doc.InternalDeclarationRegion = new DomRegion(context.Location, TextLocation.Empty);
                        context.StateTag = 1;
                        return(null);
                    }
                    break;

                case 1:
                    if (c == '<')
                    {
                        context.StateTag = 2;
                        return(null);
                    }
                    else if (c == ']')
                    {
                        context.StateTag = 0;
                        doc.InternalDeclarationRegion = new DomRegion(doc.InternalDeclarationRegion.Begin, context.Location);
                        return(null);
                    }
                    break;

                case 2:
                    if (c == '>')
                    {
                        context.StateTag = 1;
                    }
                    return(null);

                default:
                    throw new InvalidOperationException();
                }
            }

            doc = (XDocType)context.Nodes.Pop();
            if (c == '<')
            {
                rollback = string.Empty;
                context.LogError("Doctype ended prematurely.");
            }
            else if (c != '>')
            {
                context.LogError("Unexpected character '" + c + "' in doctype.");
            }

            if (context.BuildTree)
            {
                doc.End(context.Location);
                ((XContainer)context.Nodes.Peek()).AddChildNode(doc);
            }
            return(Parent);
        }
Example #39
0
		public override XmlParserState PushChar (char c, IXmlParserContext context, ref string rollback)
		{
			if (c == '<') {
				if (context.StateTag != FREE)
					context.LogError ("Incomplete tag opening; encountered unexpected '<'.",
						new DomRegion (
							context.LocationMinus (LengthFromOpenBracket (context) + 1),
							context.LocationMinus (1)));
				context.StateTag = BRACKET;
				return null;
			}
			
			switch (context.StateTag) {
			case FREE:
				//FIXME: handle entities?
				return null;
				
			case BRACKET:
				if (c == '?') {
					rollback = string.Empty;
					return this.ProcessingInstructionState;
				} else if (c == '!') {
					context.StateTag = BRACKET_EXCLAM;
					return null;
				} else if (c == '/') {
					return this.ClosingTagState;
				} else if (char.IsLetter (c) || c == '_') {
					rollback = string.Empty;
					return TagState;
				}
				break;
				
			case BRACKET_EXCLAM:
				if (c == '[') {
					context.StateTag = CDATA;
					return null;
				} else if (c == '-') {
					context.StateTag = COMMENT;
					return null;
				} else if (c == 'D') {
					context.StateTag = DOCTYPE;
					return null;
				}
				break;
			
			case COMMENT:
				if (c == '-')
					return CommentState;
				break;
				
			case CDATA:
				string cdataStr = "CDATA[";
				if (c == cdataStr [context.KeywordBuilder.Length]) {
					context.KeywordBuilder.Append (c);
					if (context.KeywordBuilder.Length < cdataStr.Length)
						return null;
					return CDataState;
				}
				context.KeywordBuilder.Length = 0;
				break;
				
			case DOCTYPE:
				string docTypeStr = "OCTYPE";
				if (c == docTypeStr [context.KeywordBuilder.Length]) {
					context.KeywordBuilder.Append (c);
					if (context.KeywordBuilder.Length < docTypeStr.Length)
						return null;
					return DocTypeState;
				} else {
					context.KeywordBuilder.Length = 0;
				}
				break;
			}
			
			context.LogError ("Incomplete tag opening; encountered unexpected character '" + c + "'.",
				new DomRegion (
					context.LocationMinus (LengthFromOpenBracket (context)),
					context.Location));
			
			context.StateTag = FREE;
			return null;
		}
Example #40
0
        public override XmlParserState PushChar(char c, IXmlParserContext context, ref string rollback)
        {
            if (c == '<')
            {
                if (context.StateTag != FREE)
                {
                    context.LogError("Incomplete tag opening; encountered unexpected '<'.",
                                     new DomRegion(
                                         context.LocationMinus(LengthFromOpenBracket(context) + 1),
                                         context.LocationMinus(1)));
                }
                context.StateTag = BRACKET;
                return(null);
            }

            switch (context.StateTag)
            {
            case FREE:
                //FIXME: handle entities?
                return(null);

            case BRACKET:
                if (c == '?')
                {
                    rollback = string.Empty;
                    return(this.ProcessingInstructionState);
                }
                else if (c == '!')
                {
                    context.StateTag = BRACKET_EXCLAM;
                    return(null);
                }
                else if (c == '/')
                {
                    return(this.ClosingTagState);
                }
                else if (char.IsLetter(c) || c == '_')
                {
                    rollback = string.Empty;
                    return(TagState);
                }
                break;

            case BRACKET_EXCLAM:
                if (c == '[')
                {
                    context.StateTag = CDATA;
                    return(null);
                }
                else if (c == '-')
                {
                    context.StateTag = COMMENT;
                    return(null);
                }
                else if (c == 'D')
                {
                    context.StateTag = DOCTYPE;
                    return(null);
                }
                break;

            case COMMENT:
                if (c == '-')
                {
                    return(CommentState);
                }
                break;

            case CDATA:
                string cdataStr = "CDATA[";
                if (c == cdataStr [context.KeywordBuilder.Length])
                {
                    context.KeywordBuilder.Append(c);
                    if (context.KeywordBuilder.Length < cdataStr.Length)
                    {
                        return(null);
                    }
                    return(CDataState);
                }
                context.KeywordBuilder.Length = 0;
                break;

            case DOCTYPE:
                string docTypeStr = "OCTYPE";
                if (c == docTypeStr [context.KeywordBuilder.Length])
                {
                    context.KeywordBuilder.Append(c);
                    if (context.KeywordBuilder.Length < docTypeStr.Length)
                    {
                        return(null);
                    }
                    return(DocTypeState);
                }
                else
                {
                    context.KeywordBuilder.Length = 0;
                }
                break;
            }

            context.LogError("Incomplete tag opening; encountered unexpected character '" + c + "'.",
                             new DomRegion(
                                 context.LocationMinus(LengthFromOpenBracket(context)),
                                 context.Location));

            context.StateTag = FREE;
            return(null);
        }
		XmlParserState SwitchToStatement (IXmlParserContext context, ref string rollback)
		{
			string key = context.KeywordBuilder.ToString ();
			var stm = new RazorStatement (context.LocationMinus (key.Length + 2)) { Name = key.Trim () };
			context.Nodes.Push (stm);
			rollback = String.Empty;
			return EnsureSetAndAdopted<RazorStatementState> (ref statementState);
		}