Formatted the files in the scanner package
This commit is contained in:
parent
dd8f4172e4
commit
4a5f169076
|
@ -1,8 +1,8 @@
|
|||
package scanner;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Removes Lisp comments from an input stream.
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
|
||||
package scanner;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* A <code>LispScanner</code> converts a stream of bytes into a stream of Lisp
|
||||
|
@ -29,10 +29,10 @@ public class LispScanner {
|
|||
* specified input stream.
|
||||
*
|
||||
* @param in
|
||||
* the input stream to obtain Lisp tokens from (must not be
|
||||
* <code>null</code>)
|
||||
* the input stream to obtain Lisp tokens from (must not be
|
||||
* <code>null</code>)
|
||||
* @param fileName
|
||||
* the name of the file that <code>in</code> is reading from
|
||||
* the name of the file that <code>in</code> is reading from
|
||||
*/
|
||||
public LispScanner(InputStream in, String fileName) {
|
||||
this.inStream = new LispFilterInputStream(new BufferedInputStream(in));
|
||||
|
@ -48,9 +48,9 @@ public class LispScanner {
|
|||
* to <code>nextToken</code> have been made yet, this method returns
|
||||
* <code>null</code>.
|
||||
*
|
||||
* @return
|
||||
* the last Lisp token returned from this scanner or <code>null</code> (if
|
||||
* no tokens have been returned from this scanner yet)
|
||||
* @return the last Lisp token returned from this scanner or
|
||||
* <code>null</code> (if no tokens have been returned from this
|
||||
* scanner yet)
|
||||
*/
|
||||
public Token getCurrToken() {
|
||||
return currToken;
|
||||
|
@ -59,13 +59,13 @@ public class LispScanner {
|
|||
/**
|
||||
* Returns the next Lisp token from this scanner.
|
||||
*
|
||||
* @return
|
||||
* the next Lisp token from this scanner.
|
||||
* @return the next Lisp token from this scanner.
|
||||
* @throws RuntimeException
|
||||
* Indicates that an illegal character or an unterminated quoted string
|
||||
* was encountered in the input stream (not counting comments).
|
||||
* Indicates that an illegal character or an unterminated quoted
|
||||
* string was encountered in the input stream (not counting
|
||||
* comments).
|
||||
* @throws IOException
|
||||
* Indicates that an I/O error has occurred.
|
||||
* Indicates that an I/O error has occurred.
|
||||
*/
|
||||
public Token nextToken() throws IOException {
|
||||
currToken = retrieveNextToken();
|
||||
|
@ -78,8 +78,8 @@ public class LispScanner {
|
|||
// Returns: the next Lisp token found in 'inStream'
|
||||
// Precondition: 'inStream' must not be null.
|
||||
// Throws: RuntimeException - Indicates that an illegal character or an
|
||||
// unterminated quoted string was encountered in
|
||||
// 'inStream'.
|
||||
// unterminated quoted string was encountered in
|
||||
// 'inStream'.
|
||||
// Throws: IOException - Indicates that an I/O error has occurred.
|
||||
private Token retrieveNextToken() throws IOException {
|
||||
int c;
|
||||
|
@ -100,39 +100,25 @@ public class LispScanner {
|
|||
|
||||
break;
|
||||
case '(':
|
||||
return new Token(Token.Type.LEFT_PAREN,
|
||||
"(",
|
||||
fileName,
|
||||
line,
|
||||
column);
|
||||
return new Token(Token.Type.LEFT_PAREN, "(", fileName, line, column);
|
||||
case ')':
|
||||
return new Token(Token.Type.RIGHT_PAREN,
|
||||
")",
|
||||
fileName,
|
||||
line,
|
||||
column);
|
||||
return new Token(Token.Type.RIGHT_PAREN, ")", fileName, line, column);
|
||||
case '\'':
|
||||
return new Token(Token.Type.QUOTE_MARK,
|
||||
"\'",
|
||||
fileName,
|
||||
line,
|
||||
column);
|
||||
return new Token(Token.Type.QUOTE_MARK, "\'", fileName, line, column);
|
||||
case '\"':
|
||||
return retrieveString(nextChar);
|
||||
default:
|
||||
if (Character.isWhitespace(nextChar)) { // skip whitespace
|
||||
if (Character.isWhitespace(nextChar)) { // skip whitespace
|
||||
continue;
|
||||
} else if (Character.isDigit(nextChar)) { // number
|
||||
} else if (Character.isDigit(nextChar)) { // number
|
||||
return retrieveNumber(nextChar);
|
||||
} else if (isLegalIdChar(nextChar)) { // identifier
|
||||
} else if (isLegalIdChar(nextChar)) { // identifier
|
||||
return retrieveIdentifier(nextChar);
|
||||
} else {
|
||||
// 'nextChar' can not start any Lisp token
|
||||
|
||||
throw new RuntimeException("illegal character " +
|
||||
"\'" + nextChar + "\'" +
|
||||
" - line " + line +
|
||||
" column " + column);
|
||||
throw new RuntimeException(
|
||||
"illegal character " + "\'" + nextChar + "\'" + " - line " + line + " column " + column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -145,14 +131,14 @@ public class LispScanner {
|
|||
// Retrieve a quoted string token from 'inStream'.
|
||||
//
|
||||
// Parameters: firstDoubleQuote - the opening double quote of this quoted
|
||||
// string
|
||||
// string
|
||||
// Returns: a quoted string token obtained from 'instream'
|
||||
// Throws: RuntimeException - Indicates that this quoted string was
|
||||
// missing its terminating double quote.
|
||||
// missing its terminating double quote.
|
||||
// Throws: IOException - Indicates that an I/O error has occurred.
|
||||
// Precondition: 'firstDoubleQuote' must be the leading double quote
|
||||
// character of this quoted string and 'inStream' must not
|
||||
// be null.
|
||||
// character of this quoted string and 'inStream' must not
|
||||
// be null.
|
||||
private Token retrieveString(char firstDoubleQuote) throws IOException {
|
||||
StringBuffer text = new StringBuffer();
|
||||
int startLine = line;
|
||||
|
@ -169,24 +155,20 @@ public class LispScanner {
|
|||
++column;
|
||||
text.append(nextChar);
|
||||
|
||||
switch(nextChar) {
|
||||
case '\n':
|
||||
++line;
|
||||
column = 0;
|
||||
switch (nextChar) {
|
||||
case '\n':
|
||||
++line;
|
||||
column = 0;
|
||||
|
||||
break;
|
||||
case '\"':
|
||||
if (prevChar != '\\') {
|
||||
// we have found the terminating double quote
|
||||
break;
|
||||
case '\"':
|
||||
if (prevChar != '\\') {
|
||||
// we have found the terminating double quote
|
||||
|
||||
return new Token(Token.Type.STRING,
|
||||
text.toString(),
|
||||
fileName,
|
||||
startLine,
|
||||
startColumn);
|
||||
}
|
||||
return new Token(Token.Type.STRING, text.toString(), fileName, startLine, startColumn);
|
||||
}
|
||||
|
||||
// this is an escaped double quote
|
||||
// this is an escaped double quote
|
||||
}
|
||||
|
||||
prevChar = nextChar;
|
||||
|
@ -195,9 +177,7 @@ public class LispScanner {
|
|||
// the end of 'inStream' was reached before the terminating double
|
||||
// quote
|
||||
|
||||
throw new RuntimeException("unterminated quoted string" +
|
||||
" - line " + startLine +
|
||||
" column " + startColumn);
|
||||
throw new RuntimeException("unterminated quoted string" + " - line " + startLine + " column " + startColumn);
|
||||
}
|
||||
|
||||
// Retrieve a number token from 'inStream'.
|
||||
|
@ -206,7 +186,7 @@ public class LispScanner {
|
|||
// Returns: a number token obtained from 'inStream'
|
||||
// Throws: IOException - Indicates that an I/O error has occurred.
|
||||
// Precondition: 'firstDigit' must be the first digit of this number and
|
||||
// 'inStream' must not be null.
|
||||
// 'inStream' must not be null.
|
||||
private Token retrieveNumber(char firstDigit) throws IOException {
|
||||
StringBuffer text = new StringBuffer();
|
||||
int startColumn = column;
|
||||
|
@ -227,13 +207,9 @@ public class LispScanner {
|
|||
} else {
|
||||
// we have reached the end of the number
|
||||
|
||||
inStream.reset(); // unread the last character
|
||||
inStream.reset(); // unread the last character
|
||||
|
||||
return new Token(Token.Type.NUMBER,
|
||||
text.toString(),
|
||||
fileName,
|
||||
line,
|
||||
startColumn);
|
||||
return new Token(Token.Type.NUMBER, text.toString(), fileName, line, startColumn);
|
||||
}
|
||||
|
||||
inStream.mark(1);
|
||||
|
@ -242,11 +218,7 @@ public class LispScanner {
|
|||
// there are no more bytes to be read from 'inStream' after this number
|
||||
// token
|
||||
|
||||
return new Token(Token.Type.NUMBER,
|
||||
text.toString(),
|
||||
fileName,
|
||||
line,
|
||||
startColumn);
|
||||
return new Token(Token.Type.NUMBER, text.toString(), fileName, line, startColumn);
|
||||
}
|
||||
|
||||
// Retrieve an identifier token from 'inStream'.
|
||||
|
@ -255,7 +227,7 @@ public class LispScanner {
|
|||
// Returns: an identifier token obtained from 'inStream'
|
||||
// Throws: IOException - Indicates that an I/O error has occurred.
|
||||
// Precondition: 'firsChar' must be the first character of this identifier
|
||||
// and 'inStream' must not be null.
|
||||
// and 'inStream' must not be null.
|
||||
private Token retrieveIdentifier(char firstChar) throws IOException {
|
||||
StringBuffer text = new StringBuffer();
|
||||
int startColumn = column;
|
||||
|
@ -276,13 +248,9 @@ public class LispScanner {
|
|||
} else {
|
||||
// we have reached the end of this identifier
|
||||
|
||||
inStream.reset(); // unread the last character
|
||||
inStream.reset(); // unread the last character
|
||||
|
||||
return new Token(Token.Type.IDENTIFIER,
|
||||
text.toString(),
|
||||
fileName,
|
||||
line,
|
||||
startColumn);
|
||||
return new Token(Token.Type.IDENTIFIER, text.toString(), fileName, line, startColumn);
|
||||
}
|
||||
|
||||
inStream.mark(1);
|
||||
|
@ -291,30 +259,17 @@ public class LispScanner {
|
|||
// there are no more bytes to be read from 'inStream' after this
|
||||
// identifier token
|
||||
|
||||
return new Token(Token.Type.IDENTIFIER,
|
||||
text.toString(),
|
||||
fileName,
|
||||
line,
|
||||
startColumn);
|
||||
return new Token(Token.Type.IDENTIFIER, text.toString(), fileName, line, startColumn);
|
||||
}
|
||||
|
||||
// Test if a character is legal to be contained within an identifier in
|
||||
// Lisp.
|
||||
//
|
||||
// Returns: 'true' if the character can be found within an identifier in
|
||||
// Lisp; 'false' otherwise
|
||||
// Lisp; 'false' otherwise
|
||||
private boolean isLegalIdChar(char c) {
|
||||
return ((! Character.isWhitespace(c)) && (c != '\"')
|
||||
&& (c != '\'')
|
||||
&& (c != '\\')
|
||||
&& (c != '`')
|
||||
&& (c != '(')
|
||||
&& (c != ')')
|
||||
&& (c != '[')
|
||||
&& (c != ']')
|
||||
&& (c != '#')
|
||||
&& (c != '.')
|
||||
&& (c != ';'));
|
||||
return ((!Character.isWhitespace(c)) && (c != '\"') && (c != '\'') && (c != '\\') && (c != '`') && (c != '(')
|
||||
&& (c != ')') && (c != '[') && (c != ']') && (c != '#') && (c != '.') && (c != ';'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -49,15 +49,15 @@ public class Token {
|
|||
* and column number.
|
||||
*
|
||||
* @param type
|
||||
* the type of this token
|
||||
* the type of this token
|
||||
* @param text
|
||||
* the text associated with this token
|
||||
* the text associated with this token
|
||||
* @param fName
|
||||
* the name of the file that this token is located in
|
||||
* the name of the file that this token is located in
|
||||
* @param line
|
||||
* the line number that this token is found on
|
||||
* the line number that this token is found on
|
||||
* @param column
|
||||
* the column number that this token is found on
|
||||
* the column number that this token is found on
|
||||
*/
|
||||
public Token(Type type, String text, String fName, int line, int column) {
|
||||
this.type = type;
|
||||
|
@ -70,8 +70,7 @@ public class Token {
|
|||
/**
|
||||
* Accessor method to determine the type of this token.
|
||||
*
|
||||
* @return
|
||||
* the type of this token
|
||||
* @return the type of this token
|
||||
*/
|
||||
public Type getType() {
|
||||
return type;
|
||||
|
@ -80,8 +79,7 @@ public class Token {
|
|||
/**
|
||||
* Accessor method to determine the text associated with this token.
|
||||
*
|
||||
* @return
|
||||
* the text associated with this token
|
||||
* @return the text associated with this token
|
||||
*/
|
||||
public String getText() {
|
||||
return text;
|
||||
|
@ -91,8 +89,7 @@ public class Token {
|
|||
* Accessor method to determine the name of the file that this token was
|
||||
* located in.
|
||||
*
|
||||
* @return
|
||||
* the name of the file that this token was located in
|
||||
* @return the name of the file that this token was located in
|
||||
*/
|
||||
public String getFName() {
|
||||
return fName;
|
||||
|
@ -102,8 +99,7 @@ public class Token {
|
|||
* Accessor method to determine the line number that this token was found
|
||||
* on.
|
||||
*
|
||||
* @return
|
||||
* the line number this token was found on
|
||||
* @return the line number this token was found on
|
||||
*/
|
||||
public int getLine() {
|
||||
return line;
|
||||
|
@ -113,8 +109,7 @@ public class Token {
|
|||
* Accessor method to determine the column number that this token was found
|
||||
* on.
|
||||
*
|
||||
* @return
|
||||
* the column number this token was found on
|
||||
* @return the column number this token was found on
|
||||
*/
|
||||
public int getColumn() {
|
||||
return column;
|
||||
|
|
Loading…
Reference in New Issue