/// <summary> /// </summary> /// <param name="ast">The syntax tree to scan</param> /// <param name="symbol">Might not be a child symbol of ast</param> /// <param name="ctxt">The context required to search for symbols</param> /// <returns></returns> public static IEnumerable<ISyntaxRegion> Scan(DModule ast, INode symbol, ResolutionContext ctxt, bool includeDefinition = true) { if (ast == null || symbol == null || ctxt == null) return null; var f = new ReferencesFinder(symbol, ast, ctxt); using(ctxt.Push(ast)) ast.Accept (f); var nodeRoot = symbol.NodeRoot as DModule; if (includeDefinition && nodeRoot != null && nodeRoot.FileName == ast.FileName) { var dc = symbol.Parent as DClassLike; if (dc != null && dc.ClassType == D_Parser.Parser.DTokens.Template && dc.NameHash == symbol.NameHash) { f.l.Insert(0, new IdentifierDeclaration(dc.NameHash) { Location = dc.NameLocation, EndLocation = new CodeLocation(dc.NameLocation.Column + dc.Name.Length, dc.NameLocation.Line) }); } f.l.Insert(0, new IdentifierDeclaration(symbol.NameHash) { Location = symbol.NameLocation, EndLocation = new CodeLocation(symbol.NameLocation.Column + symbol.Name.Length, symbol.NameLocation.Line) }); } return f.l; }
public static Dictionary<int, Dictionary<ISyntaxRegion, byte>> Scan(DModule ast, ResolutionContext ctxt) { if (ast == null) return new Dictionary<int, Dictionary<ISyntaxRegion,byte>>(); var typeRefFinder = new TypeReferenceFinder(ctxt); ContextFrame backupFrame = null; if(ctxt.ScopedBlock == ast) backupFrame = ctxt.Pop (); /* if (ctxt.CurrentContext == null) { ctxt.Push(backupFrame); backupFrame = null; }*/ //typeRefFinder.ast = ast; // Enum all identifiers ast.Accept (typeRefFinder); if (backupFrame != null) ctxt.Push (backupFrame); // Crawl through all remaining expressions by evaluating their types and check if they're actual type references. /*typeRefFinder.queueCount = typeRefFinder.q.Count; typeRefFinder.ResolveAllIdentifiers(); */ return typeRefFinder.Matches; }
public virtual void Visit(DModule n) { VisitBlock(n); if (n.OptionalModuleStatement != null) n.OptionalModuleStatement.Accept(this); }
/// <summary> /// Finds the last import statement and returns its end location (the position after the semicolon). /// If no import but module statement was found, the end location of this module statement will be returned. /// </summary> public static CodeLocation FindLastImportStatementEndLocation(DModule m, string moduleCode = null) { IStatement lastStmt = null; foreach (var s in m.StaticStatements) if (s is ImportStatement) lastStmt = s; else if (lastStmt != null) break; if (lastStmt != null) return lastStmt.EndLocation; if (m.OptionalModuleStatement != null) return m.OptionalModuleStatement.EndLocation; if (moduleCode != null) using(var sr = new StringReader(moduleCode)) using (var lx = new Lexer(sr) { OnlyEnlistDDocComments = false }) { lx.NextToken(); if (lx.Comments.Count != 0) return lx.Comments[lx.Comments.Count - 1].EndPosition; } return new CodeLocation(1, 1); }
public static ResolutionContext CreateDefCtxt(string scopedModule,out DModule mod, params string[] modules) { var pcl = CreateCache (modules); mod = pcl [0] [scopedModule]; return CreateDefCtxt (pcl, mod); }
public static EditorData CreateEditorData(Document EditorDocument, DModule Ast, CodeCompletionContext ctx, char triggerChar = '\0') { bool removeChar = char.IsLetter(triggerChar) || triggerChar == '_'; var deltaOffset = 0;//removeChar ? 1 : 0; var caretOffset = ctx.TriggerOffset - (removeChar ? 1 : 0); var caretLocation = new CodeLocation(ctx.TriggerLineOffset - deltaOffset, ctx.TriggerLine); var codeCache = CreateCacheList(EditorDocument); var ed = new EditorData { CaretLocation = caretLocation, CaretOffset = caretOffset, ModuleCode = removeChar ? EditorDocument.Editor.Text.Remove(ctx.TriggerOffset - 1, 1) : EditorDocument.Editor.Text, SyntaxTree = Ast, ParseCache = codeCache }; if (EditorDocument.HasProject) { var cfg = EditorDocument.Project.GetConfiguration(IdeApp.Workspace.ActiveConfiguration); if (cfg is DProjectConfiguration) { var dcfg = cfg as DProjectConfiguration; ed.GlobalDebugIds = dcfg.CustomDebugIdentifiers; ed.IsDebug = dcfg.DebugMode; ed.DebugLevel = dcfg.DebugLevel; ed.GlobalVersionIds = dcfg.GlobalVersionIdentifiers; double d; ulong v; if (Double.TryParse(EditorDocument.Project.Version, out d)) ed.VersionNumber = (ulong)d; else if (UInt64.TryParse(EditorDocument.Project.Version, out v)) ed.VersionNumber = v; } else if (cfg is DubProjectConfiguration) { var versions = new List<string>(VersionIdEvaluation.GetOSAndCPUVersions()); var dcfg = cfg as DubProjectConfiguration; ed.IsDebug = dcfg.DebugMode; HandleDubSettingsConditionExtraction(versions, (dcfg.ParentItem as DubProject).CommonBuildSettings); HandleDubSettingsConditionExtraction(versions, dcfg.BuildSettings); ed.GlobalVersionIds = versions.ToArray(); } } if (ed.GlobalVersionIds == null) { ed.GlobalVersionIds = VersionIdEvaluation.GetOSAndCPUVersions(); } return ed; }
public EditorPathbarProvider(Document doc, object tag) { this.document = doc; this.tag = ((D_Parser.Dom.INode)tag).Parent; var ast = document.ParsedDocument as ParsedDModule; if (ast != null) syntaxTree = ast.DDom; Reset (); }
public void Dispose() { doc = null; BlockAttributes.Clear(); BlockAttributes = null; DeclarationAttributes.Clear(); DeclarationAttributes = null; Lexer.Dispose(); Lexer = null; ParseErrors = null; }
public static bool HasMainMethod(DModule ast) { if (ast == null) return false; //TODO: pragma(main) var en = ast["main"]; if (en != null && en.Any((m) => m is DMethod)) return true; en = ast["WinMain"]; return en != null && en.Any((m) => m is DMethod); }
public override IEnumerable<RootPackage> EnumRootPackagesSurroundingModule (DModule module) { if (module == null) return globalIncludes; RootPackage pack; List<RootPackage> results; if (cache.TryGetValue (module, out results)) return results; results = new List<RootPackage> (); foreach(var prj in Ide.IdeApp.Workspace.GetAllProjects()) { var dprj = prj as AbstractDProject; if (dprj == null || !prj.IsFileInProject(module.FileName)) continue; if (dprj is DProject) Add (results, (dprj as DProject).Compiler.IncludePaths); else if (dprj is DubProject) results.AddRange (globalIncludes); foreach (var p in dprj.GetSourcePaths()) if ((pack = GlobalParseCache.GetRootPackage (p)) != null) { if (!results.Contains (pack)) { foreach (DModule m in pack) cache [m] = results; results.Add (pack); } } if (dprj.LinkedFilePackage != null) results.Add (dprj.LinkedFilePackage); Add(results, dprj.References.Includes); foreach (var depPrj in dprj.GetReferencedDProjects(Ide.IdeApp.Workspace.ActiveConfiguration)) Add(results, depPrj.GetSourcePaths()); } if (results.Count == 0) results = globalIncludes; cache [module] = results; return results; }
public static DParameterDataProvider Create(Document doc, DModule SyntaxTree, CodeCompletionContext ctx) { var caretLocation = new CodeLocation (ctx.TriggerLineOffset, ctx.TriggerLine); var edData = DResolverWrapper.CreateEditorData(doc); edData.CaretLocation=caretLocation; edData.CaretOffset=ctx.TriggerOffset; var argsResult = ParameterInsightResolution.ResolveArgumentContext (edData); if (argsResult == null || argsResult.ResolvedTypesOrMethods == null || argsResult.ResolvedTypesOrMethods.Length < 1) return null; return new DParameterDataProvider(doc, argsResult, ctx.TriggerOffset); }
public static TypeReferencesResult Scan(DModule ast, ParseCacheView pcl, ConditionalCompilationFlags compilationEnvironment = null) { if (ast == null) return new TypeReferencesResult(); var typeRefFinder = new OldTypeReferenceFinder(pcl, compilationEnvironment); typeRefFinder.ast = ast; // Enum all identifiers typeRefFinder.S(ast); // Crawl through all remaining expressions by evaluating their types and check if they're actual type references. typeRefFinder.queueCount = typeRefFinder.q.Count; typeRefFinder.ResolveAllIdentifiers(); return typeRefFinder.result; }
public static EditorData CreateEditorData(Document EditorDocument, DModule Ast, CodeCompletionContext ctx, char triggerChar = '\0') { bool removeChar = char.IsLetter(triggerChar) || triggerChar == '_' || triggerChar == '@'; var deltaOffset = 0;//removeChar ? 1 : 0; var caretOffset = ctx.TriggerOffset - (removeChar ? 1 : 0); var caretLocation = new CodeLocation(ctx.TriggerLineOffset - deltaOffset, ctx.TriggerLine); var codeCache = CreateCacheList(EditorDocument); var ed = new EditorData { CaretLocation = caretLocation, CaretOffset = caretOffset, ModuleCode = removeChar ? EditorDocument.Editor.Text.Remove(ctx.TriggerOffset - 1, 1) : EditorDocument.Editor.Text, SyntaxTree = Ast, ParseCache = codeCache }; if (EditorDocument.HasProject) { var cfg = EditorDocument.Project.GetConfiguration(Ide.IdeApp.Workspace.ActiveConfiguration) as DProjectConfiguration; if (cfg != null) { ed.GlobalDebugIds = cfg.CustomDebugIdentifiers; ed.IsDebug = cfg.DebugMode; ed.DebugLevel = cfg.DebugLevel; ed.GlobalVersionIds = cfg.GlobalVersionIdentifiers; double d; int v; if (Double.TryParse(EditorDocument.Project.Version, out d)) ed.VersionNumber = (int)d; else if (Int32.TryParse(EditorDocument.Project.Version, out v)) ed.VersionNumber = v; } } if (ed.GlobalVersionIds == null) { ed.GlobalVersionIds = VersionIdEvaluation.GetOSAndCPUVersions(); } return ed; }
public static string FormatCode(string code, DModule ast = null, IDocumentAdapter document = null, DFormattingOptions options = null, ITextEditorOptions textStyle = null) { options = options ?? DFormattingOptions.CreateDStandard(); textStyle = textStyle ?? TextEditorOptions.Default; ast = ast ?? DParser.ParseString(code) as DModule; var formattingVisitor = new DFormattingVisitor(options, document ?? new TextDocument{ Text = code }, ast, textStyle); formattingVisitor.WalkThroughAst(); var sb = new StringBuilder(code); formattingVisitor.ApplyChanges((int start, int length, string insertedText) => { sb.Remove(start,length); sb.Insert(start,insertedText); }); return sb.ToString(); }
// http://www.digitalmars.com/d/2.0/module.html /// <summary> /// Module entry point /// </summary> public DModule Root() { Step(); var module = new DModule(); module.Location = new CodeLocation(1,1); module.BlockStartLocation = new CodeLocation(1, 1); doc = module; // Now only declarations or other statements are allowed! while (!IsEOF) { DeclDef(module); } // Also track comments at a module's end e.g. for multi-line comment folding GetComments(); module.EndLocation = la.Location; return module; }
/// <summary> /// </summary> /// <param name="ast">The syntax tree to scan</param> /// <param name="symbol">Might not be a child symbol of ast</param> /// <param name="ctxt">The context required to search for symbols</param> /// <returns></returns> public static IEnumerable<ISyntaxRegion> SearchModuleForASTNodeReferences(DModule ast, INode symbol, ResolutionContext ctxt, bool includeDefinition = true) { if (ast == null || symbol == null || ctxt == null) return null; var f = new ReferencesFinder(symbol, ast, ctxt); using(ctxt.Push(ast)) ast.Accept (f); var nodeRoot = symbol.NodeRoot as DModule; if (includeDefinition && nodeRoot != null && nodeRoot.FileName == ast.FileName) { var dc = symbol.Parent as DClassLike; if (dc != null && dc.ClassType == D_Parser.Parser.DTokens.Template && dc.NameHash == symbol.NameHash) { f.l.Insert(0, new IdentifierDeclaration(dc.NameHash) { Location = dc.NameLocation, EndLocation = new CodeLocation(dc.NameLocation.Column + dc.Name.Length, dc.NameLocation.Line) }); } var loc = symbol.NameLocation; bool add = !f.l.AsParallel().Any( (o) => (o is TemplateParameter && (o as TemplateParameter).NameLocation == loc) || (o is INode && (o as INode).NameLocation == loc)); if(add) f.l.Insert(0, new IdentifierDeclaration(symbol.NameHash) { Location = loc, EndLocation = new CodeLocation(loc.Column + symbol.Name.Length, loc.Line) }); } return f.l; }
public override IEnumerable<RootPackage> EnumRootPackagesSurroundingModule(DModule module) { return packs; }
/// <summary> /// Initializes and proceed parse procedure /// </summary> /// <param name="imports">List of imports in the module</param> /// <param name="ParseStructureOnly">If true, all statements and non-declarations are ignored - useful for analysing libraries</param> /// <returns>Completely parsed module structure</returns> public DModule Parse(bool ParseStructureOnly) { this.ParseStructureOnly = ParseStructureOnly; doc=Root(); doc.ParseErrors = new System.Collections.ObjectModel.ReadOnlyCollection<ParserError>(ParseErrors); return doc; }
public static EditorData CreateEditorData(Document EditorDocument, DModule Ast, CodeCompletionContext ctx, char triggerChar = '\0') { bool removeChar = char.IsLetter(triggerChar) || triggerChar == '_'; var deltaOffset = 0;//removeChar ? 1 : 0; var caretOffset = ctx.TriggerOffset - (removeChar ? 1 : 0); var caretLocation = new CodeLocation(ctx.TriggerLineOffset - deltaOffset, ctx.TriggerLine); var codeCache = CreateParseCacheView(EditorDocument); var ed = new EditorData { CaretLocation = caretLocation, CaretOffset = caretOffset, ModuleCode = removeChar ? EditorDocument.Editor.Text.Remove(ctx.TriggerOffset - 1, 1) : EditorDocument.Editor.Text, SyntaxTree = Ast, ParseCache = codeCache }; if (EditorDocument.HasProject) { var versions = new List<string>(); var debugConstants = new List<string> (); var cfg = EditorDocument.Project.GetConfiguration(IdeApp.Workspace.ActiveConfiguration); if (cfg is DProjectConfiguration) { var dcfg = cfg as DProjectConfiguration; ed.IsDebug = dcfg.DebugMode; ed.DebugLevel = dcfg.DebugLevel; double d; ulong v; if (Double.TryParse(EditorDocument.Project.Version, out d)) ed.VersionNumber = (ulong)d; else if (UInt64.TryParse(EditorDocument.Project.Version, out v)) ed.VersionNumber = v; } else if (cfg is DubProjectConfiguration) { versions.AddRange(VersionIdEvaluation.GetOSAndCPUVersions()); var dcfg = cfg as DubProjectConfiguration; ed.IsDebug = dcfg.DebugMode; } foreach (var prj in GetProjectDependencyHierarchyToCurrentStartupProject(EditorDocument.Project, cfg.Selector)) if(prj is AbstractDProject) ExtractVersionDebugConstantsFromProject (prj as AbstractDProject, versions, debugConstants); ed.GlobalDebugIds = debugConstants.ToArray (); ed.GlobalVersionIds = versions.ToArray (); } if (ed.GlobalVersionIds == null || ed.GlobalVersionIds.Length == 0) { ed.GlobalVersionIds = VersionIdEvaluation.GetOSAndCPUVersions(); } return ed; }
/// <summary> /// Initializes and proceed parse procedure /// </summary> /// <param name="imports">List of imports in the module</param> /// <param name="ParseStructureOnly">If true, all statements and non-declarations are ignored - useful for analysing libraries</param> /// <returns>Completely parsed module structure</returns> public DModule Parse(bool ParseStructureOnly, bool KeepComments = true) { this.ParseStructureOnly = ParseStructureOnly; if(KeepComments) Lexer.OnlyEnlistDDocComments = false; doc=Root(); doc.ParseErrors = new System.Collections.ObjectModel.ReadOnlyCollection<ParserError>(ParseErrors); if(KeepComments){ doc.Comments = TrackerVariables.Comments.ToArray(); } return doc; }
public static void UpdateModuleFromText(DModule Module, string Code) { var m = DParser.ParseString(Code); Module.ParseErrors = m.ParseErrors; Module.AssignFrom(m); }
/// <summary> /// Parses the module again /// </summary> /// <param name="Module"></param> public static void UpdateModule(DModule Module) { var m = DParser.ParseFile(Module.FileName); Module.ParseErrors = m.ParseErrors; Module.AssignFrom(m); }
internal static bool RemoveModule(DModule ast, ModulePackage pack) { if (ast == null || pack == null) return false; fileLookup.Remove (ast.FileName); if (!pack.RemoveModule (ast.ModuleName ?? "")) return false; ModulePackage parPack; if (pack.IsEmpty && (parPack = pack.Parent) != null) parPack.RemovePackage (pack.Name); return true; }
public static bool RemoveModule(DModule module) { return RemoveModule (module, GetPackage (module)); }
public static ModulePackage GetPackage(DModule module, bool create = false) { if (module == null) return null; var root = GetRootPackage (module.FileName); if (root == null) return null; return root.GetOrCreateSubPackage (ModuleNameHelper.ExtractPackageName (module.ModuleName), create); }
public void UpdateModule(string filename, string srcText, bool verbose) { filename = normalizePath(filename); DModule ast; try { ast = DParser.ParseString(srcText, false); } catch(Exception ex) { ast = new DModule{ ParseErrors = new System.Collections.ObjectModel.ReadOnlyCollection<ParserError>( new List<ParserError>{ new ParserError(false, ex.Message + "\n\n" + ex.StackTrace, DTokens.Invariant, CodeLocation.Empty) }) }; //WTF } if(string.IsNullOrEmpty(ast.ModuleName)) ast.ModuleName = Path.GetFileNameWithoutExtension(filename); ast.FileName = filename; _modules [filename] = ast; GlobalParseCache.AddOrUpdateModule(ast); _sources[filename] = srcText; //MessageBox.Show("UpdateModule(" + filename + ")"); //throw new NotImplementedException(); }
private static void GetReferencesInModule(DModule ast, StringBuilder refs, DNode n, ResolutionContext ctxt) { var res = ReferencesFinder.SearchModuleForASTNodeReferences(ast, n, ctxt); int cnt = res.Count(); foreach (var r in res) { var rfilename = ast.FileName; var rloc = r.Location; var ln = String.Format("{0},{1},{2},{3}:{4}\n", rloc.Line, rloc.Column - 1, rloc.Line, rloc.Column, rfilename); refs.Append(ln); } }
public static bool AddOrUpdateModule(DModule module) { ModulePackage p; return AddOrUpdateModule (module, out p); }
public ModuleSymbol(DModule mod, ISyntaxRegion td, PackageSymbol packageBase = null) : base(mod, packageBase, (IEnumerable<TemplateParameterSymbol>)null, td) { }
public static bool AddOrUpdateModule(DModule module, out ModulePackage pack) { pack = null; if (module == null || string.IsNullOrEmpty (module.ModuleName)) return false; pack = GetPackage (module, true); if (pack == null) return false; var file = module.FileName; // Check if a module is already in the file lookup DModule oldMod; if (file != null && fileLookup.TryGetValue (file, out oldMod)) { RemoveModule (oldMod); oldMod = null; } pack.AddModule (module); fileLookup.Add (file, module); return true; }