Skip to content

Can't parse with DE-serialized parser #43

@manticore-projects

Description

@manticore-projects

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions