2 #include <msp/core/error.h>
13 Writes an integer to a Regex code string, in little-endian order.
16 void write_int(T n, Msp::Regex::Code &code)
18 for(unsigned i=0; i<sizeof(T); ++i)
23 Reads an integer from a Regex code stream, in little-endian order.
26 T read_int(Msp::Regex::Code::const_iterator &c)
29 for(unsigned i=0; i<sizeof(T); ++i)
30 result+=static_cast<unsigned char>(*c++)<<i*8;
38 Regex::Regex(const string &expr)
41 string::const_iterator iter=expr.begin();
42 code=compile(expr, iter, n_groups, false);
46 RegMatch Regex::match(const string &str) const
48 RegMatch::GroupArray groups(n_groups);
50 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
51 if(run(str, i, groups))
52 return RegMatch(str, groups);
57 string Regex::disassemble() const
61 for(Code::const_iterator i=code.begin(); i!=code.end();)
63 Code::const_iterator j=i;
64 Offset offset=i-code.begin();
65 string decompiled=disassemble_instruction(i);
68 bytes+=format(" %02X", static_cast<int>(*j)&0xFF);
69 ss<<Fmt("%3d")<<offset<<':'<<Fmt("%-9s")<<bytes;
71 ss<<"\n"<<Fmt("%15s");
72 ss<<" "<<decompiled<<'\n';
78 Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, unsigned &group, bool branch)
80 bool has_branches=false;
83 string::const_iterator end;
84 for(end=iter; end!=expr.end(); ++end)
97 throw InvalidParameterValue("Unexpected )");
113 throw InvalidParameterValue("Unmatched (");
117 unsigned this_group=group;
121 write_int<Index>(this_group, result);
124 const unsigned jump_size=1+sizeof(Offset);
128 for(string::const_iterator i=iter; i!=end;)
130 Code atom=parse_atom(expr, i, group);
134 parse_repeat(i, repeat_min, repeat_max);
136 for(unsigned j=0; j<repeat_min; ++j)
138 if(repeat_max==numeric_limits<Count>::max())
143 write_int<Offset>(atom.size()+jump_size, result);
147 write_int<Offset>(-(atom.size()+jump_size), result);
149 else if(repeat_max>repeat_min)
151 for(unsigned j=repeat_min; j<repeat_max; ++j)
154 write_int<Offset>((repeat_max-j)*(atom.size()+jump_size)-jump_size, result);
163 for(string::const_iterator i=iter;;)
165 branches.push_back(compile(expr, i, group, true));
171 unsigned n_branches=branches.size();
173 Offset offset=(n_branches-1)*jump_size+branches.front().size();
174 for(list<Code>::iterator i=++branches.begin(); i!=branches.end(); ++i)
177 write_int<Offset>(offset, result);
181 for(list<Code>::iterator i=branches.begin(); i!=branches.end();)
184 offset-=i->size()+jump_size;
186 if(i!=branches.end())
189 write_int<Offset>(offset, result);
197 write_int<Index>(this_group, result);
205 Regex::Code Regex::parse_atom(const string &expr, string::const_iterator &i, unsigned &group)
216 throw InvalidParameterValue("Stray backslash");
222 if(*i=='*' || *i=='{' || *i=='}' || *i=='+' || *i=='?' || *i=='|' || *i==')')
223 throw InvalidParameterValue("Invalid atom");
225 return parse_brackets(expr, i);
235 result=compile(expr, ++i, group, false);
243 if(static_cast<unsigned char>(*i)<=LAST_INSTRUCTION_)
253 bool Regex::parse_repeat(string::const_iterator &i, Count &rmin, Count &rmax)
255 if(*i!='*' && *i!='+' && *i!='?' && *i!='{')
258 if(*i=='*' || *i=='+')
259 rmax=numeric_limits<Count>::max();
260 if(*i=='*' || *i=='?')
265 for(++i; isdigit(*i); ++i)
266 rmin=rmin*10+(*i-'0');
274 for(; isdigit(*i); ++i)
275 rmax=rmax*10+(*i-'0');
277 throw InvalidParameterValue("Invalid bound");
280 rmax=numeric_limits<Count>::max();
285 throw InvalidParameterValue("Invalid bound");
293 Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &iter)
305 string::const_iterator end=iter;
306 for(; (end!=str.end() && *end!=']'); ++end);
308 throw InvalidParameterValue("Unmatched '['");
310 uint8_t mask[32]={0};
313 unsigned char first, last;
314 for(string::const_iterator i=iter; i!=end; ++i)
320 for(unsigned j=first; j<=c; ++j)
321 mask[j>>3]|=1<<(j&7);
326 else if(c=='-' && i!=iter && end-i>1)
331 mask[c>>3]|=1<<(c&7);
356 result.append(reinterpret_cast<char *>(mask), 32);
365 bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch::GroupArray &groups) const
368 list<RunContext> ctx;
369 ctx.push_back(RunContext());
370 ctx.front().citer=code.begin();
371 ctx.front().groups.resize(groups.size());
373 for(string::const_iterator i=begin;;)
377 c=static_cast<unsigned char>(*i);
381 for(list<RunContext>::iterator j=ctx.begin(); j!=ctx.end();)
383 bool terminate=false;
384 bool negate_match=false;
385 for(; j->citer!=code.end();)
387 Instruction instr=static_cast<Instruction>(*j->citer);
388 if(instr>LAST_INSTRUCTION_)
397 Offset offset=read_int<Offset>(j->citer);
400 else if(instr==ND_JUMP)
402 Offset offset=read_int<Offset>(j->citer);
404 ctx.back().citer+=offset;
406 else if(instr==GROUP_BEGIN)
408 Index n=read_int<Index>(j->citer);
409 if(!j->groups[n].match)
410 j->groups[n].begin=i-str.begin();
412 else if(instr==GROUP_END)
414 Index n=read_int<Index>(j->citer);
415 if(!j->groups[n].match)
417 j->groups[n].match=true;
418 j->groups[n].end=i-str.begin();
419 j->groups[n].length=j->groups[n].end-j->groups[n].begin;
426 for(unsigned k=0; (k<groups.size() && !better); ++k)
428 better=group_compare(j->groups[k], groups[k]);
429 if(group_compare(groups[k], j->groups[k]))
438 bool match_result=false;
439 bool input_consumed=false;
440 if(instr==MATCH_BEGIN)
441 match_result=(i==str.begin());
442 else if(instr==MATCH_END)
443 match_result=(i==str.end());
444 else if(instr==MATCH_CHAR)
446 match_result=(c==*j->citer++);
449 else if(instr==MATCH_RANGE)
451 unsigned char first=*j->citer++;
452 unsigned char last=*j->citer++;
453 match_result=(c>=first && c<=last);
456 else if(instr==MATCH_MASK)
459 for(unsigned k=0; k<32; ++k)
461 match_result=mask[c>>3]&(1<<(c&7));
464 else if(instr==MATCH_ANY)
470 throw Exception("Invalid instruction");
472 if(match_result==negate_match)
476 if(input_consumed || terminate)
481 if(terminate || j->citer==code.end())
487 if(i==str.end() || ctx.empty())
495 bool Regex::group_compare(const RegMatch::Group &g1, const RegMatch::Group &g2) const
500 // Any match is better than no match
504 // Earlier match is better
505 if(g1.begin<g2.begin)
507 if(g2.begin>g2.begin)
510 // Longer match at same position is better
511 return g1.end>g2.end;
514 string Regex::disassemble_instruction(Code::const_iterator &i) const
516 Instruction instr=static_cast<Instruction>(*i);
517 if(instr>=LAST_INSTRUCTION_)
522 ostringstream result;
527 Offset offset=read_int<Offset>(i);
528 result<<"JUMP "<<Fmt("%+d")<<offset<<" ("<<Fmt("%d")<<i-code.begin()+offset<<')';
533 Offset offset=read_int<Offset>(i);
534 result<<"ND_JUMP "<<Fmt("%+d")<<offset<<" ("<<Fmt("%d")<<i-code.begin()+offset<<')';
538 result<<"GROUP_BEGIN "<<read_int<Index>(i);
541 result<<"GROUP_END "<<read_int<Index>(i);
547 result<<"MATCH_BEGIN";
555 result<<"MATCH_CHAR ";
556 if(c>=0x20 && c<=0x7E)
557 result<<'\''<<c<<'\'';
559 result<<(static_cast<int>(c)&0xFF);
563 result<<"MATCH_RANGE "<<(static_cast<int>(*i++)&0xFF);
564 result<<'-'<<(static_cast<int>(*i++)&0xFF);
567 result<<"MATCH_MASK";
568 for(unsigned j=0; j<32; ++j)
569 result<<' '<<Fmt("%02X")<<(static_cast<int>(*i++)&0xFF);
574 case FIRST_INSTRUCTION_:
575 case LAST_INSTRUCTION_:
576 result<<"UNKNOWN "<<instr;