3 This file is part of libmspstrings
4 Copyright © 2007 Mikko Rasa
5 Distributed under the LGPL
8 #include <msp/core/error.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);
249 if(static_cast<unsigned char>(*i)<=LAST_INSTRUCTION_)
259 bool Regex::parse_repeat(string::const_iterator &i, Count &rmin, Count &rmax)
261 if(*i!='*' && *i!='+' && *i!='?' && *i!='{')
264 if(*i=='*' || *i=='+')
265 rmax=numeric_limits<Count>::max();
266 if(*i=='*' || *i=='?')
271 for(++i; isdigit(*i); ++i)
272 rmin=rmin*10+(*i-'0');
280 for(; isdigit(*i); ++i)
281 rmax=rmax*10+(*i-'0');
283 throw InvalidParameterValue("Invalid bound");
286 rmax=numeric_limits<Count>::max();
291 throw InvalidParameterValue("Invalid bound");
299 Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &iter)
311 string::const_iterator end=iter;
312 for(; (end!=str.end() && *end!=']'); ++end);
314 throw InvalidParameterValue("Unmatched '['");
316 uint8_t mask[32]={0};
319 unsigned char first, last;
320 for(string::const_iterator i=iter; i!=end; ++i)
326 for(unsigned j=first; j<=c; ++j)
327 mask[j>>3]|=1<<(j&7);
332 else if(c=='-' && i!=iter && end-i>1)
337 mask[c>>3]|=1<<(c&7);
362 result.append(reinterpret_cast<char *>(mask), 32);
371 bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch::GroupArray &groups) const
374 list<RunContext> ctx;
375 ctx.push_back(RunContext());
376 ctx.front().citer=code.begin();
377 ctx.front().groups.resize(groups.size());
379 for(string::const_iterator i=begin;;)
383 c=static_cast<unsigned char>(*i);
387 for(list<RunContext>::iterator j=ctx.begin(); j!=ctx.end();)
389 bool terminate=false;
390 bool negate_match=false;
391 for(; j->citer!=code.end();)
393 Instruction instr=static_cast<Instruction>(*j->citer);
394 if(instr>LAST_INSTRUCTION_)
403 Offset offset=read_int<Offset>(j->citer);
406 else if(instr==ND_JUMP)
408 Offset offset=read_int<Offset>(j->citer);
410 ctx.back().citer+=offset;
412 else if(instr==GROUP_BEGIN)
414 Index n=read_int<Index>(j->citer);
415 if(!j->groups[n].match)
416 j->groups[n].begin=i-str.begin();
418 else if(instr==GROUP_END)
420 Index n=read_int<Index>(j->citer);
421 if(!j->groups[n].match)
423 j->groups[n].match=true;
424 j->groups[n].end=i-str.begin();
425 j->groups[n].length=j->groups[n].end-j->groups[n].begin;
432 for(unsigned k=0; (k<groups.size() && !better); ++k)
434 better=group_compare(j->groups[k], groups[k]);
435 if(group_compare(groups[k], j->groups[k]))
444 bool match_result=false;
445 bool input_consumed=false;
446 if(instr==MATCH_BEGIN)
447 match_result=(i==str.begin());
448 else if(instr==MATCH_END)
449 match_result=(i==str.end());
450 else if(instr==MATCH_CHAR)
452 match_result=(c==*j->citer++);
455 else if(instr==MATCH_RANGE)
457 unsigned char first=*j->citer++;
458 unsigned char last=*j->citer++;
459 match_result=(c>=first && c<=last);
462 else if(instr==MATCH_MASK)
465 for(unsigned k=0; k<32; ++k)
467 match_result=mask[c>>3]&(1<<(c&7));
470 else if(instr==MATCH_ANY)
476 throw Exception("Invalid instruction");
478 if(match_result==negate_match)
482 if(input_consumed || terminate)
487 if(terminate || j->citer==code.end())
493 if(i==str.end() || ctx.empty())
501 bool Regex::group_compare(const RegMatch::Group &g1, const RegMatch::Group &g2) const
506 // Any match is better than no match
510 // Earlier match is better
511 if(g1.begin<g2.begin)
513 if(g2.begin>g2.begin)
516 // Longer match at same position is better
517 return g1.end>g2.end;
520 string Regex::disassemble_instruction(Code::const_iterator &i) const
522 Instruction instr=static_cast<Instruction>(*i);
523 if(instr>=LAST_INSTRUCTION_)
528 ostringstream result;
533 Offset offset=read_int<Offset>(i);
534 result<<"JUMP "<<Fmt("%+d")<<offset<<" ("<<Fmt("%d")<<i-code.begin()+offset<<')';
539 Offset offset=read_int<Offset>(i);
540 result<<"ND_JUMP "<<Fmt("%+d")<<offset<<" ("<<Fmt("%d")<<i-code.begin()+offset<<')';
544 result<<"GROUP_BEGIN "<<read_int<Index>(i);
547 result<<"GROUP_END "<<read_int<Index>(i);
553 result<<"MATCH_BEGIN";
561 result<<"MATCH_CHAR ";
562 if(c>=0x20 && c<=0x7E)
563 result<<'\''<<c<<'\'';
565 result<<(static_cast<int>(c)&0xFF);
569 result<<"MATCH_RANGE "<<(static_cast<int>(*i++)&0xFF);
570 result<<'-'<<(static_cast<int>(*i++)&0xFF);
573 result<<"MATCH_MASK";
574 for(unsigned j=0; j<32; ++j)
575 result<<' '<<Fmt("%02X")<<(static_cast<int>(*i++)&0xFF);
580 case FIRST_INSTRUCTION_:
581 case LAST_INSTRUCTION_:
582 result<<"UNKNOWN "<<instr;