📄 simpleparser.jj
字号:
{ if ( DEBUG ) System.err.println( "Building AND query: " + res ); } { qrm.add( res ); } )* { if ( qrm.size() == 1 ) return res; return new And( qrm.toArray( Queries.EMPTY_ARRAY ) ); }}/** Starting rule for an AND<-query. * @param indexAlias the index alias for the default index to be used for the query that is going to be parsed. * @return the result of the query. */Query orderedAndQuery( String indexAlias ):{ Query res; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>(); }{ res = alignQuery( indexAlias ) { if ( DEBUG ) System.err.println( "Building AND< query: " + res ); } { qrm.add( res ); } ( <OAND> res = alignQuery( indexAlias ) { if ( DEBUG ) System.err.println( "Building AND< query: " + res ); } { qrm.add( res ); } )* { if ( qrm.size() == 1 ) return res; return new OrderedAnd( qrm.toArray( Queries.EMPTY_ARRAY ) ); }}/** Starting rule for an alignment query. * @param indexAlias the index alias for the default index to be used for the query that is going to be parsed. * @return the result of the query. */Query alignQuery( String indexAlias ):{ Query alignee, aligner = null; }{ alignee = andQuery( indexAlias ) [ <ALIGN> { if ( DEBUG ) System.err.println( "Building aligned query: " + alignee ); } aligner = andQuery( indexAlias ) ] { if ( aligner == null ) return alignee; return new Align( alignee, aligner ); }}/** Starting rule for a multiterm query. * @param indexAlias the index alias for the default index to be used for the query that is going to be parsed. * @return the result of the query. */Query multiTermQuery( String indexAlias ):{ Query res; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>();}{ res = atomicQuery( indexAlias ) { if ( DEBUG ) System.err.println( "Building multiterm query: " + res ); } { qrm.add( res ); } ( <PLUS> { if ( qrm.size() == 1 && ! ( res instanceof Term ) ) throw new ParseException("Expecting a term instead of " + res ); } res = atomicQuery( indexAlias ) { if ( DEBUG ) System.err.println( "Building multiterm query: " + res ); } { if ( ! ( res instanceof Term ) ) throw new ParseException("Expecting a term instead of " + res ); qrm.add( res ); } )* { if ( qrm.size() == 1 ) return res; return new MultiTerm( qrm.toArray( new Term[ qrm.size() ] ) ); }}/** Starting rule for a quoted query. Subqueries cannot change index. * @param indexAlias the index alias for the default index to be used for the query that is going to be parsed. * @return the result of the query. */Query quotedQuery( String indexAlias ):{ Query res = null; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>(); IntArrayList gaps = new IntArrayList(); int gap = 0; boolean holeSeen = false;}{ <QUOTE> ( res = atomicSimpleQuery( indexAlias ) { qrm.add( res ); gaps.add( gap ); gap = 0; } | <HOLE> { gap++; holeSeen = true; } ) ( res = atomicSimpleQuery( indexAlias ) { qrm.add( res ); gaps.add( gap ); gap = 0; } | <HOLE> { gap++; holeSeen = true; } )* <QUOTE> { if ( qrm.size() == 1 ) { if ( res == null ) throw new ParseException( "You must specify at least one term within quotes" ); return res; } return new Consecutive( qrm.toArray( Queries.EMPTY_ARRAY ), holeSeen ? gaps.toIntArray() : null ); }}/** Starting rule for an atomic query. May be either an atomic simple query or a * quoted query. It can optionally contain an index selector and * a low-pass limit. * @param indexAlias the index alias for the default index to be used for the query that is going to be parsed. * @return the result of the query. */Query atomicQuery( String indexAlias ):{ Query res; Token t; String newIndexAlias = indexAlias; boolean negative = false;}{ [ <NOT> { negative = true; } ] [ LOOKAHEAD(2) t = <WORD> { newIndexAlias = t.image; } <COLON> ] ( res = atomicSimpleQuery( newIndexAlias ) | res = quotedQuery( newIndexAlias ) ) { if ( negative ) res = new Not( res ); if ( newIndexAlias != indexAlias ) res = new Select( newIndexAlias, res ); } [ <TILDA> t = <WORD> { int limit = 0; try { limit = Integer.parseInt( t.image ); if ( limit <= 0 ) throw new NumberFormatException(); } catch ( NumberFormatException e ) { throw new ParseException( "Expecting a positive number, found " + t.image + " instead" ); } if ( DEBUG ) System.err.println( "Building low-pass filter with threshold " + limit ); res = new LowPass( res, limit ); } ] { return res; }}/** Starting rule for an atomic simple query. May be either a word or a query * enclosed within parentheses. * @param indexAlias the index alias for the default index to be used for the query that is going to be parsed. * @return the result of the query. */Query atomicSimpleQuery( String indexAlias ):{ Query res; Token t = null, s = null, p = null;}{ [ s = <SHARP> ] t = <WORD> [ p = <PREFIX> ] { String word = t.image; if ( ! indices.contains( indexAlias ) ) throw new ParseException( "Index \"" + indexAlias + "\" does not exist" ); if ( s != null && p != null ) throw new ParseException( s + " and " + p + " cannot be used at the same time" ); final MutableString term = new MutableString(); if ( s == null ) { // Before passing the word to anyone, we pass it through the index term processor. for( int i = 0; i < word.length(); i++ ) { if ( word.charAt( i ) == '\\' ) i++; term.append( word.charAt( i ) ); } if ( p != null ) { if ( termProcessors != null && ! termProcessors.get( indexAlias ).processPrefix( term ) ) throw new ParseException( "Prefix " + term + " has not been indexed" ); } else { if ( termProcessors != null && ! termProcessors.get( indexAlias ).processTerm( term ) ) throw new ParseException( "Term " + term + " has not been indexed" ); } } if ( p == null ) { if ( s != null ) { try { return new Term( Integer.parseInt( word ) ); } catch( NumberFormatException e ) { throw new ParseException( "Malformed number: " + word ); } } else return new Term( term ); } else { return new Prefix( term ); } } | <OPEN_RANGE> [ s = <WORD> ] <INTERVAL_SEPARATOR> [ t = <WORD> ] <CLOSE_RANGE> { return new Range( s != null ? s.toString() : null, t != null ? t.toString() : null ); } | <OPEN_PAREN> res = query( indexAlias ) <CLOSE_PAREN> { return res; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -