1 8 13 package jfun.parsec; 14 15 16 import jfun.parsec.tokens.MyToken; 17 import jfun.parsec.tokens.TokenQuoted; 18 import jfun.parsec.tokens.TokenType; 19 21 22 23 30 public final class Terms implements java.io.Serializable { 31 private final Words words; 32 38 public Parser<Tok> getParser(final String ... tnames){ 39 return getParser(tnames, toLabel(tnames)); 40 } 41 48 public Parser<Tok> getParser(final String name, final String [] tnames){ 49 return getParser(name, tnames, toLabel(tnames)); 50 } 51 58 public Parser<Tok> getParser(final String [] tnames, final String expected){ 59 return getParser("term", tnames, expected); 60 } 61 69 public Parser<Tok> getParser(final String name, final String [] tnames, 70 final String expected){ 71 if(tnames.length == 0) return Parsers.zero(name); 72 final Parser<Tok>[] ps = new Parser[tnames.length]; 73 for(int i=0; i<tnames.length; i++){ 74 ps[i] = Parsers.token(isToken(words.getToken(tnames[i]))); 75 } 76 return Parsers.plus(name, ps).label(name, expected); 77 } 78 86 public Parser<Tok> getParser(final String name, final String tname, 87 final String expected){ 88 return Parsers.token(name, isToken(words.getToken(tname))) 89 .label(name, expected); 90 } 91 98 public Parser<Tok> getParser(final String tname, final String expected){ 99 return getParser("term", tname, expected); 100 } 101 107 public Parser<Tok> getParser(final String tname){ 108 return getParser(tname, tname); 109 } 110 114 public Parser<Tok> getLexer(){ 115 return words.getLexer(); 116 } 117 129 public static Terms getCaseInsensitiveInstance(final String [] ops, final String [] keywords){ 130 return new Terms(Lexers.getCaseInsensitive(ops, keywords)); 131 } 132 144 public static Terms getCaseSensitiveInstance(final String [] ops, final String [] keywords){ 145 return new Terms(Lexers.getCaseSensitive(ops, keywords)); 146 } 147 158 public static Terms getCaseInsensitiveInstance(final Parser<?> wscanner, final String [] ops, final String [] keywords){ 159 return new Terms(Lexers.getCaseInsensitive(wscanner, ops, keywords)); 160 } 161 172 public static Terms getCaseSensitiveInstance(final Parser<?> wscanner, final String [] ops, final String [] keywords){ 173 return new Terms(Lexers.getCaseSensitive(wscanner, ops, keywords)); 174 } 175 176 188 public static Terms getCaseInsensitiveInstance(final Parser<?> wscanner, final String [] ops, final String [] keywords, 189 FromString<?> toWord){ 190 return new Terms(Lexers.getCaseInsensitive(wscanner, ops, keywords, toWord)); 191 } 192 204 public static Terms getCaseSensitiveInstance(final Parser<?> wscanner, final String [] ops, final String [] keywords, 205 FromString<?> toWord){ 206 return new Terms(Lexers.getCaseSensitive(wscanner, ops, keywords, toWord)); 207 } 208 215 public static Terms getOperatorsInstance(final String ... ops){ 216 return new Terms(Lexers.getOperators(ops)); 217 } 218 219 231 public static Terms getCaseInsensitive(final String [] ops, final String [] keywords){ 232 return getCaseInsensitiveInstance(ops, keywords); 233 } 234 246 public static Terms getCaseSensitive(final String [] ops, final String [] keywords){ 247 return getCaseSensitiveInstance(ops, keywords); 248 } 249 260 public static Terms getCaseInsensitive(final Parser<?> wscanner, final String [] ops, final String [] keywords){ 261 return getCaseInsensitiveInstance(wscanner, ops, keywords); 262 } 263 274 public static Terms getCaseSensitive(final Parser<?> wscanner, final String [] ops, final String [] keywords){ 275 return getCaseSensitiveInstance(wscanner, ops, keywords); 276 } 277 278 290 public static Terms getCaseInsensitive(final Parser<?> wscanner, final String [] ops, final String [] keywords, 291 FromString<?> toWord){ 292 return getCaseInsensitiveInstance(wscanner, ops, keywords, toWord); 293 } 294 306 public static Terms getCaseSensitive(final Parser<?> wscanner, final String [] ops, final String [] keywords, 307 FromString<?> toWord){ 308 return getCaseSensitiveInstance(wscanner, ops, keywords, toWord); 309 } 310 317 public static Terms getOperators(final String ... ops){ 318 return getOperatorsInstance(ops); 319 } 320 321 322 328 public static <R> Parser<R> charParser(final String name, final FromChar<R> fc){ 329 return Parsers.token(name, new FromToken<R>(){ 330 public R fromToken(Tok ptok){ 331 final Object t = ptok.getToken(); 332 if(t instanceof Character ){ 333 final Character c = (Character )t; 334 return fc.fromChar(ptok.getIndex(), ptok.getLength(), c.charValue()); 335 } 336 else return null; 337 } 338 }); 339 } 340 346 public static <R> Parser<R> stringParser(final String name, final FromString<R> fc){ 347 return Parsers.token(name, new FromToken<R>(){ 348 public R fromToken(Tok ptok){ 349 final Object t = ptok.getToken(); 350 if(t instanceof String ){ 351 return fc.fromString(ptok.getIndex(), ptok.getLength(), t.toString()); 352 } 353 else return null; 354 } 355 }); 356 } 357 363 public static <R> Parser<R> quotedWordParser(final String name, final FromString3<R> fc){ 364 return Parsers.token(name, new FromToken<R>(){ 365 public R fromToken(Tok ptok){ 366 final Object t = ptok.getToken(); 367 if(t instanceof TokenQuoted){ 368 final TokenQuoted c = (TokenQuoted)t; 369 return fc.fromString3(ptok.getIndex(), ptok.getLength(), 370 c.getOpen(), c.getQuoted(), c.getClose()); 371 } 372 else return null; 373 } 374 }); 375 } 376 382 public static <R> Parser<R> wordParser(final String name, final FromString<R> fc){ 383 return Parsers.token(name, fromTypedToken(TokenType.Word, fc)); 384 } 385 390 @Deprecated 391 public static <R> Parser<R> myParser(final FromMyText<R> fm){ 392 return myParser("myParser", fm); 393 } 394 400 @Deprecated 401 public static <R> Parser<R> myParser(final String name, final FromMyText<R> fm){ 402 return Parsers.token(name, new FromToken<R>(){ 403 public R fromToken(Tok ptok){ 404 final Object t = ptok.getToken(); 405 if(t instanceof MyToken){ 406 final MyToken mt = (MyToken)t; 407 return fm.fromMyText(ptok.getIndex(), ptok.getLength(), 408 mt.getText(), mt.getKind()); 409 } 410 return null; 411 } 412 }); 413 } 414 420 @Deprecated 421 public static <R> Parser<R> myParser(final int kind, final FromString<R> fs){ 422 return myParser("myParser", kind, fs); 423 } 424 431 @Deprecated 432 public static <R> Parser<R> myParser(final String name, final int kind, 433 final FromString<R> fs){ 434 return Parsers.token(name, new FromToken<R>(){ 435 public R fromToken(Tok ptok){ 436 final Object t = ptok.getToken(); 437 if(t instanceof MyToken){ 438 final MyToken mt = (MyToken)t; 439 if(mt.getKind() != kind) return null; 440 return fs.fromString(ptok.getIndex(), ptok.getLength(), mt.getText()); 441 } 442 return null; 443 } 444 }); 445 446 } 447 453 public static <R> Parser<R> integerParser(final String name, final FromLong<R> fc){ 454 return Parsers.token(name, new FromToken<R>(){ 455 public R fromToken(final Tok ptok){ 456 final Object t = ptok.getToken(); 457 if(t instanceof Long ){ 458 final Long c = (Long )t; 459 return fc.fromLong(ptok.getIndex(), ptok.getLength(), c.longValue()); 460 } 461 else return null; 462 } 463 }); 464 } 465 470 public static <R> Parser<R> integerParser(final FromString<R> fs){ 471 return integerParser("integerParser", fs); 472 } 473 479 public static <R> Parser<R> integerParser(final String name, final FromString<R> fs){ 480 return Parsers.token(name, fromTypedToken(TokenType.Integer, fs)); 481 } 482 488 public static <R> Parser<R> decimalParser(final String name, final FromString<R> fc){ 489 return Parsers.token(name, fromTypedToken( 490 new TokenType[]{TokenType.Decimal, TokenType.Integer}, fc)); 491 } 492 497 public static <R> Parser<R> charParser(final FromChar<R> fc){ 498 return charParser("char", fc); 499 } 500 505 public static <R> Parser<R> stringParser(final FromString<R> fc){ 506 return stringParser("stringLiteral", fc); 507 } 508 513 public static <R> Parser<R> quotedWordParser(final FromString3<R> fc){ 514 return quotedWordParser("quotedName", fc); 515 } 516 521 public static <R> Parser<R> wordParser(final FromString<R> fc){ 522 return wordParser("name", fc); 523 } 524 529 public static <R> Parser<R> integerParser(final FromLong<R> fc){ 530 return integerParser("integer", fc); 531 } 532 537 public static <R> Parser<R> decimalParser(final FromString<R> fc){ 538 return decimalParser("decimal", fc); 539 } 540 549 public static <T,R> FromToken<R> fromTypedToken(final T type, 550 final FromString<R> f){ 551 return new IsTokenOfType<R>(f){ 552 public boolean isOfType(Object t){ 553 return t == type; 554 } 555 }; 556 } 557 558 567 public static <T,R> FromToken<R> fromTypedToken(final T[] types, 568 final FromString<R> f){ 569 return new IsTokenOfType<R>(f){ 570 public boolean isOfType(Object type){ 571 for(T t: types){ 572 if(type==t) 573 return true; 574 } 575 return false; 576 } 577 }; 578 } 579 private Terms(final Words words){ 580 this.words = words; 581 } 582 private static FromToken<Tok> isToken(final Object t){ 583 return new FromToken<Tok>(){ 584 public Tok fromToken(Tok ptok){ 585 final Object obj = ptok.getToken(); 586 if(obj != t) return null; 587 return ptok; 588 } 589 }; 590 } 591 private static String toLabel(final String [] keys){ 592 if(keys.length == 0) return ""; 593 final StringBuffer buf = new StringBuffer (); 594 buf.append('[').append(keys[0]); 595 for(int i=1; i<keys.length; i++){ 596 buf.append(',').append(keys[i]); 597 } 598 buf.append(']'); 599 return buf.toString(); 600 } 601 } 602 | Popular Tags |