1 8 13 package jfun.parsec; 14 15 import jfun.parsec.pattern.Patterns; 16 import jfun.parsec.tokens.Tokenizers; 17 18 19 27 public final class Lexers { 28 private static final Parser<Tok> _charLiteral = charLiteral("charLiteral"); 29 34 public static Parser<Tok> charLiteral(){ 35 return _charLiteral; 36 } 37 43 public static Parser<Tok> charLiteral(final String name){ 44 return Lexers.lexer(name, 45 Scanners.isQuotedChar(), 46 Tokenizers.forChar()); 47 } 48 private static final Parser<Tok> _stringLiteral = stringLiteral("stringLiteral"); 49 55 public static Parser<Tok> stringLiteral(){ 56 return lexSimpleStringLiteral(); 57 } 58 63 public static Parser<Tok> lexSimpleStringLiteral(){ 64 return _stringLiteral; 65 } 66 73 public static Parser<Tok> stringLiteral(final String name){ 74 return lexSimpleStringLiteral(name); 75 } 76 82 public static Parser<Tok> lexSimpleStringLiteral(final String name){ 83 return Lexers.lexer(name, 84 Scanners.isQuotedString(), 85 Tokenizers.forSimpleStringLiteral() 86 ); 87 } 88 private static final Parser<Tok> _sqlStringLiteral = sqlStringLiteral("string quoted by '"); 89 94 public static Parser<Tok> sqlStringLiteral(){ 95 return _sqlStringLiteral; 96 } 97 103 public static Parser<Tok> sqlStringLiteral(final String name){ 104 return Lexers.lexer(name, 105 Scanners.isSqlString(), 106 Tokenizers.forSqlStringLiteral() 107 ); 108 } 109 private static final Parser<Tok> _decimal = decimal("decimal"); 110 115 public static Parser<Tok> decimal(){ 116 return _decimal; 117 } 118 124 public static Parser<Tok> decimal(final String name){ 125 return Lexers.lexer(name, 126 Scanners.delimited(Scanners.isPattern(Patterns.isDecimal(), "decimal number")), 127 Tokenizers.forDecimal() 128 ); 129 } 130 private static final Parser<Tok> _integer = integer("integer"); 131 137 public static Parser<Tok> integer(){ 138 return _integer; 139 } 140 147 public static Parser<Tok> integer(final String name){ 148 return Lexers.lexer(name, Scanners.delimited( 149 Scanners.isPattern(Patterns.isInteger(), "integer")), 150 Tokenizers.forInteger()); 151 } 152 159 160 public static Parser<Tok> decInteger(){ 161 return lexDecLong(); 162 } 163 171 public static Parser<Tok> decInteger(final String name){ 172 return lexDecLong(name); 173 } 174 175 182 public static Parser<Tok> octInteger(){ 183 return lexOctLong(); 184 } 185 193 public static Parser<Tok> octInteger(final String name){ 194 return lexOctLong(name); 195 } 196 203 public static Parser<Tok> hexInteger(){ 204 return lexHexLong(); 205 } 206 214 public static Parser<Tok> hexInteger(final String name){ 215 return lexHexLong(name); 216 } 217 223 public static Parser<Tok> allInteger(){ 224 return lexLong(); 225 } 226 233 public static Parser<Tok> allInteger(final String name){ 234 return lexLong(name); 235 } 236 237 private static final Parser<Tok> _decLong = lexDecLong("decLong"); 238 244 public static Parser<Tok> lexDecLong(){ 245 return _decLong; 246 } 247 254 public static Parser<Tok> lexDecLong(final String name){ 255 return Lexers.lexer(name, 256 Scanners.delimited(Scanners.isPattern(Patterns.isDecInteger(), 257 "decLong")), Tokenizers.forDecLong()); 258 } 259 260 private static final Parser<Tok> _octLong = lexOctLong("octLong"); 261 267 public static Parser<Tok> lexOctLong(){ 268 return _octLong; 269 } 270 277 public static Parser<Tok> lexOctLong(final String name){ 278 return Lexers.lexer(name, 279 Scanners.delimited(Scanners.isPattern( 280 Patterns.isOctInteger(), "octLong")), Tokenizers.forOctLong()); 281 } 282 private static final Parser<Tok> _hexLong = lexHexLong("hexLong"); 283 289 public static Parser<Tok> lexHexLong(){ 290 return _hexLong; 291 } 292 299 public static Parser<Tok> lexHexLong(final String name){ 300 return Lexers.lexer(name, 301 Scanners.delimited(Scanners.isPattern(Patterns.isHexInteger(), "hexLong")) 302 , Tokenizers.forHexLong()); 303 } 304 private static final Parser<Tok> _allLong = lexLong("allLong"); 305 310 public static Parser<Tok> lexLong(){ 311 return _allLong; 312 } 313 319 public static Parser<Tok> lexLong(final String name){ 320 return Parsers.plus(name, lexHexLong(), lexDecLong(), lexOctLong()); 321 } 322 private static final Parser<Tok> _word = word("word"); 323 329 public static Parser<Tok> word(){ 330 return _word; 331 } 332 339 public static Parser<Tok> word(final String name){ 340 return Lexers.lexer(name, Scanners.delimited( 341 Scanners.isPattern(Patterns.isWord(), "word")), 342 Tokenizers.forWord()); 343 } 344 352 public static Parser<Tok> quoted(final String name, final char open, final char close){ 353 return Lexers.lexer(name, Scanners.quoted(name, open, close), 354 Tokenizers.forQuotedString(open, close)); 355 } 356 363 public static Parser<Tok> quoted(final char open, final char close){ 364 return quoted("quoted", open, close); 365 } 366 367 373 public static Words getOperators(final String ... ops){ 374 return Words.getOperators(ops); 375 } 376 387 public static Words getCaseInsensitive( 388 final String [] ops, final String [] keywords){ 389 return Words.getCaseInsensitive(ops, keywords); 390 } 391 392 403 public static Words getCaseSensitive( 404 final String [] ops, final String [] keywords){ 405 return Words.getCaseSensitive(ops, keywords); 406 } 407 417 public static Words getCaseInsensitive(final Parser<?> wscanner, 418 final String [] ops, final String [] keywords){ 419 return Words.getCaseInsensitive(wscanner, ops, keywords); 420 } 421 431 public static Words getCaseSensitive(final Parser<?> wscanner, 432 final String [] ops, final String [] keywords){ 433 return Words.getCaseSensitive(wscanner, ops, keywords); 434 } 435 446 public static Words getCaseInsensitive(final Parser<?> wscanner, 447 final String [] ops, final String [] keywords, FromString<?> toWord){ 448 return Words.getCaseInsensitive(wscanner, ops, keywords, toWord); 449 } 450 461 public static Words getCaseSensitive(final Parser<?> wscanner, 462 final String [] ops, final String [] keywords, FromString<?> toWord){ 463 return Words.getCaseSensitive(wscanner, ops, keywords, toWord); 464 } 465 474 public static Parser<Tok> lexer(final String name, final Parser<?> s, final Tokenizer tn){ 475 return lexer(name, s, tn, "lexer error"); 476 } 477 485 public static Parser<Tok> lexer(final Parser<?> s, final Tokenizer tn){ 486 return lexer("lexer", s, tn); 487 } 488 497 public static Parser<Tok> lexer(final Parser<?> s, final Tokenizer tn, final String err){ 498 return lexer("lexer", s, tn, err); 499 } 500 510 public static Parser<Tok> lexer(final String name, final Parser<?> s, final Tokenizer tn, 511 final String err){ 512 return new Parser<Tok>(name){ 513 boolean apply(final ParseContext ctxt){ 514 final int ind = ctxt.getIndex(); 515 final int from = ctxt.getAt(); 516 final Object ret = ctxt.getReturn(); 517 final int at = ctxt.getAt(); 518 final int step = ctxt.getStep(); 519 final Object ustate = ctxt.getUserState(); 520 if(!s.parse(ctxt)) return false; 522 final int len = ctxt.getAt() - from; 523 final Object tok = tn.toToken(ctxt.getSource(), from, len); 524 if(tok == null){ 525 ctxt.set(step, at, ret, ustate, ParsecError.raiseExpecting(ind, err)); 526 return false; 527 } 528 final Tok ptok = new Tok(at, len, tok); 529 ctxt.setStep(step+1); 530 ctxt.setReturn(ptok); 531 return true; 532 } 533 }; 534 } 535 545 public static Parser<Tok[]> lexeme(final String name, 546 final Parser<?> delim, final Parser<Tok> s){ 547 return delim.optional().seq(name, Parsers.sepEndBy(name, Tok.class, delim, s)); 548 } 549 558 public static Parser<Tok[]> lexeme(final Parser<?> delim, final Parser<Tok> s){ 559 return lexeme("lexeme", delim, s); 560 } 561 } 562 | Popular Tags |