/// <summary> /// Reads and reflects the given VB Classic text file into a usable form. /// </summary> /// <param name="partitionedFile">An instance of <see cref="VbPartitionedFile"/> representing the VB Classic module to reflect.</param> /// <returns></returns> /// <exception cref="ArgumentNullException"><paramref name="partitionedFile"/> was null.</exception> /// <exception cref="InvalidOperationException">There was no analyzer considered fitting for the underlying file.</exception> public static IVbModule GetReflectedModule(VbPartitionedFile partitionedFile) { if (partitionedFile == null) { throw new ArgumentNullException("partitionedFile"); } if (Tokenizer == null) { throw new InvalidOperationException("No tokenizer defined for analyzing the file!"); } IReadOnlyList <IToken> tokens = Tokenizer.GetTokens(partitionedFile.GetMergedContent()); TokenStreamReader reader = new TokenStreamReader(tokens); IAnalyzer analyzer = null; if (!AnalyzerFactory.TryGetAnalyzerForFile(reader, out analyzer)) { // TODO: Dedicated exception for this. throw new InvalidOperationException("Could not analyze the given file!"); } reader.Rewind(); return(analyzer.Analyze(reader)); }
internal static bool TryGetAnalyzerForFile(TokenStreamReader reader, out IAnalyzer analyzer) { var best = _analyzers.FirstOrDefault(_ => _.CanAnalyze(reader.Rewind())); if (best != null) { analyzer = best; return(true); } analyzer = null; return(false); }
IVbModule IAnalyzer.Analyze(TokenStreamReader reader) { var attributes = AnalyzerTools.GetAttributes(reader).ToDictionary(_ => _.Name); GlobalModule mod = new GlobalModule(); mod.Visibility = MemberVisibility.Default; mod.Name = attributes[AnalyzerConstants.AttributeName_Name].Value; foreach (IVbAttribute attribute in attributes.Values) { mod.AddMember(attribute); } foreach (IVbField field in AnalyzerTools.GetFields(reader.Rewind())) { mod.AddMember(field); } foreach (IVbMethod method in AnalyzerTools.GetMethods(reader.Rewind())) { mod.AddMember(method); } return(mod); }