julianthome / inmemantlr

ANTLR as a libray for JVM based languages
MIT License
108 stars 24 forks source link

How to use "Utility Files" #38

Open manticore-projects opened 4 years ago

manticore-projects commented 4 years ago

Dear Julian and Team,

first of all, thanks a lot for providing this project. I have figured out, how to make use of the Utility files. See the example below.

Furthermore I would like to suggest to implement the TreeNode interface in order to make it easier to use the UI. Please see the attached patch.

Cheers!

import java.awt.Dimension;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snt.inmemantlr.GenericParser;
import org.snt.inmemantlr.exceptions.CompilationException;
import org.snt.inmemantlr.exceptions.IllegalWorkflowException;
import org.snt.inmemantlr.exceptions.ParsingException;
import org.snt.inmemantlr.listener.DefaultTreeListener;
import org.snt.inmemantlr.tree.ParseTree;

/**
 *
 * @author are
 */
public class TestSimple {

  private static final Logger LOGGER = LoggerFactory.getLogger(TestSimple.class);

  public static void main(String[] args) throws Exception {
    ClassLoader cl = TestSimple.class.getClassLoader();

    File lexerFile = new File(cl.getResource("com/manticore/g4/PlSqlLexer.g4").toURI());
    File parserFile = new File(cl.getResource("com/manticore/g4/PlSqlParser.g4").toURI());

    File abstractLexerClassFile = new File(FileUtils.getTempDirectory(), "PlSqlLexerBase.java");
    abstractLexerClassFile.deleteOnExit();

    File abstractParserClassFile = new File(FileUtils.getTempDirectory(), "PlSqlParserBase.java");
    abstractLexerClassFile.deleteOnExit();

    FileUtils.copyInputStreamToFile(cl.getResourceAsStream("com/manticore/g4/PlSqlLexerBase.java.txt"), abstractLexerClassFile);
    FileUtils.copyInputStreamToFile(cl.getResourceAsStream("com/manticore/g4/PlSqlParserBase.java.txt"), abstractParserClassFile);

    GenericParser gp = new GenericParser(lexerFile, parserFile);

    gp.addUtilityJavaFiles(abstractLexerClassFile, abstractParserClassFile);

    DefaultTreeListener t = new DefaultTreeListener();
    gp.setListener(t);

    boolean compile;
    try {
      gp.compile();
      compile = true;
      // gp.writeAntlrAritfactsTo("/tmp/grammar");
      // gp.store("/tmp/grammar/gp.out", true);
    } catch (CompilationException e) {
      LOGGER.error(e.getMessage(), e);
      compile = false;
    }

    // this example shows you how one could use inmemantlr for incremental parsing
    try {
      ParseTree parseTree;
      gp.parse("SELECT a, b, c from t1 inner join t2 on t1.a=t2.b;", GenericParser.CaseSensitiveType.UPPER);
      parseTree = t.getParseTree();

      JTree tree = new JTree(parseTree.getRoot());
      JScrollPane scrollPane = new JScrollPane(tree);
      JFrame frame = new JFrame("Simple SQL parser test");
      frame.add(scrollPane);
      frame.setPreferredSize(new Dimension(480, 640));
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      frame.setVisible(true);

    } catch (IllegalWorkflowException | ParsingException e) {
      LOGGER.error(e.getMessage(), e);
    }
  }
}

ParseTreeNode.java.zip

julianthome commented 4 years ago

Awesome 👍 thanks a lot for your contribution @manticore-projects. Would you be interested and/or would it be possible for your to integrate your changes through a PR. That would make the discussion about the envisioned changes and their purpose easier.