/*
   * This method overrides the superclass method in order to calls the parser which is
   * in charge of extracting the particular CST of a provided file.
   * It requires the existence of the Parser/Lexer of the grammar in the classpath
   *
   * @see gts.modernization.launcher.Gra2MoLAbstractLauncher#parser(java.lang.String)
   */
  @Override
  protected Element parser(File path) {
    Object result = null;

    try {
      // Create the filestream
      ANTLRFileStream afs;
      if (!caseSensitive) {
        Gra2MoLLogger.getInstance().print("Creating non case sensitive parser for " + path);
        afs = new ANTLRNoCaseFileStream(path.getAbsolutePath());
      } else {
        Gra2MoLLogger.getInstance().print("Creating case sensitive parser for " + path);
        afs = new ANTLRFileStream(path.getAbsolutePath());
      }

      // Create the lexer class
      Class lexer = Class.forName(grammarName + "Lexer");
      // Locate the lexer constructor
      Constructor lexerConstructor = lexer.getConstructor(org.antlr.runtime.CharStream.class);
      // Create the lexer instance
      Lexer lexerInstance = (Lexer) lexerConstructor.newInstance((CharStream) afs);

      // Create the token stream
      CommonTokenStream cts = new CommonTokenStream(lexerInstance);

      // Create the parser class
      Class parser = Class.forName(grammarName + "Parser");
      // Locate the parser constructor
      Constructor parserConstructor = parser.getConstructor(org.antlr.runtime.TokenStream.class);
      // Create the parser instance
      Object parserInstance = parserConstructor.newInstance(cts);

      // Locate the method
      Method method = parser.getMethod(grammarMainRule, null);
      // Invoke the method
      Object returnedValue = method.invoke(parserInstance, null);
      // Extracts the returnNode field
      Class returned = returnedValue.getClass();
      Field field = returned.getField("returnNode");
      result = field.get(returnedValue);
    } catch (Exception e) {
      System.err.println("Error parsing the file " + path);
      e.printStackTrace();
    }

    if (result instanceof Node) {
      Node n = (Node) result;
      return n;
    } else {
      return null;
    }
  }
Beispiel #2
0
 /**
  * Offers a method to the subclasses to print in the logger
  *
  * @param msg
  */
 public void printError(String msg) {
   Gra2MoLLogger.getInstance().print("<Extension Error: " + msg + ">");
 }