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

Can't parse with DE-serialized parser #43

Open
manticore-projects opened this issue May 21, 2021 · 1 comment
Open

Can't parse with DE-serialized parser #43

manticore-projects opened this issue May 21, 2021 · 1 comment
Assignees
Labels

Comments

@manticore-projects
Copy link

manticore-projects commented May 21, 2021

Hi Julian,

everything works fine when creating a fresh Parser.
However, when I serialize the parser and de-serialize it (successfully without any error), the parse will fail:

Caused by: java.lang.NullPointerException
	at org.snt.inmemantlr.memobjects.MemoryByteCode.getBytes(MemoryByteCode.java:92)
	at org.snt.inmemantlr.comp.SpecialClassLoader.findClass(SpecialClassLoader.java:65)
	at org.snt.inmemantlr.comp.StringCompiler.findClass(StringCompiler.java:152)
	at org.snt.inmemantlr.comp.StringCompiler.instanciateLexer(StringCompiler.java:181)
	at org.snt.inmemantlr.GenericParser.parse(GenericParser.java:526)
	at org.snt.inmemantlr.GenericParser.parse(GenericParser.java:420)
	at com.manticore.jsqlformatter.JavaTools.formatJava(JavaTools.java:153)
	... 1 more

Shortened Java Code

    GenericParser gp =null;
    File cacheFolder = new File(System.getProperty( "java.io.tmpdir"));
    if (cacheFolder.exists() && cacheFolder.isDirectory()) {
      File cacheFile = new File(cacheFolder, "java_grammar.out");
      if (cacheFile.exists() && cacheFile.canRead()) {
        LOGGER.info("Found cache file " + cacheFile.getAbsolutePath());
        try {
          gp = GenericParser.load(cacheFile.getAbsolutePath());
        } catch (Exception ex) {
          LOGGER.log(Level.WARNING, "Failed to read the cached grammer from " + cacheFile.getAbsolutePath(), ex);
          cacheFile.delete();
        }
      }
    }

    gp.setListener(t);
    try {
      ParseTree parseTree;
      gp.parse(source.toString(), GenericParser.CaseSensitiveType.NONE);
      parseTree = t.getParseTree();
    } catch (Exception ex) {
      throw new Exception("Could not parse Java Code:\n" + javaCode, ex);
    }

Before I have serialized the Parser like shown below:

    if (gp==null) {
      LOGGER.info("Compile new Generic Parser.");
       
      gp = new GenericParser(lexerContent, parserContent);
      gp.compile();
      if (cacheFolder.exists() && cacheFolder.isDirectory()) {
        File cacheFile = new File(cacheFolder, "java_grammar.out");
        gp.store(cacheFile.getAbsolutePath(), true);
        
        LOGGER.info("Saved cache file " + cacheFile.getAbsolutePath());
      }
    }

The Parser is correct and works fine when I create it afresh.
But it fails with the NPE when loading it from the file.

I have the File java_grammar.out attached:
java_grammar.zip

@julianthome julianthome self-assigned this May 21, 2021
@spartacus7
Copy link

Hi Julian,
I have run into the same issue. Would be good to know if you're planning to fix this issue.

Thanks for this great project. though!

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

No branches or pull requests

3 participants