private bool ParseModelStatement(CodeBlockInfo block) { var currentLocation = this.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 @model 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(c => ParserHelpers.IsNewLine(c)); modelTypeName = this.Context.ContentBuffer.ToString(); this.Context.AcceptTemporaryBuffer(); } this.Context.AcceptNewLine(); } else { this.OnError(currentLocation, string.Format(CultureInfo.CurrentCulture, "@model must be followed by a type name.")); } this.CheckForInheritsAndModelStatements(); this.End(new ModelSpan(this.Context, modelTypeName)); return(false); }
public void TryMergeWithLeavesTypeVisibilityAndTrackingModeUnchanged() { // Arrange Span left = new CodeSpan(SourceLocation.Zero, "Foo", hidden: true, acceptedCharacters: AcceptedCharacters.None); Span right = new MetaCodeSpan(new SourceLocation(3, 0, 0), "Bar"); // Act bool success = left.TryMergeWith(right); // Assert Assert.IsTrue(success); Assert.AreEqual(SpanKind.Code, left.Kind); Assert.IsTrue(left.Hidden); }
/// <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); }
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); }
protected void ParseComment() { using (StartBlock(BlockType.Comment)) { SourceLocation startLocation = CurrentLocation; Context.Expect(RazorParser.StartCommentSequence[0]); End(TransitionSpan.Create(Context, hidden: false, acceptedCharacters: AcceptedCharacters.None)); Context.Expect(RazorParser.StartCommentSequence[1]); End(MetaCodeSpan.Create(Context, hidden: false, acceptedCharacters: AcceptedCharacters.None)); bool inComment = true; while (inComment && !EndOfFile) { Context.AcceptUntil(RazorParser.EndCommentSequence[0]); if (Context.Peek(RazorParser.EndCommentSequence, caseSensitive: true)) { inComment = false; } else { Context.AcceptCurrent(); } } End(CommentSpan.Create); if (EndOfFile) { OnError(startLocation, RazorResources.ParseError_RazorComment_Not_Terminated); } else { Context.Expect(RazorParser.EndCommentSequence[0]); End(MetaCodeSpan.Create(Context, hidden: false, acceptedCharacters: AcceptedCharacters.None)); Context.Expect(RazorParser.EndCommentSequence[1]); End(TransitionSpan.Create(Context, hidden: false, acceptedCharacters: AcceptedCharacters.None)); } } }
/// <summary> /// Parses the model statement. /// </summary> /// <param name="block">The code block.</param> private bool ParseModelStatement(CodeBlockInfo block) { var location = CurrentLocation; bool readWhiteSpace = RequireSingleWhiteSpace(); End(MetaCodeSpan.Create(Context, false, readWhiteSpace ? AcceptedCharacters.None : AcceptedCharacters.Any)); if (_modelOrInheritsStatementFound) { OnError(location, "The model or inherits keywords can only appear once."); } _modelOrInheritsStatementFound = true; 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); }