1 8 13 package jfun.parsec; 14 15 import java.io.PrintWriter ; 16 17 import jfun.parsec.trace.Trace; 18 import jfun.parsec.trace.Traces; 19 20 21 42 public abstract class Parser<Type> implements java.io.Serializable { 43 private final ParsingFrame getErrorFrame(ParseContext ctxt, int ind){ 44 return new ParsingFrame(ctxt.getModuleName(), 45 ctxt.getIndex(), 46 ctxt.getPositionMap().getPos(ind), this); 47 } 48 private RuntimeException wrapException(Throwable e, ParseContext ctxt, 49 int ind){ 50 if(e instanceof UserException){ 51 return (UserException)e; 52 } 53 else if(e instanceof ParserException){ 54 final ParserException pe = ((ParserException)e); 55 pe.pushFrame(getErrorFrame(ctxt, ind)); 56 return pe; 57 } 58 else{ 59 final ParserException pe = 60 new ParserException(e, null, 61 ctxt.getModuleName(), 62 ctxt.getPositionMap().getPos(ind)); 63 return pe; 64 } 65 } 66 final boolean parse(final ParseContext ctxt){ 67 final int ind = ctxt.getIndex(); 68 try{ 69 return apply(ctxt); 70 } 71 catch(RuntimeException e){ 72 throw wrapException(e, ctxt, ind); 73 } 74 } 75 abstract boolean apply(final ParseContext ctxt); 76 boolean apply(final ParseContext ctxt, final int look_ahead){ 77 return apply(ctxt); 78 } 79 80 final boolean parse(final ParseContext ctxt, final int look_ahead){ 81 final int ind = ctxt.getIndex(); 82 try{ 83 return apply(ctxt, look_ahead); 84 } 85 catch(RuntimeException e){ 86 throw wrapException(e, ctxt, ind); 87 } 88 } 89 92 private final String name; 93 Parser(final String n){ 94 this.name = n; 95 } 96 final String getName(){return name;} 97 public String toString(){return name;} 98 105 public final Parser<Type> trace(Trace<? super Type> trc){ 106 if(!Parsers.isDebugEnabled()) return this; 107 return new TracedParser(name, this, trc); 108 } 109 117 public final Parser<Type> printError(String id, final int min_steps){ 118 return trace(Traces.printError(id, new PrintWriter (System.out), min_steps)); 119 } 120 128 public final Parser<Type> printError(String id){ 129 return printError(id, 1); 130 } 131 138 public final Parser<Type> printResult(String id){ 139 return trace(Traces.printResult(id, new PrintWriter (System.out))); 140 } 141 148 public final Parser<Type> printTrace(String id){ 149 return trace(Traces.printTrace(id, new PrintWriter (System.out))); 150 } 151 157 public final <To> Parser<To> bind(final ToParser<? super Type,To> tp){ 158 return Parsers.bind("bind", this, tp); 159 } 160 167 public final <To> Parser<To> bind(final String name, 168 final ToParser<? super Type, To> tp){ 169 return Parsers.bind(name, this, tp); 170 } 171 178 public final <R> Parser<R> seq(final Parser<R> p){ 179 return Parsers.seq("seq", this, p); 180 } 181 189 public final <R> Parser<R> seq(final String name, final Parser<R> p){ 190 return Parsers.seq(name, this, p); 191 } 192 193 199 public final Parser<_> repeat(final int n){ 200 return Parsers.repeat("repeat", n, this); 201 } 202 209 public final Parser<Type[]> repeat(final Class <Type> etype, final int n){ 210 return Parsers.repeat("repeat", etype, n, this); 211 } 212 219 public final Parser<Type[]> repeat(final ArrayFactory<Type> af, final int n){ 220 return Parsers.repeat("repeat", af, n, this); 221 } 222 223 230 public final Parser<_> repeat(final String name, final int n){ 231 return Parsers.repeat(name, n, this); 232 } 233 241 public final Parser<Type[]> repeat(final String name, final Class <Type> etype, final int n){ 242 return Parsers.repeat(name, etype, n, this); 243 } 244 252 public final Parser<Type[]> repeat(final String name, final ArrayFactory<Type> af, final int n){ 253 return Parsers.repeat(name, af, n, this); 254 } 255 262 public final Parser<Type[]> many(final ArrayFactory<Type> af){ 263 return Parsers.many("many", af, this); 264 } 265 272 public final Parser<Type[]> many(final String name, final ArrayFactory<Type> af){ 273 return Parsers.many(name, af, this); 274 } 275 281 public final Parser<Type[]> many(final Class <Type> elem_type){ 282 return Parsers.many("many", elem_type, this); 283 } 284 291 public final Parser<Type[]> many(final String name, final Class <Type> elem_type){ 292 return Parsers.many(name, elem_type, this); 293 } 294 299 public final Parser<_> many(){ 300 return Parsers.many("many", this); 301 } 302 308 public final Parser<_> many(final String name){ 309 return Parsers.many(name, this); 310 } 311 312 313 314 315 316 323 public final Parser<Type[]> many(final ArrayFactory<Type> af, final int min){ 324 return Parsers.many("many", af, min, this); 325 } 326 334 public final Parser<Type[]> many(final String name, final ArrayFactory<Type> af, final int min){ 335 return Parsers.many(name, af, min, this); 336 } 337 344 public final Parser<Type[]> many(final Class <Type> elem_type, final int min){ 345 return Parsers.many("many", elem_type, min, this); 346 } 347 355 public final Parser<Type[]> many(final String name, final Class <Type> elem_type, final int min){ 356 return Parsers.many(name, elem_type, min, this); 357 } 358 363 public final Parser<_> many(final int min){ 364 return Parsers.many("many", min, this); 365 } 366 373 public final Parser<_> many(final String name, final int min){ 374 return Parsers.many(name, min, this); 375 } 376 377 378 379 380 381 389 public final Parser<Type[]> some(final ArrayFactory<Type> af, final int min, final int max){ 390 return Parsers.some("some", af, min, max, this); 391 } 392 402 public final Parser<Type[]> some(final String name, final ArrayFactory<Type> af, final int min, final int max){ 403 return Parsers.some(name, af, min, max, this); 404 } 405 414 public final Parser<Type[]> some(final Class <Type> elem_type, final int min, final int max){ 415 return Parsers.some("some", elem_type, min, max, this); 416 } 417 426 public final Parser<Type[]> some(final String name, final Class <Type> elem_type, final int min, final int max){ 427 return Parsers.some(name, elem_type, min, max, this); 428 } 429 436 public final Parser<_> some(final int min, final int max){ 437 return Parsers.some("some", min, max, this); 438 } 439 447 public final Parser<_> some(final String name, final int min, final int max){ 448 return Parsers.some(name, min, max, this); 449 } 450 451 457 public final Parser<_> some(final int max){ 458 return Parsers.some("some", max, this); 459 } 460 468 public final Parser<Type[]> some(final Class <Type> etype, final int max){ 469 return Parsers.some("some", etype, max, this); 470 } 471 478 public final Parser<Type[]> some(final ArrayFactory<Type> af, final int max){ 479 return Parsers.some("some", af, max, this); 480 } 481 488 public final Parser<_> some(final String name, final int max){ 489 return Parsers.some(name, max, this); 490 } 491 500 public final Parser<Type[]> some(final String name, final Class <Type> etype, final int max){ 501 return Parsers.some(name, etype, max, this); 502 } 503 512 public final Parser<Type[]> some(final String name, final ArrayFactory<Type> af, final int max){ 513 return Parsers.some(name, af, max, this); 514 } 515 522 public final Parser<Type[]> many1(final Class <Type> elem_type){ 523 return Parsers.many("many1", elem_type, 1, this); 524 } 525 533 public final Parser<Type[]> many1(final String name, final Class <Type> elem_type){ 534 return Parsers.many(name, elem_type, 1, this); 535 } 536 543 public final Parser<Type[]> many1(final ArrayFactory<Type> af){ 544 return Parsers.many("many1", af, 1, this); 545 } 546 554 public final Parser<Type[]> many1(final String name, final ArrayFactory<Type> af){ 555 return Parsers.many(name, af, 1, this); 556 } 557 562 public final Parser<_> many1(){ 563 return Parsers.many("many1", 1, this); 564 } 565 571 public final Parser<_> many1(final String name){ 572 return Parsers.many(name, 1, this); 573 } 574 575 579 public final Parser<Type> optional(){ 580 return Parsers.optional("optional", this); 581 } 582 587 public final Parser<Type> optional(final String name){ 588 return Parsers.optional(name, this); 589 } 590 596 public final Parser<Type> option(final Type def){ 597 return Parsers.option("option", def, this); 598 } 599 606 public final Parser<Type> option(final String name, final Type def){ 607 return Parsers.option(name, def, this); 608 } 609 613 public final Parser<?> not(){ 614 return Parsers.not("not", this); 615 } 616 621 public final Parser<?> not(final String err){ 622 return Parsers.not("not", this, err); 623 } 624 630 public final Parser<?> not(final String name, final String err){ 631 return Parsers.not(name, this, err); 632 } 633 638 public final Parser<Type> peek(){ 639 return Parsers.peek("peek", this); 640 } 641 647 public final Parser<Type> peek(final String name){ 648 return Parsers.peek(name, this); 649 } 650 655 public final <R> Parser<R> map(final Map<? super Type, R> m){ 656 return Parsers.map("map", this, m); 657 } 658 664 public final <R> Parser<R> map(final String name, final Map<? super Type, R> m){ 665 return Parsers.map(name, this, m); 666 } 667 673 public final <T, R> Parser<R> and(final Parser<T> p, final Map2<? super Type, ? super T, R> m){ 674 return Parsers.map2("and", this, p, m); 675 } 676 683 public final <T, R> Parser<R> and(final String name, final Parser<T> p, final Map2<? super Type, ? super T, R> m){ 684 return Parsers.map2(name, this, p, m); 685 } 686 692 public final Parser<Type> followedBy(final Parser<?> sep){ 693 return Parsers.followedBy("followedBy", sep, this); 694 } 695 702 public final Parser<Type> followedBy(final String name, final Parser<?> sep){ 703 return Parsers.followedBy(name, sep, this); 704 } 705 712 public final Parser<Type> atomize(final String name){ 713 return Parsers.atomize(name, this); 714 } 715 721 public final Parser<Type> atomize(){ 722 return Parsers.atomize("atomize", this); 723 } 724 731 public final <R> Parser ifelse(final Parser<R> yes, final Parser<? extends R> no){ 732 return Parsers.ifelse("ifelse", this, yes, no); 733 } 734 742 public final <R> Parser<R> ifelse(final String name, final Parser<R> yes, final Parser<R> no){ 743 return Parsers.ifelse(name, this, yes, no); 744 } 745 746 753 public final <R> Parser<R> ifelse(final ToParser<? super Type, R> yes, final Parser<R> no){ 754 return Parsers.ifelse("ifelse", this, yes, no); 755 } 756 764 public final <R> Parser<R> ifelse(final String name, final ToParser<? super Type, R> yes, final Parser<R> no){ 765 return Parsers.ifelse(name, this, yes, no); 766 } 767 789 public final Parser<Type> lookahead(final String name, final int toknum){ 790 return Parsers.lookahead(name, toknum, this); 791 } 792 813 public final Parser<Type> lookahead(final int toknum){ 814 return Parsers.lookahead("lookahead", toknum, this); 815 } 816 822 public final Parser<Type> label(final String lbl){ 823 return Parsers.label("label", lbl, this); 824 } 825 832 public final Parser<Type> label(final String name, final String lbl){ 833 return Parsers.label(name, lbl, this); 834 } 835 836 843 public final Parser<Type> isReturn(final String name, final ObjectPredicate<? super Type> op){ 844 return Parsers.isReturn(name, this, op); 845 } 846 852 public final Parser<Type> isReturn(final ObjectPredicate<? super Type> op){ 853 return Parsers.isReturn("isReturn", this, op); 854 } 855 864 public final Parser<Type> isReturn(final String name, final ObjectPredicate<? super Type> op, final String expecting){ 865 return Parsers.isReturn(name, this, op, expecting); 866 } 867 875 public final Parser<Type> isReturn(final ObjectPredicate<? super Type> op, final String expecting){ 876 return Parsers.isReturn("isReturn", this, op, expecting); 877 } 878 886 public final Parser<Type> step(String name, int n){ 887 return Parsers.step(name, n, this); 888 } 889 896 public final Parser<Type> step(final String name){ 897 return Parsers.step(name, 1, this); 898 } 899 906 public final Parser<Type> step(int n){ 907 return Parsers.step("step", n, this); 908 } 909 915 public final Parser<Type> step(){ 916 return Parsers.step("step", 1, this); 917 } 918 919 925 public final <R extends Type> Parser<R> cast(){ 926 return (Parser<R>)this; 927 } 928 935 public final <R extends Type> Parser<R> cast(Class <R> type){ 936 return cast(); 937 } 938 944 public final <R> Parser<R> convert(){ 945 return (Parser<R>)this; 946 } 947 954 public final <R> Parser<R> convert(Class <R> type){ 955 return convert(); 956 } 957 Type getReturn(ParseContext ctxt){ 958 return (Type)ctxt.getReturn(); 959 } 960 967 public final Type parse(CharSequence source, String moduleName) { 968 return Parsers.runParser(source, this, moduleName); 969 } 970 976 public final Type parse(CharSequence source) { 977 return parse(source, "source"); 978 } 979 } 980 | Popular Tags |