[Home] DMelt member access . Java source code: com.jstatcom.parser.TSDateParser
/*
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.ts.TSDate;
/* Generated By:JavaCC: Do not edit this line. TSDateParser.java */
/**
* Parser that assembles TSDate objects from an input string, for
* example
*
*
* 1960
* 1960.1
* 1960 I
* 1960 1/4
* 1960 Q4
* 1960 m11
*
*
*
*
* The following rules apply for parsing:
*
* XX x"/"y, for example 1960 1/1 is
* recognized for arbitrary sub periodicities with mainPeriod=XX, subPeriod=x,
* subPeriodicity=y
* XX ["M","m"]x, for example 1960 M1 is
* recognized as mainPeriod=XX, subPeriod=x, subPeriodicity=12 (monthly)
* XX ["Q","q"]x, for example 1960 Q1 is
* recognized as mainPeriod=XX, subPeriod=x, subPeriodicity=4 (quarterly)
* XX ["I", "II"], for example 1960 I is
* recognized as mainPeriod=XX, subPeriod=1 or 2, subPeriodicity=2 (half yearly)
* XX , for example 1960 is recognized as
* mainPeriod=XX, subPeriod=1, subPeriodicity=1 (yearly)
* XX.x , for example 1960.1 is recognized as
* mainPeriod=XX, subPeriod=x, subPeriodicity=subPeriodicity (needs to be set as
* argument)
*
*
*
* You can use the parser via the
* parseString(input, subPeriodicity) method. Whenever the
* information about the sub periodicity is contained in the input string, it is
* used and the explicit argument is ignored. But it is needed for dates coded
* as XX.x , because there is no way to retrieve the sub
* periodicity from that format, although it is often used and convenient.
*
* The parser is completely defined in a javacc grammar definition file and can
* be regenerated whenever necessary.
*
* @author Markus Kraetzig
* @see com.jstatcom.ts.TSDate
*/
public class TSDateParser implements TSDateParserConstants {
private final Stack argStack = new Stack();
public TSDateParserTokenManager token_source;
SimpleCharStream jj_input_stream;
public Token token, jj_nt;
private int jj_ntk;
private Token jj_scanpos, jj_lastpos;
private int jj_la;
public boolean lookingAhead = false;
private int jj_gen;
final private int[] jj_la1 = new int[9];
final private int[] jj_la1_0 = { 0x11, 0x11, 0xc11, 0x400, 0x400, 0x9e0,
0x9e0, 0xc00, 0x180, };
final private JJCalls[] jj_2_rtns = new JJCalls[1];
private boolean jj_rescan = false;
private int jj_gc = 0;
private java.util.Vector jj_expentries = new java.util.Vector();
private int[] jj_expentry;
private int jj_kind = -1;
private int[] jj_lasttokens = new int[100];
private int jj_endpos;
static final class JJCalls {
int gen;
Token first;
int arg;
JJCalls next;
}
/**
* Default constructor.
*/
public TSDateParser() {
this(new StringReader(""));
}
public TSDateParser(TSDateParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
public TSDateParser(java.io.InputStream stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new TSDateParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
public TSDateParser(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new TSDateParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
final public void disable_tracing() {
}
final public void enable_tracing() {
}
final public ParseException generateParseException() {
jj_expentries.removeAllElements();
boolean[] la1tokens = new boolean[14];
for (int i = 0; i < 14; i++) {
la1tokens[i] = false;
}
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 9; 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 < 14; i++) {
if (la1tokens[i]) {
jj_expentry = new int[1];
jj_expentry[0] = i;
jj_expentries.addElement(jj_expentry);
}
}
jj_endpos = 0;
jj_rescan_token();
jj_add_error_token(0, 0);
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 = lookingAhead ? jj_scanpos : 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 boolean jj_2_1(int xla) {
jj_la = xla;
jj_lastpos = jj_scanpos = token;
boolean retval = !jj_3_1();
jj_save(0, xla);
return retval;
}
final private boolean jj_3_1() {
if (jj_scan_token(INTEGER))
return true;
if (jj_la == 0 && jj_scanpos == jj_lastpos)
return false;
if (jj_scan_token(13))
return true;
if (jj_la == 0 && jj_scanpos == jj_lastpos)
return false;
return false;
}
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];
}
boolean exists = false;
for (java.util.Enumeration _enum = jj_expentries.elements(); _enum
.hasMoreElements();) {
int[] oldentry = (int[]) (_enum.nextElement());
if (oldentry.length == jj_expentry.length) {
exists = true;
for (int i = 0; i < jj_expentry.length; i++) {
if (oldentry[i] != jj_expentry[i]) {
exists = false;
break;
}
}
if (exists)
break;
}
}
if (!exists)
jj_expentries.addElement(jj_expentry);
if (pos != 0)
jj_lasttokens[(jj_endpos = pos) - 1] = kind;
}
}
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++;
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();
}
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 private void jj_rescan_token() {
jj_rescan = true;
for (int i = 0; i < 1; i++) {
JJCalls p = jj_2_rtns[i];
do {
if (p.gen > jj_gen) {
jj_la = p.arg;
jj_lastpos = jj_scanpos = p.first;
switch (i) {
case 0:
jj_3_1();
break;
}
}
p = p.next;
} while (p != null);
}
jj_rescan = false;
}
final 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;
}
final 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);
}
return (jj_scanpos.kind != kind);
}
public static void main(String args[]) throws ParseException {
TSDateParser parser = new TSDateParser(System.in);
/* Defines the variable space. */
while (true) {
System.out.print("Enter Date Expression: ");
System.out.flush();
try {
switch (parser.toeval(12)) {
case 0:
System.out.println("nothing.");
return;
case 1:
TSDate x = (TSDate) parser.argStack.pop();
System.out.println("assembled TSDate = " + x);
break;
}
} catch (ParseException x) {
System.out.println("Exiting.");
throw x;
}
}
}
/**
* Reinitializes this engine and parses an input String.
*
* @param inputString
* the String to parse
* @param subPeriodicity
* defines number of sub periods for the parser run, the argument
* is ignored if the inputString contains this information, if
* set to <1, then the string must contain the sub periodicity
* @return TSDate object if parser and assembly where successful
* @throws IllegalArgumentException
* if TSDate assembly failed due to wrong arguments
* @throws ParseException
* if parsing failed, contains error description
* @throws TokenMgrError
* if tokenizing failed, contains error description
*/
public TSDate parseString(String inputString, int subPeriodicity)
throws ParseException, TokenMgrError, IllegalArgumentException {
ReInit(new StringReader(inputString));
toeval(subPeriodicity);
return (TSDate) argStack.pop();
}
public void ReInit(TSDateParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
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 < 9; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
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 < 9; i++)
jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++)
jj_2_rtns[i] = new JJCalls();
}
final public int toeval(int subPeriodicity) throws ParseException {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MINUS:
case INTEGER:
tsdate(subPeriodicity);
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();
}
{
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");
}
/**
* Possible date formats.
*/
final public void tsdate(int subPeriodicity) throws ParseException {
Token x;
Token y;
Token minTest = new Token();
if (jj_2_1(2147483647)) {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MINUS:
minTest = jj_consume_token(MINUS);
break;
default:
jj_la1[3] = jj_gen;
;
}
x = jj_consume_token(INTEGER);
jj_consume_token(13);
y = jj_consume_token(INTEGER);
// Sub periodicity is not contained. Needs to be set before.
int mainPeriod = Double.valueOf(x.image).intValue();
int subPeriod = Double.valueOf(y.image).intValue();
if (minTest.kind == MINUS)
argStack
.push(new TSDate(-mainPeriod, subPeriod, subPeriodicity));
else
argStack
.push(new TSDate(mainPeriod, subPeriod, subPeriodicity));
} else {
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MINUS:
case INTEGER:
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MINUS:
minTest = jj_consume_token(MINUS);
break;
default:
jj_la1[4] = jj_gen;
;
}
jj_consume_token(INTEGER);
int mainPeriod = Double.valueOf(token.image).intValue();
if (minTest.kind == MINUS)
argStack.push(new TSDate(-mainPeriod, 1, 1));
else
argStack.push(new TSDate(mainPeriod, 1, 1));
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case MONTH:
case QUARTER:
case HALFYEAR1:
case HALFYEAR2:
case INTEGER:
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case INTEGER:
tsdefault();
break;
case MONTH:
tsmonthly();
break;
case QUARTER:
tsquarterly();
break;
case HALFYEAR1:
case HALFYEAR2:
tshalfyearly();
break;
default:
jj_la1[5] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
default:
jj_la1[6] = jj_gen;
;
}
break;
default:
jj_la1[7] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
}
/**
* Default 1960 1/4.
*/
final public void tsdefault() throws ParseException {
Token x;
Token y;
x = jj_consume_token(INTEGER);
jj_consume_token(DELIM);
y = jj_consume_token(INTEGER);
int subPeriod = Double.valueOf(x.image).intValue();
int subPeriodicity = Double.valueOf(y.image).intValue();
TSDate mainDate = (TSDate) argStack.pop();
argStack.push(new TSDate(mainDate.mainPeriod(), subPeriod,
subPeriodicity));
}
/**
* Half-Yearly 1960 II.
*/
final public void tshalfyearly() throws ParseException {
TSDate mainDate;
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
case HALFYEAR1:
jj_consume_token(HALFYEAR1);
mainDate = (TSDate) argStack.pop();
argStack.push(new TSDate(mainDate.mainPeriod(), 1, 2));
break;
case HALFYEAR2:
jj_consume_token(HALFYEAR2);
mainDate = (TSDate) argStack.pop();
argStack.push(new TSDate(mainDate.mainPeriod(), 2, 2));
break;
default:
jj_la1[8] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
/**
* Monthly 1960 M11.
*/
final public void tsmonthly() throws ParseException {
Token x;
jj_consume_token(MONTH);
x = jj_consume_token(INTEGER);
int subPeriod = Double.valueOf(x.image).intValue();
TSDate mainDate = (TSDate) argStack.pop();
argStack.push(new TSDate(mainDate.mainPeriod(), subPeriod, 12));
}
/**
* Quarterly 1960 Q3.
*/
final public void tsquarterly() throws ParseException {
Token x;
jj_consume_token(QUARTER);
x = jj_consume_token(INTEGER);
int subPeriod = Double.valueOf(x.image).intValue();
TSDate mainDate = (TSDate) argStack.pop();
argStack.push(new TSDate(mainDate.mainPeriod(), subPeriod, 4));
}
}
Ads help maintain this website.