ai.krr
Class LispSymbolAdaptor

java.lang.Object
  extended by ai.krr.LispSymbolAdaptor
All Implemented Interfaces:
inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>

public class LispSymbolAdaptor
extends java.lang.Object
implements inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>

This class is an adaptor between the Symbol class that can be used as an internal representation for reasoning and its external form as defined by the LISP syntax described below and in more detail in Guy L Steele's book "Common Lisp the Language".

Writing

The type of the Symbol determines how exactly it is written out. Most Symbol types are straight forward to write. NamedSymbols depend on the given properties. If the case-sensitive property is set to false, lower case letters will be escaped. The namespace-aware property determines whether the NameSpace URI is written before the symbol name.

The syntax of LISP Symbols can be defined as follows:

 <Symbol> ::= TRUE | FALSE | <double> | <int> | #\<char> | "<string>" | <NamedSymbol>
 <NamedSymbol> ::= [<URI>\#]<word> | '|'[<URI>\#]<string>'|'
 

NamedSymbols can be written in one of two ways: either as a word containing only a limited set of characters (and other characters being escaped), or as a string of arbitrary characters between vertical bars.

Reading

Parsing is performed using an underlying LexicalAnalyzer and is based on the same syntax as writing (see above). The different regular expressions defining the different Symbol types are tested and the one that matches is returned. For NamedSymbols, the token must adhere to the LISP syntax for words. Escaped characters will be un-escaped and the result will be capitalized (unless the value of the case-sensitive property is set to true). If the value of the namespace-aware property is false, an internal NamedSymbol with the given name will be returned. Otherwise the NameSpace is extracted from the word and a NamedSymbol from that NameSpace will be returned.

Properties

property possible values default
case-sensitive true, false false
namespace-aware true, false false
indent-string any string "  "

Author:
Gerhard Wickler

Field Summary
static inf.compilers.LexicalAnalyzer.TokenType bwordTT
          the token types used
static inf.compilers.LexicalAnalyzer.TokenType charrefTT
          the token types used
static inf.compilers.LexicalAnalyzer.TokenType falseTT
          the token types used
static inf.compilers.LexicalAnalyzer.TokenType integerTT
          the token types used
protected  java.util.Properties props
          the properties used for reading and writing
static inf.compilers.LexicalAnalyzer.TokenType realnumTT
          the token types used
protected static java.util.Set<java.lang.String> reservedSet
           
protected  inf.compilers.LexicalAnalyzer scanner
          the lexical analyzer used for tokenizing the input
static inf.compilers.LexicalAnalyzer.TokenType stringTT
          the token types used
static inf.compilers.LexicalAnalyzer.TokenType trueTT
          the token types used
static inf.compilers.LexicalAnalyzer.TokenType wordTT
          the token types used
 
Constructor Summary
LispSymbolAdaptor()
           This constructor creates a SyntaxAdaptor that can be used to read symbols in a KIF syntax and create a Symbol for it, or it can be used to write out a Symbol in KIF syntax.
 
Method Summary
protected  LispSymbolAdaptor clone()
           This class does not support cloning and an Exception will be thrown if this method is called.
protected static java.lang.String escapeString(java.lang.String str)
          This function escapes certain characters in the given String by inserting a backslash ('\') just before them.
 java.lang.Class<SyntaxAdaptableSymbol> getInternalClass()
           This function returns the Class that holds the internal representation this adaptor translates to and from.
 java.util.Properties getProperties()
           This function returns all the Properties used by this SyntaxAdaptor.
 java.lang.String getProperty(java.lang.String key)
           This function gets the property that is associated with the given key.
 java.lang.String getSyntaxName()
           This function returns the name that should identify the syntactical language that this adaptor translates to and from.
static NamedSymbol parseNamedSymbol(inf.compilers.LexicalAnalyzer scanner, java.util.Properties props)
           This function attempts to parse a NamedSymbol from the underlying LexicalAnalyzer.
static SyntaxAdaptableSymbol parseSymbol(inf.compilers.LexicalAnalyzer scanner, java.util.Properties props)
           This function attempts to parse a Symbol from the underlying LexicalAnalyzer.
 void prettyPrint(int indent, SyntaxAdaptableSymbol sy, java.io.Writer w)
           This function takes a logical Symbol which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the LISP syntax.
 SyntaxAdaptableSymbol read(java.io.Reader r)
           This function attempts to parse characters from the given Reader until a Symbol that represents an object in the internal representation has been parsed.
 void setProperty(java.lang.String key, java.lang.String value)
           This function sets the property associated with the given key to the given value.
protected static java.lang.String unescapeString(java.lang.String str)
          This function removes backslash characters from the given String.
 void write(SyntaxAdaptableSymbol sy, java.io.Writer w)
           This function takes a logical Symbol which is a Java Object in the internal representation, and writes it to the given Writer as a String conforming to the LISP syntax.
static void writeIndent(int indent, java.lang.String indentStr, java.io.Writer w)
           This function writes white space for indentation of a line to the given Writer.
static void writeNamedSymbol(NamedSymbol nSy, java.io.Writer w, java.util.Properties props)
           This function writes the given NamedSymbol to the given Writer.
static void writeSymbol(Symbol sy, java.io.Writer w, java.util.Properties props)
           This function writes the given Symbol to the given Writer.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

reservedSet

protected static java.util.Set<java.lang.String> reservedSet

trueTT

public static inf.compilers.LexicalAnalyzer.TokenType trueTT
the token types used


falseTT

public static inf.compilers.LexicalAnalyzer.TokenType falseTT
the token types used


charrefTT

public static inf.compilers.LexicalAnalyzer.TokenType charrefTT
the token types used


stringTT

public static inf.compilers.LexicalAnalyzer.TokenType stringTT
the token types used


integerTT

public static inf.compilers.LexicalAnalyzer.TokenType integerTT
the token types used


realnumTT

public static inf.compilers.LexicalAnalyzer.TokenType realnumTT
the token types used


wordTT

public static inf.compilers.LexicalAnalyzer.TokenType wordTT
the token types used


bwordTT

public static inf.compilers.LexicalAnalyzer.TokenType bwordTT
the token types used


props

protected java.util.Properties props
the properties used for reading and writing


scanner

protected inf.compilers.LexicalAnalyzer scanner
the lexical analyzer used for tokenizing the input

Constructor Detail

LispSymbolAdaptor

public LispSymbolAdaptor()

This constructor creates a SyntaxAdaptor that can be used to read symbols in a KIF syntax and create a Symbol for it, or it can be used to write out a Symbol in KIF syntax.

Method Detail

clone

protected LispSymbolAdaptor clone()
                           throws java.lang.CloneNotSupportedException

This class does not support cloning and an Exception will be thrown if this method is called.

Overrides:
clone in class java.lang.Object
Returns:
nothing
Throws:
java.lang.CloneNotSupportedException - will be thrown

getInternalClass

public java.lang.Class<SyntaxAdaptableSymbol> getInternalClass()

This function returns the Class that holds the internal representation this adaptor translates to and from.

Specified by:
getInternalClass in interface inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>
Returns:
the internal representation Class adapted to

getSyntaxName

public java.lang.String getSyntaxName()

This function returns the name that should identify the syntactical language that this adaptor translates to and from.

Specified by:
getSyntaxName in interface inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>
Returns:
the name of the language adapted to

write

public void write(SyntaxAdaptableSymbol sy,
                  java.io.Writer w)
           throws inf.compilers.ExpressivenessException,
                  java.io.IOException

This function takes a logical Symbol which is a Java Object in the internal representation, and writes it to the given Writer as a String conforming to the LISP syntax.

Specified by:
write in interface inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>
Parameters:
sy - a Symbol in the internal representation
w - the Writer to which the syntactical translation is written
Throws:
inf.compilers.ExpressivenessException - if the syntactical language cannot represent the given statement
java.io.IOException - if writing to the given Writer fails

prettyPrint

public void prettyPrint(int indent,
                        SyntaxAdaptableSymbol sy,
                        java.io.Writer w)
                 throws inf.compilers.ExpressivenessException,
                        java.io.IOException

This function takes a logical Symbol which is a Java Object in the internal representation, and writes it to the given Writer as a string conforming to the LISP syntax.

Specified by:
prettyPrint in interface inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>
Parameters:
indent - the amount of indentation for the first line
w - the Writer to which the syntactical translation is written
Throws:
inf.compilers.ExpressivenessException - if the syntactical language cannot represent the given statement
java.io.IOException - if writing to the given Writer fails

read

public SyntaxAdaptableSymbol read(java.io.Reader r)
                           throws inf.compilers.ExpressivenessException,
                                  java.text.ParseException,
                                  java.io.IOException

This function attempts to parse characters from the given Reader until a Symbol that represents an object in the internal representation has been parsed.

Specified by:
read in interface inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>
Parameters:
r - the Reader from which the representation is to be parsed
Returns:
the target internal representation
Throws:
inf.compilers.ExpressivenessException - if the internal representation class is not expressive enough for the given symbol
java.text.ParseException - if there is a syntax error in the given string
java.io.IOException - if reading from the Reader fails

getProperty

public java.lang.String getProperty(java.lang.String key)

This function gets the property that is associated with the given key. Note that the key should not be null.

Specified by:
getProperty in interface inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>
Returns:
the property for the given key (or null if undefined)

setProperty

public void setProperty(java.lang.String key,
                        java.lang.String value)

This function sets the property associated with the given key to the given value. The given key must not be null, but the value may be.

Specified by:
setProperty in interface inf.compilers.SyntaxAdaptor<SyntaxAdaptableSymbol>
Parameters:
key - the key with which the value is associated
value - the associated value

getProperties

public java.util.Properties getProperties()

This function returns all the Properties used by this SyntaxAdaptor.

Returns:
the Properties used by this SyntaxAdaptor

writeSymbol

public static void writeSymbol(Symbol sy,
                               java.io.Writer w,
                               java.util.Properties props)
                        throws inf.compilers.ExpressivenessException,
                               java.io.IOException

This function writes the given Symbol to the given Writer. The type of the Symbol determines how exactly it is written out.

Parameters:
sy - the Symbol to be written out
w - the Writer to which the Symbol will be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if namespace-aware is true but sy is an internal isNamedSymbol
java.io.IOException - if writing to the Writer fails

writeNamedSymbol

public static void writeNamedSymbol(NamedSymbol nSy,
                                    java.io.Writer w,
                                    java.util.Properties props)
                             throws inf.compilers.ExpressivenessException,
                                    java.io.IOException

This function writes the given NamedSymbol to the given Writer. If the case-sensitive property is set to false, lower case letters will be escaped. The namespace-aware property determines whether the NameSpace URI is written before the symbol name. If this property is set to true an attempt to write an internal NamedSymbol (which does not have an associated NameSpace) will cause an ExpressivenessException.

Parameters:
nSy - the NamedSymbol to be written out
w - the Writer to which the NamedSymbol will be written
props - the Properties used for writing
Throws:
inf.compilers.ExpressivenessException - if namespace-aware is true but nSy is internal
java.io.IOException - if writing to the Writer fails

writeIndent

public static void writeIndent(int indent,
                               java.lang.String indentStr,
                               java.io.Writer w)
                        throws java.io.IOException

This function writes white space for indentation of a line to the given Writer. The given integer determines the amount of indent, where the String associated with the property indent-string represents a single indent.

Parameters:
indent - the number of times the indent-string is to be written
indentStr - the String defining a single indentation
w - the Writer to which the indent-strings will be written
Throws:
java.io.IOException - if writing to the Writer fails

parseSymbol

public static SyntaxAdaptableSymbol parseSymbol(inf.compilers.LexicalAnalyzer scanner,
                                                java.util.Properties props)
                                         throws inf.compilers.ExpressivenessException,
                                                java.text.ParseException,
                                                java.io.IOException

This function attempts to parse a Symbol from the underlying LexicalAnalyzer. The different regular expressions defining the different Symbol types are tested and the one that matches is returned. The syntax for Symbols is as follows:

 <Symbol> ::= TRUE | FALSE | <double> | <int> | #\<char> | "<string>" | <NamedSymbol>
 

Parameters:
scanner - the LexicalAnalyzer from which character input is read
props - the properties used for reading
Returns:
the Symbol corresponding to the next token from the underlying LexicalAnalyzer
Throws:
inf.compilers.ExpressivenessException - if a named symbol cannot be expressed
java.text.ParseException - if the LISP syntax for Symbols is violated
java.io.IOException - if reading from the LexicalAnalyzer fails

parseNamedSymbol

public static NamedSymbol parseNamedSymbol(inf.compilers.LexicalAnalyzer scanner,
                                           java.util.Properties props)
                                    throws inf.compilers.ExpressivenessException,
                                           java.text.ParseException,
                                           java.io.IOException

This function attempts to parse a NamedSymbol from the underlying LexicalAnalyzer. Thus, the next token must adhere to the LISP syntax for words or a ParseException will be thrown. Escaped characters will be un-escaped and the result will be capitalized (unless the value of the case-sensitive property is set to true). If the word is a reserved word in LISP a ParseException will be thrown. If the value of the namespace-aware property is false, an internal NamedSymbol with the given name will be returned. Otherwise the NameSpace is extracted from the word and a NamedSymbol from that NameSpace will be returned. The syntax for NamedSymbols is as follows:

 <NamedSymbol> ::= [<URI>\#]<word> | '|'[<URI>\#]<string>'|'
 

Parameters:
scanner - the LexicalAnalyzer from which character input is read
props - the properties used for reading
Returns:
the NamedSymbol corresponding to the next token from the underlying LexicalAnalyzer
Throws:
inf.compilers.ExpressivenessException - not thrown here
java.text.ParseException - if the KIF syntax for words is violated
java.io.IOException - if reading from the LexicalAnalyzer fails

escapeString

protected static java.lang.String escapeString(java.lang.String str)
This function escapes certain characters in the given String by inserting a backslash ('\') just before them. To write a LISP string only the double-quote character and the backslash character need to be escaped by inserting a backslash just before them.

Parameters:
str - the String that is to be escaped for writing
Returns:
a new String with backslash characters inserted

unescapeString

protected static java.lang.String unescapeString(java.lang.String str)
This function removes backslash characters from the given String. Any character that is preceded by a backslash can be in included in a LISP string, which means the backslash character has to be removed during reading.

Parameters:
str - the String that is to be un-escaped for reading
Returns:
a new String with backslash characters removed