Exemplo n.º 1
0
        private Dictionary <string, Language> CreateExtensionMappingForRunner(Src2SrcMLRunner2 runner)
        {
            Dictionary <string, Language> extensionMapForRunner = new Dictionary <string, Language>(StringComparer.OrdinalIgnoreCase);
            IEnumerable <KeyValuePair <string, Language> > kvps = Enumerable.Empty <KeyValuePair <string, Language> >();

            if (runner == defaultExecutable)
            {
                kvps = from kvp in ExtensionMapping
                       where defaultLanguages.Contains(kvp.Value)
                       select kvp;
            }
            else if (nonDefaultExecutables.Values.Contains(runner))
            {
                var registeredLanguages = (from kvp in nonDefaultExecutables
                                           where kvp.Value == runner
                                           select kvp.Key).ToList();
                kvps = from kvp in ExtensionMapping
                       where registeredLanguages.Contains(kvp.Value)
                       select kvp;
            }
            foreach (var kvp in kvps)
            {
                extensionMapForRunner[kvp.Key] = kvp.Value;
            }

            return(extensionMapForRunner);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a new SrcMLGenerator.
 /// </summary>
 public SrcMLGenerator() : base() {
     defaultExecutable = new Src2SrcMLRunner2();
     defaultArguments = new[] { LIT.ArgumentLabel, POS.ArgumentLabel, TYPE.ArgumentLabel };
     nonDefaultExecutables = new Dictionary<Language, Src2SrcMLRunner2>();
     nonDefaultArguments = new Dictionary<Language, string[]>();
     DetectNonDefaultExecutables();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Registers a src2srcml executable to use for the given languages.
        /// </summary>
        /// <param name="executableDirectory">The directory containing the src2srcml executable to use.</param>
        /// <param name="languages">A collection of the Languages that should be parsed by this executable.</param>
        /// <param name="namespaceArguments">The namespace arguments to use when converting to SrcML.</param>
        public void RegisterExecutable(string executableDirectory, IEnumerable <Language> languages, IEnumerable <string> namespaceArguments)
        {
            if (nonDefaultExecutables == null)
            {
                nonDefaultExecutables = new Dictionary <Language, Src2SrcMLRunner2>();
            }

            var langList     = languages.ToList();
            var dupLanguages = langList.Intersect(nonDefaultExecutables.Keys);

            if (dupLanguages.Any())
            {
                var oldExec = nonDefaultExecutables[dupLanguages.First()];
                throw new InvalidOperationException(string.Format("Executable already registered for language {0}: {1}", dupLanguages.First(), oldExec.ExecutablePath));
            }

            var runner = new Src2SrcMLRunner2(executableDirectory);

            foreach (var lang in languages)
            {
                nonDefaultExecutables[lang] = runner;
                if (namespaceArguments != null)
                {
                    nonDefaultArguments[lang] = namespaceArguments.ToArray();
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new SrcMLGenerator
 /// </summary>
 /// <param name="defaultExecutableDirectory">The directory containing the default srcml executables to use.</param>
 /// <param name="namespaceArguments">The namespace arguments to use when converting to SrcML.</param>
 public SrcMLGenerator(string defaultExecutableDirectory, IEnumerable<string> namespaceArguments) {
     defaultExecutable = new Src2SrcMLRunner2(defaultExecutableDirectory);
     defaultArguments = namespaceArguments.ToArray();
     nonDefaultExecutables = new Dictionary<Language, Src2SrcMLRunner2>();
     nonDefaultArguments = new Dictionary<Language, string[]>();
     DetectNonDefaultExecutables();
 }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new SrcMLGenerator
 /// </summary>
 /// <param name="defaultExecutableDirectory">The directory containing the default srcml executables to use.</param>
 /// <param name="namespaceArguments">The namespace arguments to use when converting to SrcML.</param>
 public SrcMLGenerator(string defaultExecutableDirectory, IEnumerable <string> namespaceArguments) : base()
 {
     defaultExecutable     = new Src2SrcMLRunner2(defaultExecutableDirectory);
     defaultArguments      = namespaceArguments.ToArray();
     nonDefaultExecutables = new Dictionary <Language, Src2SrcMLRunner2>();
     nonDefaultArguments   = new Dictionary <Language, string[]>();
     DetectNonDefaultExecutables();
 }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a new SrcMLGenerator.
 /// </summary>
 public SrcMLGenerator() : base()
 {
     defaultExecutable     = new Src2SrcMLRunner2();
     defaultArguments      = new[] { LIT.ArgumentLabel, POS.ArgumentLabel, TYPE.ArgumentLabel };
     nonDefaultExecutables = new Dictionary <Language, Src2SrcMLRunner2>();
     nonDefaultArguments   = new Dictionary <Language, string[]>();
     DetectNonDefaultExecutables();
 }
Exemplo n.º 7
0
        /// <summary>
        /// Generates a SrcML document from a collection of source files using the specified language.
        /// </summary>
        /// <param name="sourceFileNames">The source files to generate SrcML from.</param>
        /// <param name="xmlFileName">The file name to write the resulting XML to.</param>
        /// <param name="language">The language to parse the source files as.</param>
        /// <returns>A SrcMLFile for <paramref name="xmlFileName"/>.</returns>
        public void GenerateSrcMLFromFiles(IEnumerable <string> sourceFileNames, string xmlFileName, Language language)
        {
            Src2SrcMLRunner2 runner = nonDefaultExecutables.ContainsKey(language) ? nonDefaultExecutables[language] : defaultExecutable;
            var mapForRunner        = CreateExtensionMappingForRunner(runner);
            var additionalArguments = CreateArgumentsForLanguage(language);

            runner.GenerateSrcMLFromFiles(sourceFileNames, xmlFileName, language, additionalArguments, mapForRunner);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Generate SrcML from a given string of source code.
        /// </summary>
        /// <param name="source">A string containing the source code to parse.</param>
        /// <param name="language">The language to parse the string as. Language.Any is not valid.</param>
        /// <returns>XML representing the source.</returns>
        public string GenerateSrcMLFromString(string source, Language language)
        {
            if (language == Language.Any)
            {
                throw new SrcMLException("Any is not a valid language. Pick an actual language in the enumeration");
            }
            Src2SrcMLRunner2 runner = nonDefaultExecutables.ContainsKey(language) ? nonDefaultExecutables[language] : defaultExecutable;
            var additionalArguments = CreateArgumentsForLanguage(language);

            return(runner.GenerateSrcMLFromString(source, language, additionalArguments, true));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Generate a SrcML document from a single source file.
        /// </summary>
        /// <param name="sourceFileName">The path to the source file to convert.</param>
        /// <param name="xmlFileName">The file name to write the resulting XML to.</param>
        /// <param name="language">The language to parse the source file as. If this is Language.Any, then the language will be determined from the file extension.</param>
        /// <returns>A SrcMLFile for <paramref name="xmlFileName"/>.</returns>
        public void GenerateSrcMLFromFile(string sourceFileName, string xmlFileName, Language language)
        {
            if (language == Language.Any)
            {
                string ext = Path.GetExtension(sourceFileName);
                if (ext == null || !extensionMapping.ContainsKey(ext))
                {
                    throw new ArgumentException(string.Format("Unknown file extension: {0}", ext), "sourceFileName");
                }
                language = extensionMapping[ext];
            }
            Src2SrcMLRunner2 runner = nonDefaultExecutables.ContainsKey(language) ? nonDefaultExecutables[language] : defaultExecutable;
            var additionalArguments = CreateArgumentsForLanguage(language);
            var runnerExtMap        = CreateExtensionMappingForRunner(runner);

            runner.GenerateSrcMLFromFile(sourceFileName, xmlFileName, language, additionalArguments, runnerExtMap);
        }
Exemplo n.º 10
0
 private Dictionary<string, Language> CreateExtensionMappingForRunner(Src2SrcMLRunner2 runner) {
     Dictionary<string, Language> extensionMapForRunner = new Dictionary<string, Language>(StringComparer.OrdinalIgnoreCase);
     IEnumerable<KeyValuePair<string, Language>> kvps = Enumerable.Empty<KeyValuePair<string,Language>>();
     if(runner == defaultExecutable) {
         kvps = from kvp in ExtensionMapping
                where defaultLanguages.Contains(kvp.Value)
                select kvp;
     } else if(nonDefaultExecutables.Values.Contains(runner)) {
         var registeredLanguages = (from kvp in nonDefaultExecutables
                                    where kvp.Value == runner
                                    select kvp.Key).ToList();
         kvps = from kvp in ExtensionMapping
                where registeredLanguages.Contains(kvp.Value)
                select kvp;
     }
     foreach(var kvp in kvps) {
         extensionMapForRunner[kvp.Key] = kvp.Value;
     }
     
     return extensionMapForRunner;
 }
Exemplo n.º 11
0
        /// <summary>
        /// Registers a src2srcml executable to use for the given languages.
        /// </summary>
        /// <param name="executableDirectory">The directory containing the src2srcml executable to use.</param>
        /// <param name="languages">A collection of the Languages that should be parsed by this executable.</param>
        /// <param name="namespaceArguments">The namespace arguments to use when converting to SrcML.</param>
        public void RegisterExecutable(string executableDirectory, IEnumerable<Language> languages, IEnumerable<string> namespaceArguments) {
            if(nonDefaultExecutables == null) {
                nonDefaultExecutables = new Dictionary<Language, Src2SrcMLRunner2>();
            }

            var langList = languages.ToList();
            var dupLanguages = langList.Intersect(nonDefaultExecutables.Keys);
            if(dupLanguages.Any()) {
                var oldExec = nonDefaultExecutables[dupLanguages.First()];
                throw new InvalidOperationException(string.Format("Executable already registered for language {0}: {1}", dupLanguages.First(), oldExec.ExecutablePath));
            }

            var runner = new Src2SrcMLRunner2(executableDirectory);
            
            foreach(var lang in languages) {
                nonDefaultExecutables[lang] = runner;
                if(namespaceArguments != null) {
                    nonDefaultArguments[lang] = namespaceArguments.ToArray();
                }
            }
        }