Пример #1
0
        public UstNode Deserialize(string data, LanguageFlags sourceLanguage)
        {
            if (string.IsNullOrEmpty(data))
            {
                throw new ParsingException("Pattern value can not be empty.")
                      {
                          IsPattern = true
                      };
            }

            Parser.Logger       = Logger;
            UstConverter.Logger = Logger;
            DslParser.PatternContext patternContext = Parser.Parse(data);
            UstConverter.SourceLanguage = sourceLanguage;
            UstConverter.Data           = data;
            DslNode dslNode = UstConverter.Convert(patternContext);
            UstNode result  = dslNode.Collection.First();

            ResultPatternVars = dslNode.PatternVarDefs;
            var preprocessor = new UstPreprocessor();

            preprocessor.Logger = Logger;
            result = new PatternNode(result, dslNode.PatternVarDefs);
            result = preprocessor.Preprocess(result);

            return(result);
        }
Пример #2
0
        public void Preprocess_MultiMultiPattern_RemovedDuplicates()
        {
            UstNode patternWithDuplicateMultiStatementsExpressions = new PatternStatements
            {
                Statements = new List <Statement>()
                {
                    new ExpressionStatement(
                        new InvocationExpression
                    {
                        Target    = new IdToken("test_call"),
                        Arguments = new PatternExpressions
                        {
                            Collection = new List <Expression>
                            {
                                new IdToken("a"),
                                new IdToken("b"),
                                new PatternMultipleExpressions(),
                                new PatternMultipleExpressions(),
                                new IdToken("z")
                            }
                        }
                    }),

                    new PatternMultipleStatements(),
                    new PatternMultipleStatements(),

                    new ExpressionStatement(
                        new VariableDeclarationExpression
                    {
                        Type      = new TypeToken("int"),
                        Variables = new List <AssignmentExpression>()
                        {
                            new AssignmentExpression
                            {
                                Left  = new IdToken("a"),
                                Right = new IntLiteral {
                                    Value = 42
                                }
                            }
                        }
                    }
                        )
                }
            };
            var             logger       = new LoggerMessageCounter();
            var             processor    = new DslProcessor();
            UstPreprocessor preprocessor = new UstPreprocessor()
            {
                Logger = logger
            };
            UstNode result = preprocessor.Preprocess(patternWithDuplicateMultiStatementsExpressions);

            Assert.AreEqual(1, result.GetAllDescendants().Count(child => child.NodeType == NodeType.PatternMultipleStatements));
            Assert.AreEqual(1, result.GetAllDescendants().Count(child => child.NodeType == NodeType.PatternMultipleExpressions));
        }
Пример #3
0
        public void Sort_PatternVars()
        {
            var unsortedExpressions = new List <Expression>()
            {
                new IntLiteral {
                    Value = 100
                },
                new IntLiteral {
                    Value = 42
                },
                new IntLiteral {
                    Value = 0
                },
                new StringLiteral {
                    Text = "42"
                },
                new StringLiteral {
                    Text = "Hello World!"
                },
                new IdToken {
                    Id = "testId"
                },
                new IdToken {
                    Id = "42"
                },
                new PatternExpression(new StringLiteral {
                    Text = "42"
                }, true),
            };
            var expectedSortedExpressions = new List <Expression>
            {
                new StringLiteral {
                    Text = "42"
                },
                new PatternExpression(new StringLiteral {
                    Text = "42"
                }, true),
                new StringLiteral {
                    Text = "Hello World!"
                },
                new IdToken {
                    Id = "42"
                },
                new IdToken {
                    Id = "testId"
                },
                new IntLiteral {
                    Value = 0
                },
                new IntLiteral {
                    Value = 42
                },
                new IntLiteral {
                    Value = 100
                },
            };
            var patternVarDef = new PatternVarDef
            {
                Id     = "testVarDef",
                Values = unsortedExpressions
            };
            var patternVars = new PatternNode
            {
                Vars = new List <PatternVarDef>()
                {
                    patternVarDef
                },
                Node = new PatternVarRef(patternVarDef)
            };

            var             logger       = new LoggerMessageCounter();
            var             processor    = new DslProcessor();
            UstPreprocessor preprocessor = new UstPreprocessor()
            {
                Logger = logger
            };

            Expression[] resultSortedExpressions = ((PatternNode)preprocessor.Preprocess(patternVars))
                                                   .Vars.First().Values.ToArray();

            Assert.AreEqual(expectedSortedExpressions.Count, resultSortedExpressions.Length);
            for (int i = 0; i < expectedSortedExpressions.Count; i++)
            {
                Assert.IsTrue(expectedSortedExpressions[i].Equals(resultSortedExpressions[i]),
                              $"Not equal at {i} index: expected {expectedSortedExpressions[i]} not equals to {resultSortedExpressions[i]}");
            }
        }
Пример #4
0
        private void ProcessFile(string fileName, Task convertPatternsTask, WorkflowResult workflowResult, CancellationToken cancellationToken = default(CancellationToken))
        {
            ParseTree parseTree = null;

            try
            {
                Logger.LogInfo(new MessageEventArgs(MessageType.ProcessingStarted, fileName));

                parseTree = ReadAndParse(fileName, workflowResult);
                if (parseTree != null)
                {
                    workflowResult.AddResultEntity(parseTree);

                    if (Stage >= Stage.Convert)
                    {
                        var stopwatch = Stopwatch.StartNew();
                        IParseTreeToUstConverter converter = ParserConverterSets[parseTree.SourceLanguage].Converter;
                        Ust ust = converter.Convert(parseTree);
                        stopwatch.Stop();
                        Logger.LogInfo($"File {fileName} has been converted (Elapsed: {stopwatch.Elapsed}).");
                        workflowResult.AddConvertTime(stopwatch.ElapsedTicks);
                        workflowResult.AddResultEntity(ust, true);

                        cancellationToken.ThrowIfCancellationRequested();

                        if (Stage >= Stage.Preprocess)
                        {
                            if (IsIncludePreprocessing)
                            {
                                var ustPreprocessor = new UstPreprocessor()
                                {
                                    Logger = logger
                                };
                                stopwatch.Restart();
                                ust = ustPreprocessor.Preprocess(ust);
                                stopwatch.Stop();
                                Logger.LogInfo($"Ust of file {fileName} has been preprocessed (Elapsed: {stopwatch.Elapsed}).");
                                workflowResult.AddPreprocessTime(stopwatch.ElapsedTicks);
                                workflowResult.AddResultEntity(ust, false);

                                cancellationToken.ThrowIfCancellationRequested();
                            }

                            if (Stage >= Stage.Match)
                            {
                                if (!convertPatternsTask.IsCompleted)
                                {
                                    convertPatternsTask.Wait();
                                }

                                stopwatch.Restart();
                                IEnumerable <MatchingResult> matchingResults = UstPatternMatcher.Match(ust);
                                stopwatch.Stop();
                                Logger.LogInfo($"File {fileName} has been matched with patterns (Elapsed: {stopwatch.Elapsed}).");
                                workflowResult.AddMatchTime(stopwatch.ElapsedTicks);
                                workflowResult.AddResultEntity(matchingResults);

                                cancellationToken.ThrowIfCancellationRequested();
                            }
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
            finally
            {
                workflowResult.AddProcessedFilesCount(1);
                double progress = workflowResult.TotalFilesCount == 0
                    ? 1
                    : (double)workflowResult.TotalProcessedFilesCount / workflowResult.TotalFilesCount;
                Logger.LogInfo(new ProgressEventArgs(progress, fileName));
                Logger.LogInfo(new MessageEventArgs(MessageType.ProcessingCompleted, fileName));

                if (parseTree == null)
                {
                    Logger.LogInfo(new MessageEventArgs(MessageType.ProcessingIgnored, fileName));
                }

                cancellationToken.ThrowIfCancellationRequested();
            }
        }