/// <summary> /// Parser to turn lg content into a <see cref="Templates"/>. /// </summary> /// <param name="resource">LG resource.</param> /// <param name="importResolver">Resolver to resolve LG import id to template text.</param> /// <param name="expressionParser">Expression parser for parsing expressions.</param> /// <param name="cachedTemplates">Give the file path and templates to avoid parsing and to improve performance.</param> /// <param name="parentTemplates">Parent visited Templates.</param> /// <returns>new <see cref="Templates"/> entity.</returns> private static Templates InnerParseResource( LGResource resource, ImportResolverDelegate importResolver = null, ExpressionParser expressionParser = null, Dictionary <string, Templates> cachedTemplates = null, Stack <Templates> parentTemplates = null) { if (resource == null) { throw new ArgumentNullException(nameof(resource)); } cachedTemplates = cachedTemplates ?? new Dictionary <string, Templates>(); parentTemplates = parentTemplates ?? new Stack <Templates>(); if (cachedTemplates.ContainsKey(resource.Id)) { return(cachedTemplates[resource.Id]); } importResolver = importResolver ?? DefaultFileResolver; var lg = new Templates(content: resource.Content, id: resource.Id, source: resource.FullName, importResolver: importResolver, expressionParser: expressionParser); try { lg = new TemplatesTransformer(lg).Transform(AntlrParseTemplates(resource)); lg.References = GetReferences(lg, cachedTemplates, parentTemplates); new StaticChecker(lg).Check().ForEach(u => lg.Diagnostics.Add(u)); } catch (TemplateException ex) { ex.Diagnostics.ToList().ForEach(u => lg.Diagnostics.Add(u)); } return(lg); }
/// <summary> /// Parser to turn lg content into a <see cref="Templates"/> based on the original LGFile. /// </summary> /// <param name="content">Text content contains lg templates.</param> /// <param name="lg">Original LGFile.</param> /// <returns>New <see cref="Templates"/> entity.</returns> public static Templates ParseTextWithRef(string content, Templates lg) { if (lg == null) { throw new ArgumentNullException(nameof(lg)); } var newLG = new Templates(content: content, id: InlineContentId, source: InlineContentId, importResolver: lg.ImportResolver, options: lg.Options, namedReferences: lg.NamedReferences); try { var resource = new LGResource(InlineContentId, InlineContentId, content); newLG = new TemplatesTransformer(newLG).Transform(AntlrParseTemplates(resource)); newLG.References = GetReferences(newLG) .Union(lg.References) .Union(new List <Templates> { lg }) .ToList(); new StaticChecker(newLG).Check().ForEach(u => newLG.Diagnostics.Add(u)); } catch (TemplateException ex) { ex.Diagnostics.ToList().ForEach(u => newLG.Diagnostics.Add(u)); } return(newLG); }
/// <summary> /// Parser to turn lg content into a <see cref="Templates"/>. /// </summary> /// <param name="content">Text content contains lg templates.</param> /// <param name="id">Id is the identifier of content. If importResolver is null, id must be a full path string. </param> /// <param name="importResolver">Resolver to resolve LG import id to template text.</param> /// <param name="expressionParser">Expression parser for parsing expressions.</param> /// <param name="cachedTemplates">Give the file path and templates to avoid parsing and to improve performance.</param> /// <returns>new <see cref="Templates"/> entity.</returns> private static Templates InnerParseText( string content, string id = "", ImportResolverDelegate importResolver = null, ExpressionParser expressionParser = null, Dictionary <string, Templates> cachedTemplates = null) { cachedTemplates = cachedTemplates ?? new Dictionary <string, Templates>(); if (cachedTemplates.ContainsKey(id)) { return(cachedTemplates[id]); } importResolver = importResolver ?? DefaultFileResolver; var lg = new Templates(content: content, id: id, importResolver: importResolver, expressionParser: expressionParser); try { lg = new TemplatesTransformer(lg).Transform(AntlrParseTemplates(content, id)); lg.References = GetReferences(lg, cachedTemplates); new StaticChecker(lg).Check().ForEach(u => lg.Diagnostics.Add(u)); } catch (TemplateException ex) { ex.Diagnostics.ToList().ForEach(u => lg.Diagnostics.Add(u)); } return(lg); }
/// <summary> /// Update an existing template. /// </summary> /// <param name="templateName">Original template name. The only id of a template.</param> /// <param name="newTemplateName">New template Name.</param> /// <param name="parameters">New params.</param> /// <param name="templateBody">New template body.</param> /// <returns>Updated LG file.</returns> public Templates UpdateTemplate(string templateName, string newTemplateName, List <string> parameters, string templateBody) { var template = this.FirstOrDefault(u => u.Name == templateName); if (template != null) { ClearDiagnostics(); var templateNameLine = BuildTemplateNameLine(newTemplateName, parameters); var newTemplateBody = ConvertTemplateBody(templateBody); var content = $"{templateNameLine}{newLine}{newTemplateBody}"; // update content this.Content = ReplaceRangeContent( this.Content, template.SourceRange.Range.Start.Line - 1, template.SourceRange.Range.End.Line - 1, content); var updatedTemplates = new Templates(content: string.Empty, id: Id, importResolver: ImportResolver, expressionParser: ExpressionParser); updatedTemplates = new TemplatesTransformer(updatedTemplates).Transform(AntlrParseTemplates(content, Id)); var originStartLine = template.SourceRange.Range.Start.Line - 1; AppendDiagnosticsWithOffset(updatedTemplates.Diagnostics, originStartLine); var newTemplate = updatedTemplates.FirstOrDefault(); if (newTemplate != null) { AdjustRangeForUpdateTemplate(template, newTemplate); new StaticChecker(this).Check().ForEach(u => this.Diagnostics.Add(u)); } } return(this); }
/// <summary> /// Add a new template and return LG File. /// </summary> /// <param name="templateName">New template name.</param> /// <param name="parameters">New params.</param> /// <param name="templateBody">New template body.</param> /// <returns>Updated LG file.</returns> public Templates AddTemplate(string templateName, List<string> parameters, string templateBody) { var template = this.FirstOrDefault(u => u.Name == templateName); if (template != null) { throw new Exception(TemplateErrors.TemplateExist(templateName)); } ClearDiagnostics(); var templateNameLine = BuildTemplateNameLine(templateName, parameters); var newTemplateBody = ConvertTemplateBody(templateBody); var content = $"{templateNameLine}{newLine}{newTemplateBody}"; var originStartLine = GetLinesOfText(this.Content).Length; // update content this.Content = $"{Content}{newLine}{templateNameLine}{newLine}{newTemplateBody}"; var newTemplates = new Templates(content: string.Empty, id: Id, importResolver: ImportResolver, expressionParser: ExpressionParser); newTemplates = new TemplatesTransformer(newTemplates).Transform(AntlrParseTemplates(content, Id)); AppendDiagnosticsWithOffset(newTemplates.Diagnostics, originStartLine); var newTemplate = newTemplates.FirstOrDefault(); if (newTemplate != null) { AdjustRangeForAddTemplate(newTemplate, originStartLine); this.Add(newTemplate); new StaticChecker(this).Check().ForEach(u => this.Diagnostics.Add(u)); } return this; }