Exemple #1
0
  /**
   * Return the directory containing the grammar file for this grammar. normally this is a relative
   * path from current directory. People will often do "java org.antlr.Tool grammars/*.g3" So the
   * file will be "grammars/foo.g3" etc... This method returns "grammars".
   *
   * <p>If we have been given a specific input directory as a base, then we must find the directory
   * relative to this directory, unless the file name is given to us in absolute terms.
   */
  public String getFileDirectory(String fileName) {

    File f;
    if (haveInputDir && !fileName.startsWith(File.separator)) {
      f = new File(inputDirectory, fileName);
    } else {
      f = new File(fileName);
    }
    // And ask Java what the base directory of this location is
    //
    return f.getParent();
  }
Exemple #2
0
  /** Get a grammar mentioned on the command-line and any delegates */
  public Grammar getRootGrammar(String grammarFileName) throws IOException {
    // StringTemplate.setLintMode(true);
    // grammars mentioned on command line are either roots or single grammars.
    // create the necessary composite in case it's got delegates; even
    // single grammar needs it to get token types.
    CompositeGrammar composite = new CompositeGrammar();
    Grammar grammar = new Grammar(this, grammarFileName, composite);
    composite.setDelegationRoot(grammar);
    FileReader fr = null;
    File f = null;

    if (haveInputDir) {
      f = new File(inputDirectory, grammarFileName);
    } else {
      f = new File(grammarFileName);
    }

    // Store the location of this grammar as if we import files, we can then
    // search for imports in the same location as the original grammar as well as in
    // the lib directory.
    //
    parentGrammarDirectory = f.getParent();

    if (grammarFileName.lastIndexOf(File.separatorChar) == -1) {
      grammarOutputDirectory = ".";
    } else {
      grammarOutputDirectory =
          grammarFileName.substring(0, grammarFileName.lastIndexOf(File.separatorChar));
    }
    fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
    grammar.parseAndBuildAST(br);
    composite.watchNFAConversion = internalOption_watchNFAConversion;
    br.close();
    fr.close();
    return grammar;
  }