private void Evaluate(ConfigParserResult parserResult) { using (Trace.WithIndent().Information("Evaluating configuration script")) { _compilation.Generate(parserResult.Declarations, parserResult.Body, AssemblyLoader.ModuleTypes, _engine.Namespaces); WriteScript(ConfigCompilation.AssemblyName, _compilation.Code); _engine.RawAssemblies.Add(_compilation.Compile(_engine.Assemblies)); _compilation.Invoke(_engine); } }
public static ConfigParserResult Parse(string code, IPreprocessor preprocessor) { ConfigParserResult result = new ConfigParserResult(); List <string> lines = code.Replace("\r", "").Split(new[] { '\n' }, StringSplitOptions.None).ToList(); // Find all directives for (int c = 0; c < lines.Count; c++) { if (lines[c].StartsWith("#")) { int firstSpace = lines[c].IndexOf(' '); if (firstSpace != -1) { string directive = lines[c].Substring(1, firstSpace - 1); if (preprocessor.ContainsDirective(directive)) { result.DirectiveValues.Add(new DirectiveValue(c + 1, directive, lines[c].Substring(firstSpace + 1).Trim())); lines[c] = "//" + lines[c]; // Just comment out the directive so it still shows in exports } } } } // Get declarations int startLine = 1; int declarationLine = lines.FindIndex(x => { string trimmed = x.TrimEnd(); return(trimmed.Length > 0 && trimmed.All(y => y == '-')); }); if (declarationLine != -1) { List <string> declarationLines = lines.Take(declarationLine).ToList(); result.Declarations = $"#line {startLine}{Environment.NewLine}{string.Join(Environment.NewLine, declarationLines)}"; startLine += declarationLines.Count + 1; lines.RemoveRange(0, declarationLine + 1); } // Get body result.Body = $"#line {startLine}{Environment.NewLine}{string.Join(Environment.NewLine, lines)}"; return(result); }
// Setup is separated from config by a line with only '-' characters // If no such line exists, then the entire script is treated as config public void Configure(string script) { CheckDisposed(); if (_configured) { throw new InvalidOperationException("Configuration has already been performed."); } _configured = true; // Parse the script (or use an empty result if no script) ConfigParserResult parserResult = string.IsNullOrWhiteSpace(script) ? new ConfigParserResult() : ConfigParser.Parse(script, _preprocessor); // Process preprocessor directives _preprocessor.ProcessDirectives(this, parserResult.DirectiveValues); // Initialize and evaluate the script Initialize(); Evaluate(parserResult); }