fri.patterns.interpreter.parsergenerator.builder
Class SerializedLexer

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

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

Buffering Lexers. SerializedLexer will build the Lexer from scratch only the first time. Every following call will load the serialized Lexer from filesystem.

The time to build a lexer from scratch is equal to deserializing it in most cases. So a standalone lexer can be built without this class. When needed for a Parser, use the SerializedParser factory!

This factory will separate the passed syntax into parser and lexer syntax if token and ignored symbol Lists are null. So take care to use "token" and "ignored" rules within syntax to achieve the desired result!

Example (syntax input from a file):

        File ebnfFile = ...;
        Lexer lexer = new SerializedLexer().get(ebnfFile);
        
or (syntax input from a Reader, must pass a filename):
        Reader ebnfReader = ...;
        Lexer lexer = new SerializedLexer().get(ebnfReader, "MyLexer.ser");
        

Author:
(c) 2002, Fritz Ritzberger

Field Summary
protected  boolean PRODUCTION
           
 
Constructor Summary
SerializedLexer()
          Create a Lexer factory that caches built Lexers.
SerializedLexer(boolean production)
          Create a Lexer factory that caches built Lexers.
 
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.
 Lexer buildAndStoreLexer(java.lang.Object syntaxInput, java.lang.String baseName, java.util.List tokenSymbols, java.util.List ignoredSymbols)
          Builds a lexer from passed syntax and stores it to a File (when PRODUCTION is true, this is default).
protected  java.lang.String ensureDirectory(java.lang.String fileName)
          Creates the directory of passed filename if it does not exist.
 Lexer get(java.lang.Object syntaxInput)
          Builds the Lexer from scratch if not found in filesystem, else loads the serialized Lexer.
 Lexer get(java.lang.Object syntaxInput, java.lang.String baseName)
          Builds the Lexer from scratch if not found in filesystem, else loads the serialized Lexer.
 Lexer get(java.lang.Object syntaxInput, java.lang.String baseName, java.util.List tokenSymbols, java.util.List ignoredSymbols)
          Builds the Lexer from scratch if not found in filesystem, else loads the serialized Lexer.
 SyntaxSeparation getSyntaxSeparation()
          If the lexer was built from scratch, the SyntaxSeparation object returned will not be null and can be used to retrieve the parser syntax, else null is returned, as the separation is not available in serialized Lexer.
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  LexerBuilder newLexerBuilder(Syntax syntax, java.util.List ignoredSymbols)
          To be overridden when a modified LexerBuilder is needed.
protected  SyntaxSeparation newSyntaxSeparation(Syntax syntax)
          To be overridden when a modified SyntaxSeparation is needed.
protected  java.lang.Object read(java.lang.String fileName)
          Deserializes an object from filesystem.
 Lexer readLexer(java.lang.Object syntaxInput, java.lang.String baseName)
          Tries to read the lexer from a serialized file.
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
 

Field Detail

PRODUCTION

protected boolean PRODUCTION
Constructor Detail

SerializedLexer

public SerializedLexer()
Create a Lexer factory that caches built Lexers.


SerializedLexer

public SerializedLexer(boolean production)
Create a Lexer factory that caches built Lexers. @param production when false the Lexer will not be serialized.

Method Detail

get

public Lexer get(java.lang.Object syntaxInput)
          throws java.lang.Exception
Builds the Lexer from scratch if not found in filesystem, else loads the serialized Lexer.

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

get

public Lexer get(java.lang.Object syntaxInput,
                 java.lang.String baseName)
          throws java.lang.Exception
Builds the Lexer from scratch if not found in filesystem, else loads the serialized Lexer.

Parameters:
syntaxInput - the Lexer 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 Lexer, or one built from scratch that gets written to filesystem.
java.lang.Exception

get

public Lexer get(java.lang.Object syntaxInput,
                 java.lang.String baseName,
                 java.util.List tokenSymbols,
                 java.util.List ignoredSymbols)
          throws java.lang.Exception
Builds the Lexer from scratch if not found in filesystem, else loads the serialized Lexer.

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

readLexer

public Lexer readLexer(java.lang.Object syntaxInput,
                       java.lang.String baseName)
Tries to read the lexer from a serialized file. One of the two arguments must be non-null.

Parameters:
syntaxInput - the lexer syntax input to retrieve a default name when it is a File
baseName - if baseName is "Xml", the file "XmlLexer.ser" will be read, can be null

buildAndStoreLexer

public Lexer buildAndStoreLexer(java.lang.Object syntaxInput,
                                java.lang.String baseName,
                                java.util.List tokenSymbols,
                                java.util.List ignoredSymbols)
                         throws java.lang.Exception
Builds a lexer from passed syntax and stores it to a File (when PRODUCTION is true, this is default).

Parameters:
syntaxInput - the lexer syntax input
baseName - a file basename, if "Xml", the file "XmlLexer.ser" will be written
java.lang.Exception

newSyntaxSeparation

protected SyntaxSeparation newSyntaxSeparation(Syntax syntax)
                                        throws SyntaxException
To be overridden when a modified SyntaxSeparation is needed.

SyntaxException

newLexerBuilder

protected LexerBuilder newLexerBuilder(Syntax syntax,
                                       java.util.List ignoredSymbols)
                                throws LexerException,
                                       SyntaxException
To be overridden when a modified LexerBuilder is needed.

LexerException
SyntaxException

getSyntaxSeparation

public SyntaxSeparation getSyntaxSeparation()
If the lexer was built from scratch, the SyntaxSeparation object returned will not be null and can be used to retrieve the parser syntax, else null is returned, as the separation is not available in serialized Lexer.


main

public static void main(java.lang.String[] args)
Test main. Building serialized Lexer takes 330, building from scratch takes 130 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.