1 8 13 package jfun.parsec; 14 15 16 70 public final class Parsers { 71 private static boolean debugged = true; 72 76 public synchronized static boolean isDebugEnabled() { 77 return debugged; 78 } 79 84 public synchronized static void setDebug(boolean debugged) { 85 Parsers.debugged = debugged; 86 } 87 101 public static <R> R runParser(final CharSequence src, final Parser<R> p, 102 final PositionMap pmap, final String module) { 103 final ScannerState ctxt = new ScannerState(src, 0, module, pmap, null); 104 return ParserInternals.runParser(ctxt, p, pmap); 105 } 106 107 116 public static <R> R runParser(final CharSequence src, final Parser<R> p, 117 final String module) { 118 return runParser(src, p, new DefaultPositionMap(src, 1, 1), module); 119 } 120 121 148 public static <R> R runParser(final Tok[] toks, 149 final int end_index, final Parser<R> p, final ShowToken show, 150 final String eof_title, final PositionMap pmap, final String module) 151 throws ParserException { 152 final ParserState s0 = new ParserState(null, toks, 0, module, pmap, 153 end_index, eof_title, show); 154 return ParserInternals.runParser(s0, p, pmap); 155 } 156 157 167 public static Parser<?> runnable(String name, Runnable runnable) { 168 return new ActionParser(name, runnable); 169 } 170 171 179 public static Parser<?> runnable(Runnable runnable) { 180 return runnable("runnable", runnable); 181 } 182 183 199 public static <R> Parser<R> parseTokens( 200 final Parser<Tok[]> lexer, final Parser<R> p, 201 final String module) { 202 return parseTokens("parseTokens", lexer, p, module); 203 } 204 205 223 public static <R> Parser<R> parseTokens(final String name, 224 final Parser<Tok[]> lexer, final Parser<R> p, 225 final String module) { 226 return parseTokens(name, "EOF", Token2String.instance(), lexer, p, module); 227 } 228 229 249 public static <R> Parser<R> parseTokens(final String eof_title, 250 final ShowToken show, 251 final Parser<Tok[]> lexer, final Parser<R> p, String module) { 252 return parseTokens("parseTokens", eof_title, show, lexer, p, module); 253 } 254 255 277 public static <R> Parser<R> parseTokens(final String name, 278 final String eof_title, final ShowToken show, 279 final Parser<Tok[]> lexer, final Parser<R> p, 280 final String module) { 281 return new NestedParser<R>(name, show, module, lexer, p, eof_title); 282 } 283 284 285 private static final Parser<?> _one = one("one"); 286 287 293 public static Parser<?> one() { 294 return _one; 295 } 296 297 305 public static Parser<?> one(final String name) { 306 return new OneParser(name); 307 } 308 309 private static final Parser _zero = zero("zero"); 310 311 318 public static <x> Parser<x> zero() { 319 return _zero; 320 } 321 322 331 public static <x> Parser<x> zero(final String name) { 332 return new ZeroParser<x>(name); 333 } 334 335 342 public static <R> Parser<R> retn(final R r) { 343 return retn("return", r); 344 } 345 346 355 public static <R> Parser<R> retn(final String name, final R r) { 356 return new ReturnParser<R>(name, r); 357 } 358 359 372 public static <From, To> Parser<To> bind(final String name, 373 final Parser<From> p, final ToParser<? super From, To> f) { 374 return new Parser<To>(name) { 375 boolean apply(final ParseContext ctxt) { 376 if (!p.parse(ctxt)) 377 return false; 378 return runNext(ctxt, f); 379 } 380 }; 381 } 382 383 private static final ToParser _toReturn = toReturn("return"); 384 385 392 public static <T> ToParser<T, T> toReturn() { 393 return _toReturn; 394 } 395 396 403 public static <T> ToParser<T, T> toReturn(final String name) { 404 return new ToReturn<T>(name); 405 } 406 407 private static final ToParser _toOne = toParser(one()); 408 409 416 public static <x> ToParser<x, ?> toOne() { 417 return _toOne; 418 } 419 420 428 public static <x> ToParser<x, ?> toOne(final String name) { 429 Parser<?> p = one(name); 430 return toParser(p); 431 } 432 433 private static final ToParser _toZero = toParser(zero()); 434 435 442 public static <x, y> ToParser<x, y> toZero() { 443 return _toZero; 444 } 445 446 454 public static <x, y> ToParser<x, y> toZero(final String name) { 455 Parser<y> p = zero(name); 456 return toParser(p); 457 } 458 459 467 public static <x, R> ToParser<x, R> toParser(final Parser<R> parser){ 468 return new ToParser<x, R>() { 469 public Parser<R> toParser(final x v) { 470 return parser; 471 } 472 }; 473 } 474 485 public static <T> ToParser<T, T> bindAll(final ToParser<T, T>... binders) { 486 return bindAll("bindAll", binders); 487 } 488 489 501 public static <T> ToParser<T, T> bindAll(final String name, 502 final ToParser<T, T>... binders) { 503 if (binders.length == 0) 504 return toReturn(); 505 if (binders.length == 1) 506 return binders[0]; 507 return new AllBinders<T>(name, binders); 508 } 509 510 520 public static <R> Parser<R> seq(final Parser<?> p1, final Parser<R> p2) { 521 return seq(">>", p1, p2); 522 } 523 524 535 public static <R> Parser<R> seq(final String name, final Parser<?> p1, 536 final Parser<R> p2) { 537 return new SeqParser<R>(name, p1, p2); 540 } 541 542 552 public static <R> Parser<R> seq(final Parser<?> p1, final Parser<?> p2, 553 final Parser<R> p3) { 554 return seq(">>", p1, p2, p3); 555 } 556 557 568 public static <R> Parser<R> seq(final String name, final Parser<?> p1, 569 final Parser<?> p2, final Parser<R> p3) { 570 return new Seq3Parser<R>(name, p3, p1, p2); 571 } 572 573 584 public static <R> Parser<R> seq(final Parser<?> p1, final Parser<?> p2, 585 final Parser<?> p3, final Parser<R> p4) { 586 return seq(">>", p1, p2, p3, p4); 587 } 588 589 601 public static <R> Parser<R> seq(final String name, final Parser<?> p1, 602 final Parser<?> p2, final Parser<?> p3, final Parser<R> p4) { 603 return new Seq4Parser<R>(name, p4, p1, p2, p3); 604 } 605 606 618 public static <R> Parser<R> seq(final Parser<?> p1, final Parser<?> p2, 619 final Parser<?> p3, final Parser<?> p4, final Parser<R> p5) { 620 return seq(">>", p1, p2, p3, p4, p5); 621 } 622 623 636 public static <R> Parser<R> seq(final String name, final Parser<?> p1, 637 final Parser<?> p2, final Parser<?> p3, final Parser<?> p4, 638 final Parser<R> p5) { 639 return new Seq5Parser<R>(name, p1, p3, p5, p4, p2); 640 } 641 642 648 public static <A,B> Parser<Pair<A,B>> pair(Parser<A> p1, Parser<B> p2){ 649 return pair("pair", p1, p2); 650 } 651 658 public static <A,B,C> Parser<Tuple3<A,B,C>> tuple(Parser<A> p1, Parser<B> p2, Parser<C> p3){ 659 return tuple("tuple3", p1, p2, p3); 660 } 661 669 public static <A,B,C,D> Parser<Tuple4<A,B,C,D>> tuple( 670 Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4){ 671 return tuple("tuple4", p1, p2, p3, p4); 672 } 673 682 public static <A,B,C,D,E> Parser<Tuple5<A,B,C,D,E>> tuple( 683 Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Parser<E> p5){ 684 return tuple("tuple5", p1, p2, p3, p4, p5); 685 } 686 693 public static <A,B> Parser<Pair<A,B>> pair(String name, Parser<A> p1, Parser<B> p2){ 694 final Map2<A,B,Pair<A,B>> m2 = Maps.id2(); 695 return map2(name, p1, p2, m2); 696 } 697 705 public static <A,B,C> Parser<Tuple3<A,B,C>> tuple(String name, 706 Parser<A> p1, Parser<B> p2, Parser<C> p3){ 707 final Map3<A,B,C,Tuple3<A,B,C>> m3 = Maps.id3(); 708 return map3(name, p1, p2, p3, m3); 709 } 710 719 public static <A,B,C,D> Parser<Tuple4<A,B,C,D>> tuple(String name, 720 Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4){ 721 final Map4<A,B,C,D,Tuple4<A,B,C,D>> m4 = Maps.id4(); 722 return map4(name, p1, p2, p3, p4, m4); 723 } 724 734 public static <A,B,C,D,E> Parser<Tuple5<A,B,C,D,E>> tuple(String name, 735 Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Parser<E> p5){ 736 final Map5<A,B,C,D,E,Tuple5<A,B,C,D,E>> m5 = Maps.id5(); 737 return map5(name, p1, p2, p3, p4, p5, m5); 738 } 739 751 public static Parser<?> sequence(final Parser<?>... ps) { 752 return seqAll(ps); 753 } 754 755 768 public static Parser<?> sequence(final String name, final Parser<?>... ps) { 769 return seqAll(name, ps); 770 } 771 772 784 public static Parser<Object > seqAll(final Parser<?>[] ps) { 785 return seqAll("seqAll", ps); 786 } 787 788 802 public static Parser<Object > seqAll(final String name, final Parser<?>[] ps) { 803 if (ps.length == 0) 804 return one().convert(); 805 if (ps.length == 1) 806 return ps[0].convert(); 807 return _seqAll(name, ps); 808 } 809 810 private static Parser<Object > _seqAll(final String name, final Parser<?>[] ps) { 811 return new SequenceParser(name, ps); 812 } 813 814 824 public static <R> Parser<R> isReturn(final String name, 825 final ObjectPredicate<R> op) { 826 return new IsReturnParser<R>(name, op); 827 } 828 829 842 public static <R> Parser<R> isReturn(final String name, final Parser<R> p, 843 final ObjectPredicate<? super R> op) { 844 final Parser<R> p2 = isReturn(name, op).convert(); 845 return seq(name, p, p2).atomize(name); 846 } 847 848 863 public static <R> Parser<R> isReturn(final String name, final Parser<R> p, 864 final ObjectPredicate<? super R> op, final String expecting) { 865 final Parser<R> p2 = isReturn(name, op).convert(); 866 return p.seq(name, p2.label(name, expecting)).atomize(name); 867 } 868 869 878 @Deprecated 879 public static Parser<?> isState(final ObjectPredicate<Object > op) { 880 return isState("isState", op); 881 } 882 883 894 @Deprecated 895 public static Parser<?> isState(final String name, 896 final ObjectPredicate<Object > op) { 897 return new Parser<Object >(name) { 898 boolean apply(final ParseContext ctxt) { 899 final Object r = ctxt.getUserState(); 900 return op.isObject(r); 901 } 902 }; 903 } 904 905 917 public static <R> Parser<R> step(final String name, final int n, 918 final Parser<R> p) { 919 if (n < 0) 920 throw new IllegalArgumentException ("" + n + "<0"); 921 return new StepParser<R>(name, p, n); 922 } 923 924 949 public static <R> Parser<R> lookahead(final String name, final int toknum, 950 final Parser<R> p) { 951 if (toknum <= 0) 952 return p; 953 return new LookaheadParser<R>(name, p, toknum); 954 } 955 956 970 public static <R> Parser<R> mapn(final Parser<?>[] ps, final Mapn<R> mn) { 971 return mapn("mapn", ps, mn); 972 } 973 974 990 public static <R> Parser<R> mapn(final String name, final Parser<?>[] ps, 991 final Mapn<R> mn) { 992 if (ps.length == 0) { 993 final R r = mn.map(_empty_array); 994 return retn(r); 995 } 996 return mapn(name, ArrayFactories.defaultFactory(), ps, mn); 997 } 998 999 1015 public static <E, R> Parser<R> mapn(final Class <? super E> etype, 1016 final Parser<E>[] ps, final Mapn<R> mn) { 1017 return mapn("mapn", etype, ps, mn); 1018 } 1019 1020 1038 public static <E, R> Parser<R> mapn(final String name, 1039 final Class <? super E> etype, final Parser<E>[] ps, final Mapn<R> mn) { 1040 return mapn(name, ArrayFactories.typedFactory(etype), ps, mn); 1041 } 1042 1043 1059 public static <E, R> Parser<R> mapn(final ArrayFactory<?> af, 1060 final Parser<?>[] ps, final Mapn<R> mn) { 1061 return mapn("mapn", af, ps, mn); 1062 } 1063 1064 1082 public static <R> Parser<R> mapn(final String name, 1083 final ArrayFactory<?> af, final Parser<?>[] ps, final Mapn<R> mn) { 1084 return _mapn(name, af, ps, mn); 1085 } 1086 1087 private static <R> Parser<R> _mapn(final String name, 1088 final ArrayFactory<?> af, final Parser<?>[] ps, final Mapn<R> mn) { 1089 return new MapnParser<R>(name, mn, af, ps); 1090 } 1091 1092 1101 public static <R> Parser<R> fail(final String msg) { 1102 return fail("fail", msg); 1103 } 1104 1105 1116 public static <R> Parser<R> fail(final String name, final String msg) { 1117 return new FailureParser<R>(name, msg); 1118 } 1119 1120 1137 public static <C, R> Parser<R> ifelse(final String name, final Parser<C> p, 1138 final ToParser<? super C, R> yes, final Parser<? extends R> no) { 1139 return new IfElseParser<R, C>(name, no, p, yes); 1140 } 1141 1142 1159 public static <C, R> Parser<R> ifelse(final String name, final Parser<C> p, 1160 final Parser<R> yes, final Parser<? extends R> no) { 1161 final ToParser<C, R> binder = toParser(yes); 1162 return ifelse(name, p, binder, no); 1163 1170 } 1171 1182 public static <R> Parser<R> plus(final Parser<R> p1, final Parser<? extends R> p2){ 1183 return plus("plus", p1, p2); 1184 } 1185 1197 public static <R> Parser<R> plus(final String name, final Parser<R> p1, final Parser<? extends R> p2){ 1198 return plus(name, new Parser[]{p1, p2}); 1199 1211 } 1212 1223 public static <R> Parser<R> plus(final Parser<R> p1, 1224 final Parser<? extends R> p2, final Parser<? extends R> p3){ 1225 return plus("plus", p1, p2, p3); 1226 } 1227 1239 public static <R> Parser<R> plus(final String name, final Parser<R> p1, 1240 final Parser<? extends R> p2, final Parser<? extends R> p3){ 1241 return plus(name, new Parser[]{p1, p2, p3}); 1242 } 1243 1255 public static <R> Parser<R> plus(final Parser<R> p1, 1256 final Parser<? extends R> p2, final Parser<? extends R> p3, final Parser<? extends R> p4){ 1257 return plus("plus", p1, p2, p3, p4); 1258 } 1259 1272 public static <R> Parser<R> plus(final String name, final Parser<R> p1, final Parser<? extends R> p2, 1273 final Parser<? extends R> p3, final Parser<? extends R> p4){ 1274 return plus(name, new Parser[]{p1, p2, p3, p4}); 1275 } 1276 1289 public static <R> Parser<R> plus(final Parser<R> p1, final Parser<? extends R> p2, final Parser<? extends R> p3, 1290 final Parser<? extends R> p4, final Parser<? extends R> p5){ 1291 return plus("plus", p1, p2, p3, p4, p5); 1292 } 1293 1307 public static <R> Parser<R> plus(final String name, 1308 final Parser<R> p1, final Parser<? extends R> p2, final Parser<? extends R> p3, 1309 final Parser<? extends R> p4, final Parser<? extends R> p5){ 1310 return plus(name, new Parser[]{p1, p2, p3, p4, p5}); 1311 } 1312 1322 public static <R> Parser<R> plus(String name, final Parser<R>... ps) { 1323 return sum(name, ps).convert(); 1324 } 1325 1326 1334 public static <R> Parser<R> plus(final Parser<R>... ps) { 1335 return sum("plus", ps).convert(); 1336 } 1337 1338 1352 public static Parser<Object > sum(final Parser<?>... ps) { 1353 return sum("sum", ps); 1354 } 1355 1356 1372 public static Parser<Object > sum(final String name, final Parser<?>... ps) { 1373 if (ps.length == 0) 1374 return zero(); 1375 if (ps.length == 1) 1376 return ps[0].convert(); 1377 return _plusAll(name, ps); 1378 } 1379 1380 1392 public static <R> Parser<R> longer(final String name, final Parser<R> p1, 1393 final Parser<R> p2) { 1394 return _longest(name, p1, p2); 1395 } 1396 1397 1408 1409 public static <R> Parser<R> longest(final String name, final Parser<R>... ps) { 1410 if (ps.length == 0) 1411 return zero(); 1412 if (ps.length == 1) 1413 return ps[0]; 1414 return _longest(name, ps); 1415 } 1416 1417 1429 1430 public static <R> Parser<R> shorter(final String name, final Parser<R> p1, 1431 final Parser<R> p2) { 1432 return _shortest(name, p1, p2); 1433 } 1434 1435 1446 1447 public static <R> Parser<R> shortest(final String name, final Parser<R>... ps) { 1448 if (ps.length == 0) 1449 return zero(); 1450 if (ps.length == 1) 1451 return ps[0]; 1452 return _shortest(name, ps); 1453 } 1454 1455 private static <R> Parser<R> _shortest(final String name, 1456 final Parser<R>... ps) { 1457 return _alternate(name, ps, IntOrders.lt()); 1458 } 1459 1460 private static <R> Parser<R> _longest(final String name, 1461 final Parser<R>... ps) { 1462 return _alternate(name, ps, IntOrders.gt()); 1463 } 1464 1465 1475 public static <R> Parser<R> longer(final Parser<R> p1, final Parser<R> p2) { 1476 return longer("longer", p1, p2); 1477 } 1478 1479 1488 1489 public static <R> Parser<R> longest(final Parser<R>... ps) { 1490 return longest("longest", ps); 1491 } 1492 1493 1503 1504 public static <R> Parser<R> shorter(final Parser<R> p1, final Parser<R> p2) { 1505 return shorter("shorter", p1, p2); 1506 } 1507 1508 1517 1518 public static <R> Parser<R> shortest(final Parser<R>... ps) { 1519 return shortest("shortest", ps); 1520 } 1521 1530 public static Parser<Object > or(String name, final Parser<?>... alternatives){ 1531 return new OrParser(name, alternatives); 1532 } 1533 1541 public static Parser<Object > or(final Parser<?>... alternatives){ 1542 return or("or", alternatives); 1543 } 1544 1553 public static <T> Parser<T> alt(String name, final Parser<T>... alternatives){ 1554 return new OrParser(name, alternatives); 1555 } 1556 1564 public static <T> Parser<T> alt(final Parser<T>... alternatives){ 1565 return alt("or", alternatives); 1566 } 1567 private static Parser<Object > _plusAll(final String name, final Parser<?>... ps) { 1568 return new SumParser(name, ps); 1569 } 1570 1571 private static <R> Parser<R> _alternate(final String name, 1572 final Parser<R>[] ps, final IntOrder ord) { 1573 return new BestParser<R>(name, ps, ord); 1574 } 1575 1576 1577 1578 1579 1589 public static <R> Parser<R> isConsumed(final Parser<R> p) { 1590 return isConsumed("isConsumed", p); 1591 } 1592 1593 1605 public static <R> Parser<R> isConsumed(final Parser<R> p, final String err) { 1606 return isConsumed("isConsumed", p, err); 1607 } 1608 1609 1621 public static <R> Parser<R> isConsumed(final String name, final Parser<R> p) { 1622 return isConsumed(name, p, "input not consumed"); 1623 } 1624 1625 1639 public static <R> Parser<R> isConsumed(final String name, final Parser<R> p, 1640 final String err) { 1641 return new IsConsumedParser<R>(name, p, err); 1642 } 1643 1644 1656 public static <R> Parser<R> notConsumed(final Parser<R> p) { 1657 return notConsumed("notConsumed", p); 1658 } 1659 1660 1674 public static <R> Parser<R> notConsumed(final Parser<R> p, final String err) { 1675 return notConsumed("notConsumed", p, err); 1676 } 1677 1678 1692 public static <R> Parser<R> notConsumed(final String name, final Parser<R> p) { 1693 return notConsumed(name, p, "input consumed"); 1694 } 1695 1696 1712 public static <R> Parser<R> notConsumed(final String name, final Parser<R> p, 1713 final String err) { 1714 return new NotConsumedParser<R>(name, p, err); 1715 } 1716 1717 1727 public static <R> Parser<R> lazy(final ParserEval<R> p) { 1728 return lazy("lazy", p); 1729 } 1730 1731 1738 public static <R> Parser<R> lazy(final Parser<R>[] placeholder, final int pos){ 1739 return lazy(new ParserEval<R>(){ 1740 public Parser<R> eval() { 1741 return placeholder[pos]; 1742 } 1743 }); 1744 } 1745 1751 public static <R> Parser<R> lazy(final Parser<R>[] placeholder){ 1752 return lazy(placeholder, 0); 1753 } 1754 1766 public static <R> Parser<R> lazy(final String name, final ParserEval<R> p) { 1767 return LazyParser.instance(name, p); 1768 } 1769 1770 private static final Parser<Object > _getState = getState("getState"); 1771 1772 1780 @Deprecated 1781 public static Parser<Object > getState() { 1782 return _getState; 1783 } 1784 1785 1794 @Deprecated 1795 public static Parser<Object > getState(final String name) { 1796 return transformState(name, Maps.id()); 1797 } 1798 1799 1809 @Deprecated 1810 public static Parser<Object > setState(final Object s) { 1811 return setState("setState", s); 1812 } 1813 1814 1826 @Deprecated 1827 public static Parser<Object > setState(final String name, final Object s) { 1828 return transformState(name, Maps.cnst(s)); 1829 } 1830 1831 1842 @Deprecated 1843 public static <State> Parser<State> transformState(final Map<State, ?> m) { 1844 return transformState("transformState", m); 1845 } 1846 1847 1860 @Deprecated 1861 public static <State> Parser<State> transformState(final String name, 1862 final Map<State, ?> m) { 1863 return new Parser<State>(name) { 1864 boolean apply(final ParseContext ctxt) { 1865 final Object ustate = ctxt.getUserState(); 1866 ctxt.setUserState(m.map((State) ustate)); 1867 ctxt.setReturn(ustate); 1868 return true; 1869 } 1870 }; 1871 } 1872 1873 private static final Parser<Integer > _getIndex = getIndex("getIndex"); 1874 1875 1882 public static Parser<Integer > getIndex() { 1883 return _getIndex; 1884 } 1885 1886 1895 public static Parser<Integer > getIndex(final String name) { 1896 return new GetIndexParser(name); 1897 } 1898 1899 1910 public static <R> Parser<R> peek(final String name, final Parser<R> p) { 1911 return new PeekParser<R>(name, p); 1912 } 1913 1914 1926 public static <R> Parser<R> atomize(final String name, final Parser<R> p) { 1927 return new AtomicParser<R>(name, p); 1928 } 1929 1930 1941 public static <R> Parser<R> tryParser(final Parser<R> p, 1942 final Catch<? extends R> hdl) { 1943 return tryParser("try_catch", p, hdl); 1944 } 1945 1946 1959 public static <R> Parser<R> tryParser(final String name, final Parser<R> p, 1960 final Catch<? extends R> hdl) { 1961 return new TryParser<R>(name, p, hdl); 1962 } 1963 1964 1977 public static <R, From> Parser<R> map(final String name, 1978 final Parser<From> p, final Map<? super From, R> m) { 1979 return new MapParser<R, From>(name, p, m); 1980 } 1981 1982 1991 public static <x> Parser<x> unexpected(final String msg) { 1992 return unexpected("unexpected", msg); 1993 } 1994 1995 2006 public static <x> Parser<x> unexpected(final String name, final String msg) { 2007 return new UnexpectedParser<x>(name, msg); 2008 } 2009 2010 2022 public static <R> Parser<R> token(final FromToken<R> ft) { 2023 return token("token", ft); 2024 } 2025 2026 2040 public static <R> Parser<R> token(final String name, final FromToken<R> ft) { 2041 return new IsTokenParser<R>(name, ft); 2042 } 2043 2044 2055 public static Parser<Tok> token(final Object t) { 2056 return token("token", t); 2057 } 2058 2059 2072 public static Parser<Tok> token(final String name, final Object t) { 2073 return token(name, IsToken.instance(t)); 2075 } 2076 2077 2091 public static <R> Parser<R> label(final String name, final String lbl, 2092 final Parser<R> p) { 2093 final Parser<R> otherwise = expect(lbl); 2094 return plus(name, p, otherwise); 2095 2107 } 2108 2118 public static <x> Parser<x> expect(final String name, final String lbl){ 2119 return new ExpectParser<x>(name, lbl); 2120 } 2121 2129 public static <x> Parser<x> expect(final String lbl){ 2130 return expect("expect", lbl); 2131 } 2132 2141 public static <R> Parser<R> raise(final Object e) { 2142 return raise("raise", e); 2143 } 2144 2145 2156 public static <R> Parser<R> raise(final String name, final Object e) { 2157 return new RaiseParser<R>(name, e); 2158 } 2159 2160 2177 public static <From, A extends From, R, To extends R> Parser<R> manyAccum( 2178 final String name, final Accumulatable<From, To> accm, final int min, 2179 final Parser<A> p) { 2180 if (min < 0) 2181 throw new IllegalArgumentException ("min<0"); 2182 return new ManyAccumMinParser<R, From, To, A>(name, accm, min, p); 2183 } 2184 2185 2200 public static <From, A extends From, R, To extends R> Parser<R> manyAccum( 2201 final String name, final Accumulatable<From, To> accm, final Parser<A> p) { 2202 return new ManyAccumParser<R, From, To, A>(name, accm, p); 2203 } 2204 2205 2225 public static <From, A extends From, R, To extends R> Parser<R> someAccum( 2226 final String name, final Accumulatable<From, To> accm, final int min, 2227 final int max, final Parser<A> p) { 2228 if (min < 0 || max < 0 || min > max) 2229 throw new IllegalArgumentException (); 2230 return new ManyAccumMinMaxParser<R, From, To, A>(name, max, p, min, accm); 2231 } 2232 2233 2250 public static <From, A extends From, R, To extends R> Parser<R> someAccum( 2251 final String name, final Accumulatable<From, To> accm, final int max, 2252 final Parser<A> p) { 2253 if (max < 0) 2254 throw new IllegalArgumentException ("max<0"); 2255 return new SomeAccumMaxParser<R, From, To, A>(name, max, accm, p); 2256 } 2257 2258 private static final Parser<?> _eof = eof("EOF"); 2259 2260 2267 public static Parser<?> eof() { 2268 return _eof; 2269 } 2270 2271 2279 public static Parser<?> eof(final String msg) { 2280 return eof("eof", msg); 2281 } 2282 2283 2292 public static Parser<?> eof(final String name, final String msg) { 2293 return new EofParser(name, msg); 2294 } 2295 2296 2305 public static Parser<?> not(final String name, final Parser<?> p) { 2306 return not(name, p, p.getName()); 2307 } 2308 2309 2319 public static Parser<?> not(final String name, final Parser<?> p, 2320 final String errmsg) { 2321 return ifelse(name, p.peek(), unexpected(errmsg), one()); 2322 } 2323 2324 2325 2326 2340 public static <R> Parser<_> many(final String name, final int min, 2341 final Parser<?> p) { 2342 return new ManyMinParser(name, p, min); 2343 } 2344 2345 2356 public static Parser<_> many(final String name, final Parser<?> p) { 2357 return new ManyParser(name, p); 2358 } 2359 2360 2376 public static <R> Parser<R[]> many(final String name, final Class <R> etype, 2377 final int min, final Parser<? extends R> p) { 2378 return many(name, ArrayFactories.typedFactory(etype), min, p); 2379 } 2380 2381 2395 public static <R> Parser<R[]> many(final String name, final Class <R> etype, 2396 final Parser<? extends R> p) { 2397 return many(name, ArrayFactories.typedFactory(etype), p); 2398 } 2399 2400 2416 public static <R> Parser<R[]> many(final String name, 2417 final ArrayFactory<R> af, final int min, final Parser<? extends R> p) { 2418 return manyAccum(name, getArrayAccumulatable(af), min, p); 2419 } 2420 2421 2435 public static <R> Parser<R[]> many(final String name, 2436 final ArrayFactory<R> af, final Parser<? extends R> p) { 2437 return manyAccum(name, getArrayAccumulatable(af), p); 2438 } 2439 2440 2456 public static <R> Parser<_> some(final String name, final int min, 2457 final int max, final Parser<?> p) { 2458 if (min < 0 || max < 0 || min > max) 2459 throw new IllegalArgumentException (); 2460 if (max == 0) 2461 return _retn_unit(); 2462 return new SomeMinMaxParser(name, max, p, min); 2463 } 2464 2465 2479 public static Parser<_> some(final String name, final int max, 2480 final Parser<?> p) { 2481 if (max < 0) 2482 throw new IllegalArgumentException ("max<0"); 2483 if (max == 0) 2484 return _retn_unit(); 2485 return new SomeMaxParser(name, max, p); 2486 } 2487 2488 2507 public static <R> Parser<R[]> some(final String name, 2508 final ArrayFactory<R> af, final int min, final int max, 2509 final Parser<? extends R> p) { 2510 return someAccum(name, getArrayAccumulatable(af), min, max, p); 2511 } 2512 2513 2529 public static <R> Parser<R[]> some(final String name, 2530 final ArrayFactory<R> af, final int max, final Parser<? extends R> p) { 2531 return someAccum(name, getArrayAccumulatable(af), max, p); 2532 } 2533 2534 2553 public static <R> Parser<R[]> some(final String name, Class <R> etype, 2554 final int min, final int max, final Parser<? extends R> p) { 2555 return some(name, ArrayFactories.typedFactory(etype), min, max, p); 2556 } 2557 2558 2574 public static <R> Parser<R[]> some(final String name, Class <R> etype, 2575 final int max, final Parser<? extends R> p) { 2576 return some(name, ArrayFactories.typedFactory(etype), max, p); 2577 } 2578 2579 2592 public static <A, B, R> Parser<R> map2(final Parser<A> p1, 2593 final Parser<B> p2, final Map2<? super A, ? super B, R> m2) { 2594 return map2("map2", p1, p2, m2); 2595 } 2596 2597 2612 public static <A, B, R> Parser<R> map2(final String name, final Parser<A> p1, 2613 final Parser<B> p2, final Map2<? super A, ? super B, R> m2) { 2614 return new Map2Parser<R, A, B>(name, m2, p1, p2); 2615 } 2616 2617 2632 public static <A, B, C, R> Parser<R> map3(final Parser<A> p1, 2633 final Parser<B> p2, final Parser<C> p3, 2634 final Map3<? super A, ? super B, ? super C, R> m3) { 2635 return map3("map3", p1, p2, p3, m3); 2636 } 2637 2638 2655 public static <A, B, C, R> Parser<R> map3(final String name, 2656 final Parser<A> p1, final Parser<B> p2, final Parser<C> p3, 2657 final Map3<? super A, ? super B, ? super C, R> m3) { 2658 return new Map3Parser<R, A, B, C>(name, m3, p2, p1, p3); 2659 } 2660 2661 2678 public static <A, B, C, D, R> Parser<R> map4(final Parser<A> p1, 2679 final Parser<B> p2, final Parser<C> p3, final Parser<D> p4, 2680 final Map4<? super A, ? super B, ? super C, ? super D, R> m4) { 2681 return map4("map4", p1, p2, p3, p4, m4); 2682 } 2683 2684 2703 public static <A, B, C, D, R> Parser<R> map4(final String name, 2704 final Parser<A> p1, final Parser<B> p2, final Parser<C> p3, 2705 final Parser<D> p4, 2706 final Map4<? super A, ? super B, ? super C, ? super D, R> m4) { 2707 return new Map4Parser<R, A, B, C, D>(name, m4, p3, p2, p1, p4); 2708 } 2709 2710 2730 public static <A, B, C, D, E, R> Parser<R> map5(final Parser<A> p1, 2731 final Parser<B> p2, final Parser<C> p3, final Parser<D> p4, 2732 final Parser<E> p5, 2733 final Map5<? super A, ? super B, ? super C, ? super D, ? super E, R> m5) { 2734 return map5("map5", p1, p2, p3, p4, p5, m5); 2735 } 2736 2737 2759 public static <A, B, C, D, E, R> Parser<R> map5(final String name, 2760 final Parser<A> p1, final Parser<B> p2, final Parser<C> p3, 2761 final Parser<D> p4, final Parser<E> p5, 2762 final Map5<? super A, ? super B, ? super C, ? super D, ? super E, R> m5) { 2763 return new Map5Parser<R, A, B, C, D, E>(name, p1, p3, p4, p2, m5, p5); 2764 } 2765 2766 2779 public static <R> Parser<R> option(final R v, final Parser<R> p) { 2780 return option("option", v, p); 2781 } 2782 2783 2798 public static <R> Parser<R> option(final String name, final R v, 2799 final Parser<R> p) { 2800 return plus(name, p, retn(v)); 2801 } 2802 2803 2813 public static <R> Parser<R> optional(final Parser<R> p) { 2814 return optional("optional", p); 2815 } 2816 2817 2829 public static <R> Parser<R> optional(final String name, final Parser<R> p) { 2830 return plus(name, p, _retn_null); 2831 } 2832 2833 2849 public static <R> Parser<R> between(final Parser<?> open, 2850 final Parser<?> close, final Parser<R> p) { 2851 return between("between", open, close, p); 2852 } 2853 2854 2872 public static <R> Parser<R> between(final String name, final Parser<?> open, 2873 final Parser<?> close, final Parser<R> p) { 2874 return seq(name, open, p.followedBy(close)); 2875 } 2876 2877 2893 public static Parser<_> sepBy1(final Parser<?> sep, final Parser<?> p) { 2894 return sepBy1("sepBy1", sep, p); 2895 } 2896 2897 2915 public static Parser<_> sepBy1(final String name, final Parser<?> sep, 2916 final Parser<?> p) { 2917 final Parser<?> sepp = seq(sep, p); 2919 return seq(name, p, sepp.many()); 2920 } 2921 2922 2940 public static <R> Parser<R[]> sepBy1(final Class <R> etype, 2941 final Parser<?> sep, final Parser<? extends R> p) { 2942 return sepBy1("sepBy1", etype, sep, p); 2943 } 2944 2945 2965 public static <R> Parser<R[]> sepBy1(final String name, final Class <R> etype, 2966 final Parser<?> sep, final Parser<? extends R> p) { 2967 return sepBy1(name, ArrayFactories.typedFactory(etype), sep, p); 2968 } 2969 2970 2989 public static <R> Parser<R[]> sepBy1(final ArrayFactory<R> af, 2990 final Parser<?> sep, final Parser<? extends R> p) { 2991 return sepBy1("sepBy1", af, sep, p); 2992 } 2993 2994 3015 public static <R, A extends R> Parser<R[]> sepBy1(final String name, 3016 final ArrayFactory<R> af, final Parser<?> sep, final Parser<A> p) { 3017 final Parser<A> sepp = seq(sep, p); 3018 final ToParser<A, R[]> binder = new ToParser<A, R[]>() { 3019 public Parser<R[]> toParser(final A v) { 3020 return manyAccum("sepBy1", getArrayAccumulatable(af, v), sepp); 3021 } 3022 }; 3023 return bind(name, p, binder); 3024 } 3025 3026 3042 public static Parser<_> sepBy(final Parser<?> sep, final Parser<?> p) { 3043 return sepBy("sepBy", sep, p); 3044 } 3045 3046 3064 public static Parser<_> sepBy(final String name, final Parser<?> sep, 3065 final Parser<?> p) { 3066 return plus(name, sepBy1(sep, p), _retn_unit()); 3067 } 3068 3069 3087 public static <R> Parser<R[]> sepBy(final Class <R> etype, 3088 final Parser<?> sep, final Parser<? extends R> p) { 3089 return sepBy("sepBy", etype, sep, p); 3090 } 3091 3092 3111 public static <R> Parser<R[]> sepBy(final ArrayFactory<R> af, 3112 final Parser<?> sep, final Parser<? extends R> p) { 3113 return sepBy("sepBy", af, sep, p); 3114 } 3115 3116 3137 public static <R> Parser<R[]> sepBy(final String name, 3138 final ArrayFactory<R> af, final Parser<?> sep, final Parser<? extends R> p) { 3139 return option(name, af.createArray(0), sepBy1(af, sep, p)); 3140 } 3141 3142 3162 public static <R> Parser<R[]> sepBy(final String name, final Class <R> etype, 3163 final Parser<?> sep, final Parser<? extends R> p) { 3164 return sepBy(name, ArrayFactories.typedFactory(etype), sep, p); 3165 } 3166 3167 3182 public static <R> Parser<R> followedBy(final String name, 3183 final Parser<?> sep, final Parser<R> p) { 3184 return new FollowedByParser<R>(name, sep, p); 3185 } 3186 3187 3203 public static Parser<_> endBy(final Parser<?> sep, final Parser<?> p) { 3204 return endBy("endBy", sep, p); 3205 } 3206 3207 3225 public static Parser<_> endBy(final String name, final Parser<?> sep, 3226 final Parser<?> p) { 3227 return p.followedBy(sep).many(name); 3228 } 3229 3230 3248 public static <R> Parser<R[]> endBy(final Class <R> etype, 3249 final Parser<?> sep, final Parser<? extends R> p) { 3250 return endBy("endBy", etype, sep, p); 3251 } 3252 3253 3271 public static <R> Parser<R[]> endBy(final ArrayFactory<R> af, 3272 final Parser<?> sep, final Parser<? extends R> p) { 3273 return endBy("endBy", af, sep, p); 3274 } 3275 3276 3296 public static <R> Parser<R[]> endBy(final String name, final Class <R> etype, 3297 final Parser<?> sep, final Parser<? extends R> p) { 3298 return many(name, etype, p.followedBy(sep)); 3299 } 3300 3301 3322 public static <R> Parser<R[]> endBy(final String name, 3323 final ArrayFactory<R> af, final Parser<?> sep, final Parser<? extends R> p) { 3324 return many(name, af, p.followedBy(sep)); 3325 } 3326 3327 3343 public static Parser<_> endBy1(final Parser<?> sep, final Parser<?> p) { 3344 return endBy1("endBy1", sep, p); 3345 } 3346 3347 3365 public static Parser<_> endBy1(final String name, final Parser<?> sep, 3366 final Parser<?> p) { 3367 return p.followedBy(sep).many1(name); 3368 } 3369 3370 3388 public static <R> Parser<R[]> endBy1(final Class <R> etype, 3389 final Parser<?> sep, final Parser<? extends R> p) { 3390 return endBy1("endBy1", etype, sep, p); 3391 } 3392 3393 3412 public static <R> Parser<R[]> endBy1(final ArrayFactory<R> af, 3413 final Parser<?> sep, final Parser<? extends R> p) { 3414 return endBy1("endBy1", af, sep, p); 3415 } 3416 3417 3438 public static <R> Parser<R[]> endBy1(final String name, 3439 final ArrayFactory<R> af, final Parser<?> sep, final Parser<? extends R> p) { 3440 return many(name, af, 1, p.followedBy(sep)); 3441 } 3442 3443 3463 public static <R> Parser<R[]> endBy1(final String name, final Class <R> etype, 3464 final Parser<?> sep, final Parser<? extends R> p) { 3465 return many(name, etype, 1, p.followedBy(sep)); 3466 } 3467 3468 3485 public static Parser<_> sepEndBy(final Parser<?> sep, final Parser<?> p) { 3486 return sepEndBy("sepEndBy", sep, p); 3487 } 3488 3489 3508 public static <R> Parser<R[]> sepEndBy(final Class <R> etype, 3509 final Parser<?> sep, final Parser<? extends R> p) { 3510 return sepEndBy("sepEndBy", etype, sep, p); 3511 } 3512 3513 3533 public static <R> Parser<R[]> sepEndBy(final ArrayFactory<R> af, 3534 final Parser<?> sep, final Parser<? extends R> p) { 3535 return sepEndBy("sepEndBy", af, sep, p); 3536 } 3537 3538 3557 public static Parser<_> sepEndBy(final String name, final Parser<?> sep, 3558 final Parser<?> p) { 3559 return plus(name, sepEndBy1(sep, p), _retn_unit()); 3560 } 3561 3562 3583 public static <R> Parser<R[]> sepEndBy(final String name, 3584 final Class <R> etype, final Parser<?> sep, final Parser<? extends R> p) { 3585 return sepEndBy(name, ArrayFactories.typedFactory(etype), sep, p); 3586 } 3587 3588 3610 public static <R> Parser<R[]> sepEndBy(final String name, 3611 final ArrayFactory<R> af, final Parser<?> sep, final Parser<? extends R> p) { 3612 return option(name, af.createArray(0), sepEndBy1(af, sep, p)); 3613 } 3614 3615 3632 public static Parser<_> sepEndBy1(final Parser<?> sep, final Parser<?> p) { 3633 return sepEndBy1("sepEndBy1", sep, p); 3634 } 3635 3636 3655 public static Parser<_> sepEndBy1(final String name, final Parser<?> sep, 3656 final Parser<?> p) { 3657 final _ end = new _(); 3659 final Catch<_> catcher = new Catch1<_>(end); 3660 final Parser<?> x = seq(sep, sum(p, raise(end))); 3661 return seq(p, tryParser(x.many(name), catcher)); 3662 } 3663 3664 3683 public static <R> Parser<R[]> sepEndBy1(final Class <R> etype, 3684 final Parser<?> sep, final Parser<? extends R> p) { 3685 return sepEndBy1("sepEndBy1", etype, sep, p); 3686 } 3687 3688 3708 public static <R> Parser<R[]> sepEndBy1(final ArrayFactory<R> af, 3709 final Parser<?> sep, final Parser<? extends R> p) { 3710 return sepEndBy1("sepEndBy1", af, sep, p); 3711 } 3712 3713 3734 public static <R> Parser<R[]> sepEndBy1(final String name, 3735 final Class <R> etype, final Parser<?> sep, final Parser<? extends R> p) { 3736 return sepEndBy1(name, ArrayFactories.typedFactory(etype), sep, p); 3737 } 3738 3739 3761 public static <R> Parser<R[]> sepEndBy1(final String name, 3762 final ArrayFactory<R> af, final Parser<?> sep, final Parser<? extends R> p) { 3763 3770 return bind(name, p, new ToParser<R, R[]>() { 3771 public Parser<R[]> toParser(final R v) { 3772 return delimitedBy(name, getArrayAccumulatable(af, v), sep, p); 3773 } 3774 }); 3775 } 3776 3777 private static <E1, E extends E1, R, R1 extends R> Parser<R> delimitedBy( 3778 final String name, final Accumulatable<E1, R1> accm, 3779 final Parser<?> delim, final Parser<E> p) { 3780 return new DelimitedByParser<R, E1, R1, E>(name, p, accm, delim); 3781 } 3782 3783 3796 public static Parser<_> repeat(final String name, final int n, 3797 final Parser<?> p) { 3798 return new RepeatParser(name, p, n); 3799 } 3800 3801 3817 public static <R> Parser<R[]> repeat(final String name, final Class <R> etype, 3818 final int n, final Parser<? extends R> p) { 3819 return repeat(name, ArrayFactories.typedFactory(etype), n, p); 3820 } 3821 3822 3838 public static <R> Parser<R[]> repeat(final String name, 3839 final ArrayFactory<R> af, final int n, final Parser<? extends R> p) { 3840 return new RepeatArrayParser<R>(name, af, n, p); 3841 } 3842 3843 3857 public static <T> Parser<T> prefix( 3858 final Parser<? extends Map<? super T, T>> op, final Parser<? extends T> p) { 3859 return prefix("prefix", op, p); 3860 } 3861 3862 private static <T> T postfix_thread_maps(T a, final Map<? super T, T>[] ms) { 3863 for (int i = 0; i < ms.length; i++) { 3864 final Map<? super T, T> m = ms[i]; 3865 a = m.map(a); 3866 } 3867 return a; 3868 } 3869 3870 private static <T> T prefix_thread_maps(T a, final Map<? super T, T>[] ms) { 3871 for (int i = ms.length - 1; i >= 0; i--) { 3872 final Map<? super T, T> m = ms[i]; 3873 a = m.map(a); 3874 } 3875 return a; 3876 } 3877 3878 private static final Map2 _prefix_map2 = _get_prefix_map2(); 3879 3880 private static <T> Map2<Map<? super T, T>[], T, T> _get_prefix_map2() { 3881 return new Map2<Map<? super T, T>[], T, T>() { 3882 public T map(final Map<? super T, T>[] ops, final T a) { 3883 return prefix_thread_maps(a, ops); 3884 } 3885 }; 3886 } 3887 3888 3904 public static <T> Parser<T> prefix(final String name, 3905 final Parser<? extends Map<? super T, T>> op, final Parser<? extends T> p) { 3906 final Parser<Map> _op = op.convert(); return map2(name, _op.many("prefix", Map.class), p, _prefix_map2); 3909 } 3910 3911 3925 public static <T> Parser<T> postfix( 3926 final Parser<? extends Map<? super T, T>> op, final Parser<? extends T> p) { 3927 return postfix("postfix", op, p); 3928 } 3929 3930 private static final Map2 _postfix_map2 = _get_postfix_map2(); 3931 3932 private static <T> Map2<T, Map<? super T, T>[], T> _get_postfix_map2() { 3933 return new Map2<T, Map<? super T, T>[], T>() { 3934 public T map(final T a, final Map<? super T, T>[] ops) { 3935 return postfix_thread_maps(a, ops); 3936 } 3937 }; 3938 } 3939 3940 3956 public static <T> Parser<T> postfix(final String name, 3957 final Parser<? extends Map<? super T, T>> op, final Parser<? extends T> p) { 3958 final Parser<Map> _op = op.convert(); 3959 final Parser ops = _op.many(name, Map.class); 3960 return map2(name, p, ops, _postfix_map2); 3961 } 3962 3963 3977 public static <T> Parser<T> infixn( 3978 final Parser<? extends Map2<? super T, ? super T, T>> op, 3979 final Parser<? extends T> operand) { 3980 return infixn("infixn", op, operand); 3981 } 3982 3983 3999 public static <T> Parser<T> infixn(final String name, 4000 final Parser<? extends Map2<? super T, ? super T, T>> op, 4001 final Parser<? extends T> operand) { 4002 return operand.bind(name, new ToParser<T, T>() { 4003 public Parser<T> toParser(final T a) { 4004 final Parser<T> shift = op.and(operand, 4005 new Map2<Map2<? super T, ? super T, T>, T, T>() { 4006 public T map(final Map2<? super T, ? super T, T> m2, final T b) { 4007 return m2.map(a, b); 4008 } 4009 }); 4010 return Parsers.plus(shift, retn(a)); 4011 } 4012 }); 4013 } 4014 4015 4030 public static <T> Parser<T> infixl( 4031 final Parser<? extends Map2<? super T, ? super T, T>> op, 4032 final Parser<? extends T> p) { 4033 return infixl("infixl", op, p); 4034 } 4035 4036 4053 public static <T> Parser<T> infixl(final String name, 4054 final Parser<? extends Map2<? super T, ? super T, T>> op, 4055 final Parser<? extends T> p) { 4056 final Parser op_and_rhs = op_rhs(op, p); 4057 return bind(name, p, new ToParser<T, T>() { 4058 public Parser<T> toParser(final T a) { 4059 return manyAccum("infixl", lassocAccumulatable(a), op_and_rhs); 4060 } 4061 }); 4062 } 4064 4065 4080 public static <T> Parser<T> infixr( 4081 final Parser<? extends Map2<? super T, ? super T, T>> op, 4082 final Parser<? extends T> p) { 4083 return infixr("infixr", op, p); 4084 } 4085 4086 private static final class Rhs<T> { 4088 final Map2<? super T, ? super T, T> op; 4089 4090 final T rhs; 4091 4092 Rhs(final Map2<? super T, ? super T, T> op, final T rhs) { 4093 this.op = op; 4094 this.rhs = rhs; 4095 } 4096 } 4097 4098 private static final Map2 _infixr_map2 = _get_infixr_map2(); 4099 4100 private static <T> Map2<Map2<? super T, ? super T, T>, T, Rhs<T>> _get_infixr_map2() { 4101 return new Map2<Map2<? super T, ? super T, T>, T, Rhs<T>>() { 4102 public Rhs<T> map(final Map2<? super T, ? super T, T> m2, final T b) { 4103 return new Rhs<T>(m2, b); 4104 } 4105 }; 4106 } 4107 4108 private static final Map2 _calc_infixr = _get_calc_infixr(); 4109 4110 private static final <T> Map2<T, Rhs<T>[], T> _get_calc_infixr() { 4111 return new Map2<T, Rhs<T>[], T>() { 4112 public T map(final T a, final Rhs<T>[] rhss) { 4113 if (rhss.length == 0) 4114 return a; 4115 T o2 = rhss[rhss.length - 1].rhs; 4116 for (int i = rhss.length - 1; i > 0; i--) { 4117 final Map2<? super T, ? super T, T> m2 = rhss[i].op; 4118 final T o1 = rhss[i - 1].rhs; 4119 o2 = m2.map(o1, o2); 4120 } 4121 return rhss[0].op.map(a, o2); 4122 } 4123 }; 4124 } 4125 4126 4143 public static <T> Parser<T> infixr(final String name, 4144 final Parser<? extends Map2<? super T, ? super T, T>> op, 4145 final Parser<? extends T> p) { 4146 final Parser<Rhs> op_rhs2 = map2(op, p, _infixr_map2); 4147 final Parser<Rhs[]> tmp = op_rhs2.many(name, Rhs.class); return map2(p, tmp, _calc_infixr); 4150 } 4151 4152 4159 public static Parser<Tok> anyToken() { 4160 return anyToken("anyToken"); 4161 } 4162 4163 private static final FromToken<Tok> _any_tok = new FromToken<Tok>() { 4164 public Tok fromToken(final Tok tok) { 4165 return tok; 4166 } 4167 }; 4168 4169 4179 public static Parser<Tok> anyToken(final String name) { 4180 return token(name, _any_tok); 4181 } 4182 4183 private static <T> Accumulatable<Map<? super T, ? extends T>, T> lassocAccumulatable( 4184 final T init) { 4185 return new Accumulatable<Map<? super T, ? extends T>, T>() { 4186 public Accumulator<Map<? super T, ? extends T>, T> getAccumulator() { 4187 return new Accumulator<Map<? super T, ? extends T>, T>() { 4188 private T a = init; 4189 4190 public void accumulate(final Map<? super T, ? extends T> m) { 4191 a = m.map(a); 4192 } 4193 4194 public T getResult() { 4195 return a; 4196 } 4197 }; 4198 } 4199 }; 4200 } 4201 4202 private static final Map2 _op_rhs_map2 = _getOperatorRhsMap2(); 4203 4204 private static <A, B, R> Map2<Map2<A, B, R>, B, Map<A, R>> _getOperatorRhsMap2() { 4205 return new Map2<Map2<A, B, R>, B, Map<A, R>>() { 4206 public Map<A, R> map(final Map2<A, B, R> op, final B b) { 4207 return new Map<A, R>() { 4208 public R map(final A a) { 4209 return op.map(a, b); 4210 } 4211 }; 4212 } 4213 }; 4214 } 4215 4216 private static <B, R, Op extends Map2<? super B, ? super B, R>> Parserop_rhs( 4218 final Parser<Op> op, final Parser<B> rhs) { 4219 return map2(op, rhs, _op_rhs_map2); 4220 } 4221 4222 private static <From> boolean runToParser(final ToParser<? super From, ?> p, 4223 final ParseContext state) { 4224 return p.toParser((From) state.getReturn()).parse(state); 4225 } 4226 4227 private static <From> boolean runNext(final ParseContext s, 4228 final ToParser<? super From, ?> p) { 4229 return runToParser(p, s); 4230 } 4231 4232 private static <E, T extends E> Accumulatable<T, E[]> getArrayAccumulatable( 4233 final ArrayFactory<E> af) { 4234 return new Accumulatable<T, E[]>() { 4235 public Accumulator<T, E[]> getAccumulator() { 4236 return new ArrayAccumulator<E, T>(af, new java.util.ArrayList <T>()); 4237 } 4238 }; 4239 } 4240 4241 private static <E, T extends E> Accumulatable<T, E[]> getArrayAccumulatable( 4242 final ArrayFactory<E> af, final T init) { 4243 return new Accumulatable<T, E[]>() { 4244 public Accumulator<T, E[]> getAccumulator() { 4245 final java.util.ArrayList <T> a = new java.util.ArrayList <T>(); 4246 a.add(init); 4247 return new ArrayAccumulator<E, T>(af, a); 4248 } 4249 }; 4250 } 4251 4252 private static final Object [] _empty_array = new Object [0]; 4253 private static final Parser _retn_null = retn(null); 4254 private static final Parser<_> _retn_unit(){ 4255 return _retn_null; 4256 } 4257} 4258 | Popular Tags |