206 const char *start =
pattern.c_str();
207 const char *ps = start;
214 std::vector<size_t> captureStack;
215 size_t nextCaptureId = 0;
217 auto addToken = [&](
PToken tok)
220 data.emplace_back(tok);
223 auto getNextCharacter = [&]() ->
PToken
233 case 'n': result =
PToken(
'\n');
break;
234 case 'r': result =
PToken(
'\r');
break;
235 case 't': result =
PToken(
'\t');
break;
246 for (
int i=0;i<2 && (cs=(*(ps+1)));i++)
248 int d = (cs>=
'a' && cs<=
'f') ? cs-
'a'+10 :
249 (cs>=
'A' && cs<=
'F') ? cs-
'A'+10 :
250 (cs>=
'0' && cs<=
'9') ? cs-
'0' :
252 if (d>=0) { v<<=4; v|=d; ps++; }
else break;
257 case '\0': ps--;
break;
271 prevTokenPos = tokenPos;
276 prevTokenPos = tokenPos;
281 prevTokenPos = tokenPos;
286 prevTokenPos = tokenPos;
288 size_t id = ++nextCaptureId;
289 data.back().setValue(
static_cast<uint16_t
>(
id));
290 captureStack.push_back(
id);
295 prevTokenPos = tokenPos;
296 if (captureStack.empty())
301 size_t id = captureStack.back();
302 captureStack.pop_back();
304 data.back().setValue(
static_cast<uint16_t
>(
id));
309 prevTokenPos = tokenPos;
311 if (*ps==0) {
error=
true;
return; }
312 bool esc = *ps==
'\\';
313 PToken tok = getNextCharacter();
319 if (*ps==0) {
error=
true;
return; }
320 tok = getNextCharacter();
327 uint16_t numTokens=0;
330 if (c==
'-' && *(ps+1)!=
']' && *(ps+1)!=0)
334 PToken endTok = getNextCharacter();
358 if (*ps==0) {
error=
true;
return; }
360 tok = getNextCharacter();
362 tok.
value()==
static_cast<uint16_t
>(
']'))
366 if (*ps==0) {
error=
true;
return; }
370 data[prevTokenPos].setValue(numTokens);
377 if (prevTokenPos==-1)
382 switch (
data[prevTokenPos].kind())
394 int ddiff =
static_cast<int>(tokenPos-prevTokenPos);
400 std::copy_n(
data.begin()+prevTokenPos,ddiff,
data.begin()+tokenPos);
408 while (prevTokenPos>0 && depth>0)
415 data.insert(
data.begin()+prevTokenPos,
425 prevTokenPos = tokenPos;
426 addToken(getNextCharacter());
431 if (!captureStack.empty())
483 DBG(
"%d:matchAt(tokenPos=%zu, str='%s', pos=%zu)\n",level,tokenPos,pos<str.length() ? str.substr(pos).c_str() :
"",pos);
484 auto isStartIdChar = [](
char c) {
return isalpha(c) || c==
'_'; };
485 auto isIdChar = [](
char c) {
return isalnum(c) || c==
'_'; };
486 auto matchCharClass = [
this,isStartIdChar,isIdChar](
size_t tp,
char c) ->
bool
490 uint16_t numFields = tok.
value();
492 for (uint16_t i=0;i<numFields;i++)
507 uint16_t v =
static_cast<uint16_t
>(c);
508 if (tok.
from()<=v && v<=tok.
to())
515 DBG(
"matchCharClass(tp=%zu,c=%c (x%02x))=%d\n",tp,c,c,negate?!found:found);
516 return negate ? !found : found;
519 enum SequenceType { Star, Optional, OptionalRange };
520 auto processSequence = [
this,&tokenPos,&tokenLen,&index,&str,&matchCharClass,
521 &isStartIdChar,&isIdChar,&
match,&level,&pos](SequenceType type) ->
bool
523 size_t startIndex = index;
524 size_t len = str.length();
530 size_t groupId = tok.
value();
531 size_t innerStart = tokenPos + 1;
534 size_t tp = innerStart;
536 while (tp<tokenLen && depth>0)
542 if (depth!=0)
return false;
543 size_t endCapturePos = tp - 1;
544 size_t afterSeqPos = endCapturePos + 2;
549 bool innerOk =
matchAt(innerStart,endCapturePos,str,tmp,index,level+1);
552 size_t capLen = tmp.
length();
555 for (
size_t gid=1; gid<tmp.
size(); gid++)
558 size_t sl = tmp[gid].
length();
559 if (sp!=std::string::npos && sl!=std::string::npos)
561 match.startCapture(gid,sp);
562 match.endCapture(gid,sp+sl);
566 match.startCapture(groupId,index);
567 match.endCapture(groupId,index+capLen);
569 bool ok =
matchAt(afterSeqPos,tokenLen,str,
match,index+capLen,level+1);
572 match.setMatch(pos,(index+capLen)-pos+
match.length());
578 match.startCapture(groupId,index);
579 match.endCapture(groupId,index);
581 bool ok2 =
matchAt(afterSeqPos,tokenLen,str,
match,index,level+1);
593 while (index<len && str[index]==c_tok) { index++;
if (type==Optional)
break; }
598 while (index<len && matchCharClass(tokenPos,str[index])) { index++;
if (type==Optional)
break; }
599 tokenPos+=tok.
value()+1;
603 while (index<len && isStartIdChar(str[index])) { index++;
if (type==Optional)
break; }
608 while (index<len && isIdChar(str[index])) { index++;
if (type==Optional)
break; }
613 while (index<len &&
isspace(str[index])) { index++;
if (type==Optional)
break; }
618 while (index<len &&
isdigit(str[index])) { index++;
if (type==Optional)
break; }
623 if (type==Optional) index++;
else index = str.length();
628 size_t tokenStart = ++tokenPos;
631 rangeMatch.
init(str,0);
632 bool found =
matchAt(tokenStart,tokenPos,str,rangeMatch,index,level+1);
635 index+=rangeMatch.
length();
640 while (index>=startIndex)
643 bool found =
matchAt(tokenPos,tokenLen,str,
match,index,level+1);
655 while (tokenPos<tokenLen)
658 DBG(
"loop tokenPos=%zu token=%s\n",tokenPos,tok.
kindStr());
662 if (index>=str.length() || str[index]!=c_tok)
return false;
668 if (index>=str.length() || !matchCharClass(tokenPos,str[index]))
return false;
670 tokenPos+=tok.
value()+1;
677 if (index>=str.length() || !isStartIdChar(str[index]))
return false;
681 if (index>=str.length() || !isIdChar(str[index]))
return false;
685 if (index>=str.length() || !
isspace(str[index]))
return false;
689 if (index>=str.length() || !
isdigit(str[index]))
return false;
693 if (index!=pos)
return false;
696 if (index<str.length())
return false;
699 DBG(
"BeginOfWord: index=%zu isIdChar(%c)=%d prev.isIdChar(%c)=%d\n",
700 index,str[index],isIdChar(str[index]),
701 index>0?str[index]-1:0,
702 index>0?isIdChar(str[index-1]):-1);
703 if (index>=str.length() ||
704 !isIdChar(str[index]) ||
705 (index>0 && isIdChar(str[index-1])))
return false;
708 DBG(
"EndOfWord: index=%zu pos=%zu idIdChar(%c)=%d prev.isIsChar(%c)=%d\n",
709 index,pos,str[index],isIdChar(str[index]),
710 index==0 ? 0 : str[index-1],
711 index==0 ? -1 : isIdChar(str[index-1]));
712 if (index<str.length() &&
713 (isIdChar(str[index]) || index==0 || !isIdChar(str[index-1])))
return false;
716 DBG(
"BeginCapture(%zu) gid=%u\n",index,tok.
value());
720 DBG(
"EndCapture(%zu) gid=%u\n",index,tok.
value());
724 if (index>=str.length())
return false;
728 return processSequence(Star);
732 return processSequence(OptionalRange);
736 return processSequence(Optional);
744 match.setMatch(pos,index-pos);