Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use "Utility Files" #38

Open
manticore-projects opened this issue Oct 9, 2020 · 1 comment
Open

How to use "Utility Files" #38

manticore-projects opened this issue Oct 9, 2020 · 1 comment

Comments

@manticore-projects
Copy link

manticore-projects commented Oct 9, 2020

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants