public static bool TryGetLineStatements(string text, int lineNumber, out IList<IParseTree> statementTrees, out IList<IToken> tokens) { Contract.Requires<ArgumentNullException>(text != null, "text"); Contract.Requires<ArgumentOutOfRangeException>(lineNumber >= 0); try { AntlrInputStream input = new AntlrInputStream(text); JavaLexer lexer = new JavaLexer(new JavaUnicodeStreamV4(input)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); JavaParser parser = new JavaParser(tokenStream); parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.BuildParseTree = true; JavaParser.CompilationUnitContext result = parser.compilationUnit(); statementTrees = null; tokens = tokenStream.GetTokens(); AssociatedTreeListener listener = new AssociatedTreeListener(lineNumber, tokens); ParseTreeWalker.Default.Walk(listener, result); statementTrees = listener.StatementTrees; return true; } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) throw; statementTrees = null; tokens = null; return false; } }
public static bool TryGetLineStatements(string text, int lineNumber, out IList <IParseTree> statementTrees, out IList <IToken> tokens) { Contract.Requires <ArgumentNullException>(text != null, "text"); Contract.Requires <ArgumentOutOfRangeException>(lineNumber >= 0); try { AntlrInputStream input = new AntlrInputStream(text); JavaLexer lexer = new JavaLexer(new JavaUnicodeStreamV4(input)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); JavaParser parser = new JavaParser(tokenStream); parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.BuildParseTree = true; JavaParser.CompilationUnitContext result = parser.compilationUnit(); statementTrees = null; tokens = tokenStream.GetTokens(); AssociatedTreeListener listener = new AssociatedTreeListener(lineNumber, tokens); ParseTreeWalker.Default.Walk(listener, result); statementTrees = listener.StatementTrees; return(true); } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) { throw; } statementTrees = null; tokens = null; return(false); } }
private bool TryGetAssociatedTree(out IParseTree associatedTree, out IList <IToken> tokens) { try { string sourcePath = _location.GetSourcePath(); if (!File.Exists(sourcePath)) { associatedTree = null; tokens = null; return(false); } string text = File.ReadAllText(sourcePath); AntlrInputStream input = new AntlrInputStream(text); JavaLexer lexer = new JavaLexer(new JavaUnicodeStreamV4(input)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); JavaParser parser = new JavaParser(tokenStream); parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.BuildParseTree = true; JavaParser.CompilationUnitContext result = parser.compilationUnit(); associatedTree = null; tokens = tokenStream.GetTokens(); AssociatedTreeListener listener = new AssociatedTreeListener(_location, tokens); ParseTreeWalker.Default.Walk(listener, result); List <IParseTree> potentialTrees = listener.AssociatedTree; if (potentialTrees.Count == 1) { associatedTree = potentialTrees[0]; } else if (potentialTrees.Count > 1) { byte[] bytecode = _location.GetMethod().GetBytecodes(); DisassembledMethod disassembledMethod = BytecodeDisassembler.Disassemble(bytecode); var constantPool = _location.GetDeclaringType().GetConstantPool(); var exceptionTable = _location.GetMethod().GetExceptionTable(); ImmutableList <int?> evaluationStackDepths = BytecodeDisassembler.GetEvaluationStackDepths(disassembledMethod, constantPool, exceptionTable); ReadOnlyCollection <ILocation> locations = _location.GetMethod().GetLineLocations(); // find all bytecode offsets with evaluation stack depth 0 on the current line List <int> relevantOffsets = new List <int>(); for (int i = 0; i < locations.Count; i++) { if (locations[i].GetLineNumber() != _location.GetLineNumber()) { continue; } long offsetLimit = i < locations.Count - 1 ? locations[i + 1].GetCodeIndex() : bytecode.Length; // start with the instruction for this bytecode offset for (int j = GetInstructionAtOffset(disassembledMethod, locations[i].GetCodeIndex()); j >= 0 && j < disassembledMethod.Instructions.Count && disassembledMethod.Instructions[j].Offset < offsetLimit; j++) { if (evaluationStackDepths[j] == 0) { // ignore unconditional branches if (disassembledMethod.Instructions[j].OpCode.FlowControl == JavaFlowControl.Branch) { continue; } relevantOffsets.Add(disassembledMethod.Instructions[j].Offset); } } } if (relevantOffsets.Count == potentialTrees.Count) { // heuristic: assume they appear in the same order as the source code on this line int treeIndex = relevantOffsets.IndexOf((int)_location.GetCodeIndex()); if (treeIndex >= 0) { associatedTree = potentialTrees[treeIndex]; } } } if (associatedTree == null) { tokens = null; return(false); } return(true); } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) { throw; } associatedTree = null; tokens = null; return(false); } }
private bool TryGetAssociatedTree(out IParseTree associatedTree, out IList<IToken> tokens) { try { string sourcePath = _location.GetSourcePath(); if (!File.Exists(sourcePath)) { associatedTree = null; tokens = null; return false; } string text = File.ReadAllText(sourcePath); AntlrInputStream input = new AntlrInputStream(text); JavaLexer lexer = new JavaLexer(new JavaUnicodeStreamV4(input)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); JavaParser parser = new JavaParser(tokenStream); parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.BuildParseTree = true; JavaParser.CompilationUnitContext result = parser.compilationUnit(); associatedTree = null; tokens = tokenStream.GetTokens(); AssociatedTreeListener listener = new AssociatedTreeListener(_location, tokens); ParseTreeWalker.Default.Walk(listener, result); List<IParseTree> potentialTrees = listener.AssociatedTree; if (potentialTrees.Count == 1) { associatedTree = potentialTrees[0]; } else if (potentialTrees.Count > 1) { byte[] bytecode = _location.GetMethod().GetBytecodes(); DisassembledMethod disassembledMethod = BytecodeDisassembler.Disassemble(bytecode); var constantPool = _location.GetDeclaringType().GetConstantPool(); ReadOnlyCollection<ExceptionTableEntry> exceptionTable; try { exceptionTable = _location.GetMethod().GetExceptionTable(); } catch (DebuggerException) { exceptionTable = new ReadOnlyCollection<ExceptionTableEntry>(new ExceptionTableEntry[0]); } ImmutableList<int?> evaluationStackDepths = BytecodeDisassembler.GetEvaluationStackDepths(disassembledMethod, constantPool, exceptionTable); ReadOnlyCollection<ILocation> locations = _location.GetMethod().GetLineLocations(); // find all bytecode offsets with evaluation stack depth 0 on the current line List<int> relevantOffsets = new List<int>(); for (int i = 0; i < locations.Count; i++) { if (locations[i].GetLineNumber() != _location.GetLineNumber()) continue; long offsetLimit = i < locations.Count - 1 ? locations[i + 1].GetCodeIndex() : bytecode.Length; // start with the instruction for this bytecode offset for (int j = GetInstructionAtOffset(disassembledMethod, locations[i].GetCodeIndex()); j >= 0 && j < disassembledMethod.Instructions.Count && disassembledMethod.Instructions[j].Offset < offsetLimit; j++) { if (evaluationStackDepths[j] == 0) { // ignore unconditional branches if (disassembledMethod.Instructions[j].OpCode.FlowControl == JavaFlowControl.Branch) continue; relevantOffsets.Add(disassembledMethod.Instructions[j].Offset); } } } if (relevantOffsets.Count == potentialTrees.Count) { // heuristic: assume they appear in the same order as the source code on this line int treeIndex = relevantOffsets.IndexOf((int)_location.GetCodeIndex()); if (treeIndex >= 0) associatedTree = potentialTrees[treeIndex]; } } if (associatedTree == null) { tokens = null; return false; } return true; } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) throw; associatedTree = null; tokens = null; return false; } }