List of usage examples for org.antlr.v4.runtime ANTLRFileStream ANTLRFileStream
public ANTLRFileStream(String fileName) throws IOException
From source file:SparqlMain.java
License:Apache License
/** * * @param args//from w w w. jav a2s . c o m */ public static void main(String args[]) throws Exception { System.out.println("Work on file " + args[0]); int lineWidth = 80; if (args.length >= 2) { lineWidth = Integer.parseInt(args[1]); } SparqlLexer lex = null; try { lex = new SparqlLexer(new ANTLRFileStream(args[0])); } catch (IOException ex) { Logger.getLogger(SparqlMain.class.getName()).log(Level.SEVERE, null, ex); } CommonTokenStream tokens = new CommonTokenStream(lex); System.out.println("Tokens: -------------------------------"); tokens.fill(); System.out.println("Number of tokens " + tokens.getTokens().size()); List tokenList = tokens.getTokens(); System.out.println("TokenList: -------------------------------"); Iterator it = tokenList.iterator(); while (it.hasNext()) { Token t = (Token) it.next(); System.out.println(t.toString()); } System.out.flush(); System.out.println("Input from token list: -------------------------------"); it = tokenList.iterator(); while (it.hasNext()) { Token t = (Token) it.next(); if (t.getType() != SparqlParser.EOF) { if (t.getType() == SparqlParser.WS || t.getType() == SparqlParser.COMMENT) { String s = t.getText(); s = s.replace("\r\n", "\n"); if (!System.lineSeparator().equals("\n")) { s = s.replace("\n", System.lineSeparator()); } System.out.print(s); } else { System.out.print(t.getText()); } } } System.out.flush(); SparqlParser parser = new SparqlParser(tokens); parser.setBuildParseTree(true); System.out.println("Start parsing: -------------------------------"); System.out.flush(); ParserRuleContext t = parser.query(); System.out.flush(); System.out.println("Parse tree: -------------------------------"); System.out.println(t.toStringTree(parser)); // visualize parse tree in dialog box t.inspect(parser); if (parser.getNumberOfSyntaxErrors() <= 0) { //ParseTreeWalker walker = new ParseTreeWalker(); String groupFile = "ident.stg"; if (args.length > 1) { groupFile = args[1]; } System.out.println("Read StringTemplate Group File: " + groupFile + "-------------------------------"); STGroup g = new STGroupFile(groupFile); IdentVisitor visitor = new IdentVisitor(); visitor.setSTGroup(g); ST query = visitor.visit(t); System.out.println("Emit reformatted query: -------------------------------"); System.out.println(query.render(lineWidth)); System.out.println("Emit original query: -------------------------------"); String q = query.render(lineWidth); /* get common token stream */ File tmpFile = File.createTempFile("query_", ".rq"); FileOutputStream fo = new FileOutputStream(tmpFile); OutputStreamWriter ow = new OutputStreamWriter(fo, "UTF8"); ow.write(q); ow.close(); /* transformation pipline * step 1: Unicode pre-processing * step 2: Lexical analysis */ lex = new SparqlLexer(new ANTLRFileStream(tmpFile.getCanonicalPath(), "UTF8")); tokens = new CommonTokenStream(lex); List formattedTokenList = tokens.getTokens(); it = tokenList.iterator(); Iterator fit = formattedTokenList.iterator(); boolean lineSeparatorHasToBeModified = !System.lineSeparator().equals("\n"); while (it.hasNext()) { Token originalToken = (Token) it.next(); if (originalToken.getType() != SparqlParser.EOF) { if (originalToken.getType() == SparqlParser.WS || originalToken.getType() == SparqlParser.COMMENT) { String s = originalToken.getText(); s = s.replace("\r\n", "\n"); if (lineSeparatorHasToBeModified) { s = s.replace("\n", System.lineSeparator()); } System.out.print(s); } else { System.out.print(originalToken.getText()); } } } System.out.flush(); } System.out.println("-------------------------------"); System.out.println("Number of errors encountered: " + parser.getNumberOfSyntaxErrors()); }
From source file:Utah01.java
/** * @param args the command line arguments *///from w w w . j a va2 s. com public static void main(String[] args) { // TODO code application logic here try { // make Lexer ANTLRFileStream inputStream = new ANTLRFileStream(args[0]); utah01Lexer lexer = new utah01Lexer(inputStream); // make Parser CommonTokenStream tokens = new CommonTokenStream(lexer); utah01Parser parser = new utah01Parser(tokens); // make Walker ParseTreeWalker parseTreeWalker = new ParseTreeWalker(); utah01ListenerForJSON listener = new utah01ListenerForJSON(); ParserRuleContext parserRuleContext = parser.start(); // do walk parseTreeWalker.walk(listener, parserRuleContext); } catch (Exception e) { System.out.println(e); } //System.out.print("number of elements = "); //System.out.println(SymbolTable.count); }
From source file:basicint.Run.java
/** * @param args the command line arguments *//*from www. j a v a 2s .c o m*/ public static void main(String[] args) throws Exception { String fn = ""; if (args.length > 0) { fn = args[0]; } else { System.out.println("Usage: basic filename.basic"); System.exit(0); } ANTLRInputStream input = new ANTLRFileStream(fn); BasicLexer lexer = new BasicLexer(input); TokenStream tokens = new BufferedTokenStream(lexer); BasicParser parser = new BasicParser(tokens); parser.program(); }
From source file:basicintast.Run.java
/** * @param args the command line arguments */// w ww. j av a2 s . co m public static void main(String[] args) throws IOException { ANTLRInputStream input = new ANTLRFileStream("input.basic"); BasicLexer lexer = new BasicLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); BasicParser parser = new BasicParser(tokens); ParseTree tree = parser.program(); BasicVisitor eval = new BasicVisitorImpl(); eval.visit(tree); }
From source file:boa.compiler.BoaCompiler.java
License:Apache License
public static void main(final String[] args) throws IOException { CommandLine cl = processCommandLineOptions(args); if (cl == null) return;//from w w w . j a v a 2s . c o m final ArrayList<File> inputFiles = BoaCompiler.inputFiles; // get the name of the generated class final String className = getGeneratedClass(cl); // get the filename of the jar we will be writing final String jarName; if (cl.hasOption('o')) jarName = cl.getOptionValue('o'); else jarName = className + ".jar"; // make the output directory final File outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString()); final File outputSrcDir = new File(outputRoot, "boa"); if (!outputSrcDir.mkdirs()) throw new IOException("unable to mkdir " + outputSrcDir); // find custom libs to load final List<URL> libs = new ArrayList<URL>(); if (cl.hasOption('l')) for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL()); final File outputFile = new File(outputSrcDir, className + ".java"); final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile)); try { final List<String> jobnames = new ArrayList<String>(); final List<String> jobs = new ArrayList<String>(); boolean isSimple = true; final List<Program> visitorPrograms = new ArrayList<Program>(); SymbolTable.initialize(libs); for (int i = 0; i < inputFiles.size(); i++) { final File f = inputFiles.get(i); try { final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath())); lexer.removeErrorListeners(); lexer.addErrorListener(new LexerErrorListener()); final CommonTokenStream tokens = new CommonTokenStream(lexer); final BoaParser parser = new BoaParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException { throw new ParseCancellationException(e); } }); final BoaErrorListener parserErrorListener = new ParserErrorListener(); final Start p = parse(tokens, parser, parserErrorListener); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final String jobName = "" + i; try { if (!parserErrorListener.hasError) { new TypeCheckingVisitor().start(p, new SymbolTable()); final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor(); simpleVisitor.start(p); LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex")); isSimple &= !simpleVisitor.isComplex(); new InheritedAttributeTransformer().start(p); new LocalAggregationTransformer().start(p); // if a job has no visitor, let it have its own method // also let jobs have own methods if visitor merging is disabled if (!simpleVisitor.isComplex() || cl.hasOption("nv") || inputFiles.size() == 1) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(jobName); } // if a job has visitors, fuse them all together into a single program else { p.getProgram().jobName = jobName; visitorPrograms.add(p.getProgram()); } } } catch (final TypeCheckException e) { parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e); } } catch (final Exception e) { System.err.print(f.getName() + ": compilation failed: "); e.printStackTrace(); } } final int maxVisitors; if (cl.hasOption('v')) maxVisitors = Integer.parseInt(cl.getOptionValue('v')); else maxVisitors = Integer.MAX_VALUE; if (!visitorPrograms.isEmpty()) try { for (final Program p : new VisitorMergingTransformer().mergePrograms(visitorPrograms, maxVisitors)) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } catch (final Exception e) { System.err.println("error fusing visitors - falling back: " + e); e.printStackTrace(); for (final Program p : visitorPrograms) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } if (jobs.size() == 0) throw new RuntimeException("no files compiled without error"); final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program"); st.add("name", className); st.add("numreducers", inputFiles.size()); st.add("jobs", jobs); st.add("jobnames", jobnames); st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings); st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings); st.add("splitsize", isSimple ? 64 * 1024 * 1024 : 10 * 1024 * 1024); o.write(st.render().getBytes()); } finally { o.close(); } compileGeneratedSrc(cl, jarName, outputRoot, outputFile); }
From source file:boa.compiler.BoaCompiler.java
License:Apache License
public static void parseOnly(final String[] args) throws IOException { CommandLine cl = processParseCommandLineOptions(args); if (cl == null) return;/*from ww w .j av a 2s. c o m*/ final ArrayList<File> inputFiles = BoaCompiler.inputFiles; // find custom libs to load final List<URL> libs = new ArrayList<URL>(); if (cl.hasOption('l')) for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL()); final List<String> jobnames = new ArrayList<String>(); final List<String> jobs = new ArrayList<String>(); boolean isSimple = true; final List<Program> visitorPrograms = new ArrayList<Program>(); SymbolTable.initialize(libs); for (int i = 0; i < inputFiles.size(); i++) { final File f = inputFiles.get(i); try { final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath())); lexer.removeErrorListeners(); lexer.addErrorListener(new LexerErrorListener()); final CommonTokenStream tokens = new CommonTokenStream(lexer); final BoaParser parser = new BoaParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException { throw new ParseCancellationException(e); } }); final BoaErrorListener parserErrorListener = new ParserErrorListener(); final Start p = parse(tokens, parser, parserErrorListener); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final String jobName = "" + i; try { if (!parserErrorListener.hasError) { new TypeCheckingVisitor().start(p, new SymbolTable()); final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor(); simpleVisitor.start(p); LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex")); isSimple &= !simpleVisitor.isComplex(); new InheritedAttributeTransformer().start(p); new LocalAggregationTransformer().start(p); // if a job has no visitor, let it have its own method // also let jobs have own methods if visitor merging is disabled if (!simpleVisitor.isComplex() || cl.hasOption("nv") || inputFiles.size() == 1) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(jobName); } // if a job has visitors, fuse them all together into a single program else { p.getProgram().jobName = jobName; visitorPrograms.add(p.getProgram()); } } } catch (final TypeCheckException e) { parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e); } } catch (final Exception e) { System.err.print(f.getName() + ": parsing failed: "); e.printStackTrace(); } } }
From source file:carbon.compiler.CarbonCompiler.java
License:BSD License
public List<ParseTree> getTrees() { List<ParseTree> roots = new ArrayList<ParseTree>(); for (String fileName : targetFiles) { try {/*from w w w . ja v a2 s. c om*/ File file = new File(fileName); if (!file.isAbsolute()) { file = new File(System.getProperty("user.dir"), fileName); } ANTLRFileStream input = new ANTLRFileStream(file.getAbsolutePath()); CarbonLexer lexer = new CarbonLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CarbonParser parser = new CarbonParser(tokens); roots.add(parser.file()); } catch (IOException ioe) { //errorManager.toolError(ErrorKind.CANNOT_OPEN_FILE, ""); throw new RuntimeException(ioe); } } return roots; }
From source file:cloudlens.parser.ASTBuilder.java
License:Apache License
public static List<ASTElement> parseFile(String file) throws IOException, ASTException { fileName = file;/* w w w . j a v a2 s. co m*/ final ANTLRFileStream script = new ANTLRFileStream(file); return parse(script); }
From source file:com.eprosima.fastrtps.fastrtpsgen.java
License:Apache License
private Project parseIDL(String idlFilename) { boolean returnedValue = false; String idlParseFileName = idlFilename; Project project = null;//w w w . j av a 2 s . c o m String onlyFileName = Util.getIDLFileNameOnly(idlFilename); if (!m_ppDisable) { idlParseFileName = callPreprocessor(idlFilename); } if (idlParseFileName != null) { Context ctx = new Context(onlyFileName, idlFilename, m_includePaths, m_subscribercode, m_publishercode, m_localAppProduct); if (fusion_) ctx.setActivateFusion(true); // Create default @Key annotation. AnnotationDeclaration keyann = ctx.createAnnotationDeclaration("Key", null); keyann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "true")); // Create default @Topic annotation. AnnotationDeclaration topicann = ctx.createAnnotationDeclaration("Topic", null); topicann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "true")); // Create template manager TemplateManager tmanager = new TemplateManager("FastCdrCommon:eprosima:Common"); List<TemplateExtension> extensions = new ArrayList<TemplateExtension>(); // Load common types template extensions.add(new TemplateExtension("struct_type", "keyFunctionHeadersStruct")); extensions.add(new TemplateExtension("union_type", "keyFunctionHeadersUnion")); tmanager.addGroup("TypesHeader", extensions); extensions.clear(); extensions.add(new TemplateExtension("struct_type", "keyFunctionSourcesStruct")); tmanager.addGroup("TypesSource", extensions); // TODO: Uncomment following lines and create templates // Load Types common templates tmanager.addGroup("RTPSPubSubTypeHeader"); tmanager.addGroup("RTPSPubSubTypeSource"); // Load Publisher templates tmanager.addGroup("RTPSPublisherHeader"); tmanager.addGroup("RTPSPublisherSource"); // Load Subscriber templates tmanager.addGroup("RTPSSubscriberHeader"); tmanager.addGroup("RTPSSubscriberSource"); // Load PubSubMain template tmanager.addGroup("RTPSPubSubMain"); // Add JNI sources. if (m_languageOption == LANGUAGE.JAVA) { tmanager.addGroup("JNIHeader"); tmanager.addGroup("JNISource"); tmanager.addGroup("JavaSource"); // Set package in context. ctx.setPackage(m_package); } // Create main template TemplateGroup maintemplates = tmanager.createTemplateGroup("main"); maintemplates.setAttribute("ctx", ctx); try { ANTLRFileStream input = new ANTLRFileStream(idlParseFileName); IDLLexer lexer = new IDLLexer(input); lexer.setContext(ctx); CommonTokenStream tokens = new CommonTokenStream(lexer); IDLParser parser = new IDLParser(tokens); // Pass the finelame without the extension Specification specification = parser.specification(ctx, tmanager, maintemplates).spec; returnedValue = specification != null; } catch (FileNotFoundException ex) { System.out.println(ColorMessage.error("FileNotFounException") + "The File " + idlParseFileName + " was not found."); } /* catch (ParseException ex) { System.out.println(ColorMessage.error("ParseException") + ex.getMessage()); }*/ catch (Exception ex) { System.out.println(ColorMessage.error("Exception") + ex.getMessage()); } if (returnedValue) { // Create information of project for solution project = new Project(onlyFileName, idlFilename, ctx.getDependencies()); System.out.println("Generating Type definition files..."); if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + ".h", maintemplates.getTemplate("TypesHeader"), m_replace)) { if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + ".cxx", maintemplates.getTemplate("TypesSource"), m_replace)) { project.addCommonIncludeFile(onlyFileName + ".h"); project.addCommonSrcFile(onlyFileName + ".cxx"); } } // TODO: Uncomment following lines and create templates if (ctx.existsLastStructure()) { m_atLeastOneStructure = true; project.setHasStruct(true); System.out.println("Generating TopicDataTypes files..."); if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + "PubSubTypes.h", maintemplates.getTemplate("RTPSPubSubTypeHeader"), m_replace)) { if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + "PubSubTypes.cxx", maintemplates.getTemplate("RTPSPubSubTypeSource"), m_replace)) { project.addProjectIncludeFile(onlyFileName + "PubSubTypes.h"); project.addProjectSrcFile(onlyFileName + "PubSubTypes.cxx"); } } if (m_exampleOption != null) { System.out.println("Generating Publisher files..."); if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + "Publisher.h", maintemplates.getTemplate("RTPSPublisherHeader"), m_replace)) { if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + "Publisher.cxx", maintemplates.getTemplate("RTPSPublisherSource"), m_replace)) { project.addProjectIncludeFile(onlyFileName + "Publisher.h"); project.addProjectSrcFile(onlyFileName + "Publisher.cxx"); } } System.out.println("Generating Subscriber files..."); if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + "Subscriber.h", maintemplates.getTemplate("RTPSSubscriberHeader"), m_replace)) { if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + "Subscriber.cxx", maintemplates.getTemplate("RTPSSubscriberSource"), m_replace)) { project.addProjectIncludeFile(onlyFileName + "Subscriber.h"); project.addProjectSrcFile(onlyFileName + "Subscriber.cxx"); } } System.out.println("Generating main file..."); if (returnedValue = Utils.writeFile(m_outputDir + onlyFileName + "PubSubMain.cxx", maintemplates.getTemplate("RTPSPubSubMain"), m_replace)) { project.addProjectSrcFile(onlyFileName + "PubSubMain.cxx"); } } } } // Java support (Java classes and JNI code) if (returnedValue && m_languageOption == LANGUAGE.JAVA) { String outputDir = m_outputDir; // Make directories from package. if (!m_package.isEmpty()) { outputDir = m_outputDir + File.separator + m_package.replace('.', File.separatorChar); File dirs = new File(outputDir); if (!dirs.exists()) { if (!dirs.mkdirs()) { System.out .println(ColorMessage.error() + "Cannot create directories for Java packages."); return null; } } } // Java classes. TypesGenerator typeGen = new TypesGenerator(tmanager, m_outputDir, m_replace); TypeCode.javapackage = m_package + (m_package.isEmpty() ? "" : "."); if (!typeGen.generate(ctx, outputDir + File.separator, m_package, onlyFileName, null)) { System.out.println(ColorMessage.error() + "generating Java types"); return null; } if (ctx.existsLastStructure()) { System.out.println("Generando fichero " + m_outputDir + onlyFileName + "PubSub.java"); if (!Utils.writeFile(outputDir + File.separator + onlyFileName + "PubSub.java", maintemplates.getTemplate("JavaSource"), m_replace)) return null; // Call javah application for each structure. if (!callJavah(idlFilename)) return null; } if (Utils.writeFile(m_outputDir + onlyFileName + "PubSubJNII.h", maintemplates.getTemplate("JNIHeader"), m_replace)) project.addJniIncludeFile(onlyFileName + "PubSubJNII.h"); else return null; StringTemplate jnisourceTemplate = maintemplates.getTemplate("JNISource"); if (Utils.writeFile(m_outputDir + onlyFileName + "PubSubJNI.cxx", jnisourceTemplate, m_replace)) project.addJniSrcFile(onlyFileName + "PubSubJNI.cxx"); else return null; } } return returnedValue ? project : null; }
From source file:com.javasimple.parser.JavaClassTransformer.java
License:Open Source License
public static void parse(final JavaApplicationTransformer app, final String fileName) throws IOException { final ANTLRFileStream fileStream = new ANTLRFileStream(fileName); final JavaLexer lexer = new JavaLexer(fileStream); final CommonTokenStream tokenStream = new CommonTokenStream(lexer); final JavaParser parser = new JavaParser(tokenStream); final JavaBaseListener listener; final LinkedList<TypeReference> unresolvedTypeReferences = new LinkedList<TypeReference>(); listener = new JavaBaseListener() { // Handle JavaClass Declaration private String packageName; private List<String> imports; private Stack<Object> processStack; private int declaredElements = 0; private boolean processingBlock() { return !processStack.isEmpty() && processStack.peek() instanceof Boolean; }/* w w w.jav a 2 s .co m*/ @Override public void enterBlock(JavaParser.BlockContext ctx) { processStack.push(true); } @Override public void exitBlock(JavaParser.BlockContext ctx) { if (processStack.size() >= 2) { Object element = processStack.get(processStack.size() - 2); if (element instanceof StringBuilder) { // Process method/constructor source code Utils.convertirTexto(ctx.children, (StringBuilder) element); } } processStack.pop(); } @Override public void enterClassCreatorRest(JavaParser.ClassCreatorRestContext ctx) { processStack.push(true); } @Override public void exitClassCreatorRest(JavaParser.ClassCreatorRestContext ctx) { processStack.pop(); } // // @Override // public void enterMethodBody(JavaParser.MethodBodyContext ctx) { // classStack.push(true); // } // // @Override // public void exitMethodBody(JavaParser.MethodBodyContext ctx) { // classStack.pop(); // } // @Override public void enterConstructorBody(JavaParser.ConstructorBodyContext ctx) { if (processingBlock()) { return; } processStack.add(new StringBuilder()); } @Override public void enterMethodBody(JavaParser.MethodBodyContext ctx) { if (processingBlock()) { return; } processStack.push(new StringBuilder()); } @Override public void enterCompilationUnit(JavaParser.CompilationUnitContext ctx) { imports = new ArrayList<String>(); processStack = new Stack(); } @Override public void exitPackageDeclaration(JavaParser.PackageDeclarationContext ctx) { packageName = ctx.qualifiedName().getText(); } @Override public void exitImportDeclaration(JavaParser.ImportDeclarationContext ctx) { final String importText = ctx.getText(); // from 6 to discard 'import' // Until length-1 to discard ';' imports.add(importText.substring(6, importText.length() - 1)); } @Override public void enterInterfaceDeclaration(JavaParser.InterfaceDeclarationContext ctx) { if (processingBlock()) { return; } processStack.push(null); declaredElements++; } @Override public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) { if (processingBlock()) { return; } processStack.push(null); declaredElements++; } @Override public void enterAnnotationTypeDeclaration(JavaParser.AnnotationTypeDeclarationContext ctx) { if (processingBlock()) { return; } processStack.push(null); } @Override public void enterEnumDeclaration(JavaParser.EnumDeclarationContext ctx) { if (processingBlock()) { return; } processStack.push(null); declaredElements++; } @Override public void exitInterfaceDeclaration(JavaParser.InterfaceDeclarationContext ctx) { if (processingBlock()) { return; } try { JavaInterface javaInterface; if (declaredElements == 1) { javaInterface = (JavaInterface) app.createJavaElement(SymbolType.Interface, packageName, ctx.Identifier().getText()); } else { javaInterface = (JavaInterface) JavasimpleFactory.eINSTANCE.createJavaInterface(); javaInterface.setName(ctx.Identifier().getText()); declaredElements--; } if (ctx.typeList() != null) { for (JavaParser.TypeContext typeContext : ctx.typeList().type()) { registerUnresolvedReference(javaInterface, SymbolType.Interface, JavasimplePackage.eINSTANCE.getJavaInterface_InheritedInterfaces(), typeContext); } } processClassOrInteface(javaInterface); } catch (Exception e) { e.printStackTrace(); } } @Override public void exitEnumDeclaration(JavaParser.EnumDeclarationContext ctx) { if (processingBlock()) { return; } try { JavaEnumeration javaEnumeration; if (declaredElements == 1) { javaEnumeration = (JavaEnumeration) app.createJavaElement(SymbolType.Enumeration, packageName, ctx.Identifier().getText()); declaredElements--; } else { javaEnumeration = (JavaEnumeration) JavasimpleFactory.eINSTANCE.createJavaEnumeration(); javaEnumeration.setName(ctx.Identifier().getText()); declaredElements--; } processClassOrInteface(javaEnumeration); } catch (Exception e) { e.printStackTrace(); } } @Override public void exitClassDeclaration(JavaParser.ClassDeclarationContext ctx) { if (processingBlock()) { return; } try { JavaClass javaClass; if (declaredElements == 1) { javaClass = (JavaClass) app.createJavaElement(SymbolType.Class, packageName, ctx.Identifier().getText()); } else { // System.out.println("Inner Class " + fileName + "--->" // + ctx.Identifier().getText()); javaClass = JavasimpleFactory.eINSTANCE.createJavaClass(); javaClass.setName(ctx.Identifier().getText()); javaClass.setPackageName(fileName); } declaredElements--; registerUnresolvedReference(javaClass, SymbolType.Class, JavasimplePackage.eINSTANCE.getJavaClass_SuperClass(), ctx.type()); if (ctx.typeList() != null) { for (JavaParser.TypeContext typeContext : ctx.typeList().type()) { registerUnresolvedReference(javaClass, SymbolType.Interface, JavasimplePackage.eINSTANCE.getJavaClass_Implements(), typeContext); } } processClassOrInteface(javaClass); } catch (Exception e) { e.printStackTrace(); } } @Override public void exitAnnotationTypeDeclaration(JavaParser.AnnotationTypeDeclarationContext ctx) { if (processingBlock()) { return; } try { AnnotationType aType = (AnnotationType) app.createJavaElement(SymbolType.AnnotationType, packageName, ctx.Identifier().getText()); processClassOrInteface(aType); } catch (Throwable e) { e.printStackTrace(); } } private void processClassOrInteface(ClassOrInterface element) { try { Object top; while (!processStack.isEmpty() && (top = processStack.pop()) != null) { if (top instanceof ClassConstructor) { if (element instanceof JavaClass) { ((JavaClass) element).getConstructors().add(0, (ClassConstructor) top); } else if (top instanceof JavaClass) { ((JavaEnumeration) element).getConstructors().add(0, (ClassConstructor) top); } } else if (top instanceof JavaInterface) { element.getInnerInterfaces().add(0, (JavaInterface) top); } else if (top instanceof JavaEnumeration) { element.getInnerEnumerations().add(0, (JavaEnumeration) top); } else if (top instanceof AnnotationType) { element.getInnerAnotationTypes().add(0, (AnnotationType) top); } else if (top instanceof JavaClass) { element.getInnerClasses().add(0, (JavaClass) top); } else if (top instanceof JavaField) { element.getFields().add(0, (JavaField) top); } else if (top instanceof JavaMethod) { element.getMethods().add(0, (JavaMethod) top); } else if (top instanceof EnumConstant) { ((JavaEnumeration) element).getConstans().add(0, (EnumConstant) top); } else if (top instanceof AnnotationMethod) { ((AnnotationType) element).getAnnotationsMethods().add(0, (AnnotationMethod) top); } else if (top instanceof Documentation) { ((Documentable) element).setDocumentation((Documentation) top); } else { throw new RuntimeException("UnProcess element in the stack :" + top); } } processModifiers(element); if (processStack.isEmpty()) { // System.out.println("End to ptocess: " + fileName); for (String importName : imports) { final JavaImport importV = JavasimpleFactory.eINSTANCE.createJavaImport(); importV.setName(importName); element.getImports().add(0, importV); } } processStack.push(element); // If is the main element register all subclases if (declaredElements == 0) { registerInnerElements(element); } } catch (Exception e) { System.out.println("----->" + fileName + "<------"); System.out.println(element.getName()); System.out.println(processStack); e.printStackTrace(); } } private void registerInnerElements(ClassOrInterface element) { registerElements(element.getInnerClasses()); registerElements(element.getInnerAnotationTypes()); registerElements(element.getInnerEnumerations()); registerElements(element.getInnerInterfaces()); } private void registerElements(List elements) { for (Object elementi : elements) { app.registerInnerElement(((ClassOrInterface) elementi).getQualifiedName(), elementi); registerInnerElements((ClassOrInterface) elementi); } } @Override public void exitEnumConstant(JavaParser.EnumConstantContext ctx) { if (processingBlock()) { return; } final EnumConstant enumConstant = JavasimpleFactory.eINSTANCE.createEnumConstant(); enumConstant.setName(ctx.Identifier().getText()); processModifiers(enumConstant); processStack.push(enumConstant); } @Override public void exitConstDeclaration(JavaParser.ConstDeclarationContext ctx) { if (processingBlock()) { return; } try { final ArrayList<String> fieldsNames = new ArrayList<String>(ctx.constantDeclarator().size()); final ArrayList<VariableInitializerContext> defaultValues = new ArrayList<VariableInitializerContext>( ctx.constantDeclarator().size()); final ArrayList<Boolean> arrayFlags = new ArrayList<Boolean>(ctx.constantDeclarator().size()); for (JavaParser.ConstantDeclaratorContext constantDeclaratorContext : ctx .constantDeclarator()) { fieldsNames.add(constantDeclaratorContext.Identifier().getText()); if (constantDeclaratorContext.variableInitializer() != null) { defaultValues.add(constantDeclaratorContext.variableInitializer()); } else { defaultValues.add(null); } if (constantDeclaratorContext.getText().split("=")[0].endsWith("[]")) { arrayFlags.add(true); } else { arrayFlags.add(false); } } processFieldDeclaration(ctx.type(), fieldsNames, defaultValues, arrayFlags); } catch (Exception e) { e.printStackTrace(); } } @Override public void exitAnnotationTypeElementRest(JavaParser.AnnotationTypeElementRestContext ctx) { if (processingBlock()) { return; } if (ctx.type() != null) { if (ctx.annotationMethodOrConstantRest() != null) { if (ctx.annotationMethodOrConstantRest().annotationMethodRest() != null) { final JavaParser.AnnotationMethodRestContext restContext = ctx .annotationMethodOrConstantRest().annotationMethodRest(); final AnnotationMethod method = JavasimpleFactory.eINSTANCE.createAnnotationMethod(); method.setName(restContext.Identifier().getText()); if (processStack.peek() instanceof AnnotationParam) { method.setDefaultValue((AnnotationParam) processStack.pop()); } // TODO: Check registerUnresolvedReference(method, SymbolType.Class, JavasimplePackage.eINSTANCE.getAnnotationMethod_Type(), ctx.type()); // method.setType(); method.setArray(isArray(ctx.type())); processModifiers(method); processStack.push(method); } else if (ctx.annotationMethodOrConstantRest().annotationConstantRest() != null) { JavaParser.AnnotationConstantRestContext restContext = ctx .annotationMethodOrConstantRest().annotationConstantRest(); processVariableDeclarators(ctx.type(), restContext.variableDeclarators()); } } } } @Override public void exitFieldDeclaration(JavaParser.FieldDeclarationContext ctx) { if (processingBlock()) { return; } processVariableDeclarators(ctx.type(), ctx.variableDeclarators()); } private void processVariableDeclarators(JavaParser.TypeContext typeContext, JavaParser.VariableDeclaratorsContext ctx) { try { final ArrayList<String> fieldsNames = new ArrayList<String>(ctx.variableDeclarator().size()); final ArrayList<VariableInitializerContext> defaultValues = new ArrayList<VariableInitializerContext>( ctx.variableDeclarator().size()); final ArrayList<Boolean> arrayFlags = new ArrayList<Boolean>(ctx.variableDeclarator().size()); for (JavaParser.VariableDeclaratorContext variableDeclaratorContext : ctx .variableDeclarator()) { fieldsNames.add(variableDeclaratorContext.variableDeclaratorId().Identifier().getText()); if (variableDeclaratorContext.variableInitializer() != null) { defaultValues.add(variableDeclaratorContext.variableInitializer()); } else { defaultValues.add(null); } if (variableDeclaratorContext.variableDeclaratorId().getText().endsWith("[]")) { arrayFlags.add(true); } else { arrayFlags.add(false); } } processFieldDeclaration(typeContext, fieldsNames, defaultValues, arrayFlags); } catch (Exception e) { e.printStackTrace(); } } private void processFieldDeclaration(final JavaParser.TypeContext typeContext, final List<String> fieldsNames, final List<VariableInitializerContext> defaultValues, final List<Boolean> arrayFlags) { try { // final com.javasimple.Type type = getType(typeContext); for (int i = 0; i < fieldsNames.size(); i++) { final String fieldName = fieldsNames.get(i); final VariableInitializerContext defaultValue = defaultValues.get(i); final boolean isArray = arrayFlags.get(i); final JavaField field = JavasimpleFactory.eINSTANCE.createJavaField(); registerUnresolvedReference(field, SymbolType.Class, JavasimplePackage.eINSTANCE.getJavaField_Type(), typeContext); // field.setType(type); field.setName(fieldName); if (defaultValue != null) { final String expression = app.handler.processFieldDefaultValueExpression(defaultValue); field.setDefaultValue(expression); } if (isArray(typeContext) || isArray) { field.setArray(true); } else { field.setArray(false); } // TODO: FIX IT It should apply to all the fields processModifiers(field); processStack.push(field); } } catch (Exception e) { e.printStackTrace(); } } @Override public void exitEveryRule(ParserRuleContext ctx) { if (ctx.getClass().equals(TypeDeclarationContext.class) || ctx.getClass().equals(ClassBodyDeclarationContext.class)) { int tokPos = ctx.getStart().getTokenIndex(); final List<Token> hiddenTokens = tokenStream.getHiddenTokensToLeft(tokPos); String doc = null; if (hiddenTokens != null) { for (int i = hiddenTokens.size() - 1; i > -1 && doc == null; i--) { final Token refTok = hiddenTokens.get(i); if (refTok != null) { doc = refTok.getText(); if (doc.startsWith("/**") == false) { doc = null; } } } } if (doc != null) { Documentation documentation = JavadocParser.parse(doc); Object obj = processStack.peek(); if (obj instanceof Documentable) { ((Documentable) obj).setDocumentation(documentation); } } } } @Override public void enterConstructorDeclaration(JavaParser.ConstructorDeclarationContext ctx) { super.enterConstructorDeclaration(ctx); // To change body of // generated methods, // choose Tools | // Templates. } @Override public void exitConstructorDeclaration(JavaParser.ConstructorDeclarationContext ctx) { if (processingBlock()) { return; } try { ClassConstructor classConstructor = JavasimpleFactory.eINSTANCE.createClassConstructor(); classConstructor.setName(ctx.Identifier().getText()); Object top; // Parameters while (processStack.isEmpty() == false && (top = processStack.pop()) != null) { if (top instanceof Parameter) { classConstructor.getParameters().add(0, (Parameter) top); } else if (top instanceof StringBuilder) { classConstructor.setSource(((StringBuilder) top).toString()); } } processModifiers(classConstructor); processStack.push(classConstructor); } catch (Exception e) { e.printStackTrace(); } } @Override public void exitInterfaceMethodDeclaration(JavaParser.InterfaceMethodDeclarationContext ctx) { if (processingBlock()) { return; } processMethod(ctx.Identifier().getText(), ctx.type(), null); } @Override public void exitMethodDeclaration(JavaParser.MethodDeclarationContext ctx) { if (processingBlock()) { return; } if (ctx.qualifiedNameList() != null) { processMethod(ctx.Identifier().getText(), ctx.type(), ctx.qualifiedNameList().qualifiedName()); } else { processMethod(ctx.Identifier().getText(), ctx.type(), null); } } private void processMethod(String identifier, JavaParser.TypeContext type, List<JavaParser.QualifiedNameContext> exceptions) { try { JavaMethod method = JavasimpleFactory.eINSTANCE.createJavaMethod(); method.setName(identifier); registerUnresolvedReference(method, SymbolType.Class, JavasimplePackage.eINSTANCE.getJavaMethod_ReturnType(), type); if (type != null) { method.setReturnArray(type.getText().endsWith("[]")); } // method.setReturnType(getType(type)); Object element = processStack.peek(); if (element instanceof StringBuilder) { method.setSource(((StringBuilder) element).toString()); processStack.pop(); } Object top; // Read parameters until find null while (processStack.isEmpty() == false && (top = processStack.pop()) != null) { if (top instanceof Parameter) { method.getParameters().add(0, (Parameter) top); } } processModifiers(method); if (exceptions != null) { for (JavaParser.QualifiedNameContext exception : exceptions) { registerUnresolvedReference(method, SymbolType.Class, JavasimplePackage.eINSTANCE.getJavaMethod_Exceptions(), exception.getText()); } } processStack.push(method); } catch (Exception e) { e.printStackTrace(); } } @Override public void enterFormalParameters(JavaParser.FormalParametersContext ctx) { if (processingBlock()) { return; } processStack.push(null); } @Override public void exitFormalParameter(JavaParser.FormalParameterContext ctx) { if (processingBlock()) { return; } Parameter param = JavasimpleFactory.eINSTANCE.createParameter(); param.setName(ctx.variableDeclaratorId().Identifier().getText()); registerUnresolvedReference(param, SymbolType.Class, JavasimplePackage.eINSTANCE.getParameter_Type(), ctx.type()); // param.setType(getType(ctx.type())); param.setArray(isArray(ctx.type())); processModifiers(param); processStack.push(param); } @Override public void exitLastFormalParameter(JavaParser.LastFormalParameterContext ctx) { if (processingBlock()) { return; } Parameter param = JavasimpleFactory.eINSTANCE.createParameter(); param.setName(ctx.variableDeclaratorId().Identifier().getText()); registerUnresolvedReference(param, SymbolType.Class, JavasimplePackage.eINSTANCE.getParameter_Type(), ctx.type()); // param.setType(getType(ctx.type())); param.setArray(isArray(ctx.type())); param.setVarArray(true); processModifiers(param); processStack.push(param); } // @Override // public void // enterElementValuePair(JavaParser.ElementValuePairContext ctx) { // classStack.push(null); // } @Override public void enterElementValueArrayInitializer(JavaParser.ElementValueArrayInitializerContext ctx) { if (processingBlock()) { return; } processStack.push(null); } @Override public void exitElementValue(JavaParser.ElementValueContext ctx) { if (processingBlock()) {/* ctx.getText() */ return; } try { AnnotationParam param = null; if (ctx.expression() != null) { AnnotationParamValueExpression expression = JavasimpleFactory.eINSTANCE .createAnnotationParamValueExpression(); final String valueExpression = app.handler .processAnnotationValueExpresion(ctx.expression()); expression.setValue(valueExpression); param = expression; } else if (ctx.annotation() != null) { AnnotationParamValueAnnotation expression = JavasimpleFactory.eINSTANCE .createAnnotationParamValueAnnotation(); expression.setAnnotation((Annotation) processStack.pop()); param = expression; } else if (ctx.elementValueArrayInitializer() != null) { AnnotationParamValueArray expression = JavasimpleFactory.eINSTANCE .createAnnotationParamValueArray(); Object top; while ((top = processStack.pop()) != null) { if (top instanceof Annotation) { AnnotationParamValueAnnotation arrayElement = JavasimpleFactory.eINSTANCE .createAnnotationParamValueAnnotation(); arrayElement.setAnnotation((Annotation) top); expression.getValues().add(0, arrayElement); } else { try { expression.getValues().add(0, (AnnotationParam) top); } catch (Exception ex) { ex.printStackTrace(); } } } param = expression; } processStack.push(param); } catch (Exception e) { e.printStackTrace(); } } @Override public void exitElementValuePair(JavaParser.ElementValuePairContext ctx) { if (processingBlock()) { return; } final AnnotationParam param = (AnnotationParam) processStack.peek(); if (ctx.Identifier() != null) { if (param == null) { throw new RuntimeException(); } param.setKey(ctx.Identifier().getText()); } } @Override public void enterAnnotation(JavaParser.AnnotationContext ctx) { if (processingBlock()) { return; } processStack.push(null); } @Override public void exitAnnotation(JavaParser.AnnotationContext ctx) { if (processingBlock()) { return; } try { // final String annotationPackageName = getPackage(imports, // ctx.annotationName().qualifiedName().getText()); // AnnotationType aType = (AnnotationType) // app.getOrCreateReferencedJavaElement(SymbolType.AnnotationType, // packageName, annotationPackageName, // ctx.annotationName().qualifiedName().getText()); Annotation an = JavasimpleFactory.eINSTANCE.createAnnotation(); registerUnresolvedReference(an, SymbolType.AnnotationType, JavasimplePackage.eINSTANCE.getAnnotation_AnnotationType(), ctx.annotationName().qualifiedName().getText()); // an.setAnnotationType(aType); Object top; while (!processStack.isEmpty() && (top = processStack.pop()) != null) { AnnotationParam param = (AnnotationParam) top; if (param.getKey() == null) { an.setValue(param); } else { an.getParameters().add(0, param); } } processStack.push(an); } catch (Exception e) { e.printStackTrace(); } } /** * Generic method to put in the stack the modifiers * * @param ctx */ @Override public void exitModifier(JavaParser.ModifierContext ctx) { if (processingBlock()) { return; } if (ctx.classOrInterfaceModifier() == null) { processStack.push(Modifier.getByName(ctx.getText())); } } /** * Put in the stack the modifiers of a class or interface * * @param ctx */ @Override public void exitClassOrInterfaceModifier(JavaParser.ClassOrInterfaceModifierContext ctx) { if (processingBlock()) { return; } /** * The ClassOrInterfaceContext include the annotation we only * add when is not an annotation */ if (ctx.annotation() == null) { processStack.push(Modifier.getByName(ctx.getText())); } } /** * Put in the stack the modifiers of a variable AKA final * * @param ctx */ @Override public void exitVariableModifier(JavaParser.VariableModifierContext ctx) { if (processingBlock()) { return; } if (ctx.annotation() == null) { processStack.push(Modifier.getByName(ctx.getText())); } } /** * Search in the stack for all the modifiers * * @param se */ private void processModifiers(StructuralElement se) { String a; Object top; try { while (!processStack.isEmpty() && (processStack.peek() instanceof Modifier || processStack.peek() instanceof Annotation)) { top = processStack.pop(); if (top instanceof Annotation) { se.getAnnotations().add(0, (Annotation) top); } else if (top instanceof Modifier) { se.getModifiers().add(0, (Modifier) top); } } } catch (Exception e) { System.out.println("----->" + fileName + "<------"); System.out.println(((NamedElement) se).getName()); System.out.println(processStack); e.printStackTrace(); } } private void registerUnresolvedReference(EObject javaElement, SymbolType symbolType, EReference feature, JavaParser.TypeContext type) { if (type != null) { unresolvedTypeReferences .add(new TypeReference(javaElement, symbolType, feature, packageName, imports, type)); } } private void registerUnresolvedReference(EObject javaElement, SymbolType symbolType, EReference feature, String qualifiedName) { if (qualifiedName != null) { unresolvedTypeReferences.add(new TypeReference(javaElement, symbolType, feature, packageName, imports, qualifiedName)); } } private boolean isArray(final JavaParser.TypeContext typeContext) { if (typeContext == null) { return false; } else { return typeContext.getText().endsWith("[]"); } } }; // parser.addParseListener(listener); // parser.addErrorListener(new ConsoleErrorListener()); ParseTreeWalker walker = new ParseTreeWalker(); ParseTree pTree = parser.compilationUnit(); walker.walk(listener, pTree); app.registerUnresolvedReferences(unresolvedTypeReferences); // lexer.getInputStream(). }