tribble.net.ftp.shell
Class CommandParser

java.lang.Object
  extended by tribble.net.ftp.shell.CommandParser
All Implemented Interfaces:
CommandCodes, CommandTokens, VarNames

 class CommandParser
extends java.lang.Object
implements CommandCodes, CommandTokens, VarNames

FTP command script parser. Parses a command script text input stream containing FTP commands into a parse tree.

The parser uses a recursive-descent LL(1) parsing algorithm.

See the package summary for details about syntax and lexicon.

Example

  Loop1:
    foreach *.tar
    {
        get $F
        if ($Error != 0)
            continue Loop1
        del $F
        !tar -xf $F
        !del $F
    }

The example script above parses into the following command parse tree:

  1  (block "file")
  2    (filename "foo.ftp"
  3    (label "loop1")
  4    (foreach &1 "remote" "*.tar" 0 nil)
  5      (get &1 "$F")
  6      (if (!= "$Error" "0"))
  7        (continue "loop1")
  8      (del &1 "$F")
  9      (! "tar" "-xf" "$F")
 10      (! "del" "$F")
Source code:
Available at: http://david.tribble.com/src/java/tribble/net/ftp/shell/CommandParser.java
Documentation:
Available at: http://david.tribble.com/docs/tribble/net/ftp/shell/CommandParser.html

Since:
API 1.0, 2007-03-09
Version:
$Revision: 1.57 $ $Date: 2008/09/14 17:48:49 $
Author:
David R. Tribble (david@tribble.com).

Copyright ©2007-2008 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.

See Also:
CommandLexer, CommandTokens, CommandNode, BlockDef

Field Summary
(package private)  int m_lineNo
          Source line number of the last token read.
(package private)  int m_nErrors
          Errors produced.
(package private)  int m_nWarnings
          Warnings produced.
(package private) static java.lang.String REV
          Revision information.
 
Fields inherited from interface tribble.net.ftp.shell.CommandCodes
CMD__UNDEF, CMD_ADD, CMD_AND, CMD_APPEND, CMD_BINARY, CMD_BLOCK, CMD_BREAK, CMD_BUILTIN, CMD_CALL, CMD_CDUP, CMD_CHDIR, CMD_CLOSE, CMD_CONCAT, CMD_COND, CMD_CONNECT, CMD_CONTINUE, CMD_DEBUG, CMD_DECR, CMD_DELETE, CMD_DIR, CMD_DIV, CMD_ECHO, CMD_EQ, CMD_EXEC, CMD_EXIT, CMD_FALSE, CMD_FILENAME, CMD_FOR, CMD_FOREACH, CMD_FUNC, CMD_FUNC_FORMAT, CMD_FUNC_INDEX, CMD_FUNC_LCASE, CMD_FUNC_LEN, CMD_FUNC_NORM, CMD_FUNC_REPL, CMD_FUNC_RINDEX, CMD_FUNC_SUB, CMD_FUNC_TRIM, CMD_FUNC_UCASE, CMD_GE, CMD_GET, CMD_GLOB, CMD_GOTO, CMD_GT, CMD_HELP, CMD_IF, CMD_INCR, CMD_LABEL, CMD_LCHDIR, CMD_LE, CMD_LOCAL, CMD_LT, CMD_MATCH, CMD_MGET, CMD_MKDIR, CMD_MOD, CMD_MPUT, CMD_MUL, CMD_NE, CMD_NEG, CMD_NMATCH, CMD_NO_N, CMD_NOP, CMD_NOT, CMD_OPEN, CMD_OR, CMD_POS, CMD_PRINT, CMD_PUT, CMD_PWD, CMD_READ, CMD_REMOTE, CMD_RENAME, CMD_REPEAT, CMD_RETURN, CMD_RMDIR, CMD_SET, CMD_SHELL, CMD_SLEEP, CMD_STATUS, CMD_STREQ, CMD_SUB, CMD_SUBSCR, CMD_SYSTEM, CMD_TEST, CMD_TEST_DIR, CMD_TEST_EXEC, CMD_TEST_EXISTS, CMD_TEST_FILE, CMD_TEST_MODTIME, CMD_TEST_READ, CMD_TEST_SIZE, CMD_TEST_WRITE, CMD_TEXT, CMD_TIMEOUT, CMD_TOK, CMD_TRUE, CMD_USER, CMD_VAR, CMD_WAITFOR, CMD_WHILE, CMD_WRITE, CMD_X_INTERRUPT, CMD_X_SESSIONS, CMD_X_STACK, CMD_X_STOP, CMD_X_VARS, CMD_X_VERBOSE, VAL_EMPTY, VAL_FALSE, VAL_FALSE_WD, VAL_ONE, VAL_TRUE, VAL_TRUE_WD, VAL_ZERO
 
Fields inherited from interface tribble.net.ftp.shell.CommandTokens
TOK__NL, TOK_ADD, TOK_AMPER, TOK_AND, TOK_APP, TOK_APPEND, TOK_ASC, TOK_ASCII, TOK_BANG, TOK_BEGIN, TOK_BIN, TOK_BINARY, TOK_BREAK, TOK_BYE, TOK_CALL, TOK_CD, TOK_CDUP, TOK_CENT, TOK_CHDIR, TOK_CLOSE, TOK_COLON, TOK_COMMA, TOK_CONCAT, TOK_CONNECT, TOK_CONTINUE, TOK_DEL, TOK_DELETE, TOK_DIR, TOK_DISC, TOK_DISCONNECT, TOK_DIV, TOK_DIVIDE, TOK_DO, TOK_DOLLAR, TOK_ECHO, TOK_ELSE, TOK_END, TOK_EQ, TOK_EQ2, TOK_ERROR, TOK_EXEC, TOK_EXIT, TOK_FALSE, TOK_FOR, TOK_FOREACH, TOK_FORMAT, TOK_FUNC, TOK_GE, TOK_GET, TOK_GLOB, TOK_GOTO, TOK_GT, TOK_GT2, TOK_HELP, TOK_IF, TOK_IN, TOK_INDEX, TOK_LABEL, TOK_LB, TOK_LC, TOK_LCASE, TOK_LCD, TOK_LCHDIR, TOK_LE, TOK_LEN, TOK_LOCAL, TOK_LP, TOK_LS, TOK_LT, TOK_MATCH, TOK_MAX, TOK_MGET, TOK_MKDIR, TOK_MOD, TOK_MPUT, TOK_MUL, TOK_NE, TOK_NEG, TOK_NMATCH, TOK_NO_N, TOK_NOP, TOK_NORM, TOK_NOT, TOK_OPEN, TOK_OR, TOK_PING, TOK_PLUS, TOK_PORT, TOK_PRINT, TOK_PROC, TOK_PROMPT, TOK_PUT, TOK_PWD, TOK_QUES, TOK_QUIT, TOK_RB, TOK_RC, TOK_READ, TOK_REMOTE, TOK_REN, TOK_RENAME, TOK_REPEAT, TOK_REPL, TOK_RETURN, TOK_RINDEX, TOK_RMDIR, TOK_RP, TOK_SECT, TOK_SET, TOK_SHELL, TOK_SHIFT, TOK_SLEEP, TOK_STATUS, TOK_SUB, TOK_SUBSTR, TOK_SYSTEM, TOK_TEST_D, TOK_TEST_DIR, TOK_TEST_E, TOK_TEST_EXEC, TOK_TEST_EXISTS, TOK_TEST_F, TOK_TEST_FILE, TOK_TEST_M, TOK_TEST_MTIME, TOK_TEST_R, TOK_TEST_READ, TOK_TEST_SIZE, TOK_TEST_W, TOK_TEST_WRITE, TOK_TEST_X, TOK_TEST_Z, TOK_TIMEOUT, TOK_TIMES, TOK_TRIM, TOK_TRUE, TOK_UCASE, TOK_UP, TOK_USER, TOK_VAR, TOK_WHILE, TOK_WRITE, TOK_X_INTERRUPT, TOK_X_SESSIONS, TOK_X_STACK, TOK_X_STOP, TOK_X_VARS, TOK_X_VERBOSE
 
Fields inherited from interface tribble.net.ftp.shell.VarNames
VAR_CMDPORT, VAR_COMMANDCNT, VAR_DATAPORT, VAR_ERROR, VAR_ERRORCMD, VAR_ERRORCNT, VAR_ERRORMSG, VAR_FILE, VAR_FILE_DIR, VAR_FILE_DIRSEP, VAR_FILE_EXT, VAR_FILE_FILE, VAR_FILE_GROUP, VAR_FILE_HOST, VAR_FILE_NAME, VAR_FILE_OWNER, VAR_FILE_PATH, VAR_FILE_PERMS, VAR_FILE_SIZE, VAR_FILE_TYPE, VAR_FILENAME, VAR_HOST, VAR_LINENUM, VAR_LOCALDIR, VAR_PASSWORD, VAR_PORT, VAR_RANDOM, VAR_REMOTEDIR, VAR_SESSIONCNT, VAR_TIME, VAR_TIMEOUT, VAR_TZOFFSET, VAR_USER
 
Constructor Summary
CommandParser(java.io.Reader in)
          Constructor.
CommandParser(java.io.Reader in, java.io.Writer out)
          Constructor.
 
Method Summary
(package private)  CommandNode parse()
          Parse a command script.
(package private)  CommandNode parseAnExpr()
          Parse an expression.
(package private)  java.lang.String readToken()
          Read the next input token from the source script.
(package private)  void setSourceName(java.lang.String name)
          Establish the name of the source stream (the command script).
(package private)  void unReadToken(java.lang.String tok)
          Push back the last token read from the input source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REV

static final java.lang.String REV
Revision information.

See Also:
Constant Field Values

m_lineNo

int m_lineNo
Source line number of the last token read.


m_nErrors

int m_nErrors
Errors produced.


m_nWarnings

int m_nWarnings
Warnings produced.

Constructor Detail

CommandParser

CommandParser(java.io.Reader in)
Constructor.

Parameters:
in - Command script input stream.
Since:
1.1, 2007-03-09

CommandParser

CommandParser(java.io.Reader in,
              java.io.Writer out)
Constructor.

Parameters:
in - Command script input stream.
out - Error/warning message output stream.
Since:
1.4, 2007-03-14
Method Detail

setSourceName

void setSourceName(java.lang.String name)
Establish the name of the source stream (the command script).

Parameters:
name - Name of the input stream, typically a filename.
Since:
1.10, 2007-03-20

parse

CommandNode parse()
            throws java.text.ParseException,
                   java.io.IOException
Parse a command script.

Parameters:
in - Input stream containing the contents of the command script.
Returns:
Execution parse tree.
Throws:
java.text.ParseException
java.io.IOException
Since:
1.1, 2007-03-09

parseAnExpr

CommandNode parseAnExpr()
                  throws java.text.ParseException,
                         java.io.IOException
Parse an expression. Note: This is a helper method meant only for testing.

Throws:
java.text.ParseException
java.io.IOException
Since:
1.7, 2007-03-15

readToken

java.lang.String readToken()
                     throws java.io.IOException
Read the next input token from the source script. This also updates m_lineNo to reflect the source line number of the returned token.

Tokens are composed of simple keywords (e.g., get), or quoted literals (e.g., "*.txt", 'get.*'). Tokens may contain embedded variable sequences (e.g., "${name}").

Blank lines are ignored. Comments start with # and end at the end of the line (newline), and are ignored.

Tokens cannot be longer than 2,000 characters.

Returns:
The next token text, or CommandTokens.TOK__NL if an end-of-line (newline) was read, or null if the end of the source stream was reached. Note that the stream is not closed after the end is reached.
Throws:
java.io.IOException
Since:
1.1, 2007-03-11

unReadToken

void unReadToken(java.lang.String tok)
Push back the last token read from the input source.

Since:
1.2, 2007-03-13
See Also:
readToken()