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
-
applet
- the applet doing the parsing
-
isBoolean
-
-
isChar
-
-
isDouble
-
-
isEOF
-
-
isID
-
-
isInt
-
-
isString
-
-
labels
- given an obect find its #DEF/#USE name (used by the unparser)
-
line
-
-
pos
-
-
tBoolean
-
-
tChar
-
-
tDouble
-
-
tID
-
-
tInt
-
-
tString
-
-
Parser()
- create a new parser with a blank symbol table
-
Parser(String)
- create a new parser with a blank symbol table, and start parsing the string
-
checkEOF()
- Check to make sure parser is now at the end of the input stream.
-
close()
- Close the file being parsed.
-
enterScope()
- create a new hash table for symbols in a new scope.
-
error(String)
- If an error occurs during parsing, call this to throw an exception
and abort the parse.
-
exitScope()
- Delete the most recent hash table when exiting a scope
-
getScannerMode()
- Get the scanner's mode, such as 0=C, 1=HTML, 2=LISP.
-
getSymbol(String)
- Return the object associated with a symbol.
-
getToken()
- Gets the next token from the stream.
-
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.
-
parseBoolean(boolean)
- If next token is a boolean, return it.
-
parseChar(char, boolean)
- Consume next token if it is character c, else do nothing.
-
parseClass(String, int, boolean)
- Loads a Parsable class from disk, instantiates it,
calls its parse() method, and returns whatever parse() returns.
-
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.
-
parseClassList(String, int, boolean)
- Parse objects of class clss as long as possible, returning a vector of results.
-
parseDouble___(boolean)
- If next token is a double, return it.
-
parseID(String, boolean)
- Consume next token if it is the identifier id, else do nothing.
-
parseInt___(boolean)
- If next token is an int, return it.
-
parseLong___(boolean)
- If next token is a long, return it.
-
parseString(boolean)
- If next token is a String, return it.
-
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.
-
parseTypeList(String, int, boolean)
- Parse objects of type type as long as possible, returning a vector of results.
-
setScannerMode(int)
- Set the scanner into a given mode such as C, HTML, or LISP tokens.
-
setSymbol(String, Object)
- Associate a symbol with an object in the current scope.
-
startParseFile(String)
- starts the parser at the beginning of the given file.
-
startParseInputStream(InputStream)
- starts the parser at the beginning of the given InputStream.
-
startParseString(String)
- starts the parser at the beginning of the given string.
-
startParseURL(String)
- starts the parser at the beginning of the given URL.
applet
public Applet applet
- the applet doing the parsing
labels
public Hashtable labels
- given an obect find its #DEF/#USE name (used by the unparser)
isChar
protected boolean isChar
isString
protected boolean isString
isInt
protected boolean isInt
isDouble
protected boolean isDouble
isID
protected boolean isID
isEOF
protected boolean isEOF
isBoolean
protected boolean isBoolean
tChar
protected char tChar
tInt
protected long tInt
tDouble
protected double tDouble
tString
protected String tString
tID
protected String tID
tBoolean
protected boolean tBoolean
line
protected int line
pos
protected int pos
Parser
public Parser()
- create a new parser with a blank symbol table
Parser
public Parser(String str)
- create a new parser with a blank symbol table, and start parsing the string
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.
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.
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.
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.
close
public void close()
- Close the file being parsed.
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
setSymbol
public void setSymbol(String sym,
Object obj)
- Associate a symbol with an object in the current scope.
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.
enterScope
public void enterScope()
- create a new hash table for symbols in a new scope.
exitScope
public void exitScope()
- Delete the most recent hash table when exiting a scope
setScannerMode
public void setScannerMode(int mode)
- Set the scanner into a given mode such as C, HTML, or LISP tokens.
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