📄 simpleparser.java
字号:
/* Generated By:JavaCC: Do not edit this line. SimpleParser.java */ package it.unimi.dsi.mg4j.query.parser; import it.unimi.dsi.lang.*; import it.unimi.dsi.mg4j.index.*; import it.unimi.dsi.mg4j.query.nodes.*; import it.unimi.dsi.fastutil.objects.*; import it.unimi.dsi.fastutil.ints.*; import java.util.*;/* * MG4J: Managing Gigabytes for Java * * Copyright (C) 2006-2007 Paolo Boldi and Sebastiano Vigna * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * *//** A simple parser that transform a query string into a {@linkplain it.unimi.dsi.mg4j.query.nodes query}. * * <P>The parser supports multiple indices. You must provide a set of * <em>index aliases</em> that the user will use to select indices, and the name of the default index * alias to be used. After that, you parse a query by using the {@link #parse(String)} method. * * <P>The parser generated by JavaCC for this class will break terms using operators and * nonspace-to-space transitions. Operators can be included in terms, if needed, using * the backslash, which acts as an escape character, and makes the next character * (usually an operator) a standard character. The syntax of the parser can be seen in action * in the documentation of the package {@link it.unimi.dsi.mg4j.search}. * * <P>The parser returns a {@link it.unimi.dsi.mg4j.query.nodes.Query}—an abstract * representation of the query string that can be turned later into a * {@link it.unimi.dsi.mg4j.search.DocumentIterator}. * * <P>If a {@link TermProcessor} is specified, it will be applied to the terms found in the * query string. This can lead to transformations (e.g., downcasing) or generate an exception * if the query string contains terms filtered by the term processor. * * @author Sebastiano Vigna * @author Paolo Boldi * @since 1.0.1 * */public class SimpleParser implements QueryParser, SimpleParserConstants { /** The set of index aliases. */ public Set<String> indices; /** The default index alias. */ public String defaultIndex; /** The term processor for each index. */ public Map<String,? extends TermProcessor> termProcessors; /** The query visitor used to check for Select nodes. */ private CheckForSelectQueryVisitor visitor; private final static boolean DEBUG = false; /** Creates a parser. * * @param indices the set of index aliases. * @param defaultIndex the default index alias to be used when parsing the query. * @param termProcessors a map from index aliases to the corresponding term processor, or <code>null</code> * for no term processing. */ public SimpleParser( final Set<String> indices, final String defaultIndex, final Map<String,? extends TermProcessor> termProcessors ) { this( new java.io.StringReader( "" ) ); // Just for initialization purposes... this.indices = indices; this.defaultIndex = defaultIndex; this.termProcessors = termProcessors; this.visitor = new CheckForSelectQueryVisitor( defaultIndex ); } /** Creates a parser with no term processing. * * @param indices the set of index aliases. * @param defaultIndex the default index alias to be used when parsing the query. */ public SimpleParser( final Set<String> indices, final String defaultIndex ) { this( indices, defaultIndex, null ); } /** Creates a parser for a single nameless index with no term processing. * * <P>Parsers created by this constructor allow only nameless access, * both in the query and in the interval-iterator methods. */ public SimpleParser() { this( ObjectSets.singleton( "" ), "" ); } /** Creates a parser for a single nameless index with a given term processor. * * <P>Parsers created by this constructor allow only nameless access, * both in the query and in the interval-iterator methods. */ public SimpleParser( final TermProcessor termProcessor ) { this( ObjectSets.singleton( "" ), "", Object2ObjectMaps.singleton( "", termProcessor ) ); } public SimpleParser copy() { return new SimpleParser( indices, defaultIndex, termProcessors ); } /** Parses the given query, returning the corresponding query result. * @param text the query to be parsed. * @return an abstract representation of <code>query</code>. * @throws QueryParserException if a parse exception has taken place during query parsing. * @throws TokenMgrError if a tokenization exception has taken place during query parsing. */ public Query parse( String text ) throws QueryParserException { ReInit( new java.io.StringReader( text ) ); try { final Query query = query( defaultIndex ); try { visitor.prepare(); if ( query.accept( visitor ) == null ) throw new ParseException( visitor.errorMessage ); } catch( QueryBuilderVisitorException e ) {} return query; } catch( ParseException e ) { throw new QueryParserException( e ); } }/** Parser. *//** Starting rule for a difference 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. */ final public Query query(String indexAlias) throws ParseException { Query minuend, subtrahend = null; Token leftMargin = null, rightMargin = null; minuend = orQuery(indexAlias); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case MINUS: jj_consume_token(MINUS); if ( DEBUG ) System.err.println( "Building difference query" ); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case OPEN_ENLARGE: jj_consume_token(OPEN_ENLARGE); leftMargin = jj_consume_token(WORD); jj_consume_token(COLON); rightMargin = jj_consume_token(WORD); jj_consume_token(CLOSE_ENLARGE); break; default: jj_la1[0] = jj_gen; ; } subtrahend = orQuery( indexAlias ); break; default: jj_la1[1] = jj_gen; ; } if ( subtrahend == null ) {if (true) return minuend;} {if (true) return new Difference( minuend, subtrahend, leftMargin == null ? 0 : Integer.parseInt( leftMargin.image ), rightMargin == null ? 0 : Integer.parseInt( rightMargin.image ) );} throw new Error("Missing return statement in function"); }/** Starting rule for a OR-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. */ final public Query orQuery(String indexAlias) throws ParseException { Query res; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>(); res = orderedAndQuery(indexAlias); qrm.add( res ); label_1: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case OR: ; break; default: jj_la1[2] = jj_gen; break label_1; } jj_consume_token(OR); if ( DEBUG ) System.err.println( "Building OR query" ); res = orderedAndQuery(indexAlias); qrm.add( res ); } if ( qrm.size() == 1 ) {if (true) return res;} {if (true) return new Or( qrm.toArray( Queries.EMPTY_ARRAY ) );} throw new Error("Missing return statement in function"); }/** Starting rule for an AND-query (the AND token is optional). * @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. */ final public Query andQuery(String indexAlias) throws ParseException { Query res; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>(); res = multiTermQuery(indexAlias); if ( DEBUG ) System.err.println( "Building AND query: " + res ); qrm.add( res ); label_2: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case AND: case NOT: case OPEN_PAREN: case OPEN_RANGE: case QUOTE: case SHARP: case WORD: ; break; default: jj_la1[3] = jj_gen; break label_2; } switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case AND: jj_consume_token(AND); break; default: jj_la1[4] = jj_gen; ; } res = multiTermQuery(indexAlias); if ( DEBUG ) System.err.println( "Building AND query: " + res ); qrm.add( res ); } if ( qrm.size() == 1 ) {if (true) return res;} {if (true) return new And( qrm.toArray( Queries.EMPTY_ARRAY ) );} throw new Error("Missing return statement in function"); }/** 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. */ final public Query orderedAndQuery(String indexAlias) throws ParseException { Query res; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>(); res = alignQuery(indexAlias); if ( DEBUG ) System.err.println( "Building AND< query: " + res ); qrm.add( res ); label_3: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case OAND: ; break; default: jj_la1[5] = jj_gen; break label_3; } jj_consume_token(OAND); res = alignQuery(indexAlias); if ( DEBUG ) System.err.println( "Building AND< query: " + res ); qrm.add( res ); } if ( qrm.size() == 1 ) {if (true) return res;} {if (true) return new OrderedAnd( qrm.toArray( Queries.EMPTY_ARRAY ) );} throw new Error("Missing return statement in function"); }/** 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. */ final public Query alignQuery(String indexAlias) throws ParseException { Query alignee, aligner = null; alignee = andQuery(indexAlias); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case ALIGN: jj_consume_token(ALIGN); if ( DEBUG ) System.err.println( "Building aligned query: " + alignee ); aligner = andQuery(indexAlias); break; default: jj_la1[6] = jj_gen; ; } if ( aligner == null ) {if (true) return alignee;} {if (true) return new Align( alignee, aligner );} throw new Error("Missing return statement in function"); }/** 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. */ final public Query multiTermQuery(String indexAlias) throws ParseException { Query res; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>(); res = atomicQuery(indexAlias); if ( DEBUG ) System.err.println( "Building multiterm query: " + res ); qrm.add( res ); label_4: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case PLUS: ; break; default: jj_la1[7] = jj_gen; break label_4; } jj_consume_token(PLUS); if ( qrm.size() == 1 && ! ( res instanceof Term ) ) {if (true) 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 ) ) {if (true) throw new ParseException("Expecting a term instead of " + res );} qrm.add( res ); } if ( qrm.size() == 1 ) {if (true) return res;} {if (true) return new MultiTerm( qrm.toArray( new Term[ qrm.size() ] ) );} throw new Error("Missing return statement in function"); }/** 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. */ final public Query quotedQuery(String indexAlias) throws ParseException { Query res = null; ObjectArrayList<Query> qrm = new ObjectArrayList<Query>(); IntArrayList gaps = new IntArrayList(); int gap = 0; boolean holeSeen = false; jj_consume_token(QUOTE); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case OPEN_PAREN: case OPEN_RANGE: case SHARP: case WORD: res = atomicSimpleQuery(indexAlias); qrm.add( res ); gaps.add( gap ); gap = 0; break; case HOLE: jj_consume_token(HOLE); gap++; holeSeen = true; break; default: jj_la1[8] = jj_gen; jj_consume_token(-1); throw new ParseException(); } label_5: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case OPEN_PAREN: case OPEN_RANGE: case SHARP: case HOLE: case WORD: ; break; default: jj_la1[9] = jj_gen; break label_5; } switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case OPEN_PAREN: case OPEN_RANGE: case SHARP: case WORD: res = atomicSimpleQuery(indexAlias); qrm.add( res ); gaps.add( gap ); gap = 0; break; case HOLE: jj_consume_token(HOLE); gap++; holeSeen = true; break; default: jj_la1[10] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } jj_consume_token(QUOTE); if ( qrm.size() == 1 ) { if ( res == null ) {if (true) throw new ParseException( "You must specify at least one term within quotes" );} {if (true) return res;} } {if (true) return new Consecutive( qrm.toArray( Queries.EMPTY_ARRAY ), holeSeen ? gaps.toIntArray() : null );} throw new Error("Missing return statement in function"); }/** 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. */ final public Query atomicQuery(String indexAlias) throws ParseException { Query res; Token t; String newIndexAlias = indexAlias; boolean negative = false; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case NOT: jj_consume_token(NOT); negative = true; break; default: jj_la1[11] = jj_gen; ; } if (jj_2_1(2)) { t = jj_consume_token(WORD); newIndexAlias = t.image; jj_consume_token(COLON); } else { ; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -