public Ust VisitCompilationUnit(JavaParser.CompilationUnitContext context) { var packageDeclaration = context.packageDeclaration(); EntityDeclaration[] typeDecs = context.typeDeclaration() .Select(typeDec => Visit(typeDec) as EntityDeclaration) .Where(typeDec => typeDec != null) .ToArray(); UsingDeclaration[] importDecs = context.importDeclaration() .Select(importDec => (UsingDeclaration)Visit(importDec)) .Where(importDec => importDec != null) .ToArray(); var roots = new List <Ust>(importDecs); if (packageDeclaration != null) { var name = (StringLiteral)Visit(packageDeclaration.qualifiedName()); var ns = new NamespaceDeclaration(name, typeDecs, context.GetTextSpan()); roots.Add(ns); } else { roots.AddRange(typeDecs); } root.Nodes = roots.ToArray(); return(root); }
public override void EnterCompilationUnit([NotNull] JavaParser.CompilationUnitContext context) { Logger.Debug("parsing comppilationUnit"); var currentCompilationUnit = new CompilationUnit(context.GetText()); _classListener.setCurrentCompilationUnit(currentCompilationUnit); _project.AddComilationUnit(currentCompilationUnit); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void main(String[] args) throws Exception public static void Main(string[] args) { Thread.Sleep(10000); ANTLRFileStream input = new ANTLRFileStream(args[0]); JavaLexer lexer = new JavaLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); JavaParser parser = new JavaParser(tokens); JavaParser.CompilationUnitContext tree = parser.compilationUnit(); // System.out.println(tree.toStringTree(parser)); Thread.Sleep(10000); }
private void ParseJava(string input) { AntlrInputStream stream = new AntlrInputStream(input); ITokenSource lexer = new JavaLexer(stream); ITokenStream tokens = new CommonTokenStream(lexer); JavaParser parser = new JavaParser(tokens); parser.BuildParseTree = true; JavaParser.CompilationUnitContext tree = parser.compilationUnit(); if (tree != null) { var builder = new TreeBuilder(parser, tree, treeModel); builder.Build(); } }
public override bool VisitCompilationUnit([NotNull] JavaParser.CompilationUnitContext context) { Methods = new List <string>(); ClassesInterfaces = new List <string>(); Enums = new List <string>(); Fields = new List <string>(); InField = false; base.VisitCompilationUnit(context); if (Methods.Count > 0) { MethodOutput.WriteLine("***"); MethodOutput.WriteLine(FileName); Methods.ForEach(line => MethodOutput.WriteLine(line)); } if (ClassesInterfaces.Count > 0) { ClassOutput.WriteLine("***"); ClassOutput.WriteLine(FileName); ClassesInterfaces.ForEach(line => ClassOutput.WriteLine(line)); } if (Enums.Count > 0) { EnumOutput.WriteLine("***"); EnumOutput.WriteLine(FileName); Enums.ForEach(line => EnumOutput.WriteLine(line)); } if (Fields.Count > 0) { FieldOutput.WriteLine("***"); FieldOutput.WriteLine(FileName); Fields.ForEach(line => FieldOutput.WriteLine(line)); } return(true); }
static void Main(string[] args) { List <string> options = new List <string>(); List <string> arguments = new List <string>(); string ast_output_file = null; CommandLine.Parser.Default.ParseArguments <Options>(args) .WithParsed <Options>(o => { arguments = o.JavaFiles.ToList(); ast_output_file = o.AstOutFile; }) .WithNotParsed(a => { System.Console.Error.WriteLine(a); }); Runtime.Redirect r = new Runtime.Redirect(ast_output_file); foreach (var file_name in arguments) { var code_as_string = File.ReadAllText(file_name); var input = new AntlrInputStream(code_as_string); var lexer = new JavaLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new JavaParser(tokens); var listener = new ErrorListener <IToken>(); parser.AddErrorListener(listener); JavaParser.CompilationUnitContext tree = parser.compilationUnit(); if (listener.had_error) { return; } var sb = new StringBuilder(); var ser = new Runtime.AstHelpers(); ser.ParenthesizedAST(sb, file_name, tree, tokens); System.Console.WriteLine(sb.ToString()); } r.Dispose(); }
public override void EnterCompilationUnit(JavaParser.CompilationUnitContext context) { PackageDeclarationListener packageDeclarationListener = new PackageDeclarationListener(); if (context.packageDeclaration() != null) { context.packageDeclaration().EnterRule(packageDeclarationListener); } string qualifiedName = packageDeclarationListener.QualifiedName; TypeDeclarationListener typeDeclarationListener = new TypeDeclarationListener(filePath, qualifiedName); foreach (JavaParser.TypeDeclarationContext typeDeclarationContext in context.typeDeclaration()) { typeDeclarationContext.EnterRule(typeDeclarationListener); // this type declaration can be a class, enum, or interface if (typeDeclarationListener.OuterClassInfo != null) { OuterClassInfos.Add(typeDeclarationListener.OuterClassInfo); typeDeclarationListener.OuterClassInfo = null; } if (typeDeclarationListener.InterfaceInfo != null) { OuterClassInfos.Add(typeDeclarationListener.InterfaceInfo); typeDeclarationListener.InterfaceInfo = null; } if (typeDeclarationListener.EnumInfo != null) { OuterClassInfos.Add(typeDeclarationListener.EnumInfo); typeDeclarationListener.EnumInfo = null; } } }
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); } }
public JavaAntlrParseTree(JavaParser.CompilationUnitContext syntaxTree) : base(syntaxTree) { }
public override void ExitCompilationUnit([NotNull] JavaParser.CompilationUnitContext context) { _classListener.resetCurrentComilationUnit(); }
public override void ExitCompilationUnit([NotNull] JavaParser.CompilationUnitContext context) { _compilationUnitListener.ExitCompilationUnit(context); }