Beispiel #1
0
        /// <summary>
        /// Parses the modeltype statement.
        /// </summary>
        /// <param name="block">The code block.</param>
        public bool ParseModelTypeStatement(CodeBlockInfo block)
        {
            Contract.Requires(block != null);

            using (StartBlock(BlockType.Directive))
            {
                block.ResumeSpans(Context);

                SourceLocation location       = CurrentLocation;
                bool           readWhitespace = RequireSingleWhiteSpace();

                End(MetaCodeSpan.Create(Context, false, readWhitespace ? AcceptedCharacters.None : AcceptedCharacters.Any));

                if (_modelOrInheritsStatementFound)
                {
                    OnError(location, "The modeltype or inherits keywords can only appear once.");
                }

                _modelOrInheritsStatementFound = true;

                // Accept Whitespace up to the new line or non-whitespace character
                Context.AcceptWhiteSpace(false);

                string typeName = null;
                if (ParserHelpers.IsIdentifierStart(CurrentCharacter))
                {
                    using (Context.StartTemporaryBuffer())
                    {
                        Context.AcceptUntil(ParserHelpers.IsNewLine);
                        typeName = Context.ContentBuffer.ToString();
                        Context.AcceptTemporaryBuffer();
                    }
                    Context.AcceptNewLine();
                }
                else
                {
                    OnError(location, "Expected model identifier.");
                }
                End(new ModelSpan(Context, typeName));
            }
            return(false);
        }
Beispiel #2
0
        private bool ParseModelStatement(CodeBlockInfo block)
        {
            using (StartBlock(BlockType.Directive)) {
                block.ResumeSpans(Context);

                SourceLocation endModelLocation = CurrentLocation;
                bool           readWhitespace   = RequireSingleWhiteSpace();

                End(MetaCodeSpan.Create(Context, hidden: false, acceptedCharacters: readWhitespace ? AcceptedCharacters.None : AcceptedCharacters.Any));

                if (_modelStatementFound)
                {
                    OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, MvcResources.MvcRazorCodeParser_OnlyOneModelStatementIsAllowed, ModelTypeKeyword));
                }

                _modelStatementFound = true;

                // Accept Whitespace up to the new line or non-whitespace character
                Context.AcceptWhiteSpace(includeNewLines: false);

                string typeName = null;
                if (ParserHelpers.IsIdentifierStart(CurrentCharacter))
                {
                    using (Context.StartTemporaryBuffer()) {
                        Context.AcceptUntil(c => ParserHelpers.IsNewLine(c));
                        typeName = Context.ContentBuffer.ToString();
                        Context.AcceptTemporaryBuffer();
                    }
                    Context.AcceptNewLine();
                }
                else
                {
                    OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, MvcResources.MvcRazorCodeParser_ModelKeywordMustBeFollowedByTypeName, ModelTypeKeyword));
                }
                CheckForInheritsAndModelStatements();
                End(new ModelSpan(Context, typeName));
            }
            return(false);
        }
        private bool ParseModelStatement(CodeBlockInfo block)
        {
            using (this.StartBlock(BlockType.Directive))
            {
                block.ResumeSpans(this.Context);
                var currentLocation    = CurrentLocation;
                var acceptedCharacters = this.RequireSingleWhiteSpace() ? AcceptedCharacters.None : AcceptedCharacters.Any;

                this.End(MetaCodeSpan.Create(this.Context, false, acceptedCharacters));

                if (this.modelStatementFound)
                {
                    this.OnError(currentLocation, string.Format(CultureInfo.CurrentCulture, "Only one @ModelType statement is allowed."));
                }

                this.modelStatementFound = true;
                this.Context.AcceptWhiteSpace(false);
                string modelTypeName = null;

                if (ParserHelpers.IsIdentifierStart(this.CurrentCharacter))
                {
                    using (this.Context.StartTemporaryBuffer())
                    {
                        this.Context.AcceptUntil(ParserHelpers.IsNewLine);
                        modelTypeName = this.Context.ContentBuffer.ToString();
                        this.Context.AcceptTemporaryBuffer();
                    }
                    this.Context.AcceptNewLine();
                }
                else
                {
                    this.OnError(currentLocation, string.Format(CultureInfo.CurrentCulture, "@ModelType must be followed by a type name."));
                }

                this.CheckForInheritsAndModelStatements();
                this.End(new ModelSpan(this.Context, modelTypeName));
            }
            return(false);
        }