protected override string CreateMethodCall(StringBuilder result, IncludeDirective directive) { var parameters = GetParameters(IncludedTemplates[directive]); result.AppendLine($"\t\t\t{directive.Name}TemplateMethod({CreateCallParameters( parameters )});"); return(result.ToString()); }
private void CreateIncludeCode(StringBuilder result, IncludeDirective directive) { if (directive.Mode == IncludeMode.Method) { result.Append(BeginLinePragma(directive)); CreateMethodCall(result, directive); result.Append(EndLinePragma()); } else { result.Append(CreateImplementation(IncludedTemplates[directive])); } }
private CompositeElement ResolveIncludeDirective([NotNull] IncludeDirective directive) { var sourceFile = LogicalSourceFile; if (sourceFile == null) { return(null); } var pathWithMacros = directive.GetPathForParsing(sourceFile); var path = pathWithMacros.ResolvePath(); var includeFile = T4ParsingContextHelper.ExecuteGuarded(path, directive.Once, () => pathWithMacros.Resolve()); if (includeFile == null) { return(null); } return(BuildIncludedT4Tree(includeFile)); }
/// <summary> /// Loads and parses the file according the the #include directive /// </summary> /// <param name="fileIndex">Include file index</param> /// <param name="incDirective">Directive with the file</param> /// <param name="sourceItem">Source file item</param> /// <param name="parsedLines">Collection of source code lines</param> private bool ApplyIncludeDirective(int fileIndex, IncludeDirective incDirective, SourceFileItem sourceItem, out List <SourceLineBase> parsedLines) { parsedLines = new List <SourceLineBase>(); // --- Check the #include directive var filename = incDirective.Filename.Trim(); if (filename.StartsWith("<") && filename.EndsWith(">")) { // TODO: System include file filename = filename.Substring(1, filename.Length - 2); } // --- Now, we have the file name, calculate the path if (sourceItem.Filename != NOFILE_ITEM) { // --- The file name is taken into account as relative var dirname = Path.GetDirectoryName(sourceItem.Filename) ?? string.Empty; filename = Path.Combine(dirname, filename); } // --- Check for file existence if (!File.Exists(filename)) { ReportError(Errors.Z0300, incDirective, filename); return(false); } var fi = new FileInfo(filename); var fullName = fi.FullName; // --- Check for repetition var childItem = new SourceFileItem(fullName); if (sourceItem.ContainsInIncludeList(childItem)) { ReportError(Errors.Z0302, incDirective, filename); return(false); } // --- Check for circular reference if (!sourceItem.Include(childItem)) { ReportError(Errors.Z0303, incDirective, filename); return(false); } // --- Now, add the included item to the output _output.SourceFileList.Add(childItem); // --- Read the include file string sourceText; try { sourceText = File.ReadAllText(filename); } catch (Exception ex) { ReportError(Errors.Z0301, incDirective, filename, ex.Message); return(false); } // --- Parse the file return(ExecuteParse(fileIndex, childItem, sourceText, out parsedLines)); }
protected abstract string CreateMethodCall(StringBuilder result, IncludeDirective directive);
ProjectItemCodeDocument ParseIncludeDirective(CodeSegment includeDirective, ProjectItemCodeDocument document) { ThrowUtil.ThrowIfNull(includeDirective); ThrowUtil.ThrowIfNull(document); SmartCodeProjectAHK project = document.Project as SmartCodeProjectAHK; if(project == null) throw new NotSupportedException("Expected an instance of SmartCodeProjectAHK!"); if(_project.StartUpCodeDocument == null) return null; var libRegEx = new Regex(@"<(.*?)>"); // parse include directive var next = includeDirective.NextOmit(TokenHelper.WhiteSpaces); if(next != null) { int len = next.TokenString.Length; if(len > 0) { if(libRegEx.IsMatch(next.TokenString)) { string docName = libRegEx.Match(next.TokenString).Groups[1].Value; // seach local library files var doc = project.LocalLib.FindAllItems<ProjectItemCodeDocument>().ToList() .Find(x => Path.GetFileNameWithoutExtension(x.FilePath).Equals(docName, StringComparison.InvariantCultureIgnoreCase)); if(doc == null && project.StdLib != null) { // seach in standard library files doc = project.StdLib.FindAllItems<ProjectItemCodeDocument>().ToList() .Find(x => x.FilePath != null && Path.GetFileNameWithoutExtension(x.FilePath).Equals(docName, StringComparison.InvariantCultureIgnoreCase)); } if(doc != null) { var directive = new IncludeDirective() { ResolvedFilePath = doc.FilePath, ResolvedCodeDocument = doc }; next.CodeDOMObject = directive; } else { next.ErrorContext = new CodeError() { Description = string.Format("File not found!") }; } return doc; } else { // parse direct/relative path var directive = ParseIncludePath(document, next); var includeFilePath = directive.ResolvedFilePath; var doc = project.CodeDocuments.ToList() .Find(x => x.FilePath.Equals(includeFilePath, StringComparison.InvariantCultureIgnoreCase)); directive.ResolvedCodeDocument = doc; return doc; } } } return null; }
IncludeDirective ParseIncludePath(ProjectItemCodeDocument codeDoc, CodeSegment segment) { string workingDir = Path.GetDirectoryName(_project.StartUpCodeDocument.FilePath); StringBuilder sb = new StringBuilder(); CodeSegment next = segment; CodeSegment current = null; List<CodeSegment> _pathTokens = new List<CodeSegment>(); while(next != null && next.Token != Token.NewLine) { current = next; next = next.Next; if(current.Token == Token.TraditionalString) sb.Append(current.TokenString); else if(current.Token == Token.Deref) { if(current.Next != null && current.Next.Token == Token.Identifier) { if(current.Next.TokenString.Equals(WORKINGDIR_VAR, StringComparison.InvariantCultureIgnoreCase)) { sb.Append(workingDir); } else { RegisterError(codeDoc, current.Next, "Unknown precompiler Variable!"); } if(current.Next != null && current.Next.Next != null) next = current.Next.Next.Next; } else if(current.Next != null) { RegisterError(codeDoc, current.Next, "Expected Identifier after Deref!"); } } if(current != null) _pathTokens.Add(current); } var path = sb.ToString(); try { if(!path.Contains(':')) { path = Path.Combine(workingDir, path); } } catch { path = ""; } var directive = new IncludeDirective() { ResolvedFilePath = path }; CodeError fail = null; if(!File.Exists(path)){ var err = string.Format("File not found!\n" + path); fail = new CodeError() { Description = err }; if(_pathTokens.Any()) RegisterError(codeDoc, _pathTokens.First(), err); } foreach(var s in _pathTokens) { s.CodeDOMObject = directive; if(fail != null) { s.ErrorContext = fail; } } return directive; }