-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphinxquery.cpp
4407 lines (3699 loc) · 121 KB
/
sphinxquery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// $Id$
//
//
// Copyright (c) 2001-2015, Andrew Aksyonoff
// Copyright (c) 2008-2015, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
#include "sphinx.h"
#include "sphinxquery.h"
#include "sphinxutils.h"
#include "sphinxplugin.h"
#include <stdarg.h>
//////////////////////////////////////////////////////////////////////////
// EXTENDED PARSER RELOADED
//////////////////////////////////////////////////////////////////////////
#include "yysphinxquery.h"
// #define XQDEBUG 1
// #define XQ_DUMP_TRANSFORMED_TREE 1
// #define XQ_DUMP_NODE_ADDR 1
//////////////////////////////////////////////////////////////////////////
struct MultiformNode_t
{
XQNode_t * m_pNode;
int m_iDestStart;
int m_iDestCount;
};
class XQParser_t
{
public:
XQParser_t ();
~XQParser_t ();
public:
bool Parse ( XQQuery_t & tQuery, const char * sQuery, const CSphQuery * pQuery, const ISphTokenizer * pTokenizer, const CSphSchema * pSchema, CSphDict * pDict, const CSphIndexSettings & tSettings );
bool Error ( const char * sTemplate, ... ) __attribute__ ( ( format ( printf, 2, 3 ) ) );
void Warning ( const char * sTemplate, ... ) __attribute__ ( ( format ( printf, 2, 3 ) ) );
bool AddField ( FieldMask_t & dFields, const char * szField, int iLen );
bool ParseFields ( FieldMask_t & uFields, int & iMaxFieldPos, bool & bIgnore );
int ParseZone ( const char * pZone );
bool IsSpecial ( char c );
bool GetNumber ( const char * p );
int GetToken ( YYSTYPE * lvalp );
void HandleModifiers ( XQKeyword_t & tKeyword );
void AddQuery ( XQNode_t * pNode );
XQNode_t * AddKeyword ( const char * sKeyword, int iSkippedPosBeforeToken=0 );
XQNode_t * AddKeyword ( XQNode_t * pLeft, XQNode_t * pRight );
XQNode_t * AddOp ( XQOperator_e eOp, XQNode_t * pLeft, XQNode_t * pRight, int iOpArg=0 );
void SetPhrase ( XQNode_t * pNode, bool bSetExact );
void Cleanup ();
XQNode_t * SweepNulls ( XQNode_t * pNode );
bool FixupNots ( XQNode_t * pNode );
void FixupNulls ( XQNode_t * pNode );
void DeleteNodesWOFields ( XQNode_t * pNode );
void PhraseShiftQpos ( XQNode_t * pNode );
void FixupDestForms ();
inline void SetFieldSpec ( const FieldMask_t& uMask, int iMaxPos )
{
FixRefSpec();
m_dStateSpec.Last()->SetFieldSpec ( uMask, iMaxPos );
}
inline void SetZoneVec ( int iZoneVec, bool bZoneSpan = false )
{
FixRefSpec();
m_dStateSpec.Last()->SetZoneSpec ( m_dZoneVecs[iZoneVec], bZoneSpan );
}
inline void FixRefSpec ()
{
bool bRef = ( m_dStateSpec.GetLength()>1 && ( m_dStateSpec[m_dStateSpec.GetLength()-1]==m_dStateSpec[m_dStateSpec.GetLength()-2] ) );
if ( !bRef )
return;
XQLimitSpec_t * pSpec = m_dStateSpec.Pop();
m_dSpecPool.Add ( new XQLimitSpec_t ( *pSpec ) );
m_dStateSpec.Add ( m_dSpecPool.Last() );
}
public:
const CSphVector<int> & GetZoneVec ( int iZoneVec ) const
{
return m_dZoneVecs[iZoneVec];
}
public:
static const int MAX_TOKEN_BYTES = 3*SPH_MAX_WORD_LEN + 16;
XQQuery_t * m_pParsed;
BYTE * m_sQuery;
int m_iQueryLen;
const char * m_pLastTokenStart;
const char * m_pLastTokenEnd;
const CSphSchema * m_pSchema;
ISphTokenizer * m_pTokenizer;
CSphDict * m_pDict;
CSphVector<XQNode_t*> m_dSpawned;
XQNode_t * m_pRoot;
bool m_bStopOnInvalid;
int m_iAtomPos;
int m_iPendingNulls;
int m_iPendingType;
YYSTYPE m_tPendingToken;
bool m_bWasBlended;
bool m_bWasKeyword;
bool m_bEmpty;
bool m_bQuoted;
bool m_bEmptyStopword;
int m_iOvershortStep;
int m_iQuorumQuote;
int m_iQuorumFSlash;
bool m_bCheckNumber;
const PluginQueryTokenFilter_c * m_pPlugin;
void * m_pPluginData;
CSphVector<CSphString> m_dIntTokens;
CSphVector < CSphVector<int> > m_dZoneVecs;
CSphVector<XQLimitSpec_t *> m_dStateSpec;
CSphVector<XQLimitSpec_t *> m_dSpecPool;
CSphVector<int> m_dPhraseStar;
CSphVector<CSphString> m_dDestForms;
CSphVector<MultiformNode_t> m_dMultiforms;
};
//////////////////////////////////////////////////////////////////////////
int yylex ( YYSTYPE * lvalp, XQParser_t * pParser )
{
return pParser->GetToken ( lvalp );
}
void yyerror ( XQParser_t * pParser, const char * sMessage )
{
if ( pParser->m_pParsed->m_sParseError.IsEmpty() )
pParser->m_pParsed->m_sParseError.SetSprintf ( "%s near '%s'", sMessage, pParser->m_pLastTokenStart );
}
#include "yysphinxquery.c"
//////////////////////////////////////////////////////////////////////////
void XQLimitSpec_t::SetFieldSpec ( const FieldMask_t& uMask, int iMaxPos )
{
m_bFieldSpec = true;
m_dFieldMask = uMask;
m_iFieldMaxPos = iMaxPos;
}
/// ctor
XQNode_t::XQNode_t ( const XQLimitSpec_t & dSpec )
: m_pParent ( NULL )
, m_eOp ( SPH_QUERY_AND )
, m_iOrder ( 0 )
, m_iCounter ( 0 )
, m_iMagicHash ( 0 )
, m_iFuzzyHash ( 0 )
, m_dSpec ( dSpec )
, m_iOpArg ( 0 )
, m_iAtomPos ( -1 )
, m_iUser ( 0 )
, m_bVirtuallyPlain ( false )
, m_bNotWeighted ( false )
, m_bPercentOp ( false )
{
#ifdef XQ_DUMP_NODE_ADDR
printf ( "node new 0x%08x\n", this );
#endif
}
/// dtor
XQNode_t::~XQNode_t ()
{
#ifdef XQ_DUMP_NODE_ADDR
printf ( "node deleted %d 0x%08x\n", m_eOp, this );
#endif
ARRAY_FOREACH ( i, m_dChildren )
SafeDelete ( m_dChildren[i] );
}
void XQNode_t::SetFieldSpec ( const FieldMask_t& uMask, int iMaxPos )
{
// set it, if we do not yet have one
if ( !m_dSpec.m_bFieldSpec )
m_dSpec.SetFieldSpec ( uMask, iMaxPos );
// some of the children might not yet have a spec, even if the node itself has
// eg. 'hello @title world' (whole node has '@title' spec but 'hello' node does not have any!)
ARRAY_FOREACH ( i, m_dChildren )
m_dChildren[i]->SetFieldSpec ( uMask, iMaxPos );
}
void XQLimitSpec_t::SetZoneSpec ( const CSphVector<int> & dZones, bool bZoneSpan )
{
m_dZones = dZones;
m_bZoneSpan = bZoneSpan;
}
void XQNode_t::SetZoneSpec ( const CSphVector<int> & dZones, bool bZoneSpan )
{
// set it, if we do not yet have one
if ( !m_dSpec.m_dZones.GetLength() )
m_dSpec.SetZoneSpec ( dZones, bZoneSpan );
// some of the children might not yet have a spec, even if the node itself has
ARRAY_FOREACH ( i, m_dChildren )
m_dChildren[i]->SetZoneSpec ( dZones, bZoneSpan );
}
void XQNode_t::CopySpecs ( const XQNode_t * pSpecs )
{
if ( !pSpecs )
return;
if ( !m_dSpec.m_bFieldSpec )
m_dSpec.SetFieldSpec ( pSpecs->m_dSpec.m_dFieldMask, pSpecs->m_dSpec.m_iFieldMaxPos );
if ( !m_dSpec.m_dZones.GetLength() )
m_dSpec.SetZoneSpec ( pSpecs->m_dSpec.m_dZones, pSpecs->m_dSpec.m_bZoneSpan );
}
void XQNode_t::ClearFieldMask ()
{
m_dSpec.m_dFieldMask.SetAll();
ARRAY_FOREACH ( i, m_dChildren )
m_dChildren[i]->ClearFieldMask();
}
uint64_t XQNode_t::GetHash () const
{
if ( m_iMagicHash )
return m_iMagicHash;
XQOperator_e dZeroOp[2];
dZeroOp[0] = m_eOp;
dZeroOp[1] = (XQOperator_e) 0;
ARRAY_FOREACH ( i, m_dWords )
m_iMagicHash = 100 + ( m_iMagicHash ^ sphFNV64 ( m_dWords[i].m_sWord.cstr() ) ); // +100 to make it non-transitive
ARRAY_FOREACH ( j, m_dChildren )
m_iMagicHash = 100 + ( m_iMagicHash ^ m_dChildren[j]->GetHash() ); // +100 to make it non-transitive
m_iMagicHash += 1000000; // to immerse difference between parents and children
m_iMagicHash ^= sphFNV64 ( dZeroOp );
return m_iMagicHash;
}
uint64_t XQNode_t::GetFuzzyHash () const
{
if ( m_iFuzzyHash )
return m_iFuzzyHash;
XQOperator_e dZeroOp[2];
dZeroOp[0] = ( m_eOp==SPH_QUERY_PHRASE ? SPH_QUERY_PROXIMITY : m_eOp );
dZeroOp[1] = (XQOperator_e) 0;
ARRAY_FOREACH ( i, m_dWords )
m_iFuzzyHash = 100 + ( m_iFuzzyHash ^ sphFNV64 ( m_dWords[i].m_sWord.cstr() ) ); // +100 to make it non-transitive
ARRAY_FOREACH ( j, m_dChildren )
m_iFuzzyHash = 100 + ( m_iFuzzyHash ^ m_dChildren[j]->GetFuzzyHash () ); // +100 to make it non-transitive
m_iFuzzyHash += 1000000; // to immerse difference between parents and children
m_iFuzzyHash ^= sphFNV64 ( dZeroOp );
return m_iFuzzyHash;
}
void XQNode_t::SetOp ( XQOperator_e eOp, XQNode_t * pArg1, XQNode_t * pArg2 )
{
m_eOp = eOp;
m_dChildren.Reset();
if ( pArg1 )
{
m_dChildren.Add ( pArg1 );
pArg1->m_pParent = this;
}
if ( pArg2 )
{
m_dChildren.Add ( pArg2 );
pArg2->m_pParent = this;
}
}
int XQNode_t::FixupAtomPos()
{
assert ( m_eOp==SPH_QUERY_PROXIMITY && m_dWords.GetLength()>0 );
ARRAY_FOREACH ( i, m_dWords )
{
int iSub = m_dWords[i].m_iSkippedBefore-1;
if ( iSub>0 )
{
for ( int j = i; j < m_dWords.GetLength(); j++ )
m_dWords[j].m_iAtomPos -= iSub;
}
}
return m_dWords.Last().m_iAtomPos+1;
}
XQNode_t * XQNode_t::Clone ()
{
XQNode_t * pRet = new XQNode_t ( m_dSpec );
pRet->SetOp ( m_eOp );
pRet->m_dWords = m_dWords;
pRet->m_iOpArg = m_iOpArg;
pRet->m_iAtomPos = m_iAtomPos;
pRet->m_bVirtuallyPlain = m_bVirtuallyPlain;
pRet->m_bNotWeighted = m_bNotWeighted;
pRet->m_bPercentOp = m_bPercentOp;
if ( m_dChildren.GetLength()==0 )
return pRet;
pRet->m_dChildren.Reserve ( m_dChildren.GetLength() );
for ( int i = 0; i < m_dChildren.GetLength(); ++i )
{
pRet->m_dChildren.Add ( m_dChildren[i]->Clone() );
pRet->m_dChildren.Last()->m_pParent = pRet;
}
return pRet;
}
bool XQNode_t::ResetHash ()
{
bool bAlreadyReset = ( m_iMagicHash==0 && m_iFuzzyHash==0 );
m_iMagicHash = 0;
m_iFuzzyHash = 0;
return bAlreadyReset;
}
/// return either index of pNode among Parent.m_dChildren, or -1
static int GetNodeChildIndex ( const XQNode_t * pParent, const XQNode_t * pNode )
{
assert ( pParent && pNode );
ARRAY_FOREACH ( i, pParent->m_dChildren )
if ( pParent->m_dChildren[i]==pNode )
return i;
return -1;
}
//////////////////////////////////////////////////////////////////////////
XQParser_t::XQParser_t ()
: m_pParsed ( NULL )
, m_pLastTokenStart ( NULL )
, m_pLastTokenEnd ( NULL )
, m_pRoot ( NULL )
, m_bStopOnInvalid ( true )
, m_bWasBlended ( false )
, m_bWasKeyword ( false )
, m_bQuoted ( false )
, m_bEmptyStopword ( false )
, m_iQuorumQuote ( -1 )
, m_iQuorumFSlash ( -1 )
, m_bCheckNumber ( false )
, m_pPlugin ( NULL )
, m_pPluginData ( NULL )
{
m_dSpecPool.Add ( new XQLimitSpec_t() );
m_dStateSpec.Add ( m_dSpecPool.Last() );
}
XQParser_t::~XQParser_t ()
{
ARRAY_FOREACH ( i, m_dSpecPool )
SafeDelete ( m_dSpecPool[i] );
}
/// cleanup spawned nodes (for bailing out on errors)
void XQParser_t::Cleanup ()
{
m_dSpawned.Uniq(); // FIXME! should eliminate this by testing
ARRAY_FOREACH ( i, m_dSpawned )
{
m_dSpawned[i]->m_dChildren.Reset ();
SafeDelete ( m_dSpawned[i] );
}
m_dSpawned.Reset ();
m_dStateSpec.Reset();
}
bool XQParser_t::Error ( const char * sTemplate, ... )
{
assert ( m_pParsed );
char sBuf[256];
const char * sPrefix = "query error: ";
int iPrefix = strlen(sPrefix);
memcpy ( sBuf, sPrefix, iPrefix );
va_list ap;
va_start ( ap, sTemplate );
vsnprintf ( sBuf+iPrefix, sizeof(sBuf)-iPrefix, sTemplate, ap );
va_end ( ap );
m_pParsed->m_sParseError = sBuf;
return false;
}
void XQParser_t::Warning ( const char * sTemplate, ... )
{
assert ( m_pParsed );
char sBuf[256];
const char * sPrefix = "query warning: ";
int iPrefix = strlen(sPrefix);
memcpy ( sBuf, sPrefix, iPrefix );
va_list ap;
va_start ( ap, sTemplate );
vsnprintf ( sBuf+iPrefix, sizeof(sBuf)-iPrefix, sTemplate, ap );
va_end ( ap );
m_pParsed->m_sParseWarning = sBuf;
}
/// my special chars
bool XQParser_t::IsSpecial ( char c )
{
return c=='(' || c==')' || c=='|' || c=='-' || c=='!' || c=='@' || c=='~' || c=='"' || c=='/';
}
/// lookup field and add it into mask
bool XQParser_t::AddField ( FieldMask_t & dFields, const char * szField, int iLen )
{
CSphString sField;
sField.SetBinary ( szField, iLen );
int iField = m_pSchema->GetFieldIndex ( sField.cstr () );
if ( iField<0 )
{
if ( m_bStopOnInvalid )
return Error ( "no field '%s' found in schema", sField.cstr () );
else
Warning ( "no field '%s' found in schema", sField.cstr () );
} else
{
if ( iField>=SPH_MAX_FIELDS )
return Error ( " max %d fields allowed", SPH_MAX_FIELDS );
dFields.Set ( iField );
}
return true;
}
/// parse fields block
bool XQParser_t::ParseFields ( FieldMask_t & dFields, int & iMaxFieldPos, bool & bIgnore )
{
dFields.UnsetAll();
iMaxFieldPos = 0;
bIgnore = false;
const char * pPtr = m_pTokenizer->GetBufferPtr ();
const char * pLastPtr = m_pTokenizer->GetBufferEnd ();
if ( pPtr==pLastPtr )
return true; // silently ignore trailing field operator
bool bNegate = false;
bool bBlock = false;
// handle special modifiers
if ( *pPtr=='!' )
{
// handle @! and @!(
bNegate = true; pPtr++;
if ( *pPtr=='(' ) { bBlock = true; pPtr++; }
} else if ( *pPtr=='*' )
{
// handle @*
dFields.SetAll();
m_pTokenizer->SetBufferPtr ( pPtr+1 );
return true;
} else if ( *pPtr=='(' )
{
// handle @(
bBlock = true; pPtr++;
}
// handle invalid chars
if ( !sphIsAlpha(*pPtr) )
{
bIgnore = true;
m_pTokenizer->SetBufferPtr ( pPtr ); // ignore and re-parse (FIXME! maybe warn?)
return true;
}
assert ( sphIsAlpha(*pPtr) ); // i think i'm paranoid
// handle field specification
if ( !bBlock )
{
// handle standalone field specification
const char * pFieldStart = pPtr;
while ( sphIsAlpha(*pPtr) && pPtr<pLastPtr )
pPtr++;
assert ( pPtr-pFieldStart>0 );
if ( !AddField ( dFields, pFieldStart, pPtr-pFieldStart ) )
return false;
m_pTokenizer->SetBufferPtr ( pPtr );
if ( bNegate )
dFields.Negate();
} else
{
// handle fields block specification
assert ( sphIsAlpha(*pPtr) && bBlock ); // and complicated
bool bOK = false;
const char * pFieldStart = NULL;
while ( pPtr<pLastPtr )
{
// accumulate field name, while we can
if ( sphIsAlpha(*pPtr) )
{
if ( !pFieldStart )
pFieldStart = pPtr;
pPtr++;
continue;
}
// separator found
if ( pFieldStart==NULL )
{
CSphString sContext;
sContext.SetBinary ( pPtr, (int)( pLastPtr-pPtr ) );
return Error ( "invalid field block operator syntax near '%s'", sContext.cstr() ? sContext.cstr() : "" );
} else if ( *pPtr==',' )
{
if ( !AddField ( dFields, pFieldStart, pPtr-pFieldStart ) )
return false;
pFieldStart = NULL;
pPtr++;
} else if ( *pPtr==')' )
{
if ( !AddField ( dFields, pFieldStart, pPtr-pFieldStart ) )
return false;
m_pTokenizer->SetBufferPtr ( ++pPtr );
if ( bNegate )
dFields.Negate();
bOK = true;
break;
} else
{
return Error ( "invalid character '%c' in field block operator", *pPtr );
}
}
if ( !bOK )
return Error ( "missing closing ')' in field block operator" );
}
// handle optional position range modifier
if ( pPtr[0]=='[' && isdigit ( pPtr[1] ) )
{
// skip '[' and digits
const char * p = pPtr+1;
while ( *p && isdigit(*p) ) p++;
// check that the range ends with ']' (FIXME! maybe report an error if it does not?)
if ( *p!=']' )
return true;
// fetch my value
iMaxFieldPos = strtoul ( pPtr+1, NULL, 10 );
m_pTokenizer->SetBufferPtr ( p+1 );
}
// well done
return true;
}
/// helper find-or-add (make it generic and move to sphinxstd?)
static int GetZoneIndex ( XQQuery_t * pQuery, const CSphString & sZone )
{
ARRAY_FOREACH ( i, pQuery->m_dZones )
if ( pQuery->m_dZones[i]==sZone )
return i;
pQuery->m_dZones.Add ( sZone );
return pQuery->m_dZones.GetLength()-1;
}
/// parse zone
int XQParser_t::ParseZone ( const char * pZone )
{
const char * p = pZone;
// case one, just a single zone name
if ( sphIsAlpha ( *pZone ) )
{
// find zone name
while ( sphIsAlpha(*p) )
p++;
m_pTokenizer->SetBufferPtr ( p );
// extract and lowercase it
CSphString sZone;
sZone.SetBinary ( pZone, p-pZone );
sZone.ToLower();
// register it in zones list
int iZone = GetZoneIndex ( m_pParsed, sZone );
// create new 1-zone vector
m_dZoneVecs.Add().Add ( iZone );
return m_dZoneVecs.GetLength()-1;
}
// case two, zone block
// it must follow strict (name1,name2,...) syntax
if ( *pZone=='(' )
{
// create new zone vector
CSphVector<int> & dZones = m_dZoneVecs.Add();
p = ++pZone;
// scan names
for ( ;; )
{
// syntax error, name expected!
if ( !sphIsAlpha(*p) )
{
Error ( "unexpected character '%c' in zone block operator", *p );
return -1;
}
// scan next name
while ( sphIsAlpha(*p) )
p++;
// extract and lowercase it
CSphString sZone;
sZone.SetBinary ( pZone, p-pZone );
sZone.ToLower();
// register it in zones list
dZones.Add ( GetZoneIndex ( m_pParsed, sZone ) );
// must be either followed by comma, or closing paren
// everything else will cause syntax error
if ( *p==')' )
{
m_pTokenizer->SetBufferPtr ( p+1 );
break;
}
if ( *p==',' )
pZone = ++p;
}
return m_dZoneVecs.GetLength()-1;
}
// unhandled case
Error ( "internal error, unhandled case in ParseZone()" );
return -1;
}
bool XQParser_t::GetNumber ( const char * p )
{
int iDots = 0;
const char * sToken = p;
const char * sEnd = m_pTokenizer->GetBufferEnd ();
while ( p<sEnd && ( isdigit ( *(BYTE*)p ) || *p=='.' ) )
{
iDots += ( *p=='.' );
p++;
}
// must be float number but got many dots or only dot
if ( iDots && ( iDots>1 || p-sToken==iDots ) )
p = sToken;
// float as number allowed only as quorum argument and regular keywords stream otherwise
if ( iDots==1 && ( m_iQuorumQuote!=m_iQuorumFSlash || m_iQuorumQuote!=m_iAtomPos ) )
p = sToken;
static const int NUMBER_BUF_LEN = 10; // max strlen of int32
if ( p>sToken && p-sToken<NUMBER_BUF_LEN
&& !( *p=='-' && !( p-sToken==1 && sphIsModifier ( p[-1] ) ) ) // !bDashInside copied over from arbitration
&& ( *p=='\0' || sphIsSpace(*p) || IsSpecial(*p) ) )
{
// float as quorum argument has higher precedence than blended
bool bQuorum = ( m_iQuorumQuote==m_iQuorumFSlash && m_iQuorumFSlash==m_iAtomPos );
bool bQuorumPercent = ( bQuorum && iDots==1 );
bool bTok = ( m_pTokenizer->GetToken()!=NULL );
if ( bTok && m_pTokenizer->TokenIsBlended() && !( bQuorum || bQuorumPercent ) ) // number with blended should be tokenized as usual
{
m_pTokenizer->SkipBlended();
m_pTokenizer->SetBufferPtr ( m_pLastTokenStart );
} else if ( bTok && m_pTokenizer->WasTokenSynonym() && !( bQuorum || bQuorumPercent ) )
{
m_pTokenizer->SetBufferPtr ( m_pLastTokenStart );
} else
{
// got not a very long number followed by a whitespace or special, handle it
char sNumberBuf[NUMBER_BUF_LEN];
int iNumberLen = Min ( (int)sizeof(sNumberBuf)-1, int(p-sToken) );
memcpy ( sNumberBuf, sToken, iNumberLen );
sNumberBuf[iNumberLen] = '\0';
if ( iDots )
m_tPendingToken.tInt.fValue = (float)strtod ( sNumberBuf, NULL );
else
m_tPendingToken.tInt.iValue = atoi ( sNumberBuf );
// check if it can be used as a keyword too
m_pTokenizer->SetBuffer ( (BYTE*)sNumberBuf, iNumberLen );
sToken = (const char*) m_pTokenizer->GetToken();
m_pTokenizer->SetBuffer ( m_sQuery, m_iQueryLen );
m_pTokenizer->SetBufferPtr ( p );
m_tPendingToken.tInt.iStrIndex = -1;
if ( sToken )
{
m_dIntTokens.Add ( sToken );
m_pDict->SetApplyMorph ( m_pTokenizer->GetMorphFlag() );
if ( m_pDict->GetWordID ( (BYTE*)sToken ) )
m_tPendingToken.tInt.iStrIndex = m_dIntTokens.GetLength()-1;
else
m_dIntTokens.Pop();
m_iAtomPos++;
}
m_iPendingNulls = 0;
m_iPendingType = iDots ? TOK_FLOAT : TOK_INT;
return true;
}
}
return false;
}
/// a lexer of my own
int XQParser_t::GetToken ( YYSTYPE * lvalp )
{
bool bWasFrontModifier = false; // used to print warning
// what, noone's pending for a bending?!
if ( !m_iPendingType )
for ( ;; )
{
assert ( m_iPendingNulls==0 );
bool bWasKeyword = m_bWasKeyword;
m_bWasKeyword = false;
int iSkippedPosBeforeToken = 0;
if ( m_bWasBlended )
{
iSkippedPosBeforeToken = m_pTokenizer->SkipBlended();
m_iAtomPos += iSkippedPosBeforeToken;
}
// tricky stuff
// we need to manually check for numbers in certain states (currently, just after proximity or quorum operator)
// required because if 0-9 are not in charset_table, or min_word_len is too high,
// the tokenizer will *not* return the number as a token!
m_pLastTokenStart = m_pTokenizer->GetBufferPtr ();
m_pLastTokenEnd = m_pTokenizer->GetTokenEnd();
const char * sEnd = m_pTokenizer->GetBufferEnd ();
const char * p = m_pLastTokenStart;
while ( p<sEnd && isspace ( *(BYTE*)p ) ) p++; // to avoid CRT assertions on Windows
if ( m_bCheckNumber )
{
m_bCheckNumber = false;
if ( GetNumber(p) )
break;
}
// not a number, long number, or number not followed by a whitespace, so fallback to regular tokenizing
const char * sToken = (const char *) m_pTokenizer->GetToken ();
if ( !sToken )
{
m_iPendingNulls = m_pTokenizer->GetOvershortCount() * m_iOvershortStep;
if ( !( m_iPendingNulls || m_pTokenizer->GetBufferPtr()-p>0 ) )
return 0;
m_iPendingNulls = 0;
lvalp->pNode = AddKeyword ( NULL, iSkippedPosBeforeToken );
m_bWasKeyword = true;
return TOK_KEYWORD;
}
// now let's do some token post-processing
m_bWasBlended = m_pTokenizer->TokenIsBlended();
m_bEmpty = false;
m_iPendingNulls = m_pTokenizer->GetOvershortCount() * m_iOvershortStep;
m_iAtomPos += 1+m_iPendingNulls;
// handle NEAR (must be case-sensitive, and immediately followed by slash and int)
if ( sToken && p && !m_pTokenizer->m_bPhrase && strncmp ( p, "NEAR/", 5 )==0 && isdigit(p[5]) )
{
// extract that int
int iVal = 0;
for ( p=p+5; isdigit(*p); p++ )
iVal = iVal*10 + (*p) - '0'; // FIXME! check for overflow?
m_pTokenizer->SetBufferPtr ( p );
// we just lexed our next token
m_iPendingType = TOK_NEAR;
m_tPendingToken.tInt.iValue = iVal;
m_tPendingToken.tInt.iStrIndex = -1;
m_iAtomPos -= 1; // skip NEAR
break;
}
// handle SENTENCE
if ( sToken && p && !m_pTokenizer->m_bPhrase && !strcasecmp ( sToken, "sentence" ) && !strncmp ( p, "SENTENCE", 8 ) )
{
// we just lexed our next token
m_iPendingType = TOK_SENTENCE;
m_iAtomPos -= 1;
break;
}
// handle PARAGRAPH
if ( sToken && p && !m_pTokenizer->m_bPhrase && !strcasecmp ( sToken, "paragraph" ) && !strncmp ( p, "PARAGRAPH", 9 ) )
{
// we just lexed our next token
m_iPendingType = TOK_PARAGRAPH;
m_iAtomPos -= 1;
break;
}
// handle MAYBE
if ( sToken && p && !m_pTokenizer->m_bPhrase && !strcasecmp ( sToken, "maybe" ) && !strncmp ( p, "MAYBE", 5 ) )
{
// we just lexed our next token
m_iPendingType = TOK_MAYBE;
m_iAtomPos -= 1;
break;
}
// handle ZONE
if ( sToken && p && !m_pTokenizer->m_bPhrase && !strncmp ( p, "ZONE:", 5 )
&& ( sphIsAlpha(p[5]) || p[5]=='(' ) )
{
// ParseZone() will update tokenizer buffer ptr as needed
int iVec = ParseZone ( p+5 );
if ( iVec<0 )
return -1;
// we just lexed our next token
m_iPendingType = TOK_ZONE;
m_tPendingToken.iZoneVec = iVec;
m_iAtomPos -= 1;
break;
}
// handle ZONESPAN
if ( sToken && p && !m_pTokenizer->m_bPhrase && !strncmp ( p, "ZONESPAN:", 9 )
&& ( sphIsAlpha(p[9]) || p[9]=='(' ) )
{
// ParseZone() will update tokenizer buffer ptr as needed
int iVec = ParseZone ( p+9 );
if ( iVec<0 )
return -1;
// we just lexed our next token
m_iPendingType = TOK_ZONESPAN;
m_tPendingToken.iZoneVec = iVec;
m_iAtomPos -= 1;
break;
}
// count [ * ] at phrase node for qpos shift
// FIXME! RLP can return tokens from several buffers, all this pointer arithmetic will lead to crashes
if ( m_pTokenizer->m_bPhrase && m_pLastTokenEnd )
{
if ( strncmp ( sToken, "*", 1 )==0 )
{
m_dPhraseStar.Add ( m_iAtomPos );
} else
{
int iSpace = 0;
int iStar = 0;
const char * sCur = m_pLastTokenEnd;
const char * sEnd = m_pTokenizer->GetTokenStart();
for ( ; sCur<sEnd; sCur++ )
{
int iCur = sCur - m_pLastTokenEnd;
switch ( *sCur )
{
case '*':
iStar = sCur - m_pLastTokenEnd;
break;
case ' ':
if ( iSpace+2==iCur && iStar+1==iCur ) // match only [ * ] (separate single star) as valid shift operator
m_dPhraseStar.Add ( m_iAtomPos );
iSpace = iCur;
break;
}
}
}
}
// handle specials
if ( m_pTokenizer->WasTokenSpecial() )
{
// specials must not affect pos
m_iAtomPos--;
// some specials are especially special
if ( sToken[0]=='@' )
{
bool bIgnore;
// parse fields operator
if ( !ParseFields ( m_tPendingToken.tFieldLimit.dMask, m_tPendingToken.tFieldLimit.iMaxPos, bIgnore ) )
return -1;
if ( bIgnore )
continue;
m_iPendingType = TOK_FIELDLIMIT;
break;
} else if ( sToken[0]=='<' )
{
if ( *m_pTokenizer->GetBufferPtr()=='<' )
{
// got "<<", aka operator BEFORE
m_iPendingType = TOK_BEFORE;
break;
} else
{
// got stray '<', ignore
if ( m_iPendingNulls>0 )
{
m_iPendingNulls = 0;
lvalp->pNode = AddKeyword ( NULL, iSkippedPosBeforeToken );
m_bWasKeyword = true;
return TOK_KEYWORD;
}