Exemple #1
0
        private void CheckPattern()
        {
            if (oldPattern != patternTextBox.Text || oldLanguages != Languages)
            {
                oldPattern   = patternTextBox.Text;
                oldLanguages = Languages;

                Dispatcher.UIThread.InvokeAsync(PatternErrors.Clear);
                patternLogger.Clear();

                UstNode patternNode = null;
                try
                {
                    if (!string.IsNullOrEmpty(patternTextBox.Text))
                    {
                        patternNode = dslProcessor.Deserialize(patternTextBox.Text, Languages);
                    }
                }
                catch
                {
                }

                if (patternLogger.ErrorCount == 0)
                {
                    PatternErrorsIsVisible = false;
                    PatternErrorsText      = "";
                    if (IsDeveloperMode && patternNode != null)
                    {
                        PatternJson = ustNodeJsonSerializer.Serialize(patternNode);
                        File.WriteAllText(Path.Combine(ServiceLocator.TempDirectory, "Pattern UST.json"), PatternJson);
                    }
                }
                else
                {
                    PatternErrorsIsVisible = true;
                    PatternErrorsText      = $"ERRORS ({patternLogger.ErrorCount})";
                    if (IsDeveloperMode)
                    {
                        PatternJson = "";
                    }
                }
                Dispatcher.UIThread.InvokeAsync(() =>
                {
                    this.RaisePropertyChanged(nameof(PatternErrorsIsVisible));
                    this.RaisePropertyChanged(nameof(PatternErrorsText));
                    this.RaisePropertyChanged(nameof(PatternJson));
                });

                if (ServiceLocator.MainWindowViewModel != null)
                {
                    ServiceLocator.MainWindowViewModel.UpdateMatchings();
                }
            }
        }
        internal void UpdateMatchings()
        {
            sourceCodeLogger.Clear();

            var sourceCodeRep = new MemoryCodeRepository(sourceCodeTextBox.Text);
            IPatternsRepository patternRepository;

            if (!string.IsNullOrEmpty(ServiceLocator.PatternViewModel.Value))
            {
                patternRepository = new DslPatternRepository(ServiceLocator.PatternViewModel.Value, ServiceLocator.PatternViewModel.Languages);
            }
            else
            {
                patternRepository = new MemoryPatternsRepository();
            }
            var workflow = new Workflow(sourceCodeRep, SelectedLanguage, patternRepository, stage: Stage);

            workflow.IsIncludeIntermediateResult = true;
            workflow.Logger = sourceCodeLogger;
            WorkflowResult workflowResult = workflow.Process();

            MatchingResultDto[] matchingResults = workflowResult.MatchingResults
                                                  .ToDto(workflow.SourceCodeRepository)
                                                  .ToArray();

            if (IsDeveloperMode)
            {
                AntlrParseTree antlrParseTree = workflowResult.ParseTrees.FirstOrDefault() as AntlrParseTree;
                if (antlrParseTree != null && antlrParseTree.SyntaxTree != null)
                {
                    Antlr4.Runtime.Parser antlrParser = (workflow.ParserConverterSets[antlrParseTree.SourceLanguage].Parser as AntlrParser).Parser;
                    string tokensString = AntlrHelper.GetTokensString(antlrParseTree.Tokens, antlrParser.Vocabulary, onlyDefaultChannel: true);
                    string treeString   = antlrParseTree.SyntaxTree.ToStringTreeIndented(antlrParser);

                    Tokens    = tokensString;
                    ParseTree = treeString;
                    File.WriteAllText(Path.Combine(ServiceLocator.TempDirectory, "Tokens.txt"), Tokens);
                    File.WriteAllText(Path.Combine(ServiceLocator.TempDirectory, "Tree.txt"), ParseTree);
                }
                if (Stage >= Stage.Convert && workflowResult.Usts.FirstOrDefault() != null)
                {
                    UstJson = jsonSerializer.Serialize(workflowResult.Usts.FirstOrDefault().Root);
                    File.WriteAllText(Path.Combine(ServiceLocator.TempDirectory, "UST.json"), UstJson);
                }
            }

            MatchingResultText = "MATCHINGS";
            if (matchingResults.Count() > 0)
            {
                MatchingResultText += " (" + matchingResults.Count() + ")";
            }

            if (sourceCodeLogger.ErrorCount == 0)
            {
                SourceCodeErrorsIsVisible = false;
                SourceCodeErrorsText      = "ERRORS";
            }
            else
            {
                SourceCodeErrorsIsVisible = true;
                SourceCodeErrorsText      = $"ERRORS ({sourceCodeLogger.ErrorCount})";
            }

            Dispatcher.UIThread.InvokeAsync(() =>
            {
                MatchingResults.Clear();
                foreach (var matchingResult in matchingResults)
                {
                    MatchingResults.Add(new MathingResultDtoWrapper(matchingResult));
                }
                this.RaisePropertyChanged(nameof(Tokens));
                this.RaisePropertyChanged(nameof(ParseTree));
                this.RaisePropertyChanged(nameof(UstJson));
                this.RaisePropertyChanged(nameof(MatchingResultText));
                this.RaisePropertyChanged(nameof(SourceCodeErrorsIsVisible));
                this.RaisePropertyChanged(nameof(SourceCodeErrorsText));
            });
        }