コード例 #1
0
ファイル: ModuleReflector.cs プロジェクト: mks786/vb6leap
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Reads the provided content, which is assumed to be a VB6 module file, and splits it into a "header" and a "code" part.
        /// </summary>
        /// <param name="content">The source code contents of a VB6 module file.</param>
        /// <returns>An instance of <see cref="VbPartitionedFile"/> that contains the provided content.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="content"/> is null.</exception>
        public static VbPartitionedFile GetPartitionedFile(string content)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            VbPartitionedFile file = new VbPartitionedFile();

            /* VB6-files are slightly odd. They don't really have a common file format (only a somewhat consistent layout).
             */
            StringBuilder sb = new StringBuilder();

            // Keep the stream open for users to close.

            string lineBefore = null;
            string line       = null;

            using (StringReader reader = new StringReader(content))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    /* If the last line was an Attribute and the current line represents user code, flush the current contents into the Preamble.
                     * This is really basic here and should be refined if some files are different.
                     */
                    if (lineBefore != null && lineBefore.StartsWith("Attribute", StringComparison.OrdinalIgnoreCase))
                    {
                        if ((string.IsNullOrWhiteSpace(line) || !line.StartsWith("Attribute", StringComparison.OrdinalIgnoreCase)) &&
                            file.Preamble == null)
                        {
                            file.Preamble = sb.ToString();
                            sb.Clear();
                        }
                    }

                    sb.AppendLine(line);

                    lineBefore = line;
                }
            }

            file.Source = sb.ToString();

            return(file);
        }
コード例 #3
0
ファイル: VbPartitionedFile.cs プロジェクト: mks786/vb6leap
        /// <summary>
        /// Reads the provided content, which is assumed to be a VB6 module file, and splits it into a "header" and a "code" part.
        /// </summary>
        /// <param name="content">The source code contents of a VB6 module file.</param>
        /// <returns>An instance of <see cref="VbPartitionedFile"/> that contains the provided content.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="content"/> is null.</exception>
        public static VbPartitionedFile GetPartitionedFile(string content)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }

            VbPartitionedFile file = new VbPartitionedFile();

            /* VB6-files are slightly odd. They don't really have a common file format (only a somewhat consistent layout).
             */
            StringBuilder sb = new StringBuilder();

            // Keep the stream open for users to close.

            string lineBefore = null;
            string line = null;

            using (StringReader reader = new StringReader(content))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    /* If the last line was an Attribute and the current line represents user code, flush the current contents into the Preamble.
                     * This is really basic here and should be refined if some files are different.
                     */
                    if (lineBefore != null && lineBefore.StartsWith("Attribute", StringComparison.OrdinalIgnoreCase))
                    {
                        if ((string.IsNullOrWhiteSpace(line) || !line.StartsWith("Attribute", StringComparison.OrdinalIgnoreCase))
                            && file.Preamble == null)
                        {
                            file.Preamble = sb.ToString();
                            sb.Clear();
                        }
                    }

                    sb.AppendLine(line);

                    lineBefore = line;
                }
            }

            file.Source = sb.ToString();

            return file;
        }
コード例 #4
0
ファイル: VB6UnresolvedFile.cs プロジェクト: mks786/vb6leap
 private IVbModule ParseModule(VbPartitionedFile partFile)
 {
     return ModuleReflector.GetReflectedModule(partFile);
 }