1 /* Generated By:JavaCC: Do not edit this line. SheetFormulaParser.java */
2 package org.simantics.spreadsheet.graph.parser;
4 import org.simantics.spreadsheet.graph.parser.ast.*;
5 import java.util.ArrayList;
7 import java.util.Collections;
10 * Parser for spreadsheet formulas
11 * @author Antti Villberg
13 public class SheetFormulaParser implements SheetFormulaParserConstants {
15 final public AstValue primary() throws ParseException {
19 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
22 {if (true) return AstNull.NULL;}
26 {if (true) return AstBoolean.TRUE;}
30 {if (true) return AstBoolean.FALSE;}
34 {if (true) return AstBoolean.TRUE;}
38 {if (true) return AstBoolean.FALSE;}
41 jj_consume_token(STRING);
42 {if (true) return new AstString(token.image);}
45 jj_consume_token(LONG_STRING);
46 {if (true) return new AstString(token.image);}
49 jj_consume_token(FLOAT);
50 {if (true) return new AstDouble(token.image);}
53 jj_consume_token(INTEGER);
54 {if (true) return new AstInteger(Integer.parseInt(token.image));}
59 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
61 tok = jj_consume_token(IDENT);
64 tok = jj_consume_token(SHEET_NAME);
69 throw new ParseException();
73 {if (true) return tempRange.inSheet(tok.image);}
74 } else if (jj_2_2(2)) {
76 {if (true) return temp;}
78 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
83 {if (true) return temp;}
89 {if (true) return temp;}
93 {if (true) return temp;}
96 jj_consume_token(IDENT);
97 {if (true) return new AstIdentifier(token.image);}
101 jj_consume_token(-1);
102 throw new ParseException();
106 throw new Error("Missing return statement in function");
109 final public AstApply apply() throws ParseException {
113 tok = jj_consume_token(IDENT);
116 {if (true) return new AstApply(tok.image, null);}
118 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
120 tok = jj_consume_token(IDENT);
124 {if (true) return new AstApply(tok.image, al);}
128 jj_consume_token(-1);
129 throw new ParseException();
132 throw new Error("Missing return statement in function");
135 final public AstValue array() throws ParseException {
136 AstArray arr = new AstArray();
139 jj_consume_token(10);
140 jj_consume_token(11);
141 {if (true) return new AstArray();}
143 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
145 jj_consume_token(10);
148 jj_consume_token(18);
151 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
180 jj_consume_token(11);
181 {if (true) return arr;}
185 jj_consume_token(-1);
186 throw new ParseException();
189 throw new Error("Missing return statement in function");
192 final public AstRange range() throws ParseException {
196 jj_consume_token(CELL_RANGE);
197 {if (true) return new AstRange(token.image);}
199 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
201 jj_consume_token(CELL_SINGLE);
202 {if (true) return new AstRange(token.image);}
205 jj_consume_token(43);
206 {if (true) return AstRange.REF;}
210 jj_consume_token(-1);
211 throw new ParseException();
214 throw new Error("Missing return statement in function");
217 final public AstArgList arglist() throws ParseException {
218 AstArgList ret = new AstArgList();
224 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
233 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
238 jj_consume_token(18);
242 jj_consume_token(-1);
243 throw new ParseException();
245 temp = AstNothing.INSTANCE;
246 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
274 {if (true) return ret;}
275 throw new Error("Missing return statement in function");
278 final public AstValue relation() throws ParseException {
282 ex = arithmetic_expression();
283 ret = new AstRelation(ex);
284 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
292 ex = arithmetic_expression();
293 ret.setRight(op, ex);
299 {if (true) return ret.simplify();}
300 throw new Error("Missing return statement in function");
303 final public String rel_op() throws ParseException {
304 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
306 jj_consume_token(22);
307 {if (true) return "< ";}
310 jj_consume_token(44);
311 {if (true) return "<=";}
314 jj_consume_token(23);
315 {if (true) return " >";}
318 jj_consume_token(45);
319 {if (true) return " >=";}
322 jj_consume_token(21);
323 {if (true) return "=";}
326 jj_consume_token(24);
327 {if (true) return "<>";}
331 jj_consume_token(-1);
332 throw new ParseException();
334 throw new Error("Missing return statement in function");
338 final public AstValue arithmetic_expression() throws ParseException {
339 AstArithmeticExpression ret;
342 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
353 ret = new AstArithmeticExpression(op, temp);
365 {if (true) return ret.simplify();}
366 throw new Error("Missing return statement in function");
369 final public String add_op() throws ParseException {
370 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
372 jj_consume_token(14);
373 {if (true) return "+";}
376 jj_consume_token(46);
377 {if (true) return "-";}
380 jj_consume_token(13);
381 {if (true) return "&";}
385 jj_consume_token(-1);
386 throw new ParseException();
388 throw new Error("Missing return statement in function");
391 final public AstValue term() throws ParseException {
396 ret = new AstTerm(temp);
399 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
412 {if (true) return ret.simplify();}
413 throw new Error("Missing return statement in function");
416 final public String mul_op() throws ParseException {
417 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
419 jj_consume_token(15);
420 {if (true) return "*";}
423 jj_consume_token(16);
424 {if (true) return "/";}
428 jj_consume_token(-1);
429 throw new ParseException();
431 throw new Error("Missing return statement in function");
434 final public AstValue factor() throws ParseException {
439 ret = new AstFactor(temp);
442 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
454 {if (true) return ret.simplify();}
455 throw new Error("Missing return statement in function");
458 final public String factor_op() throws ParseException {
459 jj_consume_token(17);
460 {if (true) return "^";}
461 throw new Error("Missing return statement in function");
464 private boolean jj_2_1(int xla) {
465 jj_la = xla; jj_lastpos = jj_scanpos = token;
466 try { return !jj_3_1(); }
467 catch(LookaheadSuccess ls) { return true; }
468 finally { jj_save(0, xla); }
471 private boolean jj_2_2(int xla) {
472 jj_la = xla; jj_lastpos = jj_scanpos = token;
473 try { return !jj_3_2(); }
474 catch(LookaheadSuccess ls) { return true; }
475 finally { jj_save(1, xla); }
478 private boolean jj_2_3(int xla) {
479 jj_la = xla; jj_lastpos = jj_scanpos = token;
480 try { return !jj_3_3(); }
481 catch(LookaheadSuccess ls) { return true; }
482 finally { jj_save(2, xla); }
485 private boolean jj_2_4(int xla) {
486 jj_la = xla; jj_lastpos = jj_scanpos = token;
487 try { return !jj_3_4(); }
488 catch(LookaheadSuccess ls) { return true; }
489 finally { jj_save(3, xla); }
492 private boolean jj_2_5(int xla) {
493 jj_la = xla; jj_lastpos = jj_scanpos = token;
494 try { return !jj_3_5(); }
495 catch(LookaheadSuccess ls) { return true; }
496 finally { jj_save(4, xla); }
499 private boolean jj_2_6(int xla) {
500 jj_la = xla; jj_lastpos = jj_scanpos = token;
501 try { return !jj_3_6(); }
502 catch(LookaheadSuccess ls) { return true; }
503 finally { jj_save(5, xla); }
506 private boolean jj_3R_29() {
511 if (jj_3R_32()) return true;
516 private boolean jj_3_4() {
517 if (jj_scan_token(10)) return true;
518 if (jj_scan_token(11)) return true;
522 private boolean jj_3R_7() {
529 if (jj_3R_12()) return true;
535 private boolean jj_3R_10() {
536 if (jj_scan_token(14)) return true;
540 private boolean jj_3R_12() {
541 if (jj_scan_token(13)) return true;
545 private boolean jj_3R_9() {
546 if (jj_scan_token(IDENT)) return true;
547 if (jj_scan_token(5)) return true;
551 private boolean jj_3R_6() {
556 if (jj_3R_9()) return true;
561 private boolean jj_3_3() {
562 if (jj_scan_token(IDENT)) return true;
563 if (jj_scan_token(5)) return true;
564 if (jj_scan_token(6)) return true;
568 private boolean jj_3R_27() {
569 if (jj_scan_token(IDENT)) return true;
573 private boolean jj_3R_26() {
574 if (jj_3R_29()) return true;
578 private boolean jj_3R_25() {
579 if (jj_3R_28()) return true;
583 private boolean jj_3R_24() {
584 if (jj_scan_token(5)) return true;
588 private boolean jj_3_2() {
589 if (jj_3R_6()) return true;
593 private boolean jj_3_1() {
596 if (jj_scan_token(38)) {
598 if (jj_scan_token(33)) return true;
600 if (jj_scan_token(42)) return true;
604 private boolean jj_3R_23() {
605 if (jj_scan_token(INTEGER)) return true;
609 private boolean jj_3_6() {
610 if (jj_3R_7()) return true;
611 if (jj_3R_8()) return true;
615 private boolean jj_3R_22() {
616 if (jj_scan_token(FLOAT)) return true;
620 private boolean jj_3R_21() {
621 if (jj_scan_token(LONG_STRING)) return true;
625 private boolean jj_3R_20() {
626 if (jj_scan_token(STRING)) return true;
630 private boolean jj_3R_19() {
631 if (jj_scan_token(29)) return true;
635 private boolean jj_3R_18() {
636 if (jj_scan_token(28)) return true;
640 private boolean jj_3R_17() {
641 if (jj_scan_token(26)) return true;
645 private boolean jj_3R_11() {
646 if (jj_scan_token(46)) return true;
650 private boolean jj_3R_16() {
651 if (jj_scan_token(25)) return true;
655 private boolean jj_3R_13() {
656 if (jj_3R_14()) return true;
660 private boolean jj_3R_14() {
691 if (jj_3R_27()) return true;
709 private boolean jj_3R_15() {
710 if (jj_scan_token(27)) return true;
714 private boolean jj_3R_31() {
715 if (jj_scan_token(43)) return true;
719 private boolean jj_3R_30() {
720 if (jj_scan_token(CELL_SINGLE)) return true;
724 private boolean jj_3R_28() {
731 if (jj_3R_31()) return true;
737 private boolean jj_3_5() {
738 if (jj_scan_token(CELL_RANGE)) return true;
742 private boolean jj_3R_8() {
743 if (jj_3R_13()) return true;
747 private boolean jj_3R_32() {
748 if (jj_scan_token(10)) return true;
752 /** Generated Token Manager. */
753 public SheetFormulaParserTokenManager token_source;
754 SimpleCharStream jj_input_stream;
755 /** Current token. */
760 private Token jj_scanpos, jj_lastpos;
763 final private int[] jj_la1 = new int[17];
764 static private int[] jj_la1_0;
765 static private int[] jj_la1_1;
770 private static void jj_la1_init_0() {
771 jj_la1_0 = new int[] {0x0,0x3e000000,0x420,0x0,0x3e006420,0x400,0x0,0x40010,0x40010,0x3e006420,0x1e00000,0x1e00000,0x6000,0x6000,0x18000,0x18000,0x20000,};
773 private static void jj_la1_init_1() {
774 jj_la1_1 = new int[] {0x42,0x305,0x868,0x40,0x4b6f,0x0,0x808,0x0,0x0,0x4b6f,0x3000,0x3000,0x4000,0x4000,0x0,0x0,0x0,};
776 final private JJCalls[] jj_2_rtns = new JJCalls[6];
777 private boolean jj_rescan = false;
778 private int jj_gc = 0;
780 /** Constructor with InputStream. */
781 public SheetFormulaParser(java.io.InputStream stream) {
784 /** Constructor with InputStream and supplied encoding */
785 public SheetFormulaParser(java.io.InputStream stream, String encoding) {
786 try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
787 token_source = new SheetFormulaParserTokenManager(jj_input_stream);
791 for (int i = 0; i < 17; i++) jj_la1[i] = -1;
792 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
796 public void ReInit(java.io.InputStream stream) {
797 ReInit(stream, null);
800 public void ReInit(java.io.InputStream stream, String encoding) {
801 try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
802 token_source.ReInit(jj_input_stream);
806 for (int i = 0; i < 17; i++) jj_la1[i] = -1;
807 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
811 public SheetFormulaParser(java.io.Reader stream) {
812 jj_input_stream = new SimpleCharStream(stream, 1, 1);
813 token_source = new SheetFormulaParserTokenManager(jj_input_stream);
817 for (int i = 0; i < 17; i++) jj_la1[i] = -1;
818 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
822 public void ReInit(java.io.Reader stream) {
823 jj_input_stream.ReInit(stream, 1, 1);
824 token_source.ReInit(jj_input_stream);
828 for (int i = 0; i < 17; i++) jj_la1[i] = -1;
829 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
832 /** Constructor with generated Token Manager. */
833 public SheetFormulaParser(SheetFormulaParserTokenManager tm) {
838 for (int i = 0; i < 17; i++) jj_la1[i] = -1;
839 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
843 public void ReInit(SheetFormulaParserTokenManager tm) {
848 for (int i = 0; i < 17; i++) jj_la1[i] = -1;
849 for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
852 private Token jj_consume_token(int kind) throws ParseException {
854 if ((oldToken = token).next != null) token = token.next;
855 else token = token.next = token_source.getNextToken();
857 if (token.kind == kind) {
861 for (int i = 0; i < jj_2_rtns.length; i++) {
862 JJCalls c = jj_2_rtns[i];
864 if (c.gen < jj_gen) c.first = null;
873 throw generateParseException();
876 static private final class LookaheadSuccess extends java.lang.Error { }
877 final private LookaheadSuccess jj_ls = new LookaheadSuccess();
878 private boolean jj_scan_token(int kind) {
879 if (jj_scanpos == jj_lastpos) {
881 if (jj_scanpos.next == null) {
882 jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
884 jj_lastpos = jj_scanpos = jj_scanpos.next;
887 jj_scanpos = jj_scanpos.next;
890 int i = 0; Token tok = token;
891 while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
892 if (tok != null) jj_add_error_token(kind, i);
894 if (jj_scanpos.kind != kind) return true;
895 if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
900 /** Get the next Token. */
901 final public Token getNextToken() {
902 if (token.next != null) token = token.next;
903 else token = token.next = token_source.getNextToken();
909 /** Get the specific Token. */
910 final public Token getToken(int index) {
912 for (int i = 0; i < index; i++) {
913 if (t.next != null) t = t.next;
914 else t = t.next = token_source.getNextToken();
919 private int jj_ntk() {
920 if ((jj_nt=token.next) == null)
921 return (jj_ntk = (token.next=token_source.getNextToken()).kind);
923 return (jj_ntk = jj_nt.kind);
926 private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
927 private int[] jj_expentry;
928 private int jj_kind = -1;
929 private int[] jj_lasttokens = new int[100];
930 private int jj_endpos;
932 private void jj_add_error_token(int kind, int pos) {
933 if (pos >= 100) return;
934 if (pos == jj_endpos + 1) {
935 jj_lasttokens[jj_endpos++] = kind;
936 } else if (jj_endpos != 0) {
937 jj_expentry = new int[jj_endpos];
938 for (int i = 0; i < jj_endpos; i++) {
939 jj_expentry[i] = jj_lasttokens[i];
941 jj_entries_loop: for (java.util.Iterator<?> it = jj_expentries.iterator(); it.hasNext();) {
942 int[] oldentry = (int[])(it.next());
943 if (oldentry.length == jj_expentry.length) {
944 for (int i = 0; i < jj_expentry.length; i++) {
945 if (oldentry[i] != jj_expentry[i]) {
946 continue jj_entries_loop;
949 jj_expentries.add(jj_expentry);
950 break jj_entries_loop;
953 if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
957 /** Generate ParseException. */
958 public ParseException generateParseException() {
959 jj_expentries.clear();
960 boolean[] la1tokens = new boolean[47];
962 la1tokens[jj_kind] = true;
965 for (int i = 0; i < 17; i++) {
966 if (jj_la1[i] == jj_gen) {
967 for (int j = 0; j < 32; j++) {
968 if ((jj_la1_0[i] & (1<<j)) != 0) {
971 if ((jj_la1_1[i] & (1<<j)) != 0) {
972 la1tokens[32+j] = true;
977 for (int i = 0; i < 47; i++) {
979 jj_expentry = new int[1];
981 jj_expentries.add(jj_expentry);
986 jj_add_error_token(0, 0);
987 int[][] exptokseq = new int[jj_expentries.size()][];
988 for (int i = 0; i < jj_expentries.size(); i++) {
989 exptokseq[i] = jj_expentries.get(i);
991 return new ParseException(token, exptokseq, tokenImage);
994 /** Enable tracing. */
995 final public void enable_tracing() {
998 /** Disable tracing. */
999 final public void disable_tracing() {
1002 private void jj_rescan_token() {
1004 for (int i = 0; i < 6; i++) {
1006 JJCalls p = jj_2_rtns[i];
1008 if (p.gen > jj_gen) {
1009 jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
1011 case 0: jj_3_1(); break;
1012 case 1: jj_3_2(); break;
1013 case 2: jj_3_3(); break;
1014 case 3: jj_3_4(); break;
1015 case 4: jj_3_5(); break;
1016 case 5: jj_3_6(); break;
1020 } while (p != null);
1021 } catch(LookaheadSuccess ls) { }
1026 private void jj_save(int index, int xla) {
1027 JJCalls p = jj_2_rtns[index];
1028 while (p.gen > jj_gen) {
1029 if (p.next == null) { p = p.next = new JJCalls(); break; }
1032 p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
1035 static final class JJCalls {