Пример #1
0
  /**
   * Static service to get a term from its string representation, providing a specific operator
   * manager
   */
  public static Term parseSingleTerm(String st, OperatorManager op) throws InvalidTermException {
    try {
      Parser p = new Parser(op, st);
      Token t = p.tokenizer.readToken();
      if (t.isEOF()) throw new InvalidTermException("Term starts with EOF");

      p.tokenizer.unreadToken(t);
      Term term = p.expr(false);
      if (term == null) throw new InvalidTermException("Term is null");
      if (!p.tokenizer.readToken().isEOF())
        throw new InvalidTermException("The entire string could not be read as one term");
      term.resolveTerm();
      return term;
    } catch (IOException ex) {
      throw new InvalidTermException("An I/O error occured");
    }
  }
Пример #2
0
  /**
   * Parses next term from the stream built on string.
   *
   * @param endNeeded <tt>true</tt> if it is required to parse the end token (a period),
   *     <tt>false</tt> otherwise.
   * @throws InvalidTermException if a syntax error is found.
   */
  public Term nextTerm(boolean endNeeded) throws InvalidTermException {
    try {
      Token t = tokenizer.readToken();
      if (t.isEOF()) return null;

      tokenizer.unreadToken(t);
      Term term = expr(false);
      if (term == null)
        /*Castagna 06/2011*/
        // throw new InvalidTermException("The parser is unable to finish");
        throw new InvalidTermException(
            "The parser is unable to finish.",
            tokenizer.offsetToRowColumn(getCurrentOffset())[0],
            tokenizer.offsetToRowColumn(getCurrentOffset())[1] - 1);
      /**/

      if (endNeeded && tokenizer.readToken().getType() != Tokenizer.END)
        /*Castagna 06/2011*/
        // throw new InvalidTermException("The term " + term + " is not ended with a period.");
        throw new InvalidTermException(
            "The term '" + term + "' is not ended with a period.",
            tokenizer.offsetToRowColumn(getCurrentOffset())[0],
            tokenizer.offsetToRowColumn(getCurrentOffset())[1] - 1);
      /**/

      term.resolveTerm();
      return term;
    } catch (IOException ex) {
      /*Castagna 06/2011*/
      // throw new InvalidTermException("An I/O error occured.");
      throw new InvalidTermException(
          "An I/O error occured.",
          tokenizer.offsetToRowColumn(getCurrentOffset())[0],
          tokenizer.offsetToRowColumn(getCurrentOffset())[1] - 1);
      /**/
    }
  }