package org.metaborg.spoofax.core.syntax;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.vfs2.FileObject;
import org.metaborg.sdf2table.io.ParseTableIO;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.jsglr.client.ParseTable;
import org.spoofax.terms.io.binary.TermReader;

public class JSGLR1FileParseTableProvider implements IParseTableProvider {
    private final FileObject resource;
    private final ITermFactory termFactory;

    private ParseTable parseTable;

    public JSGLR1FileParseTableProvider(FileObject resource, ITermFactory termFactory) {
        this.resource = resource;
        this.termFactory = termFactory;
    }

    @Override public ParseTable parseTable() throws IOException {
        if(parseTable != null) {
            return parseTable;
        }

        resource.refresh();

        if(!resource.exists()) {
            throw new IOException("Could not load parse table from " + resource + ", file does not exist");
        }

        try(final InputStream stream = resource.getContent().getInputStream()) {
            final TermReader termReader = new TermReader(termFactory);
            IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);

            // Name of parse table Java object is currently fixed as table.bin and table-completions.bin
            FileObject persistedTable;
            if(resource.getName().getBaseName().contains("-completions")) {
                persistedTable = resource.getParent().resolveFile("table-completions.bin");
            } else {
                persistedTable = resource.getParent().resolveFile("table.bin");
            }

            parseTable = new ParseTable(parseTableTerm, termFactory);

            // only read serialized table when table generation is dynamic (#states = 0)
            if(persistedTable.exists() && parseTable.getStateCount() == 0) {
                parseTable =
                    new ParseTable(parseTableTerm, termFactory, persistedTable, new ParseTableIO(persistedTable));
            }
        } catch(Exception e) {
            throw new IOException("Could not load parse table from " + resource, e);
        }

        return parseTable;
    }
}
@@ -286,16 +286,76 @@
 actory;%0A
+import org.spoofax.jsglr.client.InvalidParseTableException;%0A
 import o
@@ -1777,32 +1777,54 @@
 %0A            %7D%0A%0A
+            try %7B%0A    
             pars
@@ -1877,17 +1877,55 @@
 ctory);%0A
-%0A
+            %7D catch(Exception e) %7B%0A    
         
@@ -2013,24 +2013,28 @@
             
+   
+ 
 if(persisted
@@ -2051,47 +2051,129 @@
 ts()
- && p
+%0A                    && e.getMessage() == %22P
 arse
-T
+ t
 able
-.getStateCount() == 0) %7B%0A
+ does not contain any state and normalized grammar is null%22) %7B%0A    
     
@@ -2197,16 +2197,20 @@
 Table =%0A
+    
         
@@ -2316,16 +2316,88 @@
 able));%0A
+                %7D else %7B%0A                    throw e;%0A                %7D%0A
         

Fix loading dynamic parse table for JSGLR1.