private static void ResolveImportResources(LGFile start, HashSet <LGFile> resourcesFound, ImportResolverDelegate importResolver) { var resourceIds = start.Imports.Select(lg => lg.Id); resourcesFound.Add(start); foreach (var id in resourceIds) { try { var(content, path) = importResolver(start.Id, id); if (resourcesFound.All(u => u.Id != path)) { var childResource = ParseText(content, path, importResolver); ResolveImportResources(childResource, resourcesFound, importResolver); } } catch (LGException err) { throw err; } catch (Exception err) { throw new LGException(err.Message, new List <Diagnostic> { BuildDiagnostic(err.Message, source: start.Id) }); } } }
/// <summary> /// Parser to turn lg content into an <see cref="LGFile"/>. /// </summary> /// <param name="content">Text content contains lg templates.</param> /// <param name="id">id is the content identifier. 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> /// <returns>new <see cref="LGFile"/> entity.</returns> public static LGFile ParseText(string content, string id = "", ImportResolverDelegate importResolver = null) { importResolver = importResolver ?? DefaultFileResolver; var lgFile = new LGFile(content: content, id: id, importResolver: importResolver); var diagnostics = new List <Diagnostic>(); try { var(templates, imports, invalidTemplateErrors) = AntlrParse(content, id); lgFile.Templates = templates; lgFile.Imports = imports; diagnostics.AddRange(invalidTemplateErrors); lgFile.References = GetReferences(lgFile, importResolver); var semanticErrors = new StaticChecker(lgFile).Check(); diagnostics.AddRange(semanticErrors); } catch (LGException ex) { diagnostics.AddRange(ex.Diagnostics); } catch (Exception err) { diagnostics.Add(BuildDiagnostic(err.Message, source: id)); } lgFile.Diagnostics = diagnostics; return(lgFile); }
/// <summary> /// Use to evaluate an inline template str. /// </summary> /// <param name="lgFile">lg file.</param> /// <param name="inlineStr">inline string which will be evaluated.</param> /// <param name="scope">scope object or JToken.</param> /// <returns>Evaluate result.</returns> public static object Evaluate(this LGFile lgFile, string inlineStr, object scope = null) { if (inlineStr == null) { throw new ArgumentException("inline string is null."); } CheckErrors(lgFile.AllDiagnostics); // wrap inline string with "# name and -" to align the evaluation process var fakeTemplateId = Guid.NewGuid().ToString(); var multiLineMark = "```"; inlineStr = !inlineStr.Trim().StartsWith(multiLineMark) && inlineStr.Contains('\n') ? $"{multiLineMark}{inlineStr}{multiLineMark}" : inlineStr; var newContent = $"# {fakeTemplateId} \r\n - {inlineStr}"; var newLgFile = LGParser.ParseText(newContent, lgFile.Id, lgFile.ImportResolver); var allTemplates = lgFile.AllTemplates.Union(newLgFile.AllTemplates).ToList(); var evaluator = new Evaluator(allTemplates, lgFile.ExpressionEngine); return(evaluator.EvaluateTemplate(fakeTemplateId, scope)); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class. /// </summary> /// <param name="lgText">lg template text.</param> /// <param name="id">optional label for the source of the templates (used for labeling source of template errors).</param> /// <param name="resourceMapping">template resource loader delegate (locale) -> <see cref="ImportResolverDelegate"/>.</param> public TemplateEngineLanguageGenerator(string lgText, string id, Dictionary <string, IList <IResource> > resourceMapping) { this.Id = id ?? DEFAULTLABEL; var(_, locale) = LGResourceLoader.ParseLGFileName(id); var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping); this.lgFile = LGParser.ParseText(lgText ?? string.Empty, Id, importResolver); }
private static IList <LGFile> GetReferences(LGFile file, ImportResolverDelegate importResolver) { var resourcesFound = new HashSet <LGFile>(); ResolveImportResources(file, resourcesFound, importResolver); resourcesFound.Remove(file); return(resourcesFound.ToList()); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class. /// </summary> /// <param name="filePath">lg template file absolute path.</param> /// <param name="resourceMapping">template resource loader delegate (locale) -> <see cref="ImportResolverDelegate"/>.</param> public TemplateEngineLanguageGenerator(string filePath, Dictionary <string, IList <IResource> > resourceMapping) { filePath = PathUtils.NormalizePath(filePath); this.Id = Path.GetFileName(filePath); var(_, locale) = LGResourceLoader.ParseLGFileName(Id); var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping); this.lgFile = LGParser.ParseFile(filePath, importResolver); }
/// <summary> /// Parser to turn lg content into a <see cref="LGFile"/> based on the original LGFile. /// </summary> /// <param name="content">Text content contains lg templates.</param> /// <param name="lgFile">original LGFile.</param> /// <returns>new <see cref="LGFile"/> entity.</returns> public static LGFile ParseTextWithRef(string content, LGFile lgFile) { if (lgFile == null) { throw new ArgumentNullException(nameof(lgFile)); } var id = "inline content"; var newLgFile = new LGFile(content: content, id: id, importResolver: lgFile.ImportResolver, options: lgFile.Options); var diagnostics = new List <Diagnostic>(); try { var(templates, imports, invalidTemplateErrors, options) = AntlrParse(content, id); newLgFile.Templates = templates; newLgFile.Imports = imports; newLgFile.Options = options; diagnostics.AddRange(invalidTemplateErrors); newLgFile.References = GetReferences(newLgFile, newLgFile.ImportResolver) .Union(lgFile.References) .Union(new List <LGFile> { lgFile }) .ToList(); var semanticErrors = new StaticChecker(newLgFile).Check(); diagnostics.AddRange(semanticErrors); } catch (LGException ex) { diagnostics.AddRange(ex.Diagnostics); } catch (Exception err) { diagnostics.Add(BuildDiagnostic(err.Message, source: id)); } newLgFile.Diagnostics = diagnostics; return(newLgFile); }
public TestBotLG(TestBotAccessors accessors) { lgFile = LGParser.ParseFile(GetLGResourceFile("8.LG")); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class. /// </summary> /// <param name="engine">template engine.</param> public TemplateEngineLanguageGenerator(LGFile engine = null) { this.lgFile = engine ?? new LGFile(); }
/// <summary> /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class. /// </summary> public TemplateEngineLanguageGenerator() { this.lgFile = new LGFile(); }