📄 javacc.java
字号:
*/ public void setJDKversion(String jdkVersion) { optionalAttrs.put(JDK_VERSION, jdkVersion); } /** * The directory to write the generated files to. * If not set, the files are written to the directory * containing the grammar file. * @param outputDirectory the output directory. */ public void setOutputdirectory(File outputDirectory) { this.outputDirectory = outputDirectory; } /** * The grammar file to process. * @param targetFile the grammar file. */ public void setTarget(File targetFile) { this.targetFile = targetFile; } /** * The directory containing the JavaCC distribution. * @param javaccHome the directory. */ public void setJavacchome(File javaccHome) { this.javaccHome = javaccHome; } /** * Constructor */ public JavaCC() { cmdl.setVm(JavaEnvUtils.getJreExecutable("java")); } /** * Run the task. * @throws BuildException on error. */ public void execute() throws BuildException { // load command line with optional attributes Enumeration iter = optionalAttrs.keys(); while (iter.hasMoreElements()) { String name = (String) iter.nextElement(); Object value = optionalAttrs.get(name); cmdl.createArgument().setValue("-" + name + ":" + value.toString()); } // check the target is a file if (targetFile == null || !targetFile.isFile()) { throw new BuildException("Invalid target: " + targetFile); } // use the directory containing the target as the output directory if (outputDirectory == null) { outputDirectory = new File(targetFile.getParent()); } else if (!outputDirectory.isDirectory()) { throw new BuildException("Outputdir not a directory."); } cmdl.createArgument().setValue("-OUTPUT_DIRECTORY:" + outputDirectory.getAbsolutePath()); // determine if the generated java file is up-to-date final File javaFile = getOutputJavaFile(outputDirectory, targetFile); if (javaFile.exists() && targetFile.lastModified() < javaFile.lastModified()) { log("Target is already built - skipping (" + targetFile + ")", Project.MSG_VERBOSE); return; } cmdl.createArgument().setValue(targetFile.getAbsolutePath()); final Path classpath = cmdl.createClasspath(getProject()); final File javaccJar = JavaCC.getArchiveFile(javaccHome); classpath.createPathElement().setPath(javaccJar.getAbsolutePath()); classpath.addJavaRuntime(); cmdl.setClassname(JavaCC.getMainClass(classpath, JavaCC.TASKDEF_TYPE_JAVACC)); final Commandline.Argument arg = cmdl.createVmArgument(); arg.setValue("-mx140M"); arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath()); Execute.runCommand(this, cmdl.getCommandline()); } /** * Helper method to retrieve the path used to store the JavaCC.zip * or javacc.jar which is different from versions. * * @param home the javacc home path directory. * @throws BuildException thrown if the home directory is invalid * or if the archive could not be found despite attempts to do so. * @return the file object pointing to the JavaCC archive. */ protected static File getArchiveFile(File home) throws BuildException { return new File(home, ARCHIVE_LOCATIONS[getArchiveLocationIndex(home)]); } /** * Helper method to retrieve main class which is different from versions. * @param home the javacc home path directory. * @param type the taskdef. * @throws BuildException thrown if the home directory is invalid * or if the archive could not be found despite attempts to do so. * @return the main class for the taskdef. */ protected static String getMainClass(File home, int type) throws BuildException { Path p = new Path(null); p.createPathElement().setLocation(getArchiveFile(home)); p.addJavaRuntime(); return getMainClass(p, type); } /** * Helper method to retrieve main class which is different from versions. * @param path classpath to search in. * @param type the taskdef. * @throws BuildException thrown if the home directory is invalid * or if the archive could not be found despite attempts to do so. * @return the main class for the taskdef. * @since Ant 1.7 */ protected static String getMainClass(Path path, int type) throws BuildException { String packagePrefix = null; String mainClass = null; AntClassLoader l = new AntClassLoader(); l.setClassPath(path.concatSystemClasspath("ignore")); String javaccClass = COM_PACKAGE + COM_JAVACC_CLASS; InputStream is = l.getResourceAsStream(javaccClass.replace('.', '/') + ".class"); if (is != null) { packagePrefix = COM_PACKAGE; switch (type) { case TASKDEF_TYPE_JAVACC: mainClass = COM_JAVACC_CLASS; break; case TASKDEF_TYPE_JJTREE: mainClass = COM_JJTREE_CLASS; break; case TASKDEF_TYPE_JJDOC: mainClass = COM_JJDOC_CLASS; break; default: // Fall Through } } else { javaccClass = ORG_PACKAGE_3_1 + ORG_JAVACC_CLASS; is = l.getResourceAsStream(javaccClass.replace('.', '/') + ".class"); if (is != null) { packagePrefix = ORG_PACKAGE_3_1; } else { javaccClass = ORG_PACKAGE_3_0 + ORG_JAVACC_CLASS; is = l.getResourceAsStream(javaccClass.replace('.', '/') + ".class"); if (is != null) { packagePrefix = ORG_PACKAGE_3_0; } } if (is != null) { switch (type) { case TASKDEF_TYPE_JAVACC: mainClass = ORG_JAVACC_CLASS; break; case TASKDEF_TYPE_JJTREE: mainClass = ORG_JJTREE_CLASS; break; case TASKDEF_TYPE_JJDOC: mainClass = ORG_JJDOC_CLASS; break; default: // Fall Through } } } if (packagePrefix == null) { throw new BuildException("failed to load JavaCC"); } if (mainClass == null) { throw new BuildException("unknown task type " + type); } return packagePrefix + mainClass; } /** * Helper method to determine the archive location index. * * @param home the javacc home path directory. * @throws BuildException thrown if the home directory is invalid * or if the archive could not be found despite attempts to do so. * @return the archive location index */ private static int getArchiveLocationIndex(File home) throws BuildException { if (home == null || !home.isDirectory()) { throw new BuildException("JavaCC home must be a valid directory."); } for (int i = 0; i < ARCHIVE_LOCATIONS.length; i++) { File f = new File(home, ARCHIVE_LOCATIONS[i]); if (f.exists()) { return i; } } throw new BuildException("Could not find a path to JavaCC.zip " + "or javacc.jar from '" + home + "'."); } /** * Helper method to determine the major version number of JavaCC. * * @param home the javacc home path directory. * @throws BuildException thrown if the home directory is invalid * or if the archive could not be found despite attempts to do so. * @return a the major version number */ protected static int getMajorVersionNumber(File home) throws BuildException { return ARCHIVE_LOCATIONS_VS_MAJOR_VERSION[getArchiveLocationIndex(home)]; } /** * Determines the output Java file to be generated by the given grammar * file. * */ private File getOutputJavaFile(File outputdir, File srcfile) { String path = srcfile.getPath(); // Extract file's base-name int startBasename = path.lastIndexOf(File.separator); if (startBasename != -1) { path = path.substring(startBasename + 1); } // Replace the file's extension with '.java' int startExtn = path.lastIndexOf('.'); if (startExtn != -1) { path = path.substring(0, startExtn) + ".java"; } else { path += ".java"; } // Change the directory if (outputdir != null) { path = outputdir + File.separator + path; } return new File(path); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -