Java Code Examples for java.io.StreamTokenizer#pushBack()

The following examples show how to use java.io.StreamTokenizer#pushBack() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Algorithm.java    From KEEL with GNU General Public License v3.0 6 votes vote down vote up
/** Puts the tokenizer in the first token of the next line.
 *
 * @param tokenizer The tokenizer which reads this function.
 *
 * @return True if reaches the end of file. False otherwise.
 *
 * @throws Exception	If cannot read the tokenizer.
 */
protected boolean getNextToken( StreamTokenizer tokenizer ) throws Exception
{
  try
  {
    if ( tokenizer.nextToken() == StreamTokenizer.TT_EOF )
      return false;
    else
    {
      tokenizer.pushBack();
      while ( tokenizer.nextToken() != StreamTokenizer.TT_EOL );
      while ( tokenizer.nextToken() == StreamTokenizer.TT_EOL );

      if ( tokenizer.sval == null )
        return false;
      else
        return true;
    }
  }
  catch( Exception e )
  {
    System.err.println( e.getMessage() );
    return false;
  }
}
 
Example 2
Source File: ParseExpression.java    From icafe with Eclipse Public License 1.0 6 votes vote down vote up
static Expression sum(StreamTokenizer st) throws SyntaxError {
    Expression result;
    boolean done = false;

    result = term(st);

    while (! done) {
        try {
            switch (st.nextToken()) {
                case '+':
                    result = new Expression(OP_ADD, result, term(st));
                    break;
                case '-':
                    result = new Expression(OP_SUB, result, term(st));
                    break;
                default :
                    st.pushBack();
                    done = true;
                    break;
            }
        } catch (IOException ioe) {
            throw new SyntaxError("Caught an I/O Exception.");
        }
    }
    return result;
}
 
Example 3
Source File: ParseExpression.java    From icafe with Eclipse Public License 1.0 6 votes vote down vote up
static Expression factor(StreamTokenizer st) throws SyntaxError {
    Expression result;

    result = primary(st);
    try {
        switch (st.nextToken()) {
            case '^':
                result = new Expression(OP_EXP, result, factor(st));
                break;
            default:
                st.pushBack();
                break;
        }
    } catch (IOException ioe) {
        throw new SyntaxError("Caught an I/O Exception.");
    }
    return result;
}
 
Example 4
Source File: Algorithm.java    From KEEL with GNU General Public License v3.0 6 votes vote down vote up
/** Puts the tokenizer in the first token of the next line.
 *
 * @param tokenizer The tokenizer which reads this function.
 *
 * @return True if reaches the end of file. False otherwise.
 *
 * @throws Exception	If cannot read the tokenizer.
 */
protected boolean getNextToken( StreamTokenizer tokenizer ) throws Exception
{
  try
  {
    if ( tokenizer.nextToken() == StreamTokenizer.TT_EOF )
      return false;
    else
    {
      tokenizer.pushBack();
      while ( tokenizer.nextToken() != StreamTokenizer.TT_EOL );
      while ( tokenizer.nextToken() == StreamTokenizer.TT_EOL );

      if ( tokenizer.sval == null )
        return false;
      else
        return true;
    }
  }
  catch( Exception e )
  {
    System.err.println( e.getMessage() );
    return false;
  }
}
 
Example 5
Source File: Algorithm.java    From KEEL with GNU General Public License v3.0 6 votes vote down vote up
/** Puts the tokenizer in the first token of the next line.
 *
 * @param tokenizer The tokenizer which reads this function.
 *
 * @return True if reaches the end of file. False otherwise.
 *
 * @throws Exception	If cannot read the tokenizer.
 */
protected boolean getNextToken( StreamTokenizer tokenizer ) throws Exception
{
  try
  {
    if ( tokenizer.nextToken() == StreamTokenizer.TT_EOF )
      return false;
    else
    {
      tokenizer.pushBack();
      while ( tokenizer.nextToken() != StreamTokenizer.TT_EOL );
      while ( tokenizer.nextToken() == StreamTokenizer.TT_EOL );

      if ( tokenizer.sval == null )
        return false;
      else
        return true;
    }
  }
  catch( Exception e )
  {
    System.err.println( e.getMessage() );
    return false;
  }
}
 
Example 6
Source File: Algorithm.java    From KEEL with GNU General Public License v3.0 6 votes vote down vote up
/** Puts the tokenizer in the first token of the next line.
 *
 * @param tokenizer The tokenizer which reads this function.
 *
 * @return True if reaches the end of file. False otherwise.
 *
 * @throws Exception	If cannot read the tokenizer.
 */
protected boolean getNextToken( StreamTokenizer tokenizer ) throws Exception
{
  try
  {
    if ( tokenizer.nextToken() == StreamTokenizer.TT_EOF )
      return false;
    else
    {
      tokenizer.pushBack();
      while ( tokenizer.nextToken() != StreamTokenizer.TT_EOL );
      while ( tokenizer.nextToken() == StreamTokenizer.TT_EOL );

      if ( tokenizer.sval == null )
        return false;
      else
        return true;
    }
  }
  catch( Exception e )
  {
    System.err.println( e.getMessage() );
    return false;
  }
}
 
Example 7
Source File: RunCART.java    From KEEL with GNU General Public License v3.0 5 votes vote down vote up
/** Puts the tokenizer in the first token of the next line.
 *
 * @param tokenizer		The tokenizer which reads this function.
 *
 * @return				True if reaches the end of file. False otherwise.
 *
 * @throws Exception	If cannot read the tokenizer.
 */
private boolean getNextToken(StreamTokenizer tokenizer) {
	try {
		if (tokenizer.nextToken() == StreamTokenizer.TT_EOF) {
			return false;
		} else {
			tokenizer.pushBack();

			while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
				;
			}

			while (tokenizer.nextToken() == StreamTokenizer.TT_EOL) {
				;
			}

			if (tokenizer.sval == null) {
				return false;
			} else {
				return true;
			}
		}
	} catch (Exception e) {
		System.err.println(e.getMessage());

		return false;
	}
}
 
Example 8
Source File: Tools.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Skips all tokens before next end of line (EOL). */
public static void waitForEOL(StreamTokenizer tokenizer) throws IOException {

	while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
		// skip everything until EOL
	}
	tokenizer.pushBack();
}
 
Example 9
Source File: Instances.java    From KEEL with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reads and skips all tokens before next end of line token.
 * 
 * @param tokenizer
 *            the stream tokenizer
 */
protected void readTillEOL(StreamTokenizer tokenizer) throws IOException {

	while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
	}
	;
	tokenizer.pushBack();
}
 
Example 10
Source File: Parser.java    From Jatalog with Apache License 2.0 5 votes vote down vote up
private static Expr parseBuiltInPredicate(String lhs, StreamTokenizer scan) throws DatalogException {
    try {
        String operator;
        scan.nextToken();
        if(scan.ttype == StreamTokenizer.TT_WORD) {
            // At some point I was going to have "eq" and "ne" for string comparisons, but it wasn't a good idea.
            operator = scan.sval;
        } else {
            operator = Character.toString((char)scan.ttype);
            scan.nextToken();
            if(scan.ttype == '=' || scan.ttype == '>') {
                operator = operator + Character.toString((char)scan.ttype);
            } else {
                scan.pushBack();
            }
        }

        if(!validOperators.contains(operator)) {
            throw new DatalogException("Invalid operator '" + operator + "'");
        }

        String rhs = null;
        scan.nextToken();
        if(scan.ttype == StreamTokenizer.TT_WORD) {
            rhs = scan.sval;
        } else if(scan.ttype == '"' || scan.ttype == '\'') {
            rhs = scan.sval;
        } else if(scan.ttype == StreamTokenizer.TT_NUMBER) {
            rhs = numberToString(scan.nval);
        } else {
            throw new DatalogException("[line " + scan.lineno() + "] Right hand side of expression expected");
        }

        return new Expr(operator, lhs, rhs);

    } catch (IOException e) {
        throw new DatalogException(e);
    }
}
 
Example 11
Source File: Json.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Object parseArray(StreamTokenizer tokens) throws IOException {
  assert (tokens.ttype == LBRACKET);
  List<Object> array = new ArrayList<>();
  loop: for (;;) {
    int token = tokens.nextToken();
    switch (token) {
      case TT_EOL:
        break; // ignore
      case TT_EOF:
        throw new IOException("Unexpected eof");
      case RBRACKET:
        break loop;
      default:
        tokens.pushBack();
        Object o = parseR(tokens);
        tokens.nextToken();
        if (tokens.ttype == TT_EOF)
          break;
        else if (tokens.ttype == RBRACKET)
          tokens.pushBack();
        else if (tokens.ttype != COMMA)
          throw new IOException("Missing comma in list");
        array.add(o);
    }
  }
  return array;
}
 
Example 12
Source File: PigMacro.java    From spork with Apache License 2.0 5 votes vote down vote up
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;
    }
    if (next) st.pushBack();
 
    return false;
}
 
Example 13
Source File: Parser.java    From MOE with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Map<String, String> parseOptions(StreamTokenizer input) throws ParseError {
  try {
    int result = input.nextToken();
    if (result == StreamTokenizer.TT_EOF) {
      return ImmutableMap.<String, String>of();
    }
    if (result != '(') {
      input.pushBack();
      return ImmutableMap.<String, String>of();
    }

    ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder();
    while (true) {
      result = input.nextToken();
      if (result == StreamTokenizer.TT_EOF) {
        throw new ParseError("options not terminated by \")\"");
      }

      if (result == ')') {
        return builder.build();
      }

      input.pushBack();
      ParseOptionResult r = parseOption(input);
      builder.put(r.key, r.value);

      result = input.nextToken();
      if (result == ')') {
        return builder.build();
      }

      if (result != ',') {
        throw new ParseError("text after option must be \",\" or \")\"");
      }
    }
  } catch (IOException e) {
    throw new ParseError(e.getMessage());
  }
}
 
Example 14
Source File: PigMacro.java    From spork with Apache License 2.0 5 votes vote down vote up
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) st.pushBack();
    return false;
}
 
Example 15
Source File: ParseExpression.java    From icafe with Eclipse Public License 1.0 5 votes vote down vote up
static Expression element(StreamTokenizer st) throws SyntaxError {
    Expression result = null;

    try {
        switch (st.nextToken()) {
            case StreamTokenizer.TT_NUMBER :
                result = new ConstantExpression(st.nval);
                break;

            case StreamTokenizer.TT_WORD :
                result = new VariableExpression(st.sval);
                break;
            case '(' :
                result = expression(st);
                st.nextToken();
                if (st.ttype != ')') {
                    st.pushBack();
                    throw new SyntaxError("Mismatched parenthesis.");
                }
                break;
            default:
                st.pushBack();
                throw new SyntaxError("Unexpected symbol on input.");
        }
    } catch (IOException ioe) {
        throw new SyntaxError("Caught an I/O exception.");
    }
    return result;
}
 
Example 16
Source File: ParseExpression.java    From icafe with Eclipse Public License 1.0 5 votes vote down vote up
static Expression primary(StreamTokenizer st) throws SyntaxError {
    try {
        switch (st.nextToken()) {
            case '!' :
                return new Expression(OP_NOT, primary(st));
            case '-' :
                return new Expression(OP_NEG, primary(st));
            default:
                st.pushBack();
                return element(st);
        }
    } catch (IOException ioe) {
        throw new SyntaxError("Caught an I/O Exception.");
    }
}
 
Example 17
Source File: PigMacro.java    From spork with Apache License 2.0 5 votes vote down vote up
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;
    }
    if (next) st.pushBack();
    return false;
}
 
Example 18
Source File: PigMacro.java    From spork with Apache License 2.0 5 votes vote down vote up
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();
    }
    st.pushBack();
}
 
Example 19
Source File: Jatalog.java    From Jatalog with Apache License 2.0 4 votes vote down vote up
/**
   * Executes all the statements in a file/string or another object wrapped by a {@link java.io.Reader}.
   * <p>
   * An optional {@link QueryOutput} object can be supplied as a parameter to output the results of multiple queries.
   * </p><p>
   * This is how to interpret the returned {@code Collection<Map<String, String>>}, assuming you store it in a variable
   * called {@code answers}: 
* </p>
   * <ul>
* <li> If {@code answers} is {@code null}, the statement didn't produce any results; i.e. it was a fact or a rule, not a query.
* <li> If {@code answers} is empty, then it was a query that doesn't have any answers, so the output is "No."
* <li> If {@code answers} is a list of empty maps, then it was the type of query that only wanted a yes/no
*     answer, like {@code siblings(alice,bob)?} and the answer is "Yes."
* <li> Otherwise {@code answers} is a list of all bindings that satisfy the query.
* </ul>
   * @param reader The reader from which the statements are read.
   * @param output The object through which output should be written. Can be {@code null} in which case no output will be written.
   * @return The answer of the last statement in the file, as a Collection of variable mappings.
   * @throws DatalogException on syntax and I/O errors encountered while executing. 
   * @see QueryOutput
   */
  public Collection<Map<String, String>> executeAll(Reader reader, QueryOutput output) throws DatalogException {
      try {
          StreamTokenizer scan = getTokenizer(reader);
          
          // Tracks the last query's answers
          Collection<Map<String, String>> answers = null;
          scan.nextToken();
          while(scan.ttype != StreamTokenizer.TT_EOF) {
              scan.pushBack();
              answers = executeSingleStatement(scan, reader, output);
              scan.nextToken();
          }            
          return answers;
      } catch (IOException e) {
          throw new DatalogException(e);
      }
  }
 
Example 20
Source File: SimpleWKTShapeParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** checks if the next token is a number */
private static boolean isNumberNext(StreamTokenizer stream) throws IOException {
  final int type = stream.nextToken();
  stream.pushBack();
  return type == StreamTokenizer.TT_WORD;
}