1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 *
7 * Changes are Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
8 ------------------------------------------------------------------------------*/
9 #ifndef _lucene_queryParser_Lexer_
10 #define _lucene_queryParser_Lexer_
11 
12 #if defined(_LUCENE_PRAGMA_ONCE)
13 # pragma once
14 #endif
15 
16 #include "CLucene/util/FastCharStream.h"
17 #include "CLucene/util/Reader.h"
18 #include "CLucene/util/StringBuffer.h"
19 
20 #include "TokenList.h"
21 
CL_NS_DEF(queryParser)22 CL_NS_DEF(queryParser)
23 class QueryParserBase;
24 	// A simple Lexer that is used by QueryParser.
25 	class Lexer:LUCENE_BASE
26 	{
27 	private:
28 		CL_NS(util)::FastCharStream* reader;
29 		QueryParserBase* queryparser; //holds the queryparser so that we can do callbacks
30 		bool delSR;  //Indicates if the reader must be deleted or not
31 
32     public:
33 		// Initializes a new instance of the Lexer class with the specified
34 		// query to lex.
35 		Lexer(QueryParserBase* queryparser, const TCHAR* query);
36 
37 		// Initializes a new instance of the Lexer class with the specified
38 		// TextReader to lex.
39 		Lexer(QueryParserBase* queryparser, CL_NS(util)::Reader* source);
40 
41 		//Breaks the input stream onto the tokens list tokens
42 		void Lex(TokenList *tokens);
43 
44 		~Lexer();
45 
46 	private:
47 		bool GetNextToken(QueryToken* token);
48 
49 		// Reads an integer number. buf should quite large, probably as large as a field should ever be
50 		void ReadIntegerNumber(const TCHAR ch, TCHAR* buf, int buflen);
51 
52 		// Reads an inclusive range like [some words]
53 		bool ReadInclusiveRange(const TCHAR prev, QueryToken* token);
54 
55 		// Reads an exclusive range like {some words}
56 		bool ReadExclusiveRange(const TCHAR prev, QueryToken* token);
57 
58 		// Reads quoted string like "something else"
59 		bool ReadQuoted(const TCHAR prev, QueryToken* token);
60 
61 		bool ReadTerm(const TCHAR prev, QueryToken* token);
62 
63         //reads an escaped character into the buf. Buf requires at least 3 characters
64 		bool ReadEscape(const TCHAR prev, TCHAR* buf);
65 	};
66 CL_NS_END
67 #endif
68