List of usage examples for java.io StreamTokenizer pushBack
public void pushBack()
From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java
private static boolean matchChar(StreamTokenizer st, int c, boolean next) throws IOException { int type = next ? st.nextToken() : st.ttype; if (type == c) return true; if (next)/*from w w w . jav a2s .c om*/ st.pushBack(); return false; }
From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java
private static boolean matchWord(StreamTokenizer st, String word, boolean next) throws IOException { int type = next ? st.nextToken() : st.ttype; if (type == StreamTokenizer.TT_WORD && st.sval.equalsIgnoreCase(word)) { return true; }/* w w w.j a v a 2 s. c o m*/ if (next) st.pushBack(); return false; }
From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java
private static boolean matchDollarAlias(StreamTokenizer st, boolean next) throws IOException { int type = next ? st.nextToken() : st.ttype; if (type == StreamTokenizer.TT_WORD && st.sval.charAt(0) == '$' && st.sval.length() > 1) { return true; }// w w w . jav a2 s .c o m if (next) st.pushBack(); return false; }
From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java
private static void skipSingleLineComment(StreamTokenizer st) throws IOException { int lineNo = st.lineno(); int lookahead = st.nextToken(); while (lookahead != StreamTokenizer.TT_EOF && lookahead != '\n') { if (st.lineno() > lineNo) break; lookahead = st.nextToken();//ww w. j ava 2s. co m } st.pushBack(); }
From source file:Command.java
/** * This static method creates a Command using the specified target object, * and the specified string. The string should contain method name followed * by an optional parenthesized comma-separated argument list and a * semicolon. The arguments may be boolean, integer or double literals, or * double-quoted strings. The parser is lenient about missing commas, * semicolons and quotes, but throws an IOException if it cannot parse the * string.//www .ja v a2s .com */ public static Command parse(Object target, String text) throws IOException { String methodname; // The name of the method ArrayList args = new ArrayList(); // Hold arguments as we parse them. ArrayList types = new ArrayList(); // Hold argument types. // Convert the string into a character stream, and use the // StreamTokenizer class to convert it into a stream of tokens StreamTokenizer t = new StreamTokenizer(new StringReader(text)); // The first token must be the method name int c = t.nextToken(); // read a token if (c != t.TT_WORD) // check the token type throw new IOException("Missing method name for command"); methodname = t.sval; // Remember the method name // Now we either need a semicolon or a open paren c = t.nextToken(); if (c == '(') { // If we see an open paren, then parse an arg list for (;;) { // Loop 'till end of arglist c = t.nextToken(); // Read next token if (c == ')') { // See if we're done parsing arguments. c = t.nextToken(); // If so, parse an optional semicolon if (c != ';') t.pushBack(); break; // Now stop the loop. } // Otherwise, the token is an argument; figure out its type if (c == t.TT_WORD) { // If the token is an identifier, parse boolean literals, // and treat any other tokens as unquoted string literals. if (t.sval.equals("true")) { // Boolean literal args.add(Boolean.TRUE); types.add(boolean.class); } else if (t.sval.equals("false")) { // Boolean literal args.add(Boolean.FALSE); types.add(boolean.class); } else { // Assume its a string args.add(t.sval); types.add(String.class); } } else if (c == '"') { // If the token is a quoted string args.add(t.sval); types.add(String.class); } else if (c == t.TT_NUMBER) { // If the token is a number int i = (int) t.nval; if (i == t.nval) { // Check if its an integer // Note: this code treats a token like "2.0" as an int! args.add(new Integer(i)); types.add(int.class); } else { // Otherwise, its a double args.add(new Double(t.nval)); types.add(double.class); } } else { // Any other token is an error throw new IOException( "Unexpected token " + t.sval + " in argument list of " + methodname + "()."); } // Next should be a comma, but we don't complain if its not c = t.nextToken(); if (c != ',') t.pushBack(); } } else if (c != ';') { // if a method name is not followed by a paren t.pushBack(); // then allow a semi-colon but don't require it. } // We've parsed the argument list. // Next, convert the lists of argument values and types to arrays Object[] argValues = args.toArray(); Class[] argtypes = (Class[]) types.toArray(new Class[argValues.length]); // At this point, we've got a method name, and arrays of argument // values and types. Use reflection on the class of the target object // to find a method with the given name and argument types. Throw // an exception if we can't find the named method. Method method; try { method = target.getClass().getMethod(methodname, argtypes); } catch (Exception e) { throw new IOException("No such method found, or wrong argument " + "types: " + methodname); } // Finally, create and return a Command object, using the target object // passed to this method, the Method object we obtained above, and // the array of argument values we parsed from the string. return new Command(target, method, argValues); }
From source file:com.rapidminer.tools.Tools.java
/** Skips all tokens before next end of line (EOL). */ public static void waitForEOL(StreamTokenizer tokenizer) throws IOException { // skip everything until EOL while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) { }//from w w w .ja va 2 s .c o m ; tokenizer.pushBack(); }
From source file:com.redskyit.scriptDriver.RunTests.java
private void parseParams(StreamTokenizer tokenizer, ParamHandler handler) throws Exception { tokenizer.nextToken();//from w ww . j a v a 2 s. c om if (tokenizer.ttype == '(') { System.out.print(" ("); tokenizer.nextToken(); while (tokenizer.ttype == StreamTokenizer.TT_WORD || tokenizer.ttype == ',' || tokenizer.ttype == ')') { System.out.print(' '); String arg; switch (tokenizer.ttype) { case ',': arg = ","; break; case ')': System.out.print(")"); return; default: arg = tokenizer.sval; } System.out.print(arg); handler.processParam(tokenizer, arg); tokenizer.nextToken(); } System.out.println(); throw new Exception("args unexpectd token " + tokenizer.ttype); } tokenizer.pushBack(); // no arguments }
From source file:com.redskyit.scriptDriver.RunTests.java
private void parseBlock(ExecutionContext script, StreamTokenizer tokenizer, BlockHandler handler) throws Exception { tokenizer.nextToken();/* ww w .j ava 2 s .c o m*/ if (tokenizer.ttype == '{') { int braceLevel = 1; System.out.print(" {"); tokenizer.eolIsSignificant(true); tokenizer.nextToken(); while (tokenizer.ttype == StreamTokenizer.TT_WORD || tokenizer.ttype == '"' || tokenizer.ttype == StreamTokenizer.TT_NUMBER || tokenizer.ttype == '\n' || tokenizer.ttype == '{' || tokenizer.ttype == '}' || tokenizer.ttype == ',' || tokenizer.ttype == '*' || tokenizer.ttype == ':') { System.out.print(' '); String arg; switch (tokenizer.ttype) { case '{': braceLevel++; arg = "{"; break; case '}': if (--braceLevel == 0) { System.out.print("}"); tokenizer.eolIsSignificant(false); return; } arg = "}"; break; case StreamTokenizer.TT_NUMBER: arg = String.valueOf(tokenizer.nval); break; case StreamTokenizer.TT_EOL: arg = "\n"; break; case '"': // 5 backslashed required in replace string because its processed once // as a string (yielding \\") and then again by the replace method // of the regular expression (so \\" becomes \") arg = '"' + script.getExpandedString(tokenizer).replaceAll("\"", "\\\\\"") + '"'; break; case ',': arg = ","; break; case ':': arg = ":"; break; case '*': arg = "*"; break; default: arg = script.getExpandedString(tokenizer); } System.out.print(arg); handler.parseToken(tokenizer, arg); tokenizer.nextToken(); } System.out.println(); throw new Exception("args unexpectd token " + tokenizer.ttype); } tokenizer.pushBack(); // no arguments }