Jatha Software

org.jatha.read
Class LispParser

java.lang.Object
  extended by org.jatha.read.LispParser

public class LispParser
extends Object

A parser that reads LISP-syntax text from a text stream or string. It recognizes all standard LISP datatypes, although not structured ones. This function is designed to fulfill the function of the reader in a LISP read-eval-print loop. Once the LISP parser is instantiated, the parse() function can be used to read from a string or stream. It is best not to instantiate the LispParser yourself. Instead, do the following:

   1.  LispParser parser = Jatha.getParser();
   2.  parser.setInputString(myString);
   3.  parser.setCaseSensitivity(LispParser.PRESERVE);
   4.  LispValue result = parser.parse();
 
Normal usage is to parse a string. If you want to use a Reader, do: new PushbackReader(myReader).

See Also:
LispValue

Field Summary
(package private) static char AT_SIGN
           
(package private) static char BACK_QUOTE
           
private  int BackQuoteLevel
           
(package private) static char BACKSLASH
           
(package private) static char COLON
           
(package private) static char COMMA
           
(package private) static char DECIMAL
           
(package private) static char DOUBLE_QUOTE
           
static int DOWNCASE
           
(package private) static char EQUAL_SIGN
           
private  int f_caseSensitivity
           
private  Jatha f_lisp
           
private static LispParser f_myParser
           
(package private) static char HYPHEN
           
private  PushbackReader inputReader
           
(package private) static char LEFT_ANGLE_BRACKET
           
(package private) static char LEFT_PAREN
           
(package private) static char OR_BAR
           
(package private) static char PERIOD
           
(package private) static char POUND
           
static int PRESERVE
           
(package private) static int READING_BACKQUOTED_LIST
           
(package private) static int READING_CHARACTER
           
(package private) static int READING_MIXED_CASE_SYMBOL
           
(package private) static int READING_NOTHING
           
(package private) static int READING_STRING
           
(package private) static int READING_SYMBOL
           
(package private) static char RIGHT_ANGLE_BRACKET
           
(package private) static char RIGHT_PAREN
           
(package private) static char SEMICOLON
           
(package private) static char SINGLE_QUOTE
           
(package private) static char UNDERSCORE
           
static int UPCASE
           
 
Constructor Summary
LispParser(Jatha lisp, Reader inStream)
           
LispParser(Jatha lisp, Reader inStream, int caseSensitivity)
          Allows you to create a parser that handles input case conversion as you like.
LispParser(Jatha lisp, String inString)
           
LispParser(Jatha lisp, String inString, int caseSensitivity)
          Allows you to create a parser that handles input case conversion as you like.
 
Method Summary
 LispValue applyReaderMacro(PushbackReader stream)
          This routine is called by parse when it encounters a pound (#) mark.
static int firstCharNotInSet(int startIndex, String str, String charSet)
          The equivalent of the C function 'strspn'.
 int getCaseSensitivity()
          Retrieves the current case-sensitivity of the parser.
 PushbackReader getInputReader()
          Gets the current reader to be parsed.
static boolean hasBalancedParentheses(Jatha lisp, LispValue input)
          Returns true if the input expression has balanced parentheses
static boolean hasBalancedParentheses(Jatha lisp, String input)
          Returns true if the input expression has balanced parentheses
(package private)  boolean INTEGER_token_p(String str)
           
(package private)  boolean isAtSign(char x)
           
(package private)  boolean isBackQuote(char x)
           
(package private)  boolean isBackSlash(char x)
           
(package private)  boolean isColon(char x)
           
(package private)  boolean isComma(char x)
           
(package private)  boolean isDoubleQuote(char x)
           
(package private)  boolean isLeftAngleBracket(char x)
           
(package private)  boolean isLparen(char x)
           
(package private)  boolean isOrBar(char x)
           
(package private)  boolean isPeriod(char x)
           
(package private)  boolean isPound(char x)
           
(package private)  boolean isQuote(char x)
           
(package private)  boolean isRightAngleBracket(char x)
           
(package private)  boolean isRparen(char x)
           
(package private)  boolean isSemi(char x)
           
(package private)  boolean isSpace(char x)
           
(package private)  boolean isTerminator(char x)
           
(package private)  boolean NIL_token_p(String str)
           
 LispValue parse()
          Parse() assumes that there is only one expression in the input string or file.
 LispValue read_backquoted_list_token(PushbackReader stream)
          This library can't read backquotes yet.
 LispValue read_backquoted_token(PushbackReader stream)
          This routine is called by parse when it encounters a backquote mark.
(package private)  LispValue read_comma_token(PushbackReader stream)
          This routine is called by parse when it encounters a comma, which is only legal inside a backquote.
 LispValue read_list_token(PushbackReader stream)
          Reads one list expression from the input stream and returns it.
(package private)  LispValue read_quoted_token(PushbackReader stream)
          This routine is called by parse when it encounters a quote mark.
 LispValue read()
          Reads one s-expression from the input stream (a string or file).
(package private)  boolean REAL_token_p(String str)
          Does NOT recognize an isolated '+' or '-' as a real number.
 void setCaseSensitivity(int caseSensitivity)
          Sets the current case-sensitivity of the parser.
 void setInputReader(PushbackReader inputReader)
          Sets the input reader for the Parser.
 void setInputString(String s)
          Sets the input string for the parser.
 void simple_parser_test()
           
(package private)  boolean STRING_token_p(String str)
           
(package private)  boolean SYMBOL_token_p(String str)
           
(package private)  boolean T_token_p(String str)
           
 void test_parser_loop()
           
 void test_parser(String s)
           
 LispValue tokenToLispValue(String token)
          Converts a string to a LISP value such as NIL, T, an integer, a real number, a string or a symbol.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UPCASE

public static final int UPCASE
See Also:
Constant Field Values

DOWNCASE

public static final int DOWNCASE
See Also:
Constant Field Values

PRESERVE

public static final int PRESERVE
See Also:
Constant Field Values

AT_SIGN

static final char AT_SIGN
See Also:
Constant Field Values

BACK_QUOTE

static final char BACK_QUOTE
See Also:
Constant Field Values

BACKSLASH

static final char BACKSLASH
See Also:
Constant Field Values

COLON

static final char COLON
See Also:
Constant Field Values

COMMA

static final char COMMA
See Also:
Constant Field Values

DECIMAL

static final char DECIMAL
See Also:
Constant Field Values

DOUBLE_QUOTE

static final char DOUBLE_QUOTE
See Also:
Constant Field Values

EQUAL_SIGN

static final char EQUAL_SIGN
See Also:
Constant Field Values

LEFT_ANGLE_BRACKET

static final char LEFT_ANGLE_BRACKET
See Also:
Constant Field Values

LEFT_PAREN

static final char LEFT_PAREN
See Also:
Constant Field Values

HYPHEN

static final char HYPHEN
See Also:
Constant Field Values

OR_BAR

static final char OR_BAR
See Also:
Constant Field Values

POUND

static final char POUND
See Also:
Constant Field Values

PERIOD

static final char PERIOD
See Also:
Constant Field Values

RIGHT_PAREN

static final char RIGHT_PAREN
See Also:
Constant Field Values

SEMICOLON

static final char SEMICOLON
See Also:
Constant Field Values

RIGHT_ANGLE_BRACKET

static final char RIGHT_ANGLE_BRACKET
See Also:
Constant Field Values

SINGLE_QUOTE

static final char SINGLE_QUOTE
See Also:
Constant Field Values

UNDERSCORE

static final char UNDERSCORE
See Also:
Constant Field Values

READING_NOTHING

static final int READING_NOTHING
See Also:
Constant Field Values

READING_SYMBOL

static final int READING_SYMBOL
See Also:
Constant Field Values

READING_MIXED_CASE_SYMBOL

static final int READING_MIXED_CASE_SYMBOL
See Also:
Constant Field Values

READING_CHARACTER

static final int READING_CHARACTER
See Also:
Constant Field Values

READING_STRING

static final int READING_STRING
See Also:
Constant Field Values

READING_BACKQUOTED_LIST

static final int READING_BACKQUOTED_LIST
See Also:
Constant Field Values

BackQuoteLevel

private int BackQuoteLevel

inputReader

private PushbackReader inputReader

f_caseSensitivity

private int f_caseSensitivity

f_myParser

private static LispParser f_myParser

f_lisp

private Jatha f_lisp
Constructor Detail

LispParser

public LispParser(Jatha lisp,
                  Reader inStream)

LispParser

public LispParser(Jatha lisp,
                  String inString)

LispParser

public LispParser(Jatha lisp,
                  Reader inStream,
                  int caseSensitivity)
Allows you to create a parser that handles input case conversion as you like. Default is UPCASE. Other values are DOWNCASE and PRESERVE.

Parameters:
inStream -

LispParser

public LispParser(Jatha lisp,
                  String inString,
                  int caseSensitivity)
Allows you to create a parser that handles input case conversion as you like. Default is UPCASE. Other values are DOWNCASE and PRESERVE.

Method Detail

getCaseSensitivity

public int getCaseSensitivity()
Retrieves the current case-sensitivity of the parser. It can be eiher LispParser.UPCASE, LispParser.DOWNCASE or LispParser.PRESERVE

Returns:
UPCASE, DOWNCASE or PRESERVE

setCaseSensitivity

public void setCaseSensitivity(int caseSensitivity)
Sets the current case-sensitivity of the parser. It can be eiher LispParser.UPCASE, LispParser.DOWNCASE or LispParser.PRESERVE


getInputReader

public PushbackReader getInputReader()
Gets the current reader to be parsed.


setInputReader

public void setInputReader(PushbackReader inputReader)
Sets the input reader for the Parser.


setInputString

public void setInputString(String s)
Sets the input string for the parser. This is the String to parse.


parse

public LispValue parse()
                throws EOFException
Parse() assumes that there is only one expression in the input string or file. If you need to read multiple items from a string or file, use the read() function. Parse just calls read right now.

Throws:
EOFException
See Also:
read()

read

public LispValue read()
               throws EOFException
Reads one s-expression from the input stream (a string or file). Throws an EOFxception when EOF is reached. Call this method repeatedly to do read-eval-print on a file.

Throws:
EOFException

read_list_token

public LispValue read_list_token(PushbackReader stream)
                          throws EOFException
Reads one list expression from the input stream and returns it. The input pointer should be on the character following the left parenthesis.

Throws:
EOFException

read_quoted_token

LispValue read_quoted_token(PushbackReader stream)
                      throws EOFException
This routine is called by parse when it encounters a quote mark. It calls parse recursively.

Throws:
EOFException

read_backquoted_token

public LispValue read_backquoted_token(PushbackReader stream)
                                throws EOFException
This routine is called by parse when it encounters a backquote mark. It calls parse recursively.

Throws:
EOFException

read_comma_token

LispValue read_comma_token(PushbackReader stream)
                     throws EOFException
This routine is called by parse when it encounters a comma, which is only legal inside a backquote.

Throws:
EOFException

applyReaderMacro

public LispValue applyReaderMacro(PushbackReader stream)
                           throws EOFException
This routine is called by parse when it encounters a pound (#) mark.

Throws:
EOFException

read_backquoted_list_token

public LispValue read_backquoted_list_token(PushbackReader stream)
This library can't read backquotes yet.


tokenToLispValue

public LispValue tokenToLispValue(String token)
Converts a string to a LISP value such as NIL, T, an integer, a real number, a string or a symbol.


isLparen

boolean isLparen(char x)

isRparen

boolean isRparen(char x)

isAtSign

boolean isAtSign(char x)

isBackQuote

boolean isBackQuote(char x)

isBackSlash

boolean isBackSlash(char x)

isColon

boolean isColon(char x)

isComma

boolean isComma(char x)

isDoubleQuote

boolean isDoubleQuote(char x)

isOrBar

boolean isOrBar(char x)

isPound

boolean isPound(char x)

isPeriod

boolean isPeriod(char x)

isQuote

boolean isQuote(char x)

isSemi

boolean isSemi(char x)

isLeftAngleBracket

boolean isLeftAngleBracket(char x)

isRightAngleBracket

boolean isRightAngleBracket(char x)

isSpace

boolean isSpace(char x)

isTerminator

boolean isTerminator(char x)

firstCharNotInSet

public static int firstCharNotInSet(int startIndex,
                                    String str,
                                    String charSet)
The equivalent of the C function 'strspn'. Given a string and another string representing a set of characters, this function scans the string and accepts characters that are elements of the given set of characters. It returns the index of the first element of the string that is not a member of the set of characters. For example: pos = firstCharNotInSet(0, "hello there, how are you?", "ehlort "); returns 11. If the string does not contain any of the characters in the set, str.length() is returned.


REAL_token_p

boolean REAL_token_p(String str)
Does NOT recognize an isolated '+' or '-' as a real number.


INTEGER_token_p

boolean INTEGER_token_p(String str)

NIL_token_p

boolean NIL_token_p(String str)

STRING_token_p

boolean STRING_token_p(String str)

SYMBOL_token_p

boolean SYMBOL_token_p(String str)

T_token_p

boolean T_token_p(String str)

test_parser

public void test_parser(String s)

test_parser_loop

public void test_parser_loop()
                      throws EOFException
Throws:
EOFException

hasBalancedParentheses

public static boolean hasBalancedParentheses(Jatha lisp,
                                             LispValue input)
Returns true if the input expression has balanced parentheses

Parameters:
input - a String
Returns:
true if it has balanced parentheses

hasBalancedParentheses

public static boolean hasBalancedParentheses(Jatha lisp,
                                             String input)
Returns true if the input expression has balanced parentheses

Parameters:
input - a String
Returns:
true if it has balanced parentheses

simple_parser_test

public void simple_parser_test()

Jatha Software