org.w3c.tools.sexpr
Class SimpleSExprStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by java.io.PushbackInputStream
              extended by org.w3c.tools.sexpr.SimpleSExprStream
All Implemented Interfaces:
java.io.Closeable, SExprParser, SExprStream

public class SimpleSExprStream
extends java.io.PushbackInputStream
implements SExprStream

Basic implementation of the SExprStream parser interface.


Field Summary
 
Fields inherited from class java.io.PushbackInputStream
buf, pos
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
SimpleSExprStream(java.io.InputStream input)
          Initializes the parser with no read table and no symbol table assigned.
 
Method Summary
 SExprParser addParser(char key, SExprParser parser)
          Associates a dispatch character with a parser in the read table.
 boolean getListsAsVectors()
          Checks whether lists should be parsed as Vectors or Cons cells.
 Readtable getReadtable()
          Accesses the read table of the parser.
 java.lang.StringBuffer getScratchBuffer()
          Accesses an empty string buffer available temporary storage.
 java.util.Dictionary getSymbols()
          Accesses the symbol table of the parser.
protected  boolean isAtomChar(char c, boolean first)
          Predicate function for checking if a chahracter can belong to an atom.
static void main(java.lang.String[] args)
           
protected  java.lang.Number makeNumber(java.lang.String s)
          Attempts to parse a number from the string.
 java.lang.Object parse()
          Parses a single object from the underlying input stream.
 java.lang.Object parse(char c, SExprStream stream)
          Parses a single object started by the character c.
protected  java.lang.Object parseAtom(char c)
          Parses an atom (a number or a symbol).
protected  Cons parseList()
          Parses a list (as Cons cells) sans first character.
 java.lang.String parseString()
          Parses a double-quote -delimited string (sans the first character).
protected  java.util.Vector parseVector(java.util.Vector vector, char delimiter)
          Parses a list (as a Vector) sans first character.
static void printExpr(java.lang.Object expr, java.io.PrintStream out)
          Produces a printed representation of an s-expression.
 char readSkipWhite()
          Reads from the stream, skipping whitespace and comments.
 boolean setListsAsVectors(boolean listsAsVectors)
          Controls whether lists are represented as Vectors or Cons cells.
 Readtable setReadtable(Readtable readtable)
          Assigns a new read table to the parser.
 java.util.Dictionary setSymbols(java.util.Dictionary symbols)
          Assigns a symbol table to the parser.
 
Methods inherited from class java.io.PushbackInputStream
available, close, mark, markSupported, read, read, reset, skip, unread, unread, unread
 
Methods inherited from class java.io.FilterInputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.w3c.tools.sexpr.SExprStream
read
 

Constructor Detail

SimpleSExprStream

public SimpleSExprStream(java.io.InputStream input)
Initializes the parser with no read table and no symbol table assigned. Parsed lists will be represented as Cons cells.

Method Detail

getSymbols

public java.util.Dictionary getSymbols()
Accesses the symbol table of the parser. If no symbol table has been assigned, creates an empty table.

Specified by:
getSymbols in interface SExprStream

setSymbols

public java.util.Dictionary setSymbols(java.util.Dictionary symbols)
Assigns a symbol table to the parser. Assigning null will prevent an empty symbol table to be created in the future.

Specified by:
setSymbols in interface SExprStream

getReadtable

public Readtable getReadtable()
Accesses the read table of the parser. If no read table has been assigned, creates an empty table.

Specified by:
getReadtable in interface SExprStream

setReadtable

public Readtable setReadtable(Readtable readtable)
Assigns a new read table to the parser.

Specified by:
setReadtable in interface SExprStream

getListsAsVectors

public boolean getListsAsVectors()
Checks whether lists should be parsed as Vectors or Cons cells.

Specified by:
getListsAsVectors in interface SExprStream

setListsAsVectors

public boolean setListsAsVectors(boolean listsAsVectors)
Controls whether lists are represented as Vectors or Cons cells.

Specified by:
setListsAsVectors in interface SExprStream

getScratchBuffer

public java.lang.StringBuffer getScratchBuffer()
Accesses an empty string buffer available temporary storage. This buffer can be used by sub-parsers as a scratch area. Please note that the buffer is not guarded in any way, so multithreaded and reentrant programs must worry about this themselves.

Specified by:
getScratchBuffer in interface SExprStream

parse

public java.lang.Object parse()
                       throws SExprParserException,
                              java.io.IOException
Parses a single object from the underlying input stream.

Specified by:
parse in interface SExprStream
Throws:
SExprParserException - if syntax error was detected
java.io.IOException - if any other I/O-related problem occurred

parse

public java.lang.Object parse(char c,
                              SExprStream stream)
                       throws SExprParserException,
                              java.io.IOException
Parses a single object started by the character c. Implements the SExprParser interface.

Specified by:
parse in interface SExprParser
Throws:
SExprParserException - if syntax error was detected
java.io.IOException - if any other I/O-related problem occurred

parseList

protected Cons parseList()
                  throws SExprParserException,
                         java.io.IOException
Parses a list (as Cons cells) sans first character.

Throws:
SExprParserException - if syntax error was detected
java.io.IOException - if any other I/O-related problem occurred

parseVector

protected java.util.Vector parseVector(java.util.Vector vector,
                                       char delimiter)
                                throws SExprParserException,
                                       java.io.IOException
Parses a list (as a Vector) sans first character. In order to parse list-like structures delimited by other characters than parentheses, the delimiting (ending) character has to be provided.

Throws:
SExprParserException - if syntax error was detected
java.io.IOException - if any other I/O-related problem occurred

parseAtom

protected java.lang.Object parseAtom(char c)
                              throws SExprParserException,
                                     java.io.IOException
Parses an atom (a number or a symbol). Since anything that is not a number is a symbol, syntax errors are not possible.

Throws:
SExprParserException - not signalled but useful for the protocol
java.io.IOException - if an I/O problem occurred (e.g. end of file)

parseString

public java.lang.String parseString()
                             throws SExprParserException,
                                    java.io.IOException
Parses a double-quote -delimited string (sans the first character). Please note: no escape-character interpretation is performed. Override this method for any escape character handling.

Throws:
SExprParserException - not signalled but useful for the protocol
java.io.IOException - any I/O problem (including end of file)

isAtomChar

protected boolean isAtomChar(char c,
                             boolean first)
Predicate function for checking if a chahracter can belong to an atom.

Parameters:
first - if true means that c is the first character of the atom

readSkipWhite

public char readSkipWhite()
                   throws java.io.IOException
Reads from the stream, skipping whitespace and comments.

Specified by:
readSkipWhite in interface SExprStream
Throws:
java.io.IOException - if an I/O problem occurred (including end of file)

makeNumber

protected java.lang.Number makeNumber(java.lang.String s)
                               throws java.lang.NumberFormatException
Attempts to parse a number from the string.

Throws:
java.lang.NumberFormatException - the string does not represent a number

addParser

public SExprParser addParser(char key,
                             SExprParser parser)
Associates a dispatch character with a parser in the read table.

Specified by:
addParser in interface SExprStream

printExpr

public static void printExpr(java.lang.Object expr,
                             java.io.PrintStream out)
Produces a printed representation of an s-expression.


main

public static void main(java.lang.String[] args)
                 throws SExprParserException,
                        java.io.IOException
Throws:
SExprParserException
java.io.IOException