-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathqueryparse.c
1230 lines (1107 loc) · 38.5 KB
/
queryparse.c
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
/* queryparse.c implements a parser to parse google-style queries. See
* queryparse.h for more detail
*
* note that although queryparse currently only supports parsing the
* whole query in-place, it was written to allow fairly easy conversion
* to more flexible parsing schemes.
*
* written nml 2003-07-08
*
*/
#include "firstinclude.h"
#include "queryparse.h"
#include "ascii.h"
#include "def.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
/* literals for the operators. note that AND and OR must be
* uppercase alpha characters. Other characters cannot be alpha-numeric. */
#define AND_OP "AND"
#define OR_OP "OR"
#define NOSTOP_OP '+'
#define EXCLUDE_OP '-'
#define PHRASE_DELIM '"'
#define MOD_START '['
#define MOD_STRING_END ':'
#define MOD_END ']'
struct queryparse {
const char *buf; /* buffer we're parsing from */
const char *end; /* end of the buffer */
unsigned int maxwordlen; /* maximum word len */
int state; /* parsing state we're in */
unsigned int bytes; /* number of bytes parsed thus far */
int err; /* last error to occur or 0 */
unsigned int warn; /* accumulated warnings */
};
/* parser states */
enum {
ERR = 0, /* error */
ENDFILE = 1, /* hit end-of-file (or string in this case) */
TOPLEVEL = 2, /* not in any entities */
INWORD = 3, /* in a word */
INWORD_PUNC = 5, /* in a word, after punctuation */
INWORD_NOSTOP = 6, /* in a word that shouldn't be stopped */
INWORD_NOSTOP_PUNC = 8, /* in a no-stop word, after punctuation */
INWORD_EXCLUDE = 9, /* in word that should be excluded from results*/
INWORD_EXCLUDE_PUNC = 11, /* in neg-word after punctuation */
INOR = 12, /* in an OR operator */
INAND = 13, /* in an AND operator */
INPHRASE = 14, /* in a " delimited phrase */
INPHRASE_WORD = 15, /* in a word in a phrase */
INPHRASE_WORD_PUNC = 17, /* in a word in a phrase after punctuation */
INPHRASE_END = 18, /* in a phrase that has ended */
ENDSENTENCE = 19, /* in the end of a sentence in a phrase */
ENDSENTENCE_END = 20, /* in a sentence that has ended */
INMOD_MOD = 21, /* parsing the query modifier */
INMOD = 22, /* in a query modifier */
INMOD_WORD = 23, /* in word in query modifier */
INMOD_END = 26 /* in a modifier that has ended */
};
struct queryparse *queryparse_new(unsigned int maxwordlen,
const char *query, unsigned int querylen) {
struct queryparse *parser = malloc(sizeof(*parser));
if (parser) {
parser->buf = query;
parser->end = query + querylen;
parser->bytes = 0;
parser->state = TOPLEVEL;
parser->maxwordlen = maxwordlen;
}
return parser;
}
#define CASE_END_SENTENCE \
'.': case '!': case '?'
/* jump table */
#define JUMP(state) \
if (1) { \
switch (state) { \
case TOPLEVEL: goto toplevel_label; \
case INWORD: goto inword_label; \
case INWORD_PUNC: goto punc_label; \
case INWORD_NOSTOP: goto inword_nostop_label; \
case INWORD_NOSTOP_PUNC: goto inword_nostop_punc_label; \
case INWORD_EXCLUDE: goto inword_exclude_label; \
case INWORD_EXCLUDE_PUNC: goto inword_exclude_punc_label; \
case INOR: goto inor_label; \
case INAND: goto inand_label; \
case INPHRASE: goto inphrase_label; \
case INPHRASE_WORD: goto inphrase_word_label; \
case INPHRASE_WORD_PUNC: goto inphrase_word_punc_label; \
case INPHRASE_END: goto inphrase_end_label; \
case ENDSENTENCE: goto endsentence_label; \
case ENDSENTENCE_END: goto endsentence_end_label; \
case INMOD_MOD: goto inmod_mod_label; \
case INMOD: goto inmod_label; \
case INMOD_WORD: goto inmod_word_label; \
case INMOD_END: goto inmod_end_label; \
case ENDFILE: goto endfile_label; \
default: goto err_label; \
} \
} else /* dangling else to allow function-like syntax */
enum queryparse_ret queryparse_parse(struct queryparse *parser,
char *word, unsigned int *len) {
char c;
*len = 0;
JUMP(parser->state);
/* not in any entities yet */
toplevel_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
/* note: AND and OR could be more efficiently detected by special
* casing their first values, but that would make them harder to
* change */
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = ASCII_TOLOWER(c);
} else {
word[*len] = '\0';
parser->state = TOPLEVEL;
return QUERYPARSE_WORD;
}
if (c == AND_OP[0]) {
/* it could be an AND op */
goto inand_label;
} else if (c == OR_OP[0]) {
/* it could be an OR op */
goto inor_label;
} else {
/* start of a normal word */
goto inword_label;
}
break;
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
word[*len] = '\0';
parser->state = TOPLEVEL;
return QUERYPARSE_WORD;
}
/* start of a normal word */
goto inword_label;
break;
case NOSTOP_OP:
/* start of a non-stopping word */
goto inword_nostop_label;
break;
case EXCLUDE_OP:
/* start of an excluded word */
goto inword_exclude_label;
break;
case PHRASE_DELIM:
parser->state = INPHRASE;
return QUERYPARSE_START_PHRASE;
break;
case MOD_START:
goto inmod_mod_label;
break;
case ASCII_CASE_SPACE:
/* silently eat whitespace */
break;
case '(':
parser->warn |= QUERYPARSE_WARN_PARENS_BOOLEAN; /*warn,fallthrough*/
default:
/* anything else we don't record in the word, but go to inword
* anyway (so we know when a string of junk characters occurred) */
goto inword_label;
break;
}
}
/* if we end in toplevel we just EOF */
goto endfile_label;
/* in a normal word, we may or may not have actually pushed any characters onto
* the return word */
inword_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
}
break;
case '-':
default:
/* break across two punctuation marks in a row */
goto punc_label;
case ASCII_CASE_CONTROL:
/* ignore junk characters */
break;
case ASCII_CASE_SPACE:
/* word ended */
if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in a word, we have to return it */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE; /* transition to eof */
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
}
goto endfile_label;
punc_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
}
goto inword_label;
case ASCII_CASE_PUNCTUATION:
/* break across two punctuation marks in a row */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
default:
case ASCII_CASE_CONTROL:
/* ignore junk characters */
break;
case ASCII_CASE_SPACE:
/* word ended */
if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in a word, we have to return it */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE; /* transition to eof */
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
}
goto endfile_label;
/* in a word that shouldn't be stopped (pretty much the same as normal word) */
inword_nostop_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_NOSTOP;
}
break;
case ASCII_CASE_CONTROL:
/* ignore */
break;
default:
goto inword_nostop_punc_label;
case ASCII_CASE_SPACE:
/* word ended */
if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_NOSTOP;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in a word, we have to return it */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE; /* transition to eof */
return QUERYPARSE_WORD_NOSTOP;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
}
goto endfile_label;
/* in a word that shouldn't be stopped (pretty much the same as normal word) */
inword_nostop_punc_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_NOSTOP;
}
goto inword_nostop_label;
case ASCII_CASE_CONTROL:
/* ignore */
break;
case '-':
default: /* break across punctuation */
case ASCII_CASE_SPACE:
/* word ended */
if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_NOSTOP;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in a word, we have to return it */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE; /* transition to eof */
return QUERYPARSE_WORD_NOSTOP;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
}
goto endfile_label;
/* in a word that should be excluded (pretty much the same as normal word) */
inword_exclude_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_EXCLUDE;
}
break;
case ASCII_CASE_CONTROL:
/* ignore */
break;
default:
goto inword_exclude_punc_label;
case ASCII_CASE_SPACE:
/* word ended */
if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_EXCLUDE;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in a word, we have to return it */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE; /* transition to eof */
return QUERYPARSE_WORD_EXCLUDE;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
}
goto endfile_label;
/* in a word that should be excluded (pretty much the same as normal word) */
inword_exclude_punc_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_EXCLUDE;
}
break;
case ASCII_CASE_CONTROL:
/* ignore */
break;
case '-':
default: /* break across junk characters */
case ASCII_CASE_SPACE:
/* word ended */
if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD_EXCLUDE;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in a word, we have to return it */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE; /* transition to eof */
return QUERYPARSE_WORD_EXCLUDE;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
}
goto endfile_label;
/* in what may be an OR operator */
inor_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = ASCII_TOLOWER(c);
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
}
if (c != OR_OP[*len - 1]) {
/* it turned out not to be OR, go back to regular word
* parsing */
goto inword_label;
}
break;
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
}
/* it turned out not to be OR, go back to regular word parsing */
goto inword_label;
break;
case ASCII_CASE_CONTROL:
goto inword_label;
case '-':
default:
goto punc_label;
case ASCII_CASE_SPACE:
/* word ended */
if (!OR_OP[*len]) {
/* whitespace ended OR operator */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_OR;
} else if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in an unfinished OR, return it as word */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE;
return QUERYPARSE_WORD;
} else goto inword_label;
/* in what may be an AND operator */
inand_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = ASCII_TOLOWER(c);
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
}
if (c != AND_OP[*len - 1]) {
/* it turned out not to be AND, go back to regular word
* parsing */
goto inword_label;
}
break;
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
}
/* it turned out not to be AND, go back to regular word parsing */
goto inword_label;
break;
case ASCII_CASE_CONTROL:
goto inword_label;
case '-':
default:
goto punc_label;
case ASCII_CASE_SPACE:
/* word ended */
if (!AND_OP[*len]) {
/* whitespace ended OR operator */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_AND;
} else if (*len) {
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
}
}
/* if we end in an unfinished AND, return it as word */
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE;
return QUERYPARSE_WORD;
} else goto inword_label;
/* inside a phrase */
inphrase_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = INPHRASE; /* transition back to phrase */
return QUERYPARSE_WORD;
}
/* phrase word started */
goto inphrase_word_label;
break;
case PHRASE_DELIM:
/* phrase ended */
goto inphrase_end_label;
break;
case ASCII_CASE_SPACE:
/* silently eat whitespace characters */
break;
case ASCII_CASE_CONTROL:
default:
/* phrase word started */
goto inphrase_word_label;
break;
}
}
/* if we end in an unfinished phrase, warn them */
parser->warn |= QUERYPARSE_WARN_QUOTES_UNMATCHED;
goto endfile_label;
/* in a word, inside a phrase */
inphrase_word_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = INPHRASE; /* transition back to phrase */
return QUERYPARSE_WORD;
}
break;
case PHRASE_DELIM:
/* phrase ended */
if (*len) {
word[*len] = '\0';
parser->state = INPHRASE_END;
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto inphrase_end_label;
}
break;
case '-':
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = INPHRASE; /* transition back to phrase */
return QUERYPARSE_WORD;
}
break;
case ASCII_CASE_CONTROL:
/* ignore */
break;
default:
goto inphrase_word_punc_label;
case ASCII_CASE_SPACE:
/* phrase word ended */
if (*len) {
word[*len] = '\0';
parser->state = INPHRASE;
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
case CASE_END_SENTENCE:
/* watch out for the end of sentences */
goto endsentence_label;
break;
}
}
/* if we end in a word in an unfinished phrase, warn them and return word */
parser->warn |= QUERYPARSE_WARN_QUOTES_UNMATCHED;
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE;
return QUERYPARSE_WORD;
} else goto inphrase_end_label;
inphrase_word_punc_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = INPHRASE; /* transition back to phrase */
return QUERYPARSE_WORD;
}
/* phrase word started */
goto inphrase_word_label;
break;
case PHRASE_DELIM:
/* phrase ended */
if (*len) {
word[*len] = '\0';
parser->state = INPHRASE_END;
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto inphrase_end_label;
}
break;
case ASCII_CASE_CONTROL:
/* ignore */
break;
default: /* break across junk characters */
case ASCII_CASE_SPACE:
/* phrase word ended */
if (*len) {
word[*len] = '\0';
parser->state = INPHRASE;
return QUERYPARSE_WORD;
} else {
/* it was empty, so warn them of that */
parser->warn |= QUERYPARSE_WARN_NONWORD;
goto toplevel_label;
}
break;
case CASE_END_SENTENCE:
/* watch out for the end of sentences */
goto endsentence_label;
break;
}
}
/* if we end in a word in an unfinished phrase, warn them and return word */
parser->warn |= QUERYPARSE_WARN_QUOTES_UNMATCHED;
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE;
return QUERYPARSE_WORD;
} else goto inphrase_end_label;
/* need to return phrase end and then continue */
inphrase_end_label:
parser->state = TOPLEVEL;
return QUERYPARSE_END_PHRASE;
/* need to return sentence end and then continue */
endsentence_end_label:
parser->state = INPHRASE;
return QUERYPARSE_END_SENTENCE;
/* in what might be the end of a sentence */
endsentence_label:
while (parser->buf < parser->end) {
/* don't consume this character (yet) */
c = *parser->buf;
switch (c) {
case ASCII_CASE_SPACE:
/* it was the end of a sentence */
parser->buf++; /* consume the byte */
parser->bytes++;
assert(*len);
word[*len] = '\0';
parser->state = ENDSENTENCE_END;
return QUERYPARSE_WORD;
default:
/* back to a normal phrase word */
goto inphrase_word_label;
break;
}
}
/* finishing in endsentence means we need to return the phrase word */
if (*len) {
word[*len] = '\0';
parser->state = INPHRASE;
return QUERYPARSE_WORD;
} else goto inphrase_word_label;
/* parsing the modifier of a modifier */
inmod_mod_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case '-':
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
}
break;
case MOD_STRING_END:
/* end of the modifier in the mod */
word[*len] = '\0';
parser->state = INMOD; /* transition to inmod */
return QUERYPARSE_START_MODIFIER;
break;
case MOD_START:
parser->warn |= QUERYPARSE_WARN_PARENS_NESTED; /* warn,fallthrough*/
default:
case ASCII_CASE_SPACE:
/* it wasn't a mod after all (return word) */
word[*len] = '\0';
parser->state = TOPLEVEL; /* transition back to toplevel */
return QUERYPARSE_WORD;
break;
}
}
/* if we end in a mod, return as word */
parser->warn |= QUERYPARSE_WARN_PARENS_UNMATCHED;
if (*len) {
word[*len] = '\0';
parser->state = ENDFILE;
return QUERYPARSE_WORD;
} else goto inmod_end_label;
/* in the parameters of a modifier */
inmod_label:
while (parser->buf < parser->end) {
c = *parser->buf++;
parser->bytes++;
switch (c) {
case ASCII_CASE_UPPER:
c = ASCII_TOLOWER(c);
case ASCII_CASE_LOWER:
case ASCII_CASE_DIGIT:
case ASCII_CASE_EXTENDED:
/* push character onto word */
if (*len < parser->maxwordlen) {
word[(*len)++] = c;
} else {
/* word finished due to length constraint */
word[*len] = '\0';
parser->state = INMOD; /* transition to this state */
return QUERYPARSE_WORD;
}
/* start parsing word */
goto inmod_word_label;
break;
case MOD_END:
goto inmod_end_label;
break;
case ASCII_CASE_SPACE:
/* silently eat whitespace */
break;
case MOD_START:
parser->warn |= QUERYPARSE_WARN_PARENS_NESTED; /* warn,fallthrough*/
default:
/* anything else starts a word, but isn't recorded */
goto inmod_word_label;
break;
}