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); }
protected override bool ParseInheritsStatement(CodeBlockInfo block) { this.endInheritsLocation = this.CurrentLocation; var result = base.ParseInheritsStatement(block); this.CheckForInheritsAndModelStatements(); return(result); }
protected override bool ParseInheritsStatement(CodeBlockInfo block) { _endInheritsLocation = CurrentLocation; bool result = base.ParseInheritsStatement(block); CheckForInheritsAndResourceStatements(); return(result); }
/// <summary> /// Parses the inherits statement. /// </summary> /// <param name="block">The code block.</param> protected override bool ParseInheritsStatement(CodeBlockInfo block) { var location = CurrentLocation; if (_modelOrInheritsStatementFound) { OnError(location, "The modeltype or inherits keywords can only appear once."); } _modelOrInheritsStatementFound = true; return(base.ParseInheritsStatement(block)); }
/// <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); }
/// <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); }
public override void ParseCodeBlock(CodeBlockInfo codeBlock, ParsingContext context) { JToken jToken = null; bool valid; try { jToken = JToken.Parse(codeBlock.Content); valid = true; } catch { valid = false; } if (!valid) { context.ReportWarning( WarningIDs.InvalidJsonInJsonCodeBlock, codeBlock.ContentSpan, codeBlock.Content, "JSON is not valid"); } }
private bool ParseResourceStatement(CodeBlockInfo block) { End(MetaCodeSpan.Create); SourceLocation endModelLocation = CurrentLocation; if (_modelStatementFound) { OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, "Only one '{0}' statement is allowed in a file.", ResourceKeyword)); } _modelStatementFound = 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()) { // Accept a dotted-identifier, but allow <> AcceptTypeName(); typeName = Context.ContentBuffer.ToString(); Context.AcceptTemporaryBuffer(); } } else { OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, "The '{0}' keyword must be followed by a type name on the same line.", ResourceKeyword)); } CheckForInheritsAndResourceStatements(); End(ResourceSpan.Create(Context, typeName)); return(false); }
public abstract void ParseCodeBlock(CodeBlockInfo codeBlock, ParsingContext context);
private void ParseCodeBlock(ParsingContext context) { var codeBlock = context.Object as FencedCodeBlock; var originalObject = context.Object; if (codeBlock.Lines.Count == 0) { context.ReportWarning(WarningIDs.EmptyCodeBlock, string.Empty, "Code block is empty"); return; } if (codeBlock.IsOpen) { context.ReportWarning( WarningIDs.UnclosedCodeBlock, codeBlock.Span.Start, context.Source.Text.Length - 1, string.Empty, "Code block is never closed"); return; } StringSlice slice = context.Source.Reposition(codeBlock.Span); var codeBlockInfo = new CodeBlockInfo(codeBlock, slice); if (codeBlockInfo.Content.IsEffectivelyEmpty()) { context.ReportWarning(WarningIDs.EffectivelyEmptyCodeBlock, string.Empty, "Code block is effectively empty"); return; } List <ICodeBlockParser> parsers = null; if (codeBlock.Info is null) { if (ParseUndefinedLanguages) { parsers = UndefinedLanguageParsers; } } else if (!CommonLanguages.TryGetValue(codeBlock.Info, out parsers)) { if (LanguageWhiteListTest(codeBlock.Info) && !LanguageBlackListTest(codeBlock.Info)) { foreach (var parser in Parsers) { if (parser.SupportsLanguage(codeBlock.Info)) { context.SetWarningSource(parser.Identifier); parser.ParseCodeBlock(codeBlockInfo, context); } } } return; } if (parsers is null) { return; } foreach (var parser in parsers) { context.SetWarningSource(parser.Identifier); parser.ParseCodeBlock(codeBlockInfo, context); } }