-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathpatternSystem.cpp
More file actions
1918 lines (1836 loc) · 91.5 KB
/
patternSystem.cpp
File metadata and controls
1918 lines (1836 loc) · 91.5 KB
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
#include "common.h"
/*
:testpattern ( a _( case 0 ) ) a big dog
:testpattern ( [ _big _small ] _* _( dog ) ) small cat dog
:testpattern ( _* _(dog) ) cat dog
:testpattern ( _* _one _* ( _two ) _* ( _three ) ) first one two and three
:testpattern ( _* ( _one _* ( _two ) ) _* ( _three ) ) first one two three
:testpattern ( _* _( _one _two _three ) _* _( _four _* _six ) _* ) one two three middle four five six last
:testpattern ( _* ( _one _* ( _two _* ( _omega ) ) _three _* _four ) ) one two omega three four
:testpattern ( _{ optional } _* _( dog ) ) optional cat dog
:testpattern ( { _optional } _* _( dog ) ) optional cat dog
:testpattern ( _{ _optional } _* _( dog ) ) optional cat dog
:testpattern ( [ _big small ] _* _( dog ) ) big cat dog
:testpattern ( _one _two _( three _four ) five six @_3- _three ) one two three four five six
:testpattern ( _{ _( one _two three ) } _[ _(four five six) ] ) one two three four five six
:testpattern ( _{ _( one _two three ) } _[ _(four five _six) ] ) one two three four five six
:testpattern ( _three @_0- _* _two @_0+ _* _four ) one two three four five six
:testpattern ( _three @_0- _* _one @_0+ _* _five ) one two three four five six
*/
#define INFINITE_MATCH (-(200 << 8)) // allowed to match anywhere
#define NOT_BIT 0X00010000
#define FREEMODE_BIT 0X00020000
#define QUOTE_BIT 0X00080000
#define NOTNOT_BIT 0X00400000
#define GAPSTART 0X000000FF
#define GAPLIMIT 0X0000FF00
#define GAPLIMITSHIFT 8
#define GAP_SHIFT 16
#define GAP_SLOT 0x001F0000 // std wildcard index
#define SPECIFIC_SLOT 0x1F000000 // words or bracketed items
#define SPECIFIC_SHIFT 24
#define WILDGAP 0X20000000 // start of gap is 0x000000ff, limit of gap is 0x0000ff00
#define WILDMEMORIZEGAP 0X40000000 // start of gap is 0x000000ff, limit of gap is 0x0000ff00
#define WILDMEMORIZESPECIFIC 0X80000000 // while 0x1f0000 is wildcard index to use
// bottom 16 bits hold start and range of a gap (if there is one) and bits WILDGAP or MEMORIZEWILDGAP will be on
// and slot to use is 5 bits below
// BUT it we are memorizing specific, it must use separate slot index because _*~4 _() can exist
HEAPREF heapPatternThread = NULL;
int patternDepth = 0;
int indentBasis = 1;
bool matching = false;
bool patternRetry = false;
static char kindprior[100];
bool deeptrace = false;
static char* returnPtr = NULL;
char* patternchoice = NULL;
// pattern macro calling data
static unsigned int functionNest = 0; // recursive depth of macro calling
#define MAX_PAREN_NEST 50
static char* ptrStack[MAX_PAREN_NEST];
static int argStack[MAX_PAREN_NEST];
static int baseStack[MAX_PAREN_NEST];
static uint64 matchedBits[20][4]; // nesting level zone of bit matches
static uint64 retryBits[4]; // last set of match bits before retry
unsigned int patternEvaluationCount = 0; // number of patterns evaluated in this volley
void ShowMatchResult(FunctionResult result, char* rule, char* label,int id)
{
if (trace & TRACE_LABEL && label && *label && !(trace & TRACE_PATTERN) && CheckTopicTrace())
{
if (result == NOPROBLEM_BIT) Log(FORCETABUSERLOG, "** Match: %s %d.%d %c: \r\n", label, TOPLEVELID(id), REJOINDERID(id), *rule); // \\ blocks linefeed on next Log call
else Log(FORCETABUSERLOG, "** Fail %s: %d.%d %c: \r\n", label, TOPLEVELID(id), REJOINDERID(id), *rule); // \\ blocks linefeed on next Log call
}
if (result == NOPROBLEM_BIT && trace & (TRACE_PATTERN | TRACE_MATCH | TRACE_SAMPLE) && CheckTopicTrace()) // display the entire matching responder and maybe wildcard bindings
{
if (label && *label) Log(USERLOG, "** Match: %s %s %d.%d %c: ", label, GetTopicName(currentTopicID), TOPLEVELID(id), REJOINDERID(id), *rule); // \\ blocks linefeed on next Log call
else Log(USERLOG, "** Match: %s %d.%d %c:", GetTopicName(currentTopicID), TOPLEVELID(id), REJOINDERID(id), * rule); // \\ blocks linefeed on next Log call
if (wildcardIndex)
{
Log(USERLOG," matchvar: ");
for (int i = 0; i < wildcardIndex; ++i)
{
if (i > MAX_WILDCARDS) break;
if (*wildcardOriginalText[i])
{
if (!strcmp(wildcardOriginalText[i], wildcardCanonicalText[i])) Log(USERLOG, "_%d=%s (%d-%d) ", i, wildcardOriginalText[i],wildcardPosition[i] & 0x0000ffff, wildcardPosition[i] >> 16);
else Log(USERLOG, "_%d=%s / %s (%d-%d) ", i, wildcardOriginalText[i], wildcardCanonicalText[i], wildcardPosition[i] & 0x0000ffff, wildcardPosition[i] >> 16);
}
else Log(USERLOG, "_%d=null (%d-%d) ", i, wildcardPosition[i] & 0x0000ffff, wildcardPosition[i] >> 16);
}
}
Log(USERLOG,"\r\n");
}
}
static void MarkMatchLocation(int start, int end, int depth)
{
for (int i = start; i <= end; ++i)
{
int offset = i / 64; // which unit
int index = i % 64; // which bit
uint64 mask = ((uint64)1) << index;
matchedBits[depth][offset] |= mask;
}
}
static char* BitIndex(uint64 bits, char* buffer, int offset)
{
uint64 mask = 0X0000000000000001ULL;
for (int index = 0; index <= 63; ++index)
{
if (mask & bits)
{
sprintf(buffer, "%d ", offset + index);
buffer += strlen(buffer);
}
mask <<= 1;
}
return buffer;
}
void GetPatternData(char* buffer)
{
char* original = buffer;
buffer = BitIndex(matchedBits[0][0], buffer, 0);
buffer = BitIndex(matchedBits[0][1], buffer, 64);
buffer = BitIndex(matchedBits[0][2], buffer, 128);
BitIndex(matchedBits[0][3], buffer, 192);
if (strlen(original) == 0)
{
// no final matching data, so return last retried set
buffer = BitIndex(retryBits[0], buffer, 0);
buffer = BitIndex(retryBits[1], buffer, 64);
buffer = BitIndex(retryBits[2], buffer, 128);
BitIndex(retryBits[3], buffer, 192);
}
}
static void DecodeFNRef(char* side)
{
char* at = "";
if (side[1] == USERVAR_PREFIX) at = GetUserVariable(side + 1, false, true);
else if (IsDigit(side[1])) at = FNVAR(side + 1);
at = SkipWhitespace(at);
strcpy(side, at);
}
static void DecodeAssignment(char* word, char* lhs, char* op, char* rhs)
{
// get the operator
char* assign = word + Decode(word + 1, 1); // use accelerator to point to op in the middle
strncpy(lhs, word + 2, assign - word - 2);
lhs[assign - word - 2] = 0;
assign++; // :
op[0] = *assign++;
op[1] = 0;
op[2] = 0;
if (*assign == '=') op[1] = *assign++;
strcpy(rhs, assign);
}
static void DecodeComparison(char* word, char* lhs, char* op, char* rhs)
{
// get the operator
char* compare = word + Decode(word + 1, 1); // use accelerator to point to op in the middle
strncpy(lhs, word + 2, compare - word - 2);
lhs[compare - word - 2] = 0;
*op = *compare++;
op[1] = 0;
if (*compare == '=') // was == or >= or <= or &=
{
op[1] = '=';
op[2] = 0;
++compare;
}
strcpy(rhs, compare);
}
bool MatchesPattern(char* word, char* pattern) // does word match pattern of characters and *
{
if (!*pattern && *word) return false; // no more pattern but have more word so fails
size_t len = 0;
while (IsDigit(*pattern)) len = (len * 10) + *pattern++ - '0'; // length test leading characters can be length of word
if (len && strlen(word) != len) return false; // length failed
char* start = pattern;
--pattern;
while (*++pattern && *pattern != '*' && *word) // must match leading non-wild exactly
{
if (*pattern != '.' && *pattern != GetLowercaseData(*word)) return false; // accept a single letter either correctly OR as 1 character wildcard
++word;
}
if (pattern == start && len) return true; // just a length test, no real pattern
if (!*word) return !*pattern || (*pattern == '*' && !pattern[1]); // the word is done. If pattern is done or is just a trailing wild then we are good, otherwise we are bad.
if (*word && !*pattern) return false; // pattern ran out w/o wild and word still has more
// Otherwise we have a * in the pattern now and have to match it against more word
// wildcard until does match
char find = *++pattern; // the characters AFTER wildcard
if (!find) return true; // pattern ended on wildcard - matches all the rest of the word including NO rest of word
// now resynch
--word;
while (*++word)
{
if (*pattern == GetLowercaseData(*word) && MatchesPattern(word + 1, pattern + 1)) return true;
}
return false; // failed to resynch
}
static bool SysVarExists(char* ptr) // %system variable
{
char* sysvar = SystemVariable(ptr, NULL);
if (!*sysvar) return false;
return (*sysvar) ? true : false; // value != null
}
static bool FindPartialInSentenceTest(char* test, int start, int originalstart, bool reverse,
int& actualStart, int& actualEnd)
{
if (!test || !*test) return false;
char* word = AllocateStack(NULL, MAX_WORD_SIZE);
if (reverse)
{
for (int i = originalstart - 1; i >= 1; --i) // can this be found in sentence backwards
{
MakeLowerCopy(word, wordStarts[i]);
if (unmarked[i] || !MatchesPattern(word, test)) continue; // if universally unmarked, skip it. Or if they dont match
// we have a match of a word
if (!HasMarks(i)) continue; // used ^unmark(@ _x) , making this invisible to mark system
actualStart = i;
actualEnd = i;
ReleaseStack(word);
return true;
}
}
else
{
for (int i = start + 1; i <= wordCount; ++i) // can this be found in sentence
{
if (!wordStarts[i]) continue;
MakeLowerCopy(word, wordStarts[i]);
if (unmarked[i] || !MatchesPattern(word, test)) continue; // if universally unmarked, skip it. Or if they dont match
// we have a match of a word
if (!HasMarks(i)) continue; // used ^unmark(@ _x) , making this invisible to mark system
actualStart = i;
actualEnd = i;
ReleaseStack(word);
return true;
}
}
ReleaseStack(word);
return false;
}
static bool MatchTest(bool reverse, WORDP D, int start, char* op, char* compare, int quote,
int& actualStart, int& actualEnd, bool exact, int legalgap) // is token found somewhere after start?
{
if (start == INFINITE_MATCH) start = (reverse) ? (wordCount + 1) : 0;
uppercaseFind = 0;
if (deeptrace && D) Log(USERLOG," matchtesting:%s ", D->word);
while (GetNextSpot(D, start, actualStart, actualEnd, reverse, legalgap)) // find a spot later where token is in sentence
{
if (deeptrace) Log(USERLOG," matchtest:%s %d-%d ", D->word, actualStart, actualEnd);
if (exact && (start + 1) != actualStart) return false; // we are doing _0?~hello or whatever. Must be on the mark
if (deeptrace) Log(USERLOG," matchtest:ok ");
start = actualStart; // where to try next if fail on test
if (op) // we have a test to perform
{
char* word;
if (D->word && (IsAlphaUTF8(*D->word) || D->internalBits & UTF8)) word = D->word; // implicitly all normal words are relation tested as given
else word = quote ? wordStarts[actualStart] : wordCanonical[actualStart];
int id;
if (deeptrace) Log(USERLOG," matchtest:optest ");
char* word1val = AllocateStack(NULL, MAX_WORD_SIZE);
char* word2val = AllocateStack(NULL, MAX_WORD_SIZE);
FunctionResult res = HandleRelation(word, op, compare, false, id, word1val, word2val);
ReleaseStack(word1val);
if (res & ENDCODES) continue; // failed
}
if (*D->word == '~') return true; // we CANNOT tell whether original or canon led to set...
if (!quote) return true; // can match canonical or original
// we have a match, but prove it is a original match, not a canonical one
unsigned int end = actualEnd & REMOVE_SUBJECT; // remove hidden subject field if there from fundamental meaning match
if ((int)end < actualStart) continue; // trying to match within a composite.
if (actualStart == (int)end && !stricmp(D->word, wordStarts[actualStart])) return true; // literal word match
else // match a phrase literally
{
char word[MAX_WORD_SIZE];
char* at = word;
for (int i = actualStart; i <= (int)end; ++i)
{
strcpy(at, wordStarts[i]);
at += strlen(wordStarts[i]);
if (i != (int)end) *at++ = '_';
}
*at = 0;
if (!stricmp(D->word, word)) return true;
}
}
if (deeptrace && D) Log(USERLOG," matchtest:%s failed ", D->word);
return false;
}
static bool FindPhrase(char* word, int start, bool reverse, int & actualStart, int& actualEnd)
{ // Phrases are dynamic, might not be marked, so have to check each word separately. -- faulty in not respecting ignored(unmarked) words
if (start > wordCount) return false;
bool matched = false;
actualEnd = start;
int oldend;
oldend = start = 0; // allowed to match anywhere or only next
int n = BurstWord(word);
for (int i = 0; i < n; ++i) // use the set of burst words - but "Andy Warhol" might be a SINGLE word.
{
WORDP D = FindWord(GetBurstWord(i));
matched = MatchTest(reverse, D, actualEnd, NULL, NULL, 0, actualStart, actualEnd, false, 0);
if (matched)
{
if (oldend > 0 && actualStart != (oldend + 1)) // do our words match in sequence NO. retry later in sentence
{
++start;
actualStart = actualEnd = start;
i = -1;
oldend = start = 0;
matched = false;
continue;
}
if (i == 0) start = actualStart; // where we matched INITIALLY
oldend = actualEnd & REMOVE_SUBJECT; // remove hidden subject field
}
else break;
}
if (matched) actualStart = start;
return matched;
}
static char* PushMatch(int used)
{
char* limit;
char* base = InfiniteStack(limit, "PushMatch");
int* vals = (int*)base;
for (int i = 0; i < used; ++i) *vals++ = wildcardPosition[i];
char* rest = (char*)vals;
for (int i = 0; i < used; ++i)
{
strcpy(rest, wildcardOriginalText[i]);
rest += strlen(rest) + 1;
strcpy(rest, wildcardCanonicalText[i]);
rest += strlen(rest) + 1;
}
CompleteBindStack64(rest - base, base);
return base;
}
static void PopMatch(char* base, int used)
{
int* vals = (int*)base;
for (int i = 0; i < used; ++i) wildcardPosition[i] = *vals++;
char* rest = (char*)vals;
for (int i = 0; i < used; ++i)
{
strcpy(wildcardOriginalText[i], rest);
rest += strlen(rest) + 1;
strcpy(wildcardCanonicalText[i], rest);
rest += strlen(rest) + 1;
}
ReleaseStack(base);
}
#ifdef INFORMATION
We keep a positional range reference within the sentence where we are(positionStart and positionEnd).
Before we attempt the next match we make a backup copy(oldStart and oldEnd)
so that if the match fails, we can revert back to where we were(under some circumstances).
We keep a variable firstMatched to track the first real word we have matched so far.
If the whole match is declared a failure eventually, we may be allowed to go back and
retry matching starting immediately after that location.That is, we do not do all possible backtracking
as Prolog might, but we do a cheaper form where we simply try again farther in the sentence.
Also, firstMatched is returned from a subcall, so the caller can know where to end a wildcard memorization
started before the subcall.
Some tokens create a wildcard effect, where the next thing is allowed to be some distance away.
This is tracked by wildcardSelector, and the token after the wildcard, when found, is checked to see
if its position is allowed.When we enter a choice construct like[] and {}, when a choice fails,
we reset the wildcardSelector back to originalWildcardSelector so the next choice sees the same environment.
In reverse mode, the range of positionStart and positionEnd continue to be earlier and later in the sentence,
but validation treats positionStart as the basis of measuring distance.
Rebindable refers to ability to relocate firstmatched on failure(1 means we can shift from here, 3 means we enforce spacing and cannot rebind)
Some operations like < or @_0+ force a specific position, and if no firstMatch has yet happened, then you cannot change
the start location.
returnStart and returnEnd are the range of the match that happened when making a subcall.
Startposition is where we start matching from.
#endif
bool Match(char* buffer, char* ptr, int depth, int startposition, char* kind, int rebindable, unsigned int wildcardSelector,
int &returnstart, int& returnend, int &uppercasem, int& firstMatched, int positionStart, int positionEnd, bool reverse)
{// always STARTS past initial opening thing ( [ { and ends with closing matching thing
int startdepth = globalDepth;
patternDepth = depth;
int wildgap = 0;
if (depth == 0) ++patternEvaluationCount;
if (wildcardSelector & WILDGAP && *kind == '{')
wildgap = ((wildcardSelector & GAPLIMIT) >> GAPLIMITSHIFT) + 1;
memset(&matchedBits[depth], 0, sizeof(uint64) * 4); // nesting level zone of bit matches
if (depth == 0) memset(&retryBits, 0, sizeof(uint64) * 4);
char word[MAX_WORD_SIZE];
char* orig = ptr;
int statusBits = (*kind == '<') ? FREEMODE_BIT : 0; // turns off: not, quote, startedgap, freemode ,wildselectorpassback
kindprior[depth] = *kind;
bool matched = false;
unsigned int startNest = functionNest;
int startfnvarbase = fnVarbase;
int wildcardBase = wildcardIndex;
unsigned int result = NOPROBLEM_BIT;
int bidirectional = 0;
int bidirectionalSelector = 0;
int bidirectionalWildcardIndex = 0;
WORDP D;
int beginmatch = -1; // for ( ) where did we actually start matching
bool success = false;
char* priorPiece = NULL;
if (!depth) patternRetry = false;
int at;
int slidingStart = startposition;
firstMatched = -1; // () should return spot it started (firstMatched) so caller has ability to bind any wild card before it
if (rebindable == 1) slidingStart = positionStart = INFINITE_MATCH; // INFINITE_MATCH means we are in initial startup, allows us to match ANYWHERE forward to start
int originalWildcardSelector = wildcardSelector;
positionEnd = startposition; // we scan starting 1 after this
int basicStart = startposition; // we must not match real stuff any earlier than here
char* argumentText = NULL; // pushed original text from a function arg -- function arg never decodes to name another function arg, we would have expanded it instead
uppercaseFind = -1;
if (trace & TRACE_PATTERN && CheckTopicTrace())
{
if (detailpattern )
{
char s[10];
if (positionStart == INFINITE_MATCH) strcpy(s, "?");
else sprintf(s, "%d", positionStart);
Log(USERLOG, "%s-%d-", s, positionEnd);
}
Log((depth == 0 && !csapicall && !debugcommand) ? USERLOG : USERLOG, "%s ", kind); // start on new indented line
}
while (ALWAYS) // we have a list of things, either () or { } or [ ]. We check each item until one fails in () or one succeeds in [ ] or { }
{
int oldStart = positionStart; // allows us to restore if we fail, and confirm legality of position advance.
int oldEnd = positionEnd;
int id;
char* nextTokenStart = SkipWhitespace(ptr);
returnPtr = nextTokenStart; // where we last were before token... we cant fail on _ and that's what we care about
char* starttoken = nextTokenStart;
ptr = ReadCompiledWord(nextTokenStart, word);
if (*word == '<' && word[1] == '<') ++nextTokenStart; // skip the 1st < of << form
if (*word == '>' && word[1] == '>') ++nextTokenStart; // skip the 1st > of >> form
nextTokenStart = SkipWhitespace(nextTokenStart + 1); // ignore blanks after if token is a simple single thing like !
char c = *word;
bool foundaword = false;
if (deeptrace) Log(USERLOG," token:%s ", word);
switch (c)
{
// prefixs on tokens
case '!': // NOT condition - not a stand-alone token, attached to another token
ptr = nextTokenStart;
if (*ptr == '-') // prove not found anywhere before here
{
++ptr;
char xword[MAX_WORD_SIZE];
ptr = ReadCompiledWord(ptr, xword);
WORDP D = FindWord(xword, 0, PRIMARY_CASE_ALLOWED); // word only, not ( ) stuff
int startp, endp;
if (!GetNextSpot(D, positionStart, startp, endp, true, false)) // found. too bad
{
if (trace & TRACE_PATTERN && CheckTopicTrace()) Log(USERLOG,"%s+", word);
continue;
}
matched = false;
break;
}
if (*ptr == '+') ++ptr; // normal forward (alternate notation to mere !word)
if (*ptr == '!') // !!
{
if (ptr[1] == '+') ++ptr; // ignore notation
else if (ptr[1] == '-')
{
++ptr;
char xword[MAX_WORD_SIZE];
ptr = ReadCompiledWord(ptr+1, xword);
WORDP D = FindWord(xword, 0, PRIMARY_CASE_ALLOWED); // word only, not ( ) stuff
int startp, endp;
if (!GetNextSpot(D, positionStart, startp, endp, true, false) || startp != (positionStart - 1))
{
if (trace & TRACE_PATTERN && CheckTopicTrace()) Log(USERLOG,"%s+",word);
continue;
}
matched = false;
break;
}
ptr = SkipWhitespace(ptr + 1);
statusBits |= NOTNOT_BIT;
}
if (trace & TRACE_PATTERN && CheckTopicTrace())
{
Log(USERLOG,"!");
}
statusBits |= NOT_BIT;
continue;
case '\'': // single quoted item
if (!stricmp(word, (char*)"'s"))
{
matched = MatchTest(reverse, FindWord(word), (positionEnd < basicStart && firstMatched < 0) ? basicStart : positionEnd, NULL, NULL,
statusBits & QUOTE_BIT, positionStart, positionEnd, false, 0);
if (!matched) uppercaseFind = -1;
else foundaword = true;
if (!(statusBits & NOT_BIT) && matched && firstMatched < 0) firstMatched = positionStart;
break;
}
else
{
statusBits |= QUOTE_BIT;
ptr = nextTokenStart;
if (trace & TRACE_PATTERN && CheckTopicTrace()) Log(USERLOG,"'");
continue;
}
case '_':
// a wildcard id? names a memorized value like _8
if (IsDigit(word[1]))
{
matched = GetwildcardText(GetWildcardID(word), false)[0] != 0; // simple _2 means is it defined
break;
}
// memorization coming - there can be up-to-two memorizations in progress: _* (wildmemorizegap) and _xxx (wildmemorizespecific)
// it will be gap first and specific second (either token or smear of () [] {} )
ptr = nextTokenStart;
uppercaseFind = -1;
// if we are going to memorize something AND we previously matched inside a phrase, we need to move to after phrase...
/*
// this code is very suspicious...
// - a forward match positionEnd will always be greater that positionStart, so first test is never true
// - checking for a difference of 1 ignores the fact that the range could be more so doesn't seem wholly indicative of a match
if ((positionStart - positionEnd) == 1 && !reverse) positionEnd = positionStart; // If currently matched a phrase, move to end.
else if ((positionEnd - positionStart) == 1 && reverse) positionStart = positionEnd; // If currently matched a phrase, move to end.
*/
// aba or ~dat or **ar*
if (ptr[0] != '*' || ptr[1] == '*') // wildcard word or () [] {}, not gap
{
wildcardSelector &= -1 ^ SPECIFIC_SLOT;
wildcardSelector |= (WILDMEMORIZESPECIFIC + (wildcardIndex << SPECIFIC_SHIFT));
}
// *1 or *-2 or *elle (single wild token pattern match)
else if (IsDigit(ptr[1]) || ptr[1] == '-' || IsAlphaUTF8(ptr[1]))
{
wildcardSelector &= -1 ^ SPECIFIC_SLOT;
wildcardSelector |= (WILDMEMORIZESPECIFIC + (wildcardIndex << SPECIFIC_SHIFT));
}
else // *~ or *
{
wildcardSelector &= -1 ^ GAP_SLOT;
wildcardSelector |= (WILDMEMORIZEGAP + (wildcardIndex << GAP_SHIFT)); // variable gap
}
SetWildCardNull(); // dummy match to reserve place
if (trace & TRACE_PATTERN && CheckTopicTrace() && bidirectional != 2) Log(USERLOG,"_");
continue;
case '@': // factset ref or @_2+
if (!stricmp(word, "@retry")) // when done, retry if match worked
{
patternRetry = true;
matched = true;
break;
}
if (word[1] == '_') // set positional reference @_20+ or @_0- or anchor @_20
{
if (firstMatched < 0) firstMatched = NORETRY; // cannot retry this match locally
char* end = word + 3; // skip @_2
if (IsDigit(*end)) ++end; // point to proper + or - ending
unsigned int wild = wildcardPosition[GetWildcardID(word + 1)];
int index = (wildcardSelector >> GAP_SHIFT) & 0x0000001f;
// memorize gap to end based on direction...
if (*end && (wildcardSelector & WILDMEMORIZEGAP) && !reverse) // close to end of sentence
{
positionStart = wordCount; // pretend to match at end of sentence
int start = wildcardSelector & GAPSTART;
int limit = (wildcardSelector & GAPLIMIT) >> GAPLIMITSHIFT ;
if ((positionStart + 1 - start) > limit) // too long til end
{
matched = false;
wildcardSelector &= -1 ^ (WILDMEMORIZEGAP | WILDGAP);
break;
}
if (wildcardSelector & WILDMEMORIZEGAP)
{
if ((wordCount - start) == 0) SetWildCardGivenValue((char*)"", (char*)"", start, start, index); // empty gap
else SetWildCardGiven(start, wordCount, true, index); // wildcard legal swallow between elements
wildcardSelector &= -1 ^ (WILDMEMORIZEGAP | WILDGAP);
}
}
// memorize gap to start based on direction...
if (*end && (wildcardSelector & WILDMEMORIZEGAP) && reverse) // close to start of sentence
{
positionEnd = 1; // pretend to match here (looking backwards)
int start = wildcardSelector & GAPSTART;
int limit = (wildcardSelector & GAPLIMIT) >> GAPLIMITSHIFT;
if ((start - positionEnd) > limit) // too long til end
{
matched = false;
wildcardSelector &= -1 ^ (WILDMEMORIZEGAP | WILDGAP);
break;
}
if (wildcardSelector & WILDMEMORIZEGAP)
{
if ((start - positionEnd + 1) == 0 || start == 0) SetWildCardGivenValue((char*)"", (char*)"", start, start,index); // empty gap
else SetWildCardGiven(positionEnd, start, true, index); // wildcard legal swallow between elements
wildcardSelector &= -1 ^ (WILDMEMORIZEGAP | WILDGAP);
}
}
if (*end == '-')
{
reverse = true;
positionEnd = positionStart = WILDCARD_START(wild);
// These are where the "old" match was
}
else // + and nothing both move forward.
{
int oldstart = positionStart;
int oldend = positionEnd; // will be 0 if we are starting out with no required match
positionStart = WILDCARD_START(wild);
positionEnd = WILDCARD_END(wild) & REMOVE_SUBJECT;
if (oldend && *end != '+' && positionStart != INFINITE_MATCH) // this is an anchor that might not match
{
if (wildcardSelector)
{
int start = wildcardSelector & GAPSTART;
int limit = (wildcardSelector & GAPLIMIT) >> GAPLIMITSHIFT;
if (!reverse && (positionStart < (oldend + 1) || positionStart > (start + limit)))
{
matched = false;
break;
}
else if (reverse && (positionEnd > (oldstart - 1) || positionEnd < (start - limit)))
{
matched = false;
break;
}
}
else
{
if (!reverse && positionStart != (oldend + 1))
{
matched = false;
break;
}
else if (reverse && positionEnd != (oldstart - 1))
{
matched = false;
break;
}
}
}
reverse = false;
}
if (!positionEnd && positionStart)
{
matched = false;
break;
}
if (*end)
{
oldEnd = positionEnd; // forced match ok
oldStart = positionStart;
}
if (trace & TRACE_PATTERN && CheckTopicTrace())
{
if (positionStart <= 0 || positionStart > wordCount || positionEnd <= 0 || positionEnd > wordCount) Log(USERLOG, "(index:%d)", positionEnd);
else if (positionStart == positionEnd) Log(USERLOG,"(word:%s index:%d)", wordStarts[positionEnd], positionEnd);
else Log(USERLOG,"(word:%s-%s index:%d-%d)", wordStarts[positionStart], wordStarts[positionEnd], positionStart, positionEnd);
}
if (beginmatch == -1) beginmatch = positionStart; // treat this as a real match
matched = true;
if (rebindable) rebindable = 3; // not allowed to rebind from this, it is a fixed location
}
else
{
int set = GetSetID(word);
if (set == ILLEGAL_FACTSET) matched = false;
else matched = FACTSET_COUNT(set) != 0;
}
break;
case '<': // sentence start marker OR << >> set
if (firstMatched < 0) firstMatched = NORETRY; // cannot retry this match
if (word[1] == '<') goto DOUBLELEFT; // <<
at = 0;
while (unmarked[++at] && at <= wordCount) { ; } // skip over hidden data
--at; // we perceive the start to be here
ptr = nextTokenStart;
if ((wildcardSelector & WILDGAP) && !reverse) // cannot memorize going forward to start of sentence
{
matched = false;
wildcardSelector &= -1 ^ (WILDMEMORIZEGAP | WILDGAP);
}
else { // match can FORCE it to go to start from any direction
positionStart = positionEnd = at; // idiom < * and < _* handled under *
matched = true;
}
if (matched && rebindable) rebindable = 3; // not allowed to rebind from this, it is a fixed location
break;
case '>': // sentence end marker
if (word[1] == '>') goto DOUBLERIGHT; // >> closer, and reset to start of sentence wild again...
at = positionEnd;
while (unmarked[++at] && at <= wordCount) { ; } // skip over hidden data
if (at > wordCount) at = positionEnd; // he was the end
else at = wordCount; // the presumed real end
uppercaseFind = -1;
ptr = nextTokenStart;
if (*kind == '[') rebindable = 2; // let outer level decide if it is right
// # get 3 days forecast in Orlando, New York | London and San Francisco
// u: (_and) _10 = _0
// u: TEST (@_10+ * _[ ~arrayseparator > ])
if ((wildcardSelector & WILDGAP) && reverse) // cannot memorize going backward to end of sentence
{
matched = false;
wildcardSelector &= -1 ^ (WILDMEMORIZEGAP | WILDGAP);
}
else if (reverse)
{
// match can FORCE it to go to end from any direction
positionStart = positionEnd = wordCount + 1;
matched = true;
}
else if ((wildcardSelector & WILDGAP) || positionEnd == at)// you can go to end from anywhere if you have a gap OR you are there
{
matched = true;
positionStart = positionEnd = at + 1; // pretend to match a word off end of sentence
}
else if (*kind == '[' || *kind == '{') // nested unit will figure out if legal
{
matched = true;
positionStart = positionEnd = at + 1; // pretend to match a word at end of sentence
}
else if (positionStart == INFINITE_MATCH)
{
positionStart = positionEnd = wordCount + 1;
matched = true;
}
else matched = false;
if (matched && rebindable && rebindable != 2) rebindable = 3; // not allowed to rebind from this, it is a fixed location
break;
case '*':
uppercaseFind = -1;
if (beginmatch == -1) beginmatch = startposition + 1;
if (word[1] == '-') // backward grab, -1 is word before now -- BUG does not respect unmark system
{
int atx;
if (!reverse) atx = positionEnd - (word[2] - '0') - 1; // limited to 9 back
else atx = positionEnd + (word[2] - '0') - 1;
if (reverse && atx > wordCount)
{
oldEnd = atx; // set last match AFTER our word
positionStart = positionEnd = atx - 1; // cover the word now
matched = true;
}
else if (!reverse && atx >= 0) // no earlier than pre sentence start
{
oldEnd = atx; // set last match BEFORE our word
positionStart = positionEnd = atx + 1; // cover the word now
matched = true;
}
else matched = false;
}
else if (IsDigit(word[1])) // fixed length gap
{
int atx;
int count = word[1] - '0'; // how many to swallow
if (reverse)
{
int begin = positionStart - 1;
atx = positionStart; // start here
while (count-- && --atx >= 1) // can we swallow this (not an ignored word)
{
if (unmarked[atx])
{
++count; // ignore this word
if (atx == begin) --begin; // ignore this as starter
}
}
if (atx >= 1) // pretend match
{
positionEnd = begin; // pretend match here - wildcard covers the gap
positionStart = atx;
matched = true;
}
else matched = false;
}
else
{
atx = positionEnd; // start here
int begin = positionEnd + 1;
while (count-- && ++atx <= wordCount) // can we swallow this (not an ignored word)
{
if (unmarked[atx])
{
++count; // ignore this word
if (atx == begin) ++begin; // ignore this as starter
}
}
if (atx <= wordCount) // pretend match
{
positionStart = begin; // pretend match here - wildcard covers the gap
positionEnd = atx;
matched = true;
}
else matched = false;
}
}
else if (IsAlphaUTF8(word[1]) || word[1] == '*')
matched = FindPartialInSentenceTest(word + 1, (positionEnd < basicStart && firstMatched < 0) ? basicStart : positionEnd, positionStart, reverse,
positionStart, positionEnd); // wildword match like st*m* or *team* matches steamroller
else // variable gap
{
int start = (reverse) ? (positionStart - 1) : (positionEnd + 1);
wildcardSelector |= start | WILDGAP; // cannot conflict, two wilds in a row change no position
if (word[1] == '~')
{
wildcardSelector |= (word[2] - '0') << GAPLIMITSHIFT; // *~3 - limit is 9 back
if (word[strlen(word) - 1] == 'b')
{
bidirectional = 1; // now aiming backwards
priorPiece = ptr;
bidirectionalSelector = wildcardSelector; // where to start forward direction if backward fails
reverse = !reverse; // run inverted first (presumably backward)
start = (reverse) ? (positionStart - 1) : (positionEnd + 1);
wildcardSelector &= -1 ^ 0x000000ff;
wildcardSelector |= start;
bidirectionalWildcardIndex = wildcardIndex;
}
}
else // I * meat
{
wildcardSelector |= REAL_SENTENCE_WORD_LIMIT << GAPLIMITSHIFT;
if (positionStart == 0) positionStart = INFINITE_MATCH; // < * resets to allow match anywhere
}
if (trace & TRACE_PATTERN && CheckTopicTrace()) Log(USERLOG,"%s ", word);
continue;
}
break;
case USERVAR_PREFIX: // is user variable defined
if (IsAlphaUTF8(word[1]) || word[1] == '_' || word[1] == USERVAR_PREFIX) // legal variable, not $ or $100
{
char* val = GetUserVariable(word, false, true);
matched = *val ? true : false;
}
else goto matchit;
break;
case '^': // function call, function argument or indirect function variable assign ref like ^$$tmp = null
if (IsDigit(word[1]) || word[1] == USERVAR_PREFIX || word[1] == '_') // macro argument substitution or indirect function variable
{
argumentText = ptr; // transient substitution of text
if (IsDigit(word[1])) ptr = FNVAR(word + 1);
else if (word[1] == USERVAR_PREFIX) ptr = GetUserVariable(word + 1, false, true); // get value of variable and continue in place
else ptr = wildcardCanonicalText[GetWildcardID(word + 1)]; // ordinary wildcard substituted in place (bug)?
if (trace & TRACE_PATTERN && CheckTopicTrace()) Log(USERLOG,"%s=>", word);
continue;
}
D = FindWord(word, 0); // find the function
if (!D || !(D->internalBits & FUNCTION_NAME))
{
matched = false; // shouldnt fail normally, dont disrupt api calls etc, fail quietly
}
else if (D->x.codeIndex || D->internalBits & IS_OUTPUT_MACRO) // system function or output macro- execute it
{
char* base = PushMatch(wildcardIndex);
int baseindex = wildcardIndex;
AllocateOutputBuffer();
FunctionResult result;
if (!stricmp(D->word, "^match") || !stricmp(D->word, "^mark") || !stricmp(D->word, "^unmark")) matching = true;
if (trace & TRACE_PATTERN) Log(USERLOG, "\r\n");
ptr = DoFunction(word, ptr, currentOutputBase, result);
matching = false;
PopMatch(base, baseindex);
matched = !(result & ENDCODES);
if (!stricmp(word, "^debug")) // be innocuous
{
if (*kind == '[' || *kind == '{') matched = false;
}
// allowed to do comparisons on answers from system functions but cannot have space before them, but not from user macros
if (*ptr == '!' && ptr[1] == ' ')// simple not operator or no operator
{
if (!stricmp(currentOutputBase, (char*)"0") || !stricmp(currentOutputBase, (char*)"false")) result = FAILRULE_BIT; // treat 0 and false as failure along with actual failures
}
else if (ptr[1] == '<' || ptr[1] == '>') { ; } // << and >> are not comparison operators in a pattern
else if (IsComparison(*ptr) && *(ptr - 1) != ' ' && (*ptr != '!' || ptr[1] == '=')) // ! w/o = is not a comparison
{
char op[10];
char* opptr = ptr;
*op = *opptr;
op[1] = 0;
char* rhs = ++opptr;
if (*opptr == '=') // was == or >= or <= or &=
{
op[1] = '=';
op[2] = 0;
++rhs;
}
char copy[MAX_WORD_SIZE];
ptr = ReadCompiledWord(rhs, copy);
rhs = copy;
if (*rhs == '^') // local function argument or indirect ^$ var is LHS. copy across real argument
{
char* atx = "";
if (rhs[1] == USERVAR_PREFIX) atx = GetUserVariable(rhs + 1, false, true);
else if (IsDigit(rhs[1])) atx = FNVAR(rhs + 1);
atx = SkipWhitespace(atx);
strcpy(rhs, atx);
}
if (*op == '?' && opptr[0] != '~')
{
matched = MatchTest(reverse, FindWord(currentOutputBase),
(positionEnd < basicStart && firstMatched < 0) ? basicStart : positionEnd, NULL, NULL, false,
positionStart, positionEnd, false, 0);
uppercaseFind = -1;
if (!(statusBits & NOT_BIT) && matched && firstMatched < 0) firstMatched = positionStart; // first SOLID match
}
else
{
int id;
char* word1val = AllocateStack(NULL, MAX_WORD_SIZE);
char* word2val = AllocateStack(NULL, MAX_WORD_SIZE);
result = HandleRelation(currentOutputBase, op, rhs, false, id, word1val, word2val);
ReleaseStack(word1val);
matched = (result & ENDCODES) ? 0 : 1;
}
}
FreeOutputBuffer();
}
else // user patternmacro function - execute it in pattern context as continuation of current code
{
if (functionNest >= MAX_PAREN_NEST) // fail, too deep nesting
{
matched = false;
break;
}
// save old base data
baseStack[functionNest] = callArgumentBase;
argStack[functionNest] = callArgumentIndex;
if ((trace & TRACE_PATTERN || D->internalBits & MACRO_TRACE) && CheckTopicTrace()) Log(USERLOG,"%s(",word);
ptr += 2; // skip ( and space
FunctionResult result = NOPROBLEM_BIT;
// read arguments
while (*ptr && *ptr != ')')
{
ptr = ReadArgument(ptr, buffer, result); // gets the unevealed arg
callArgumentList[callArgumentIndex++] = AllocateStack(buffer);
if ((trace & TRACE_PATTERN || D->internalBits & MACRO_TRACE) && CheckTopicTrace()) Log(USERLOG," %s, ", buffer);
*buffer = 0;
if (result != NOPROBLEM_BIT)
{
matched = false;
break;
}
}
if ((trace & TRACE_PATTERN || D->internalBits & MACRO_TRACE) && CheckTopicTrace()) Log(USERLOG,")\r\n");
callArgumentBase = fnVarbase = argStack[functionNest] - 1;
ptrStack[functionNest++] = ptr + 2; // skip closing paren and space
ptr = (char*)FindAppropriateDefinition(D, result,true);
if (ptr)
{
char* word = AllocateStack(NULL, MAX_WORD_SIZE);
ptr = ReadCompiledWord(ptr, word) + 2; // eat flags and count and (
while (*ptr) // skip over locals list (which should be null)
{
ptr = ReadCompiledWord(ptr, word);
if (*word == ')') break;
}
ReleaseStack(word);
}
else ptr = ""; // null function
if (result == NOPROBLEM_BIT) continue;
}
if (result == FAILRULE_BIT) matched = false;
break;
case 0: case '`': // end of data (argument or function - never a real rule)
if (argumentText) // return to normal from argument substitution
{
ptr = argumentText;
argumentText = NULL;
continue;
}
else if (functionNest > startNest) // function call end