transcendental-lisp/main/LispScannerDriver.java

70 lines
2.4 KiB
Java

/*
* Name: Mike Cifelli
* Course: CIS 443 - Programming Languages
* Assignment: Lisp Interpreter Phase 1 - Lexical Analysis
*/
package main;
import scanner.*;
import error.ErrorManager;
import java.io.*;
/**
* <code>LispScannerDriver</code> is a program that takes the name of a file
* as a command-line argument, retrieves all of the Lisp tokens from the file
* and prints them to the console. If no file name is provided at the
* command-line, this program will read from standard input.
*/
public class LispScannerDriver {
/**
* Obtain the Lisp tokens from the file whose name was given as a
* command-line argument and print them to the console. If no file name was
* given, retrieve the tokens from standard input.
*
* @param args
* the command-line arguments:
* <ul>
* <li><code>args[0]</code> - file name (optional)</li>
* </ul>
*/
public static void main(String[] args) {
LispScanner in = null;
if (args.length > 0) {
// a file name was given at the command-line, attempt to create a
// 'LispScanner' on it
try {
in = new LispScanner(new FileInputStream(args[0]), args[0]);
} catch (FileNotFoundException e) {
ErrorManager.generateError(e.getMessage(),
ErrorManager.CRITICAL_LEVEL);
}
} else {
// no file name was given, create a 'LispScanner' on standard input
in = new LispScanner(System.in, "System.in");
}
Token t = null;
do {
try {
t = in.nextToken();
System.out.printf("%-15s%-25s%5d%5d%25s\n", t.getType(),
t.getText(),
t.getLine(),
t.getColumn(),
t.getFName());
} catch (RuntimeException e) {
ErrorManager.generateError(e.getMessage(), 2);
} catch (IOException e) {
ErrorManager.generateError(e.getMessage(),
ErrorManager.CRITICAL_LEVEL);
}
} while ((t == null) || (t.getType() != Token.Type.EOF));
}
}