示例#1
0
        public MatchResultDto(MatchResult matchResult)
        {
            CodeFile sourceCodeFile = matchResult.SourceCodeFile;
            string   code           = sourceCodeFile.Code;

            TextSpan = matchResult.TextSpans.Where(textSpan =>
                                                   textSpan.Start >= 0 &&
                                                   textSpan.End <= textSpan.GetCodeFile(sourceCodeFile).Code.Length)
                       .Union();
            LineColumnTextSpan = sourceCodeFile.GetLineColumnTextSpan(TextSpan);

            PatternKey  = matchResult.Pattern.Key;
            SourceFile  = sourceCodeFile.FullName;
            MatchedCode = code.Substring(TextSpan);
        }
        private void ProcessError(int startIndex, int stopIndex, string msg)
        {
            int lineLinearIndex = CodeFile.GetLineLinearIndex(LineOffset);

            startIndex = startIndex + lineLinearIndex;
            stopIndex  = stopIndex + 1 + lineLinearIndex;
            if (stopIndex <= startIndex)
            {
                startIndex = stopIndex - 1;
            }
            TextSpan textSpan = TextSpan.FromBounds(startIndex, stopIndex);

            string errorMessage = $"{msg} at {CodeFile.GetLineColumnTextSpan(textSpan)}";

            Logger.LogError(new ParsingException(CodeFile, message: errorMessage)
            {
                TextSpan = textSpan,
            });
        }
示例#3
0
        public override void WriteJson(JsonWriter writer, TextSpan textSpan, JsonSerializer serializer)
        {
            try
            {
                string textSpanString;
                bool   includeFileName = textSpan.CodeFile != CurrentCodeFile;

                if (!IsLineColumn)
                {
                    textSpanString = textSpan.IsZero && EmptyTextSpanFormat != null
                        ? EmptyTextSpanFormat
                        : textSpan.ToString(includeFileName);
                }
                else
                {
                    if (textSpan.IsZero && EmptyTextSpanFormat != null)
                    {
                        textSpanString = EmptyTextSpanFormat;
                    }
                    else
                    {
                        CodeFile codeFile = textSpan.CodeFile ?? CurrentCodeFile;
                        if (codeFile != null)
                        {
                            textSpanString = codeFile.GetLineColumnTextSpan(textSpan).ToString(includeFileName);
                        }
                        else
                        {
                            Logger.LogError(JsonFile, (writer as JTokenWriter)?.CurrentToken, $"Unable convert {nameof(TextSpan)} to {nameof(LineColumnTextSpan)} due to undefined file");
                            textSpanString = LineColumnTextSpan.Zero.ToString();
                        }
                    }
                }

                writer.WriteValue(textSpanString);
            }
            catch (Exception ex) when(!(ex is ThreadAbortException))
            {
                Logger.LogError(JsonFile, (writer as JTokenWriter)?.CurrentToken, ex);
            }
        }
示例#4
0
 private void UpdateSourceCodeSelection(int index)
 {
     if (sourceCode != null)
     {
         int start = sourceCodeTextBox.SelectionStart;
         int end   = sourceCodeTextBox.SelectionEnd;
         if (start > end)
         {
             int t = start;
             start = end;
             end   = t;
         }
         var textSpan           = TextSpan.FromBounds(start, end);
         var lineColumnTextSpan = sourceCode.GetLineColumnTextSpan(textSpan);
         SourceCodeTextBoxPosition = $"Range: {lineColumnTextSpan}; LineColumn: {textSpan}";
     }
     else
     {
         SourceCodeTextBoxPosition = $"";
     }
     Dispatcher.UIThread.InvokeAsync(() => this.RaisePropertyChanged(nameof(SourceCodeTextBoxPosition)));
 }
示例#5
0
        public static void LogConversionError(this ILogger logger, Exception ex,
                                              ParserRuleContext context, CodeFile currentFileData)
        {
            StackTrace stackTrace  = new StackTrace(ex, true);
            int        frameNumber = 0;
            string     fileName    = null;
            string     methodName  = null;
            int        line        = 0;
            int        column      = 0;

            do
            {
                StackFrame frame = stackTrace.GetFrame(frameNumber);
                fileName   = frame.GetFileName();
                methodName = frame.GetMethod().Name;
                line       = frame.GetFileLineNumber();
                column     = frame.GetFileColumnNumber();
                frameNumber++;
            }while (frameNumber < stackTrace.FrameCount && (fileName == null || methodName == "Visit"));

            var                textSpan = context.GetTextSpan();
            string             exceptionText;
            LineColumnTextSpan lineColumnTextSpan = currentFileData.GetLineColumnTextSpan(textSpan);

            if (fileName != null)
            {
                exceptionText = $"{ex.Message} at method \"{methodName}\" {line}:{column} at position {lineColumnTextSpan.BeginLine}:{lineColumnTextSpan.BeginColumn} in source file";
            }
            else
            {
                exceptionText = $"{ex.Message} at position {lineColumnTextSpan.BeginLine}:{lineColumnTextSpan.BeginColumn} in source file";
            }
            logger.LogError(new ConversionException(currentFileData, message: exceptionText)
            {
                TextSpan = textSpan
            });
        }
示例#6
0
        private static void CheckJsonSerialization(string inputFileName, bool linearTextSpans = false,
                                                   bool includeTextSpans = true, bool indented = true, bool includeCode               = false,
                                                   bool checkStrict      = false, bool strict  = true, bool checkPatternSerialization = false)
        {
            string path = Path.Combine(TestUtility.TestsDataPath, inputFileName);

            // Serialization
            string[] files = File.Exists(path)
                           ? new string[] { path }
                           : Directory.GetFiles(path);
            var codeRepository = new FileCodeRepository(files);

            var workflow = new Workflow(codeRepository)
            {
                DumpStages = new HashSet <Stage>()
                {
                    Stage.Ust
                },
                DumpDir             = TestUtility.TestsOutputPath,
                IncludeCodeInDump   = includeCode,
                IndentedDump        = indented,
                StrictJson          = strict,
                DumpWithTextSpans   = includeTextSpans,
                LineColumnTextSpans = !linearTextSpans,
                Stage          = Stage.Ust,
                IsDumpPatterns = checkPatternSerialization
            };
            WorkflowResult result = workflow.Process();

            CodeFile preprocessedFile = result.SourceCodeFiles.FirstOrDefault(f => f.Name == "preprocessed.php");
            CodeFile originFile       = result.SourceCodeFiles.FirstOrDefault(f => f.Name == "origin.php");

            LineColumnTextSpan lcPreprocessedTextSpan = new LineColumnTextSpan(4, 1, 4, 3);
            LineColumnTextSpan lcOriginTextSpan       = new LineColumnTextSpan(3, 1, 3, 3, originFile);

            var jsonFiles = new List <string>();

            foreach (string file in files)
            {
                string shortFileName = Path.GetFileName(file) + ".ust.json";
                string jsonFile      = Path.Combine(TestUtility.TestsOutputPath, shortFileName);

                if (file.Contains("preprocessed.php") || checkStrict)
                {
                    string json = File.ReadAllText(jsonFile);
                    if (file.Contains("preprocessed.php"))
                    {
                        string preprocessedTextSpanString, originTextSpanString;

                        if (linearTextSpans)
                        {
                            preprocessedTextSpanString = preprocessedFile.GetTextSpan(lcPreprocessedTextSpan).ToString();
                            originTextSpanString       = originFile.GetTextSpan(lcOriginTextSpan).ToString();
                        }
                        else
                        {
                            preprocessedTextSpanString = lcPreprocessedTextSpan.ToString();
                            originTextSpanString       = lcOriginTextSpan.ToString();
                        }

                        json = json.Replace($"\"{preprocessedTextSpanString}\"", $"[ \"{lcPreprocessedTextSpan}\", \"{originTextSpanString}\" ]");
                    }

                    if (checkStrict)
                    {
                        json = json.Replace("\"Kind\": \"IntLiteral\"", "\"Kind\": \"IntLiteral\", \"ExcessProperty\": \"value\"");
                    }

                    File.WriteAllText(jsonFile, json);
                }

                jsonFiles.Add(jsonFile);
            }

            // Deserialization
            var logger            = new LoggerMessageCounter();
            var newCodeRepository = new FileCodeRepository(jsonFiles)
            {
                LoadJson = true
            };

            var newPatternsRepository = checkPatternSerialization
                ? new JsonPatternsRepository(File.ReadAllText(Path.Combine(TestUtility.TestsOutputPath, "patterns.json")))
                : inputFileName == "MultiTextSpan"
                ? (IPatternsRepository) new DslPatternRepository("a", "php")
                : new DefaultPatternRepository();
            var newWorkflow = new Workflow(newCodeRepository, newPatternsRepository)
            {
                StartStage          = Stage.Ust,
                IndentedDump        = indented,
                StrictJson          = strict,
                DumpWithTextSpans   = includeTextSpans,
                LineColumnTextSpans = !linearTextSpans,
                Logger = logger,
            };
            WorkflowResult newResult = newWorkflow.Process();

            if (checkStrict && strict)
            {
                Assert.IsTrue(logger.ErrorsString.Contains("ExcessProperty"));
            }
            else
            {
                Assert.AreEqual(0, logger.ErrorCount, logger.ErrorsString);
                Assert.GreaterOrEqual(newResult.MatchResults.Count, 1);

                if (includeTextSpans)
                {
                    if (inputFileName == "MultiTextSpan")
                    {
                        var matchResult = (MatchResult)newResult.MatchResults[1];
                        Assert.AreEqual(2, matchResult.TextSpans.Length);

                        LineColumnTextSpan actualOriginTextSpan = originFile.GetLineColumnTextSpan(matchResult.TextSpans[1]);
                        Assert.AreEqual(lcOriginTextSpan, actualOriginTextSpan);

                        LineColumnTextSpan actualPreprocessedTextSpan = preprocessedFile.GetLineColumnTextSpan(matchResult.TextSpans[0]);
                        Assert.AreEqual(lcPreprocessedTextSpan, actualPreprocessedTextSpan);
                    }
                    else
                    {
                        var match = (MatchResult)newResult.MatchResults.FirstOrDefault();
                        Assert.AreEqual(new LineColumnTextSpan(2, 1, 3, 25), result.SourceCodeFiles.First().GetLineColumnTextSpan(match.TextSpan));
                    }
                }
            }
        }