All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class parse.Parser

java.lang.Object
   |
   +----parse.Parser

public class Parser
extends Object
implements Serializable
Parse a file/URL/string, and return the Object it describes. As it finds a token in the file that is an identifier, it will interpret it as the name of a class, load and instantiate that class, and call methods on that class to find out what parameters it expects in the file. In this way, simply placing a new .class file (of type Parsable) in the directory will automatically extend the language that Parser parses. An object can be defined as part of several different languages. When parsing, and integer is passed in to select which language.

This code is (c) 1996 Leemon Baird <leemon@cs.cmu.edu>, http://www.cs.cmu.edu/~baird
The source and object code may be redistributed freely. If the code is modified, please state so in the comments.

Version:
1.0, 10 March 96
Author:
Leemon Baird

Variable Index

 o applet
the applet doing the parsing
 o isBoolean
 o isChar
 o isDouble
 o isEOF
 o isID
 o isInt
 o isString
 o labels
given an obect find its #DEF/#USE name (used by the unparser)
 o line
 o pos
 o tBoolean
 o tChar
 o tDouble
 o tID
 o tInt
 o tString

Constructor Index

 o Parser()
create a new parser with a blank symbol table
 o Parser(String)
create a new parser with a blank symbol table, and start parsing the string

Method Index

 o checkEOF()
Check to make sure parser is now at the end of the input stream.
 o close()
Close the file being parsed.
 o enterScope()
create a new hash table for symbols in a new scope.
 o error(String)
If an error occurs during parsing, call this to throw an exception and abort the parse.
 o exitScope()
Delete the most recent hash table when exiting a scope
 o getScannerMode()
Get the scanner's mode, such as 0=C, 1=HTML, 2=LISP.
 o getSymbol(String)
Return the object associated with a symbol.
 o getToken()
Gets the next token from the stream.
 o isOfType(Class, Class)
Is the specified Class of the specified Type? Checks whether the type is the same as the class, or a superclass of it, or an interface of it, and so on up the superclass/interface hierarchy.
 o parseBoolean(boolean)
If next token is a boolean, return it.
 o parseChar(char, boolean)
Consume next token if it is character c, else do nothing.
 o parseClass(String, int, boolean)
Loads a Parsable class from disk, instantiates it, calls its parse() method, and returns whatever parse() returns.
 o parseClass(String, Parsable, int, boolean)
Same as parseClass(className,lang,throwException), except that instead of instantiating a new object and parsing its parameters, it uses the preexisting object obj, and parses its parameters.
 o parseClassList(String, int, boolean)
Parse objects of class clss as long as possible, returning a vector of results.
 o parseDouble___(boolean)
If next token is a double, return it.
 o parseID(String, boolean)
Consume next token if it is the identifier id, else do nothing.
 o parseInt___(boolean)
If next token is an int, return it.
 o parseLong___(boolean)
If next token is a long, return it.
 o parseString(boolean)
If next token is a String, return it.
 o parseType(String, int, boolean)
A parsable class should call parseType during parsing if the next token to be parsed is an identifier representing the name of the class that will continue parsing from there.
 o parseTypeList(String, int, boolean)
Parse objects of type type as long as possible, returning a vector of results.
 o setScannerMode(int)
Set the scanner into a given mode such as C, HTML, or LISP tokens.
 o setSymbol(String, Object)
Associate a symbol with an object in the current scope.
 o startParseFile(String)
starts the parser at the beginning of the given file.
 o startParseInputStream(InputStream)
starts the parser at the beginning of the given InputStream.
 o startParseString(String)
starts the parser at the beginning of the given string.
 o startParseURL(String)
starts the parser at the beginning of the given URL.

Variables

 o applet
 public Applet applet
the applet doing the parsing

 o labels
 public Hashtable labels
given an obect find its #DEF/#USE name (used by the unparser)

 o isChar
 protected boolean isChar
 o isString
 protected boolean isString
 o isInt
 protected boolean isInt
 o isDouble
 protected boolean isDouble
 o isID
 protected boolean isID
 o isEOF
 protected boolean isEOF
 o isBoolean
 protected boolean isBoolean
 o tChar
 protected char tChar
 o tInt
 protected long tInt
 o tDouble
 protected double tDouble
 o tString
 protected String tString
 o tID
 protected String tID
 o tBoolean
 protected boolean tBoolean
 o line
 protected int line
 o pos
 protected int pos

Constructors

 o Parser
 public Parser()
create a new parser with a blank symbol table

 o Parser
 public Parser(String str)
create a new parser with a blank symbol table, and start parsing the string

Methods

 o startParseString
 public final boolean startParseString(String str)
starts the parser at the beginning of the given string. Returns true if successful, false if an error occurs.

 o startParseFile
 public final boolean startParseFile(String filename)
starts the parser at the beginning of the given file. Returns true if successful, false if the file doesn't exist or has a security error.

 o startParseURL
 public final boolean startParseURL(String url)
starts the parser at the beginning of the given URL. Returns true if successful, false if the URL doesn't exist or has a security error.

 o startParseInputStream
 public final boolean startParseInputStream(InputStream stream)
starts the parser at the beginning of the given InputStream. Returns true if successful, false if the URL doesn't exist or has a security error.

 o close
 public void close()
Close the file being parsed.

 o getToken
 public void getToken()
Gets the next token from the stream. It is stored in the Parser object. If there are no more tokens, it will set isEOF to true.

 o checkEOF
 public void checkEOF() throws ParserException
Check to make sure parser is now at the end of the input stream. If not, and exception is raised saying EOF was expected

 o parseChar
 public boolean parseChar(char c,
                          boolean throwException) throws ParserException
Consume next token if it is character c, else do nothing. Throw an exception if throwException and it is not the character.

Throws: ParserException
parser didn't find the required token
 o parseID
 public boolean parseID(String id,
                        boolean throwException) throws ParserException
Consume next token if it is the identifier id, else do nothing. Throw an exception if throwException and it is not the identifier id.

Throws: ParserException
parser didn't find the required token
 o parseDouble___
 public double parseDouble___(boolean throwException) throws ParserException
If next token is a double, return it. Throw an exception if throwException and it is not a double.

Throws: ParserException
parser didn't find the required token
 o parseBoolean
 public boolean parseBoolean(boolean throwException) throws ParserException
If next token is a boolean, return it. Throw an exception if throwException and it is not a boolean.

Throws: ParserException
parser didn't find the required token
 o parseInt___
 public int parseInt___(boolean throwException) throws ParserException
If next token is an int, return it. Throw an exception if throwException and it is not an int.

Throws: ParserException
parser didn't find the required token
 o parseLong___
 public long parseLong___(boolean throwException) throws ParserException
If next token is a long, return it. Throw an exception if throwException and it is not an int.

Throws: ParserException
parser didn't find the required token
 o parseString
 public String parseString(boolean throwException) throws ParserException
If next token is a String, return it. Throw an exception if throwException and it is not a String.

Throws: ParserException
parser didn't find the required token
 o parseClass
 public Parsable parseClass(String className,
                            int lang,
                            boolean throwException) throws ParserException
Loads a Parsable class from disk, instantiates it, calls its parse() method, and returns whatever parse() returns. If there is an error and throwException=false, then what it returns is undefined (so don't assume that it will return null). This works by either using the parameters array that the object returns, or by calling its parse() method. The parse() method is called only if the parameters returned were null.

Throws: ParserException
parser didn't find the required token
 o parseClass
 public Parsable parseClass(String className,
                            Parsable obj,
                            int lang,
                            boolean throwException) throws ParserException
Same as parseClass(className,lang,throwException), except that instead of instantiating a new object and parsing its parameters, it uses the preexisting object obj, and parses its parameters. The object obj must of class className. If there is a #USE command in the HTML file, then that object will be used instead of obj.

Throws: ParserException
parser didn't find the required token
 o parseType
 public Parsable parseType(String type,
                           int lang,
                           boolean throwException) throws ParserException
A parsable class should call parseType during parsing if the next token to be parsed is an identifier representing the name of the class that will continue parsing from there. That object must be of the specified type, or it is illegal. Illegal objects either cause parseClass to return null or to throw an exception, depending on whether throwException is false or true respectively. If a class is called a.b.Name, then it can be found by asking for name, Name, or a.b.Name (all three work). The actual class name must not start with a lower case. If there is an error and throwException=false, then what it returns is undefined (so don't assume that it will return null).

Throws: ParserException
parser didn't find the required token
 o parseClassList
 public Vector parseClassList(String clss,
                              int lang,
                              boolean throwException) throws ParserException
Parse objects of class clss as long as possible, returning a vector of results. If throwException, then an empty list is considered an error

Throws: ParserException
parser didn't find the required token
 o parseTypeList
 public Vector parseTypeList(String type,
                             int lang,
                             boolean throwException) throws ParserException
Parse objects of type type as long as possible, returning a vector of results. If throwException, then an empty list is considered an error. Type must be complete (e.g. myPackage.myType) not abbreviated (e.g. myType).

Throws: ParserException
parser didn't find the required token
 o error
 public void error(String expected) throws ParserException
If an error occurs during parsing, call this to throw an exception and abort the parse. The message will say the filename, what was expected (which is passed in), and the line and position of the error within that file.

Throws: ParserException
parser didn't find the required token
 o isOfType
 public static final boolean isOfType(Class clss,
                                      Class type)
Is the specified Class of the specified Type? Checks whether the type is the same as the class, or a superclass of it, or an interface of it, and so on up the superclass/interface hierarchy.

 o setSymbol
 public void setSymbol(String sym,
                       Object obj)
Associate a symbol with an object in the current scope.

 o getSymbol
 public Parsable getSymbol(String sym)
Return the object associated with a symbol. Returns one from the innermost (most recent) scope if it is multiply defined. Return null if it is not defined. Multiple calls to this return pointers to the same object.

 o enterScope
 public void enterScope()
create a new hash table for symbols in a new scope.

 o exitScope
 public void exitScope()
Delete the most recent hash table when exiting a scope

 o setScannerMode
 public void setScannerMode(int mode)
Set the scanner into a given mode such as C, HTML, or LISP tokens.

 o getScannerMode
 public int getScannerMode()
Get the scanner's mode, such as 0=C, 1=HTML, 2=LISP.


All Packages  Class Hierarchy  This Package  Previous  Next  Index