//============================================================================== // TestLexer.java //============================================================================== package tribble.net.ftp.shell; // System imports import java.io.FileReader; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.io.Reader; import java.lang.Character; import java.lang.Integer; import java.lang.Exception; import java.lang.String; import java.lang.System; /******************************************************************************* * Test driver for the lexer methods of class {@link CommandParser}. * * * @version $Revision: 1.4 $ $Date: 2007/03/27 23:47:52 $ * @since API 1.0, 2007-03-14 * @author David R. Tribble (david@tribble.com). *

* Copyright ©2007 by David R. Tribble, all rights reserved.
* Permission is granted to any person or entity except those designated by * by the United States Department of State as a terrorist, or terrorist * government or agency, to use and distribute this source code provided * that the original copyright notice remains present and unaltered. */ abstract class TestLexer { // Identification /** Revision information. */ static final String REV = "@(#)tribble/net/ftp/shell/TestLexer.java $Revision: 1.4 $ $Date: 2007/03/27 23:47:52 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static variables /** Standard output stream. */ private static PrintWriter s_stdout = new PrintWriter(System.out, true); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static methods /*************************************************************************** * Test driver for class {@link CommandParser}. * *

* Usage *

* * java tribble.net.ftp.shell.TestLexer [-c] file * * *

* Options: *

*
-c *
Read characters, not tokens, from the input file. * *
-t (optional) *
Read tokens, not characters, from the input file. * (This is the default.) *
* *

* The file is a text file containing FTP commands. * * @since 1.1, 2007-03-14 */ public static void main(String[] args) throws Exception { String fname = null; Reader in = null; boolean charOnly = false; int i; // Check args for (i = 0; i < args.length && args[i].charAt(0) == '-'; i++) { if (args[i].equals("-c")) charOnly = true; else if (args[i].equals("-t")) charOnly = false; else if (args[i].equals("-")) break; else i = args.length; } // Check usage if (i >= args.length) { // Display a usage message System.out.println("Lexically scan an FTP command script file."); System.out.println(); System.out.println("usage: java " + TestLexer.class.getName() + " [-c] file"); System.exit(255); } // Test the lexer methods try { // Open the command input stream fname = args[i]; if (fname.equals("-")) in = new InputStreamReader(System.in); else in = new FileReader(fname); // Parse the command script file if (charOnly) readChars(in, fname); else readTokens(in, fname); } finally { // Clean up if (in != null && !fname.equals("-")) in.close(); } } /*************************************************************************** * Test driver helper method, to read characters from the input lexer. * * @since 1.4, 2007-03-27 (1.3, 2007-03-15) */ private static void readChars(Reader in, String fname) throws Exception { CommandFile charIn; int nReads = 0; int nChars = 0; int lineNo = 1; // Read characters from the input stream charIn = new CommandFile(in, s_stdout, null); charIn.setSourceName(fname); for (;;) { int ch; int prevLine; // Read an input char prevLine = lineNo; lineNo = charIn.m_lineNo; ch = charIn.readChar(); nReads++; if (ch < 0) { System.out.println(nChars + ". " + lineNo + ": eof"); System.out.println(" eof" + (prevLine != lineNo ? "" : " <")); break; } else if (ch == '\n') { System.out.println(nChars + ". " + lineNo + ": eoln"); System.out.println(" eoln" + (prevLine != lineNo ? "" : " <")); } else { nChars++; System.out.print(nChars + ". " + lineNo + ": "); if (Character.isISOControl((char) ch)) { String s; s = (ch < 0x10 ? "0" : "") + Integer.toHexString(ch); s = s.toUpperCase(); System.out.println("0x" + s); } else System.out.println("'" + (char) ch + "'"); } if (ch >= 0 && nReads%10 == 0) { System.out.println(" unread 0x" + Integer.toHexString(ch)); charIn.unReadChar(ch); nChars--; } System.out.flush(); } System.out.println(); System.out.println("Lines: " + (lineNo-1) + ", characters: " + nChars); } /*************************************************************************** * Test driver helper method, to read tokens from the input lexer. * * @since 1.4, 2007-03-27 (1.3, 2007-03-15) */ private static void readTokens(Reader in, String fname) throws Exception { CommandLexer lexer; int nReads = 0; int nToks = 0; int lineNo; // Read tokens from the input stream lexer = new CommandLexer(in, s_stdout); lexer.setSourceName(fname); for (;;) { String tok; // Read an input token tok = lexer.readToken(); lineNo = lexer.m_lineNo; nReads++; System.out.print((nToks+1) + ". " + lineNo + ": "); if (tok == null) { System.out.println("eof"); break; } else { nToks++; System.out.println("[" + tok + "]"); } if (tok != null && nReads%10 == 0) { System.out.println(" unread"); lexer.unReadToken(tok); nToks--; } System.out.flush(); } System.out.println(); System.out.println("Lines: " + (lineNo-1) + ", tokens: " + nToks); } } // End TestLexer.java