fri.patterns.interpreter.parsergenerator.builder
Class SerializedTables

java.lang.Object
  |
  +--fri.patterns.interpreter.parsergenerator.builder.SerializedObject
        |
        +--fri.patterns.interpreter.parsergenerator.builder.SerializedTables

public class SerializedTables
extends fri.patterns.interpreter.parsergenerator.builder.SerializedObject

Buffering ParserTables. SerializedParserTables will build the ParserTables from scratch only the first time. Every following call will load the serialized ParserTables from filesystem.

The time to build parser tables from scratch is less or equal to deserializing it for little syntaxes. Use it only for big tables (e.g. for Java syntax).

Mind that this factory accepts a parser syntax, NOT a syntax where parser and lexer rules were mixed!

Example (with syntax input from a file):

                File syntaxInput = ...;
                ParserTables tables = new SerializedTables().get(syntaxInput);
        
or (syntax input from a Reader):
                Reader syntaxInput = ...;
                ParserTables tables = new SerializedTables().get(syntaxInput, "MyTables.ser");
        

Author:
(c) 2000, Fritz Ritzberger

Constructor Summary
SerializedTables()
          Create a ParserTables factory that buffers created parser tables.
SerializedTables(boolean production)
          Create a ParserTables factory that buffers tables once created.
 
Method Summary
static java.lang.String baseNameFromSyntax(java.lang.Object syntaxInput)
          When syntaxInput is a File, the name of the serialization file is created from its basename (without any extension), else "Unknown" is assumed as basename.
 AbstractParserTables buildAndStoreParserTables(java.lang.Class parserType, Syntax parserSyntax, java.lang.Object syntaxInput, java.lang.String baseName)
           
protected  java.lang.String ensureDirectory(java.lang.String fileName)
          Creates the directory of passed filename if it does not exist.
 AbstractParserTables get(java.lang.Class parserType, java.lang.Object syntaxInput, java.lang.String baseName)
          Creates parser tables for passed syntax input from scratch or loads serialized tables from filesystem.
 AbstractParserTables get(java.lang.Object syntaxInput)
          Creates parser tables for passed syntax input from scratch or loads serialized tables from filesystem.
 AbstractParserTables get(java.lang.Object syntaxInput, java.lang.String baseName)
          Creates parser tables for passed syntax input from scratch or loads serialized tables from filesystem.
static void main(java.lang.String[] args)
          Test main.
protected  java.lang.String makeFilePath(java.lang.String fileName)
          Puts the file into ".friware/parsers" directory in "user.home".
protected  java.lang.Object read(java.lang.String fileName)
          Deserializes an object from filesystem.
 AbstractParserTables readParserTables(java.lang.Object syntaxInput, java.lang.String baseName)
           
static Syntax toSyntax(java.lang.Object syntaxInput)
          Converts a File, InputStream, Reader, String, StringBuffer, List of Lists or String[][] to a Syntax.
protected  boolean write(java.lang.String fileName, java.lang.Object o)
          Serializes an object to filesystem.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SerializedTables

public SerializedTables()
Create a ParserTables factory that buffers created parser tables.


SerializedTables

public SerializedTables(boolean production)
Create a ParserTables factory that buffers tables once created. @param production when false the tables will not be buffered.

Method Detail

get

public AbstractParserTables get(java.lang.Object syntaxInput)
                         throws java.lang.Exception
Creates parser tables for passed syntax input from scratch or loads serialized tables from filesystem. LALRParserTables.class wil be the type of created parser tables.

Parameters:
syntaxInput - the Parser syntax as File, InputStream, List of Lists, String [][] or Syntax.
Returns:
deserialized ParserTables, or one built from scratch that gets written to filesystem.
java.lang.Exception

get

public AbstractParserTables get(java.lang.Object syntaxInput,
                                java.lang.String baseName)
                         throws java.lang.Exception
Creates parser tables for passed syntax input from scratch or loads serialized tables from filesystem. LALRParserTables.class will be the type of created parser tables.

Parameters:
syntaxInput - the Parser syntax as File, InputStream, List of Lists, String [][] or Syntax.
baseName - name of serialization file, can be null when syntaxInput is a File
Returns:
deserialized ParserTables, or one built from scratch that gets written to filesystem.
java.lang.Exception

get

public AbstractParserTables get(java.lang.Class parserType,
                                java.lang.Object syntaxInput,
                                java.lang.String baseName)
                         throws java.lang.Exception
Creates parser tables for passed syntax input from scratch or loads serialized tables from filesystem.

Parameters:
parserType - the ParserTables class, e.g. SLRParserTables.class.
syntaxInput - the Parser syntax as File, InputStream, List of Lists, String [][] or Syntax.
baseName - name of serialization file, can be null when syntaxInput is a File
Returns:
deserialized ParserTables, or one built from scratch that gets written to filesystem.
java.lang.Exception

readParserTables

public AbstractParserTables readParserTables(java.lang.Object syntaxInput,
                                             java.lang.String baseName)

buildAndStoreParserTables

public AbstractParserTables buildAndStoreParserTables(java.lang.Class parserType,
                                                      Syntax parserSyntax,
                                                      java.lang.Object syntaxInput,
                                                      java.lang.String baseName)
                                               throws java.lang.Exception
java.lang.Exception

main

public static void main(java.lang.String[] args)
Test main. Building serialized ParserTables takes 170, building from scratch takes 59 millis !!!


read

protected java.lang.Object read(java.lang.String fileName)
Deserializes an object from filesystem.

Returns:
deserialized object.

write

protected boolean write(java.lang.String fileName,
                        java.lang.Object o)
Serializes an object to filesystem.

Returns:
true on success.

baseNameFromSyntax

public static java.lang.String baseNameFromSyntax(java.lang.Object syntaxInput)
When syntaxInput is a File, the name of the serialization file is created from its basename (without any extension), else "Unknown" is assumed as basename. The returned name has no path.


toSyntax

public static Syntax toSyntax(java.lang.Object syntaxInput)
                       throws java.lang.Exception
Converts a File, InputStream, Reader, String, StringBuffer, List of Lists or String[][] to a Syntax.

java.lang.Exception

makeFilePath

protected java.lang.String makeFilePath(java.lang.String fileName)
Puts the file into ".friware/parsers" directory in "user.home".


ensureDirectory

protected java.lang.String ensureDirectory(java.lang.String fileName)
Creates the directory of passed filename if it does not exist. @return the directory name.