1 /* XmileExpressionParser.java */
2 /* Generated By:JavaCC: Do not edit this line. XmileExpressionParser.java */
3 package org.simantics.sysdyn.xmile.expressionParser;
5 import java.io.StringReader;
6 import java.lang.StringBuilder;
7 import java.util.ArrayList;
10 import org.simantics.sysdyn.xmile.XmileUtil;
12 public class XmileExpressionParser implements XmileExpressionParserConstants {
13 private static final boolean DEBUG = true;
15 public static String parse(String expression, Map<String, String> subscripts) {
16 XmileExpressionParser parser = new XmileExpressionParser(new StringReader(expression));
19 return parser.Expression(subscripts);
21 catch (ParseException e) {
27 final public String Expression(Map<String, String> subscripts) throws ParseException {String e;
28 e = BinExpr(subscripts);
30 {if ("" != null) return e;}
31 throw new Error("Missing return statement in function");
34 final public String BinOp() throws ParseException {
35 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
37 jj_consume_token(LOR);
38 {if ("" != null) return "OR";}
42 jj_consume_token(LAND);
43 {if ("" != null) return "AND";}
48 {if ("" != null) return "=";}
53 {if ("" != null) return "<";}
57 jj_consume_token(LEQ);
58 {if ("" != null) return "<=";}
63 {if ("" != null) return ">";}
67 jj_consume_token(GEQ);
68 {if ("" != null) return ">=";}
72 jj_consume_token(PLUS);
73 {if ("" != null) return "+";}
77 jj_consume_token(MINUS);
78 {if ("" != null) return "-";}
82 jj_consume_token(MULT);
83 {if ("" != null) return "*";}
87 jj_consume_token(DIV);
88 {if ("" != null) return "/";}
92 jj_consume_token(MOD);
93 {if ("" != null) return "%";}
99 throw new ParseException();
101 throw new Error("Missing return statement in function");
104 final public String BinExpr(Map<String, String> subscripts) throws ParseException {String l = null;
107 l = UnExpr(subscripts);
108 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
122 r = BinExpr(subscripts);
129 {if ("" != null) return o != null ? l + o + r : l;}
130 throw new Error("Missing return statement in function");
133 final public String UnOp() throws ParseException {
134 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
136 jj_consume_token(LNOT);
137 {if ("" != null) return "NOT";}
141 jj_consume_token(PLUS);
142 {if ("" != null) return "+";}
146 jj_consume_token(MINUS);
147 {if ("" != null) return "-";}
152 jj_consume_token(-1);
153 throw new ParseException();
155 throw new Error("Missing return statement in function");
158 final public String UnExpr(Map<String, String> subscripts) throws ParseException {String o = null;
160 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
171 e = Atom(subscripts);
172 {if ("" != null) return o != null ? o + e : e;}
173 throw new Error("Missing return statement in function");
176 final public String Atom(Map<String, String> subscripts) throws ParseException {Token t = null;
178 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
180 t = jj_consume_token(NUMBER);
181 {if ("" != null) return t.image;}
187 s = Function(subscripts);
188 {if ("" != null) return s;}
190 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
193 s = Variable(subscripts);
194 {if ("" != null) return s;}
198 jj_consume_token(LPAREN);
199 s = BinExpr(subscripts);
200 jj_consume_token(RPAREN);
201 {if ("" != null) return "(" + s + ")";}
206 jj_consume_token(-1);
207 throw new ParseException();
211 throw new Error("Missing return statement in function");
214 final public String Function(Map<String, String> subscripts) throws ParseException {Token t = null;
216 ArrayList<String> params = new ArrayList<String>();
217 t = jj_consume_token(IDENT);
218 jj_consume_token(LPAREN);
219 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
227 p = BinExpr(subscripts);
231 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
240 jj_consume_token(COMMA);
241 p = BinExpr(subscripts);
250 jj_consume_token(RPAREN);
251 String name = XmileUtil.getSysdynFunction(t.image);
252 StringBuilder f = new StringBuilder();
253 f.append(XmileUtil.FUNCTION_PREFIX);
256 for (int i = 0; i < params.size(); i++) {
257 if (i > 0) f.append(',');
258 f.append(params.get(i));
260 if (XmileUtil.needsTime(name)) {
261 f.append(XmileUtil.TIME_PARAM);
263 if (XmileUtil.needsTimeStep(name)) {
264 f.append(XmileUtil.TIME_STEP_PARAM);
266 if (XmileUtil.needsTimeStart(name)) {
267 f.append(XmileUtil.TIME_START_PARAM);
269 if (XmileUtil.needsTimeStop(name)) {
270 f.append(XmileUtil.TIME_STOP_PARAM);
273 {if ("" != null) return f.toString();}
274 throw new Error("Missing return statement in function");
277 final public String Variable(Map<String, String> subscripts) throws ParseException {Token t = null;
278 switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
280 t = jj_consume_token(IDENT);
281 {if ("" != null) return XmileUtil.normalize(t.image);}
285 t = jj_consume_token(QIDENT);
286 {if ("" != null) return t.image;}
291 jj_consume_token(-1);
292 throw new ParseException();
294 throw new Error("Missing return statement in function");
297 private boolean jj_2_1(int xla)
299 jj_la = xla; jj_lastpos = jj_scanpos = token;
300 try { return !jj_3_1(); }
301 catch(LookaheadSuccess ls) { return true; }
302 finally { jj_save(0, xla); }
305 private boolean jj_3R_2()
307 if (jj_scan_token(IDENT)) return true;
308 if (jj_scan_token(LPAREN)) return true;
312 private boolean jj_3_1()
314 if (jj_3R_2()) return true;
318 /** Generated Token Manager. */
319 public XmileExpressionParserTokenManager token_source;
320 SimpleCharStream jj_input_stream;
321 /** Current token. */
326 private Token jj_scanpos, jj_lastpos;
329 final private int[] jj_la1 = new int[9];
330 static private int[] jj_la1_0;
334 private static void jj_la1_init_0() {
335 jj_la1_0 = new int[] {0x7fec,0x7fec,0xc10,0xc10,0x80000,0x310000,0x40000,0x390c10,0x300000,};
337 final private JJCalls[] jj_2_rtns = new JJCalls[1];
338 private boolean jj_rescan = false;
339 private int jj_gc = 0;
341 /** Constructor with InputStream. */
342 public XmileExpressionParser(java.io.InputStream stream) {
345 /** Constructor with InputStream and supplied encoding */
346 public XmileExpressionParser(java.io.InputStream stream, String encoding) {
347 try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
348 token_source = new XmileExpressionParserTokenManager(jj_input_stream);
352 for (int i = 0; i < 9; i++) jj_la1[i] = -1;
353 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
357 public void ReInit(java.io.InputStream stream) {
358 ReInit(stream, null);
361 public void ReInit(java.io.InputStream stream, String encoding) {
362 try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
363 token_source.ReInit(jj_input_stream);
367 for (int i = 0; i < 9; i++) jj_la1[i] = -1;
368 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
372 public XmileExpressionParser(java.io.Reader stream) {
373 jj_input_stream = new SimpleCharStream(stream, 1, 1);
374 token_source = new XmileExpressionParserTokenManager(jj_input_stream);
378 for (int i = 0; i < 9; i++) jj_la1[i] = -1;
379 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
383 public void ReInit(java.io.Reader stream) {
384 jj_input_stream.ReInit(stream, 1, 1);
385 token_source.ReInit(jj_input_stream);
389 for (int i = 0; i < 9; i++) jj_la1[i] = -1;
390 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
393 /** Constructor with generated Token Manager. */
394 public XmileExpressionParser(XmileExpressionParserTokenManager tm) {
399 for (int i = 0; i < 9; i++) jj_la1[i] = -1;
400 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
404 public void ReInit(XmileExpressionParserTokenManager tm) {
409 for (int i = 0; i < 9; i++) jj_la1[i] = -1;
410 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
413 private Token jj_consume_token(int kind) throws ParseException {
415 if ((oldToken = token).next != null) token = token.next;
416 else token = token.next = token_source.getNextToken();
418 if (token.kind == kind) {
422 for (int i = 0; i < jj_2_rtns.length; i++) {
423 JJCalls c = jj_2_rtns[i];
425 if (c.gen < jj_gen) c.first = null;
434 throw generateParseException();
437 @SuppressWarnings("serial")
438 static private final class LookaheadSuccess extends java.lang.Error { }
439 final private LookaheadSuccess jj_ls = new LookaheadSuccess();
440 private boolean jj_scan_token(int kind) {
441 if (jj_scanpos == jj_lastpos) {
443 if (jj_scanpos.next == null) {
444 jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
446 jj_lastpos = jj_scanpos = jj_scanpos.next;
449 jj_scanpos = jj_scanpos.next;
452 int i = 0; Token tok = token;
453 while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
454 if (tok != null) jj_add_error_token(kind, i);
456 if (jj_scanpos.kind != kind) return true;
457 if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
462 /** Get the next Token. */
463 final public Token getNextToken() {
464 if (token.next != null) token = token.next;
465 else token = token.next = token_source.getNextToken();
471 /** Get the specific Token. */
472 final public Token getToken(int index) {
474 for (int i = 0; i < index; i++) {
475 if (t.next != null) t = t.next;
476 else t = t.next = token_source.getNextToken();
481 private int jj_ntk_f() {
482 if ((jj_nt=token.next) == null)
483 return (jj_ntk = (token.next=token_source.getNextToken()).kind);
485 return (jj_ntk = jj_nt.kind);
488 private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
489 private int[] jj_expentry;
490 private int jj_kind = -1;
491 private int[] jj_lasttokens = new int[100];
492 private int jj_endpos;
494 private void jj_add_error_token(int kind, int pos) {
495 if (pos >= 100) return;
496 if (pos == jj_endpos + 1) {
497 jj_lasttokens[jj_endpos++] = kind;
498 } else if (jj_endpos != 0) {
499 jj_expentry = new int[jj_endpos];
500 for (int i = 0; i < jj_endpos; i++) {
501 jj_expentry[i] = jj_lasttokens[i];
503 jj_entries_loop: for (java.util.Iterator<?> it = jj_expentries.iterator(); it.hasNext();) {
504 int[] oldentry = (int[])(it.next());
505 if (oldentry.length == jj_expentry.length) {
506 for (int i = 0; i < jj_expentry.length; i++) {
507 if (oldentry[i] != jj_expentry[i]) {
508 continue jj_entries_loop;
511 jj_expentries.add(jj_expentry);
512 break jj_entries_loop;
515 if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
519 /** Generate ParseException. */
520 public ParseException generateParseException() {
521 jj_expentries.clear();
522 boolean[] la1tokens = new boolean[22];
524 la1tokens[jj_kind] = true;
527 for (int i = 0; i < 9; i++) {
528 if (jj_la1[i] == jj_gen) {
529 for (int j = 0; j < 32; j++) {
530 if ((jj_la1_0[i] & (1<<j)) != 0) {
536 for (int i = 0; i < 22; i++) {
538 jj_expentry = new int[1];
540 jj_expentries.add(jj_expentry);
545 jj_add_error_token(0, 0);
546 int[][] exptokseq = new int[jj_expentries.size()][];
547 for (int i = 0; i < jj_expentries.size(); i++) {
548 exptokseq[i] = jj_expentries.get(i);
550 return new ParseException(token, exptokseq, tokenImage);
553 /** Enable tracing. */
554 final public void enable_tracing() {
557 /** Disable tracing. */
558 final public void disable_tracing() {
561 private void jj_rescan_token() {
563 for (int i = 0; i < 1; i++) {
565 JJCalls p = jj_2_rtns[i];
567 if (p.gen > jj_gen) {
568 jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
570 case 0: jj_3_1(); break;
575 } catch(LookaheadSuccess ls) { }
580 private void jj_save(int index, int xla) {
581 JJCalls p = jj_2_rtns[index];
582 while (p.gen > jj_gen) {
583 if (p.next == null) { p = p.next = new JJCalls(); break; }
586 p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
589 static final class JJCalls {