Formatted the files in the scanner package

This commit is contained in:
Mike Cifelli 2016-12-08 16:12:29 -05:00
parent dd8f4172e4
commit 4a5f169076
3 changed files with 60 additions and 110 deletions

View File

@ -1,8 +1,8 @@
package scanner; package scanner;
import java.io.InputStream;
import java.io.FilterInputStream; import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
/** /**
* Removes Lisp comments from an input stream. * Removes Lisp comments from an input stream.

View File

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

View File

@ -49,15 +49,15 @@ public class Token {
* and column number. * and column number.
* *
* @param type * @param type
* the type of this token * the type of this token
* @param text * @param text
* the text associated with this token * the text associated with this token
* @param fName * @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 * @param line
* the line number that this token is found on * the line number that this token is found on
* @param column * @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) { public Token(Type type, String text, String fName, int line, int column) {
this.type = type; this.type = type;
@ -70,8 +70,7 @@ public class Token {
/** /**
* Accessor method to determine the type of this token. * Accessor method to determine the type of this token.
* *
* @return * @return the type of this token
* the type of this token
*/ */
public Type getType() { public Type getType() {
return type; return type;
@ -80,8 +79,7 @@ public class Token {
/** /**
* Accessor method to determine the text associated with this token. * Accessor method to determine the text associated with this token.
* *
* @return * @return the text associated with this token
* the text associated with this token
*/ */
public String getText() { public String getText() {
return text; return text;
@ -91,8 +89,7 @@ public class Token {
* Accessor method to determine the name of the file that this token was * Accessor method to determine the name of the file that this token was
* located in. * located in.
* *
* @return * @return the name of the file that this token was located in
* the name of the file that this token was located in
*/ */
public String getFName() { public String getFName() {
return fName; return fName;
@ -102,8 +99,7 @@ public class Token {
* Accessor method to determine the line number that this token was found * Accessor method to determine the line number that this token was found
* on. * on.
* *
* @return * @return the line number this token was found on
* the line number this token was found on
*/ */
public int getLine() { public int getLine() {
return line; return line;
@ -113,8 +109,7 @@ public class Token {
* Accessor method to determine the column number that this token was found * Accessor method to determine the column number that this token was found
* on. * on.
* *
* @return * @return the column number this token was found on
* the column number this token was found on
*/ */
public int getColumn() { public int getColumn() {
return column; return column;