3 This file is part of libmspstrings
4 Copyright © 2007 Mikko Rasa
5 Distributed under the LGPL
10 #include <msp/core/except.h>
11 #include "formatter.h"
19 Writes an integer to a Regex code string, in little-endian order.
22 void write_int(T n, Msp::Regex::Code &code)
24 for(unsigned i=0; i<sizeof(T); ++i)
29 Reads an integer from a Regex code stream, in little-endian order.
32 T read_int(Msp::Regex::Code::const_iterator &c)
35 for(unsigned i=0; i<sizeof(T); ++i)
36 result+=static_cast<unsigned char>(*c++)<<i*8;
44 Regex::Regex(const string &expr)
47 string::const_iterator iter=expr.begin();
48 code=compile(expr, iter, n_groups, false);
52 RegMatch Regex::match(const string &str) const
54 RegMatch::GroupArray groups(n_groups);
56 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
57 if(run(str, i, groups))
58 return RegMatch(str, groups);
63 string Regex::disassemble() const
67 for(Code::const_iterator i=code.begin(); i!=code.end();)
69 Code::const_iterator j=i;
70 Offset offset=i-code.begin();
71 string decompiled=disassemble_instruction(i);
74 bytes+=format(" %02X", static_cast<int>(*j)&0xFF);
75 ss<<Fmt("%3d")<<offset<<':'<<Fmt("%-9s")<<bytes;
77 ss<<"\n"<<Fmt("%15s");
78 ss<<" "<<decompiled<<'\n';
84 Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, unsigned &group, bool branch)
86 bool has_branches=false;
89 string::const_iterator end;
90 for(end=iter; end!=expr.end(); ++end)
103 throw InvalidParameterValue("Unexpected )");
119 throw InvalidParameterValue("Unmatched (");
123 unsigned this_group=group;
127 write_int<Index>(this_group, result);
130 const unsigned jump_size=1+sizeof(Offset);
134 for(string::const_iterator i=iter; i!=end;)
136 Code atom=parse_atom(expr, i, group);
140 parse_repeat(i, repeat_min, repeat_max);
142 for(unsigned j=0; j<repeat_min; ++j)
144 if(repeat_max==numeric_limits<Count>::max())
149 write_int<Offset>(atom.size()+jump_size, result);
153 write_int<Offset>(-(atom.size()+jump_size), result);
155 else if(repeat_max>repeat_min)
157 for(unsigned j=repeat_min; j<repeat_max; ++j)
160 write_int<Offset>((repeat_max-j)*(atom.size()+jump_size)-jump_size, result);
169 for(string::const_iterator i=iter;;)
171 branches.push_back(compile(expr, i, group, true));
177 unsigned n_branches=branches.size();
179 Offset offset=(n_branches-1)*jump_size+branches.front().size();
180 for(list<Code>::iterator i=++branches.begin(); i!=branches.end(); ++i)
183 write_int<Offset>(offset, result);
187 for(list<Code>::iterator i=branches.begin(); i!=branches.end();)
190 offset-=i->size()+jump_size;
192 if(i!=branches.end())
195 write_int<Offset>(offset, result);
203 write_int<Index>(this_group, result);
211 Regex::Code Regex::parse_atom(const string &expr, string::const_iterator &i, unsigned &group)
222 throw InvalidParameterValue("Stray backslash");
228 if(*i=='*' || *i=='{' || *i=='}' || *i=='+' || *i=='?' || *i=='|' || *i==')')
229 throw InvalidParameterValue("Invalid atom");
231 return parse_brackets(expr, i);
241 result=compile(expr, ++i, group, false);
258 bool Regex::parse_repeat(string::const_iterator &i, Count &rmin, Count &rmax)
260 if(*i!='*' && *i!='+' && *i!='?' && *i!='{')
263 if(*i=='*' || *i=='+')
264 rmax=numeric_limits<Count>::max();
265 if(*i=='*' || *i=='?')
270 for(++i; isdigit(*i); ++i)
271 rmin=rmin*10+(*i-'0');
279 for(; isdigit(*i); ++i)
280 rmax=rmax*10+(*i-'0');
282 throw InvalidParameterValue("Invalid bound");
285 rmax=numeric_limits<Count>::max();
290 throw InvalidParameterValue("Invalid bound");
298 Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &iter)
310 string::const_iterator end=iter;
311 for(; (end!=str.end() && (end==iter || *end!=']')); ++end) ;
313 throw InvalidParameterValue("Unmatched '['");
315 uint8_t mask[32]={0};
318 unsigned char first=0, last=0;
319 for(string::const_iterator i=iter; i!=end; ++i)
325 for(unsigned j=first; j<=c; ++j)
326 mask[j>>3]|=1<<(j&7);
331 else if(c=='-' && i!=iter && end-i>1)
336 mask[c>>3]|=1<<(c&7);
361 result.append(reinterpret_cast<char *>(mask), 32);
370 bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch::GroupArray &groups) const
373 list<RunContext> ctx;
374 ctx.push_back(RunContext());
375 ctx.front().citer=code.begin();
376 ctx.front().groups.resize(groups.size());
378 for(string::const_iterator i=begin;;)
382 c=static_cast<unsigned char>(*i);
386 for(list<RunContext>::iterator j=ctx.begin(); j!=ctx.end();)
388 bool terminate=false;
389 bool negate_match=false;
390 for(; j->citer!=code.end();)
392 Instruction instr=static_cast<Instruction>(*j->citer++);
398 Offset offset=read_int<Offset>(j->citer);
401 else if(instr==ND_JUMP)
403 Offset offset=read_int<Offset>(j->citer);
405 ctx.back().citer+=offset;
407 else if(instr==GROUP_BEGIN)
409 Index n=read_int<Index>(j->citer);
410 if(!j->groups[n].match)
411 j->groups[n].begin=i-str.begin();
413 else if(instr==GROUP_END)
415 Index n=read_int<Index>(j->citer);
416 if(!j->groups[n].match)
418 j->groups[n].match=true;
419 j->groups[n].end=i-str.begin();
420 j->groups[n].length=j->groups[n].end-j->groups[n].begin;
427 for(unsigned k=0; (k<groups.size() && !better); ++k)
429 better=group_compare(j->groups[k], groups[k]);
430 if(group_compare(groups[k], j->groups[k]))
439 bool match_result=false;
440 bool input_consumed=false;
441 if(instr==MATCH_BEGIN)
442 match_result=(i==str.begin());
443 else if(instr==MATCH_END)
444 match_result=(i==str.end());
445 else if(instr==MATCH_CHAR)
447 match_result=(c==*j->citer++);
450 else if(instr==MATCH_RANGE)
452 unsigned char first=*j->citer++;
453 unsigned char last=*j->citer++;
454 match_result=(c>=first && c<=last);
457 else if(instr==MATCH_MASK)
460 for(unsigned k=0; k<32; ++k)
462 match_result=mask[c>>3]&(1<<(c&7));
465 else if(instr==MATCH_ANY)
471 throw Exception("Invalid instruction");
473 if(match_result==negate_match)
477 if(input_consumed || terminate)
482 if(terminate || j->citer==code.end())
488 if(i==str.end() || ctx.empty())
496 bool Regex::group_compare(const RegMatch::Group &g1, const RegMatch::Group &g2) const
501 // Any match is better than no match
505 // Earlier match is better
506 if(g1.begin<g2.begin)
508 if(g2.begin>g2.begin)
511 // Longer match at same position is better
512 return g1.end>g2.end;
515 string Regex::disassemble_instruction(Code::const_iterator &i) const
517 Instruction instr=static_cast<Instruction>(*i++);
519 ostringstream result;
524 Offset offset=read_int<Offset>(i);
525 result<<"JUMP "<<Fmt("%+d")<<offset<<" ("<<Fmt("%d")<<i-code.begin()+offset<<')';
530 Offset offset=read_int<Offset>(i);
531 result<<"ND_JUMP "<<Fmt("%+d")<<offset<<" ("<<Fmt("%d")<<i-code.begin()+offset<<')';
535 result<<"GROUP_BEGIN "<<read_int<Index>(i);
538 result<<"GROUP_END "<<read_int<Index>(i);
544 result<<"MATCH_BEGIN";
552 result<<"MATCH_CHAR ";
553 if(c>=0x20 && c<=0x7E)
554 result<<'\''<<c<<'\'';
556 result<<(static_cast<int>(c)&0xFF);
560 result<<"MATCH_RANGE "<<(static_cast<int>(*i++)&0xFF);
561 result<<'-'<<(static_cast<int>(*i++)&0xFF);
564 result<<"MATCH_MASK";
565 for(unsigned j=0; j<32; ++j)
566 result<<' '<<Fmt("%02X")<<(static_cast<int>(*i++)&0xFF);
572 result<<"UNKNOWN "<<instr;