Пример #1
0
        public Workflow(ISourceCodeRepository sourceCodeRepository, LanguageFlags languages,
                        IPatternsRepository patternsRepository = null, Stage stage = Stage.Match)
            : base(stage)
        {
            SourceCodeRepository = sourceCodeRepository;
            PatternsRepository   = patternsRepository ?? new DefaultPatternRepository();
            ParserConverterSets  = ParserConverterBuilder.GetParserConverterSets(languages);
            UstPatternMatcher    = new BruteForcePatternMatcher();
            IUstNodeSerializer jsonNodeSerializer = new JsonUstNodeSerializer(typeof(UstNode), typeof(PatternVarDef));
            IUstNodeSerializer dslNodeSerializer  = new DslProcessor();

            PatternConverter = new PatternConverter(new IUstNodeSerializer[] { jsonNodeSerializer, dslNodeSerializer });
            Stage            = stage;
            ThreadCount      = 1;
        }
Пример #2
0
        public override Language?Detect(string sourceCode, Language[] languages = null)
        {
            List <Language> langs = (languages ?? LanguageExt.Languages).ToList();

            // Any PHP file contains start tag.
            if (!sourceCode.Contains("<?"))
            {
                langs.Remove(Language.Php);
            }
            // Aspx and html code contains at least one tag.
            if (!openTagRegex.IsMatch(sourceCode) || !closeTagRegex.IsMatch(sourceCode))
            {
                langs.Remove(Language.Aspx);
                langs.Remove(Language.Html);
            }
            var sourceCodeFile = new SourceCodeFile("Temp Source Code")
            {
                Code = sourceCode
            };
            var parseUnits = new Queue <Tuple <Language, ParserUnit> >(langs.Count);

            langs = langs
                    .GroupBy(l => ParserConverterBuilder.GetParserConverterSet(l).Parser.Language)
                    .Select(l => l.First())
                    .ToList();

            if (langs.Count == 1)
            {
                return(langs[0]);
            }

            foreach (var language in langs)
            {
                var             logger         = new LoggerMessageCounter();
                ILanguageParser languageParser = ParserConverterBuilder.GetParserConverterSet(language).Parser;

                var task = Task.Factory.StartNew(() =>
                {
                    languageParser.Logger = logger;
                    languageParser.Parse(sourceCodeFile);
                });

                parseUnits.Enqueue(Tuple.Create(language, new ParserUnit(languageParser, logger, task)));
            }

            int      minErrorCount       = int.MaxValue;
            Language?resultWithMinErrors = null;

            // Check every parseUnit completion every 50 ms.
            while (parseUnits.Count > 0)
            {
                var pair = parseUnits.Dequeue();
                if (!pair.Item2.Task.IsCompleted)
                {
                    parseUnits.Enqueue(pair);
                    Thread.Sleep(50);
                    continue;
                }

                if (pair.Item2.Logger.ErrorCount == 0 && pair.Item1 != Language.Aspx)
                {
                    return(pair.Item1);
                }

                var errorCount = pair.Item2.ParseErrorCount;
                if (errorCount < minErrorCount)
                {
                    minErrorCount       = errorCount;
                    resultWithMinErrors = pair.Item1;
                }
            }

            return(resultWithMinErrors);
        }