/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.parser;

import java.io.StringReader;
import java.util.Stack;

import com.jstatcom.model.JSCConstants;
import com.jstatcom.model.NumberRange;
import com.jstatcom.model.NumberRangeTypes;

/* Generated By:JavaCC: Do not edit this line. NumberRangeParser.java */

/**
 * Parser that assembles number range objects from an input string, for example
 * 

* * [1,100]
* (-Infinity,Infinity)
* [lowerBound",20)
* (-Infinity, upperBound]
* [1.2,1.2]
* (-1.3e-2,1.3e-2) *
*

*

* One can use the parser via the parseString, parseStringToRange * methods. If parseString is used, symbol names may be specified * for the bounds. Valid symbol names start with a non digit and may contain * letters, digits and "_" . Reserved keywords are ("+" | "-")? "Infinity" and * "NaN" (all case insensititve). If a bound is specified as "NaN", the parser * throws an exception. If parseStringToRange is used, only * numbers are allowed for the bounds, not symbol names. *

* The parser has been generated with javacc. * * @author Markus Kraetzig * @see com.jstatcom.model.NumberRange */ public class NumberRangeParser implements NumberRangeParserConstants { private final Stack argStack = new Stack(); public NumberRangeParserTokenManager token_source; SimpleCharStream jj_input_stream; public Token token, jj_nt; private int jj_ntk; private int jj_gen; final private int[] jj_la1 = new int[10]; final private int[] jj_la1_0 = { 0x11, 0x11, 0x31, 0x11, 0x11, 0x31, 0x11a40, 0x1a40, 0x11c40, 0x1c40, }; private java.util.Vector jj_expentries = new java.util.Vector(); private int[] jj_expentry; private int jj_kind = -1; /** * Default constructor. */ public NumberRangeParser() { this(new StringReader("")); } public NumberRangeParser(NumberRangeParserTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } public NumberRangeParser(java.io.InputStream stream) { jj_input_stream = new SimpleCharStream(stream, 1, 1); token_source = new NumberRangeParserTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } public NumberRangeParser(java.io.Reader stream) { jj_input_stream = new SimpleCharStream(stream, 1, 1); token_source = new NumberRangeParserTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } final public void disable_tracing() { } final public void enable_tracing() { } final public ParseException generateParseException() { jj_expentries.removeAllElements(); boolean[] la1tokens = new boolean[18]; for (int i = 0; i < 18; i++) { la1tokens[i] = false; } if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } for (int i = 0; i < 10; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1 << j)) != 0) { la1tokens[j] = true; } } } } for (int i = 0; i < 18; i++) { if (la1tokens[i]) { jj_expentry = new int[1]; jj_expentry[0] = i; jj_expentries.addElement(jj_expentry); } } int[][] exptokseq = new int[jj_expentries.size()][]; for (int i = 0; i < jj_expentries.size(); i++) { exptokseq[i] = (int[]) jj_expentries.elementAt(i); } return new ParseException(token, exptokseq, tokenImage); } 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; } 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; } final 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++; return token; } token = oldToken; jj_kind = kind; throw generateParseException(); } final private int jj_ntk() { if ((jj_nt = token.next) == null) return (jj_ntk = (token.next = token_source.getNextToken()).kind); return (jj_ntk = jj_nt.kind); } final public void leftBracket() throws ParseException { jj_consume_token(LEFT_BRACKET); if (token.image.equals("(")) // left is open argStack.push(Boolean.valueOf("TRUE")); else argStack.push(Boolean.valueOf("FALSE")); } final public void lowerBound() throws ParseException { switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case SYMBOL_NAME: jj_consume_token(SYMBOL_NAME); argStack.push(token.image); break; case MINUS: jj_consume_token(MINUS); jj_consume_token(CONSTANT); argStack.push(Double.valueOf("-" + token.image)); break; case CONSTANT: jj_consume_token(CONSTANT); argStack.push(Double.valueOf(token.image)); break; case NEG_INF: jj_consume_token(NEG_INF); argStack.push(JSCConstants.NEG_INF); break; case NAN: jj_consume_token(NAN); { if (true) throw new ParseException("NaN is not a valid bound."); } break; default: jj_la1[6] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } final public void lowerBound_noSymbols() throws ParseException { switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case MINUS: jj_consume_token(MINUS); jj_consume_token(CONSTANT); argStack.push(Double.valueOf("-" + token.image)); break; case CONSTANT: jj_consume_token(CONSTANT); argStack.push(Double.valueOf(token.image)); break; case NEG_INF: jj_consume_token(NEG_INF); argStack.push(JSCConstants.NEG_INF); break; case NAN: jj_consume_token(NAN); { if (true) throw new ParseException("NaN is not a valid bound."); } break; default: jj_la1[7] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } public static void main(String args[]) throws ParseException { NumberRangeParser parser = new NumberRangeParser(System.in); /* Defines the variable space. */ while (true) { System.out.print("Enter Range Expression: "); System.out.flush(); try { // switch (parser.toeval_noSymbols()) { switch (parser.toeval()) { case 0: System.out.println("nothing."); return; case 1: NumberRangeTypes rangeType = (NumberRangeTypes) parser.argStack .pop(); Object upperBound = parser.argStack.pop(); Object lowerBound = parser.argStack.pop(); System.out.println("assembled Range = " + rangeType.leftBracket + lowerBound + ", " + upperBound + rangeType.rightBracket); } } catch (ParseException x) { System.out.println("Exiting."); throw x; } } } /** * Reinitializes this engine and parses an input string. In this parser run * numbers as well as symbol names are allowed for the bounds specification. *

* Valid symbol names start with a non digit and may contain letters, digits * and "_" . Reserved keywords are ("+" | "-")? "Infinity" and "NaN" (all * case insensititve). If a bound is specified as "NaN", the parser throws * an exception, because this stands usually for missing value. * * @param inputString * the string to parse * @return object array (3 x 1) if parser and assembly where successful, *
* object[0] = lower bound
* object[1] = upper bound
* object[2] = range type * * @throws ParseException * if parsing failed, contains error description * @throws TokenMgrError * if tokenizing failed, contains error description */ public Object[] parseString(String inputString) throws ParseException, TokenMgrError { ReInit(new StringReader(inputString)); toeval(); Object rangeType = argStack.pop(); Object upperBound = argStack.pop(); Object lowerBound = argStack.pop(); return new Object[] { lowerBound, upperBound, rangeType }; } /** * Reinitializes this engine and parses an input string. In this parser run, * symbol names are not allowed for the bounds specification, only numbers * with ("+" | "-")? "Infinity" as possible identifiers. * * @param inputString * the string to parse * @return a number range if parser and assembly where successful * * @throws ParseException * if parsing failed, contains error description * @throws TokenMgrError * if tokenizing failed, contains error description */ public NumberRange parseStringToRange(String inputString) throws ParseException, TokenMgrError { ReInit(new StringReader(inputString)); toeval_noSymbols(); return (NumberRange) argStack.pop(); } public void ReInit(NumberRangeParserTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } public void ReInit(java.io.InputStream 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 < 10; i++) jj_la1[i] = -1; } 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 < 10; i++) jj_la1[i] = -1; } final public void rightBracket() throws ParseException { jj_consume_token(RIGHT_BRACKET); if (token.image.equals(")")) // right is open argStack.push(Boolean.valueOf("TRUE")); else argStack.push(Boolean.valueOf("FALSE")); } // TOKEN : /* symbol names, like "lowerBound" */ // { // < SYMBOL_NAME: "\"" "\"" > // | < #SYMBOL: (()* )* > // | < #SYMBOLNOTWHITE: ~["\"", " "] > // | < #SYMBOLALLOWWHITE: ~["\""] > // } /** * Parses the expression allowing for symbol names as well as numbers. */ final public int toeval() throws ParseException { switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case LEFT_BRACKET: leftBracket(); lowerBound(); jj_consume_token(DELIM); upperBound(); rightBracket(); switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case EOL: jj_consume_token(EOL); break; case 0: jj_consume_token(0); break; default: jj_la1[0] = jj_gen; jj_consume_token(-1); throw new ParseException(); } boolean isRightOpen = ((Boolean) argStack.pop()).booleanValue(); Object upper = argStack.pop(); String upperSName = null; Double upperNumber = null; if (upper instanceof Double) upperNumber = (Double) upper; else { upperSName = upper.toString(); // String tmp = upper.toString(); // upperSName = tmp.substring(1, tmp.lastIndexOf('\"')); } Object lower = argStack.pop(); String lowerSName = null; Double lowerNumber = null; if (lower instanceof Double) lowerNumber = (Double) lower; else { lowerSName = lower.toString(); // String tmp = lower.toString(); // lowerSName = tmp.substring(1, tmp.lastIndexOf('\"')); } boolean isLeftOpen = ((Boolean) argStack.pop()).booleanValue(); NumberRangeTypes rangeType = NumberRangeTypes.getTypeForBounds( isLeftOpen, isRightOpen); if (lowerNumber != null && upperNumber != null) { if (lowerNumber.doubleValue() == upperNumber.doubleValue() && (isLeftOpen || isRightOpen)) { if (true) throw new IllegalArgumentException( "If bounds are equal, interval type must be CLOSED."); } if (lowerNumber.doubleValue() > upperNumber.doubleValue()) { if (true) throw new IllegalArgumentException( "Lower bound is greater than upper bound."); } } if (lowerSName != null && upperSName != null && (isLeftOpen || isRightOpen)) { if (lowerSName.equalsIgnoreCase(upperSName)) { if (true) throw new IllegalArgumentException( "If bounds are equal, interval type must be CLOSED."); } } if (lowerSName != null) argStack.push(lowerSName); else { if (!isLeftOpen && lowerNumber.doubleValue() == Double.NEGATIVE_INFINITY) { if (true) throw new IllegalArgumentException( "Lower bound cannot be closed and -INFINITY."); } argStack.push(lowerNumber); } if (upperSName != null) argStack.push(upperSName); else { if (!isRightOpen && upperNumber.doubleValue() == Double.POSITIVE_INFINITY) { if (true) throw new IllegalArgumentException( "Upper bound cannot be closed and +INFINITY."); } argStack.push(upperNumber); } argStack.push(rangeType); { if (true) return 1; } break; case 0: case EOL: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case EOL: jj_consume_token(EOL); break; case 0: jj_consume_token(0); break; default: jj_la1[1] = jj_gen; jj_consume_token(-1); throw new ParseException(); } { if (true) return 0; } break; default: jj_la1[2] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new Error("Missing return statement in function"); } /** * Parses the expression allowing only for numbers. */ final public int toeval_noSymbols() throws ParseException { switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case LEFT_BRACKET: leftBracket(); lowerBound_noSymbols(); jj_consume_token(DELIM); upperBound_noSymbols(); rightBracket(); switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case EOL: jj_consume_token(EOL); break; case 0: jj_consume_token(0); break; default: jj_la1[3] = jj_gen; jj_consume_token(-1); throw new ParseException(); } boolean isRightOpen = ((Boolean) argStack.pop()).booleanValue(); double upperBound = ((Double) argStack.pop()).doubleValue(); double lowerBound = ((Double) argStack.pop()).doubleValue(); boolean isLeftOpen = ((Boolean) argStack.pop()).booleanValue(); NumberRange range = new NumberRange(lowerBound, upperBound, NumberRangeTypes.getTypeForBounds(isLeftOpen, isRightOpen)); argStack.push(range); { if (true) return 1; } break; case 0: case EOL: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case EOL: jj_consume_token(EOL); break; case 0: jj_consume_token(0); break; default: jj_la1[4] = jj_gen; jj_consume_token(-1); throw new ParseException(); } { if (true) return 0; } break; default: jj_la1[5] = jj_gen; jj_consume_token(-1); throw new ParseException(); } throw new Error("Missing return statement in function"); } final public void upperBound() throws ParseException { switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case SYMBOL_NAME: jj_consume_token(SYMBOL_NAME); argStack.push(token.image); break; case MINUS: jj_consume_token(MINUS); jj_consume_token(CONSTANT); argStack.push(Double.valueOf("-" + token.image)); break; case CONSTANT: jj_consume_token(CONSTANT); argStack.push(Double.valueOf(token.image)); break; case POS_INF: jj_consume_token(POS_INF); argStack.push(JSCConstants.POS_INF); break; case NAN: jj_consume_token(NAN); { if (true) throw new ParseException("NaN is not a valid bound."); } break; default: jj_la1[8] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } final public void upperBound_noSymbols() throws ParseException { switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case MINUS: jj_consume_token(MINUS); jj_consume_token(CONSTANT); argStack.push(Double.valueOf("-" + token.image)); break; case CONSTANT: jj_consume_token(CONSTANT); argStack.push(Double.valueOf(token.image)); break; case POS_INF: jj_consume_token(POS_INF); argStack.push(JSCConstants.POS_INF); break; case NAN: jj_consume_token(NAN); { if (true) throw new ParseException("NaN is not a valid bound."); } break; default: jj_la1[9] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } }

Ads help maintain this website.