/* Generated By:JavaCC: Do not edit this line. Turtle.java */ package org.turtleshell; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.turtleshell.tasks.*; import org.turtleshell.strings.*; /** * Turtle shell syntax parser ** * This class handles all of the main syntax parsing for turtle shell. * The main components to the grammar are defined in GenCommand() and * ControlBlock(). Each method contains detailed documentation about * what is supported by the language. * * The turtle shell language is meant to be a simplified cross-platform Java * implementation of a command line shell. Turtle shell follows closely the * general syntaxes of Bash and SH; however, this does not mean that Turtle * shell is a one-to-one clone of Bash or SH. There are significant * differences because Turtle shell is much simpler. Some of the differences * are within the grammar; however, the most surprising of differences are * likely found in the implementations of Turtle shell's internal commands. * Please see each of the internal command javadoc and other documentation * for more details on how any of the interal commands differ from Bash and SH * * The basic grammar of Turtle shell supports the following basic constructions * (and combinations): * * 1. internal commands -- specified as strings * see CommandBuilder() and the class org.turtleshell.Command * * 2. external commands -- ALSO specified as strings * see CommandBuilder() and the class org.turtleshell.Command * * 3. escaped external commands -- strings preceeded by an asterisk (*) * see CommandBuilder() and the class org.turtleshell.Command * * 4. commands in parenthesis (executed as one entity) * This allows backgrounding a block of code. While each command within * the block would be ran sequentially, the entire block would be run * asynchronously with the rest of the commands. * * 5. backgrounding processes (& at the end of a command) -- nedit hello.py & * Backgrounded processes will be ran asynchronously from the main body * of the code. Please note that after the ampersand, a semicolon may * optionally be used to specify more commands on the same line. * * 6. semicolon separated command lists -- echo "hello!"; echo "This is Turtle" * All commands separated by semicolons will be ran sequentially. * The behaviour of the semicolon is the same as a newline, except * the semicolon allows for writing multiple commands on a single line. * * 7. piping program output to other commands * Commands may be separated by pipe characters (|). The pipe character * indicates that the standard input from the command follwing the pipe * character is the same as the standard output from the command * preceding the pipe character. In other words, the standard output of * the preceding command will be piped into the standard input of the * command following the pipe character. Please note that after the * filename, a semicolon may optionally be used to specify more commands * on the same line. * * 8. piping program output to a file * Additionally, commands may send their standard output to a file. * Any command followed by a greater than symbol and a file name will * send its standard output to that file. * * 9. conjugated execution (AND) * Commands may be executed in conjunction with one another. * This means that a command following a conjunction symbol (&&) * will only be executed if the return status from the command * preceding the conjunction symbol (&&) is NOT equal to zero (0). * * 10. exclusive disjunct execution (XOR) * Commands may be executed in disjunctions with one another. * This means that a command following a disjunction symbol (||) * will only be executed if the return status from the command * preceding the disjunction symbol (||) IS equal to zero (0). * * 11. If statements (without else) * * if [ condition ]; then * .... statements * fi * * 12. If statement (with else) * * if [ condition ]; then * .... statements * else * .... more statements * fi * * where condition is a conditional statement with the format * * * * can be any valid turtle shell string. * Some examples are: * * "hello world" * hello * 103 * `expr 1 + 1` * $Y * * can be any of the following * * = equal * <= less than, OR equal to * < less than, but NOT equal to * > greater than, OR equal to * >= greater than, but NOT equal to * * Therefore an example of an if statement would be: * * A=`cat value.txt` * if [ $A < 100 ]; then * echo "A is less than 100" * else * echo "A is greater than or equal to 100" * fi * * 13. While loops * While loops are blocks of code which are executed repeatedly * until a condition yields false. The format of a while loop * is as follows: * * while [ condition ]; do * .... block of code * done * * where condition is a conditional statement with the format * * * * can be any valid turtle shell string. * Some examples are: * * "hello world" * hello * 103 * `expr 1 + 1` * $Y * * can be any of the following * * = equal * <= less than, OR equal to * < less than, but NOT equal to * > greater than, OR equal to * >= greater than, but NOT equal to * * Therefore an example of a while loop would be: * * A=0 * while [ $A < 100 ]; do * A=`expr $A + 1` * echo $A * done * * 14. Executable strings * Executable strings are turtle shell string objects that have their * content dynamically generated from executing a command. An example * is `expr 1 + 1`, this would set the string to the output from the * command expr 1 + 1. In other words, the string would be 2. * Executable strings are not executed until their content is needed. * In a loop, they are executed each iteration of the loop. * * There are two formats for executable strings: * 1. the backwards quote format * * e.g., echo `expr 1 + 1` * * 2. the exec variable format * * e.g., echo $(expr 1 + 1) * * The latter (exec variable format) can be nested as such: * * echo $(expr 1 + $(cat value)) * * 15. Comments * Notes for humans, which the computer (parser) ignores. * All comments are preceeded by a pound symbol (#). ** * @author Graham Alvare * @author Brian Fristensky */ public class Turtle implements TurtleConstants { /** * The initial local environment hash map. * This is used for storing the initial values for environment variables. * These values are copied to the object-specific programenv object. */ public static TEnv localenv = new TEnv(); /** * An empty shellstring object, for general use */ public static final TString NULL_SHELL_STRING = new ShellString(""); /** * An empty array of TStrings, for general use */ public static final TString[] EMPTY_STR_ARRAY = new TString[0]; /** * Starts turtle shell ** * @param args currently no command line arguments * are supported by Turtle shell */ public static void main(String args[]) { try { System.out.println("TurtleShell: bringing back the shell!\u005cn" + "Enter a command:"); Turtle parser = new Turtle(System.in); parser.Input(true); } catch (Exception e) { e.printStackTrace(System.err); } } /** * Runs simple shell commands. * Reroutes all output to the console. ** * @param cmd the command string to run */ public static void shellCommand(String cmd) { TTask command; // obtain the process object for the command try { System.out.println("Command: " + cmd); command = new Turtle(new BufferedReader(new StringReader(cmd))).CommandList(); /*System.err.println("Command execution disabled"); System.err.println(); System.err.println("Parsed command"); System.err.println("--------------");*/ System.err.println(command); command.exec(new TEnv(localenv)); } catch (Throwable e) { // if there are any errors, print them to the error prompt System.out.println("Error executing command: " + cmd); e.printStackTrace(System.err); } } /** * The first step of the turtle shell parse tree. * This function will parse individual commands at the top level of scope * for a given turtle shell instance (or script). Commands at this level * are executed immediately after parsing. */ final public void Input(boolean prompt) throws ParseException { TTask command = null; if (prompt) { System.out.print(">"); } label_1: while (true) { command = CommandList(); // TODO: disabled for parse testing command.exec(localenv); if (prompt) { System.out.print(">"); } if (jj_2_1(2)) { ; } else { break label_1; } } jj_consume_token(0); } /** * This method generates a command list ** * The purpose of this method is to generate the body of control blocks, * which must contain multiple commands. Although the commands in a command * list are separated by new lines, each command can (because we are using * the term command here to loosely mean any object generated by GenCommand) * also be separated by semicolons (;). It will just be interpreted as a * single TTask object (generated by GenCommand(). ** * @return a list of commands. */ final public TTask CommandList() throws ParseException { TTask next = null; TTask current = null; if (jj_2_2(2)) { jj_consume_token(EOC); } else { ; } current = GenCommand(); label_2: while (true) { if (jj_2_3(2)) { ; } else { break label_2; } jj_consume_token(EOC); next = GenCommand(); current = new SequentialTask (current, next); } if (jj_2_4(2)) { jj_consume_token(0); } else { ; } {if (true) return current;} throw new Error("Missing return statement in function"); } /** * This method generates a single full command. ** * This is the MEAT of the program!!! * This method will generate any possible command structure available within * Turtle shell. All methods (except Input) are called directly or indirectly * from this method * * This method handles the bulk of the command syntax directly. This method * creates the following command relations directly: * * 1. parenthesis commands (executed as one entity) * This allows backgrounding a block of code. While each command within * the block would be ran sequentially, the entire block would be run * asynchronously with the rest of the commands. * * 2. backgrounding processes (& at the end of a command) -- nedit hello.py & * Backgrounded processes will be ran asynchronously from the main body * of the code. Please note that after the ampersand, a semicolon may * optionally be used to specify more commands on the same line. * * 3. semicolon separated command lists -- echo "hello!"; echo "Turtle shell!" * All commands separated by semicolons will be ran sequentially. The * behaviour of the semicolon is the same as a newline, except the * semicolon allows for writing multiple commands on a single line. * * 4. piping program output to other commands * Commands may be separated by pipe characters (|). The pipe character * indicates that the standard input from the command follwing the pipe * character is the same as the standard output from the command * preceding the pipe character. In other words, the standard output of * the preceding command will be piped into the standard input of the * command following the pipe character. Please note that after the file * name, a semicolon may optionally be used to specify more commands * on the same line. * * 5. piping program output to a file * Additionally, commands may send their standard output to a file. Any * command followed by a greater than symbol and a file name will send * its standard output to that file. * * 6. conjugated execution (AND) * Commands may be executed in conjunction with one another. This means * that a command following a conjunction symbol (&&) will only be * executed if the return status from the command preceding the * conjunction symbol (&&) is NOT equal to zero (0). * * 7. exclusive disjunct execution (XOR) * Commands may be executed in disjunctions with one another. This means * that a command following a disjunction symbol (||) will only be * executed if the return status from the command preceding the * disjunction symbol (||) IS equal to zero (0). * * Additionally, this method calls other syntax structues via the method * CommandBuilder() and ControlBlock(). CommandBuilder() handles piping a * file from standard input, as well as general command string parsing. * ControlBlock() handles all of the control structures available such as * loops and if-statements. For more information on these syntax structures, * please see CommandBuilder() and ControlBlock() in the javadoc documentation. */ final public TTask GenCommand() throws ParseException { Token t = null; TTask next = null; TTask current = null; TTask feed = null; TString value = NULL_SHELL_STRING; boolean background = false; if (jj_2_19(2)) { current = ControlBlock(); } else if (jj_2_20(2)) { if (jj_2_5(2)) { current = CommandBuilder(); } else if (jj_2_6(2)) { jj_consume_token(OPENP); current = CommandList(); jj_consume_token(CLOSEP); } else { jj_consume_token(-1); throw new ParseException(); } feed = current; if (jj_2_7(2)) { jj_consume_token(WSP); } else { ; } label_3: while (true) { if (jj_2_8(2)) { ; } else { break label_3; } if (jj_2_10(2)) { jj_consume_token(LT); TTask tout = new Command(new TString[]{new ShellString("cat"), Strn()}); tout.setoutput(feed); feed = tout; } else if (jj_2_11(2)) { jj_consume_token(GT); current.setoutput(new TurtleFileWriter(Strn(), false)); } else if (jj_2_12(2)) { jj_consume_token(APPEND); current.setoutput(new TurtleFileWriter(Strn(), true)); } else if (jj_2_13(2)) { jj_consume_token(BKG); background = true; if (jj_2_9(2)) { next = GenCommand(); feed = new SequentialTask (feed, next); } else { ; } } else { jj_consume_token(-1); throw new ParseException(); } if (jj_2_14(2)) { jj_consume_token(WSP); } else { ; } } if (jj_2_18(2)) { if (jj_2_15(2)) { jj_consume_token(AND); next = GenCommand(); feed = new AndTask (feed, next); } else if (jj_2_16(2)) { jj_consume_token(OR); next = GenCommand(); feed = new OrTask (feed, next); } else if (jj_2_17(2)) { jj_consume_token(PIPE); next = GenCommand(); current.setoutput(next); current = next; } else { jj_consume_token(-1); throw new ParseException(); } } else { ; } if (background) { feed.bkg(); } current = feed; } else { jj_consume_token(-1); throw new ParseException(); } {if (true) return current;} throw new Error("Missing return statement in function"); } /** * Generates a control block ** * This method generates objects necessary to represent and execute any * loops or if-statements. Currently the only control blocks suppored are: * * while-loops * if-statements * * The basic format of each is as follows * * 1. If statement format (without else) * * if [ condition ]; then * .... statements * fi * * 2. If statement format (with else) * * if [ condition ]; then * .... statements * else * .... more statements * fi * * 3. While loop * * while [ condition ]; do * .... statements * done * * The above control blocks can create almost any control-flow desired, * especially with the internal (and external) commands available in * Turtle shell. ** * @return the turtle shell executable task representing the control block */ final public TTask ControlBlock() throws ParseException { TTask test = null; TTask result = null; TTask command = null; TTask elsecmd = null; TString var = NULL_SHELL_STRING; TString list = NULL_SHELL_STRING; if (jj_2_22(2)) { jj_consume_token(WHILE); test = GenCommand(); jj_consume_token(EOC); jj_consume_token(DO); command = CommandList(); jj_consume_token(EOC); jj_consume_token(DONE); result = new WhileTask(test, command); } else if (jj_2_23(2)) { jj_consume_token(IF); test = GenCommand(); jj_consume_token(EOC); jj_consume_token(THEN); command = CommandList(); if (jj_2_21(2)) { jj_consume_token(EOC); jj_consume_token(ELSE); elsecmd = CommandList(); } else { ; } jj_consume_token(EOC); jj_consume_token(FI); result = new IfTask(test, command, elsecmd); } else if (jj_2_24(2)) { jj_consume_token(FOR); var = Strn(); jj_consume_token(IN); list = Strn(); jj_consume_token(EOC); jj_consume_token(DO); command = CommandList(); jj_consume_token(EOC); jj_consume_token(DONE); result = new IfTask(test, command, elsecmd); } else { jj_consume_token(-1); throw new ParseException(); } {if (true) return result;} throw new Error("Missing return statement in function"); } /** * Builds a command task object. ** * This method takes a command string and generates a command object from it. * The method also parses any occurances of "> filename", and configures the * command to pipe the file to the command's standard input. * * NOTE: only piping a file to standard input is parsed here. Piping output * to files and other commands is performed in GenCommand() ** * @return the executable command task */ final public TTask CommandBuilder() throws ParseException { /* The token to parse into a String value */ TTask cmd = null; TString[] cmdstr = null; cmdstr = CommandString(); cmd = new Command(cmdstr); {if (true) return cmd;} throw new Error("Missing return statement in function"); } /** * Parses command strings -- command + parameters ** * This produces an array of internal turtle shell strings. * The first index is the command to execute (e.g., echo), and every other * index within the array is a parameter for the command (e.g., hello world). ** * @return the array of command + parameters parsed */ final public TString[] CommandString() throws ParseException { TString vadd = NULL_SHELL_STRING; List command = new ArrayList(); vadd = Strn(); command.add(vadd); label_4: while (true) { if (jj_2_25(2)) { ; } else { break label_4; } jj_consume_token(WSP); vadd = Strn(); command.add(vadd); } if (jj_2_26(2)) { jj_consume_token(WSP); } else { ; } //System.err.println("Command: " + command.toString()); {if (true) return command.toArray(EMPTY_STR_ARRAY);} throw new Error("Missing return statement in function"); } /** * Parses turtle shell internal string objects. ** * Currently 4 types of internal strings are supported: * * 1. unquoted id strings -- e.g., echo * 2. quoted text strings -- e.g., "hello world!" * 3. variable strings -- e.g., $ABC * 4. executable strings -- e.g., `echo abc` ** * Executable strings are turtle shell string objects that have their content * dynamically generated from executing a command. An example is `expr 1 + 1`, * this would set the string to the output from the command expr 1 + 1. In * other words, the string would be 2. Executable strings are not executed * until their content is needed. In a loop, they are executed each iteration * of the loop. ** * @return the corresponding turtle shell internal string object */ final public TString Strn() throws ParseException { Token t = null; TString value = NULL_SHELL_STRING; TTask exec; label_5: while (true) { if (jj_2_27(2)) { t = jj_consume_token(TEXT); value = new ShellString(t.image); } else if (jj_2_28(2)) { jj_consume_token(EXP); value = new DoubleExecString(CommandList()); jj_consume_token(CLOSEP); } else { jj_consume_token(-1); throw new ParseException(); } if (jj_2_29(2)) { ; } else { break label_5; } } {if (true) return value;} throw new Error("Missing return statement in function"); } private boolean jj_2_1(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_1(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(0, xla); } } private boolean jj_2_2(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_2(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(1, xla); } } private boolean jj_2_3(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_3(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(2, xla); } } private boolean jj_2_4(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_4(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(3, xla); } } private boolean jj_2_5(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_5(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(4, xla); } } private boolean jj_2_6(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_6(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(5, xla); } } private boolean jj_2_7(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_7(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(6, xla); } } private boolean jj_2_8(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_8(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(7, xla); } } private boolean jj_2_9(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_9(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(8, xla); } } private boolean jj_2_10(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_10(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(9, xla); } } private boolean jj_2_11(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_11(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(10, xla); } } private boolean jj_2_12(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_12(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(11, xla); } } private boolean jj_2_13(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_13(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(12, xla); } } private boolean jj_2_14(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_14(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(13, xla); } } private boolean jj_2_15(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_15(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(14, xla); } } private boolean jj_2_16(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_16(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(15, xla); } } private boolean jj_2_17(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_17(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(16, xla); } } private boolean jj_2_18(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_18(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(17, xla); } } private boolean jj_2_19(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_19(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(18, xla); } } private boolean jj_2_20(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_20(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(19, xla); } } private boolean jj_2_21(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_21(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(20, xla); } } private boolean jj_2_22(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_22(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(21, xla); } } private boolean jj_2_23(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_23(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(22, xla); } } private boolean jj_2_24(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_24(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(23, xla); } } private boolean jj_2_25(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_25(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(24, xla); } } private boolean jj_2_26(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_26(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(25, xla); } } private boolean jj_2_27(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_27(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(26, xla); } } private boolean jj_2_28(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_28(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(27, xla); } } private boolean jj_2_29(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_29(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(28, xla); } } private boolean jj_3R_9() { Token xsp; xsp = jj_scanpos; if (jj_3_22()) { jj_scanpos = xsp; if (jj_3_23()) { jj_scanpos = xsp; if (jj_3_24()) return true; } } return false; } private boolean jj_3_9() { if (jj_3R_7()) return true; return false; } private boolean jj_3_1() { if (jj_3R_6()) return true; return false; } private boolean jj_3_14() { if (jj_scan_token(WSP)) return true; return false; } private boolean jj_3_17() { if (jj_scan_token(PIPE)) return true; if (jj_3R_7()) return true; return false; } private boolean jj_3_16() { if (jj_scan_token(OR)) return true; if (jj_3R_7()) return true; return false; } private boolean jj_3_18() { Token xsp; xsp = jj_scanpos; if (jj_3_15()) { jj_scanpos = xsp; if (jj_3_16()) { jj_scanpos = xsp; if (jj_3_17()) return true; } } return false; } private boolean jj_3_15() { if (jj_scan_token(AND)) return true; if (jj_3R_7()) return true; return false; } private boolean jj_3_13() { if (jj_scan_token(BKG)) return true; Token xsp; xsp = jj_scanpos; if (jj_3_9()) jj_scanpos = xsp; return false; } private boolean jj_3_12() { if (jj_scan_token(APPEND)) return true; return false; } private boolean jj_3_11() { if (jj_scan_token(GT)) return true; return false; } private boolean jj_3R_11() { if (jj_3R_10()) return true; Token xsp; while (true) { xsp = jj_scanpos; if (jj_3_25()) { jj_scanpos = xsp; break; } } xsp = jj_scanpos; if (jj_3_26()) jj_scanpos = xsp; return false; } private boolean jj_3_10() { if (jj_scan_token(LT)) return true; return false; } private boolean jj_3_26() { if (jj_scan_token(WSP)) return true; return false; } private boolean jj_3_5() { if (jj_3R_8()) return true; return false; } private boolean jj_3_25() { if (jj_scan_token(WSP)) return true; if (jj_3R_10()) return true; return false; } private boolean jj_3_4() { if (jj_scan_token(0)) return true; return false; } private boolean jj_3_8() { Token xsp; xsp = jj_scanpos; if (jj_3_10()) { jj_scanpos = xsp; if (jj_3_11()) { jj_scanpos = xsp; if (jj_3_12()) { jj_scanpos = xsp; if (jj_3_13()) return true; } } } xsp = jj_scanpos; if (jj_3_14()) jj_scanpos = xsp; return false; } private boolean jj_3_3() { if (jj_scan_token(EOC)) return true; if (jj_3R_7()) return true; return false; } private boolean jj_3_21() { if (jj_scan_token(EOC)) return true; if (jj_scan_token(ELSE)) return true; return false; } private boolean jj_3_7() { if (jj_scan_token(WSP)) return true; return false; } private boolean jj_3_24() { if (jj_scan_token(FOR)) return true; if (jj_3R_10()) return true; return false; } private boolean jj_3_2() { if (jj_scan_token(EOC)) return true; return false; } private boolean jj_3R_6() { Token xsp; xsp = jj_scanpos; if (jj_3_2()) jj_scanpos = xsp; if (jj_3R_7()) return true; while (true) { xsp = jj_scanpos; if (jj_3_3()) { jj_scanpos = xsp; break; } } xsp = jj_scanpos; if (jj_3_4()) jj_scanpos = xsp; return false; } private boolean jj_3_6() { if (jj_scan_token(OPENP)) return true; if (jj_3R_6()) return true; return false; } private boolean jj_3_28() { if (jj_scan_token(EXP)) return true; if (jj_scan_token(CLOSEP)) return true; return false; } private boolean jj_3_20() { Token xsp; xsp = jj_scanpos; if (jj_3_5()) { jj_scanpos = xsp; if (jj_3_6()) return true; } xsp = jj_scanpos; if (jj_3_7()) jj_scanpos = xsp; while (true) { xsp = jj_scanpos; if (jj_3_8()) { jj_scanpos = xsp; break; } } xsp = jj_scanpos; if (jj_3_18()) jj_scanpos = xsp; return false; } private boolean jj_3_29() { Token xsp; xsp = jj_scanpos; if (jj_3_27()) { jj_scanpos = xsp; if (jj_3_28()) return true; } return false; } private boolean jj_3_27() { if (jj_scan_token(TEXT)) return true; return false; } private boolean jj_3_19() { if (jj_3R_9()) return true; return false; } private boolean jj_3_23() { if (jj_scan_token(IF)) return true; if (jj_3R_7()) return true; return false; } private boolean jj_3R_10() { Token xsp; if (jj_3_29()) return true; while (true) { xsp = jj_scanpos; if (jj_3_29()) { jj_scanpos = xsp; break; } } return false; } private boolean jj_3R_7() { Token xsp; xsp = jj_scanpos; if (jj_3_19()) { jj_scanpos = xsp; if (jj_3_20()) return true; } return false; } private boolean jj_3_22() { if (jj_scan_token(WHILE)) return true; if (jj_3R_7()) return true; return false; } private boolean jj_3R_8() { if (jj_3R_11()) return true; return false; } /** Generated Token Manager. */ public TurtleTokenManager token_source; SimpleCharStream jj_input_stream; /** Current token. */ public Token token; /** Next token. */ public Token jj_nt; private int jj_ntk; private Token jj_scanpos, jj_lastpos; private int jj_la; private int jj_gen; final private int[] jj_la1 = new int[0]; static private int[] jj_la1_0; static { jj_la1_init_0(); } private static void jj_la1_init_0() { jj_la1_0 = new int[] {}; } final private JJCalls[] jj_2_rtns = new JJCalls[29]; private boolean jj_rescan = false; private int jj_gc = 0; /** Constructor with InputStream. */ public Turtle(java.io.InputStream stream) { this(stream, null); } /** Constructor with InputStream and supplied encoding */ public Turtle(java.io.InputStream stream, String encoding) { try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source = new TurtleTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 0; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } /** Reinitialise. */ public void ReInit(java.io.InputStream stream) { ReInit(stream, null); } /** Reinitialise. */ public void ReInit(java.io.InputStream stream, String encoding) { try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 0; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } /** Constructor. */ public Turtle(java.io.Reader stream) { jj_input_stream = new SimpleCharStream(stream, 1, 1); token_source = new TurtleTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 0; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } /** Reinitialise. */ public void ReInit(java.io.Reader stream) { jj_input_stream.ReInit(stream, 1, 1); token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 0; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } /** Constructor with generated Token Manager. */ public Turtle(TurtleTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 0; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } /** Reinitialise. */ public void ReInit(TurtleTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 0; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } private Token jj_consume_token(int kind) throws ParseException { Token oldToken; if ((oldToken = token).next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; if (token.kind == kind) { jj_gen++; if (++jj_gc > 100) { jj_gc = 0; for (int i = 0; i < jj_2_rtns.length; i++) { JJCalls c = jj_2_rtns[i]; while (c != null) { if (c.gen < jj_gen) c.first = null; c = c.next; } } } return token; } token = oldToken; jj_kind = kind; throw generateParseException(); } static private final class LookaheadSuccess extends java.lang.Error { } final private LookaheadSuccess jj_ls = new LookaheadSuccess(); private boolean jj_scan_token(int kind) { if (jj_scanpos == jj_lastpos) { jj_la--; if (jj_scanpos.next == null) { jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken(); } else { jj_lastpos = jj_scanpos = jj_scanpos.next; } } else { jj_scanpos = jj_scanpos.next; } if (jj_rescan) { int i = 0; Token tok = token; while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; } if (tok != null) jj_add_error_token(kind, i); } if (jj_scanpos.kind != kind) return true; if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls; return false; } /** Get the next Token. */ final public Token getNextToken() { if (token.next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; jj_gen++; return token; } /** Get the specific Token. */ final public Token getToken(int index) { Token t = token; for (int i = 0; i < index; i++) { if (t.next != null) t = t.next; else t = t.next = token_source.getNextToken(); } return t; } private int jj_ntk() { if ((jj_nt=token.next) == null) return (jj_ntk = (token.next=token_source.getNextToken()).kind); else return (jj_ntk = jj_nt.kind); } private java.util.List jj_expentries = new java.util.ArrayList(); private int[] jj_expentry; private int jj_kind = -1; private int[] jj_lasttokens = new int[100]; private int jj_endpos; private void jj_add_error_token(int kind, int pos) { if (pos >= 100) return; if (pos == jj_endpos + 1) { jj_lasttokens[jj_endpos++] = kind; } else if (jj_endpos != 0) { jj_expentry = new int[jj_endpos]; for (int i = 0; i < jj_endpos; i++) { jj_expentry[i] = jj_lasttokens[i]; } jj_entries_loop: for (java.util.Iterator it = jj_expentries.iterator(); it.hasNext();) { int[] oldentry = (int[])(it.next()); if (oldentry.length == jj_expentry.length) { for (int i = 0; i < jj_expentry.length; i++) { if (oldentry[i] != jj_expentry[i]) { continue jj_entries_loop; } } jj_expentries.add(jj_expentry); break jj_entries_loop; } } if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind; } } /** Generate ParseException. */ public ParseException generateParseException() { jj_expentries.clear(); boolean[] la1tokens = new boolean[24]; if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } for (int i = 0; i < 0; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1< jj_gen) { jj_la = p.arg; jj_lastpos = jj_scanpos = p.first; switch (i) { case 0: jj_3_1(); break; case 1: jj_3_2(); break; case 2: jj_3_3(); break; case 3: jj_3_4(); break; case 4: jj_3_5(); break; case 5: jj_3_6(); break; case 6: jj_3_7(); break; case 7: jj_3_8(); break; case 8: jj_3_9(); break; case 9: jj_3_10(); break; case 10: jj_3_11(); break; case 11: jj_3_12(); break; case 12: jj_3_13(); break; case 13: jj_3_14(); break; case 14: jj_3_15(); break; case 15: jj_3_16(); break; case 16: jj_3_17(); break; case 17: jj_3_18(); break; case 18: jj_3_19(); break; case 19: jj_3_20(); break; case 20: jj_3_21(); break; case 21: jj_3_22(); break; case 22: jj_3_23(); break; case 23: jj_3_24(); break; case 24: jj_3_25(); break; case 25: jj_3_26(); break; case 26: jj_3_27(); break; case 27: jj_3_28(); break; case 28: jj_3_29(); break; } } p = p.next; } while (p != null); } catch(LookaheadSuccess ls) { } } jj_rescan = false; } private void jj_save(int index, int xla) { JJCalls p = jj_2_rtns[index]; while (p.gen > jj_gen) { if (p.next == null) { p = p.next = new JJCalls(); break; } p = p.next; } p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla; } static final class JJCalls { int gen; Token first; int arg; JJCalls next; } }