grep and egrep grammars

llvm-svn: 109534
This commit is contained in:
Howard Hinnant 2010-07-27 19:53:10 +00:00
parent 6b197e0651
commit 93da3b2e41
3 changed files with 222 additions and 0 deletions

View File

@ -2701,6 +2701,12 @@ private:
template <class _ForwardIterator>
_ForwardIterator
__parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
template <class _ForwardIterator>
_ForwardIterator
__parse_grep(_ForwardIterator __first, _ForwardIterator __last);
template <class _ForwardIterator>
_ForwardIterator
__parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
void __push_l_anchor() {__left_anchor_ = true;}
void __push_r_anchor();
@ -2832,8 +2838,10 @@ basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
case awk:
break;
case grep:
__parse_grep(__first, __last);
break;
case egrep:
__parse_egrep(__first, __last);
break;
default:
throw regex_error(regex_constants::__re_err_grammar);
@ -4108,6 +4116,68 @@ basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first
return __first;
}
template <class _CharT, class _Traits>
template <class _ForwardIterator>
_ForwardIterator
basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
_ForwardIterator __last)
{
__owns_one_state<_CharT>* __sa = __end_;
_ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
if (__t1 != __first)
__parse_basic_reg_exp(__first, __t1);
else
__push_empty();
__first = __t1;
if (__first != __last)
++__first;
while (__first != __last)
{
__t1 = _STD::find(__first, __last, _CharT('\n'));
__owns_one_state<_CharT>* __sb = __end_;
if (__t1 != __first)
__parse_basic_reg_exp(__first, __t1);
else
__push_empty();
__push_alternation(__sa, __sb);
__first = __t1;
if (__first != __last)
++__first;
}
return __first;
}
template <class _CharT, class _Traits>
template <class _ForwardIterator>
_ForwardIterator
basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
_ForwardIterator __last)
{
__owns_one_state<_CharT>* __sa = __end_;
_ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
if (__t1 != __first)
__parse_extended_reg_exp(__first, __t1);
else
__push_empty();
__first = __t1;
if (__first != __last)
++__first;
while (__first != __last)
{
__t1 = _STD::find(__first, __last, _CharT('\n'));
__owns_one_state<_CharT>* __sb = __end_;
if (__t1 != __first)
__parse_extended_reg_exp(__first, __t1);
else
__push_empty();
__push_alternation(__sa, __sb);
__first = __t1;
if (__first != __last)
++__first;
}
return __first;
}
template <class _CharT, class _Traits>
void
basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,

View File

@ -0,0 +1,92 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class BidirectionalIterator, class Allocator, class charT, class traits>
// bool
// regex_search(BidirectionalIterator first, BidirectionalIterator last,
// match_results<BidirectionalIterator, Allocator>& m,
// const basic_regex<charT, traits>& e,
// regex_constants::match_flag_type flags = regex_constants::match_default);
#include <iostream>
#include <regex>
#include <cassert>
#include "../../iterators.h"
int main()
{
{
std::cmatch m;
const char s[] = "tournament";
assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
std::regex_constants::egrep)));
assert(m.size() == 1);
assert(!m.prefix().matched);
assert(m.prefix().first == s);
assert(m.prefix().second == m[0].first);
assert(!m.suffix().matched);
assert(m.suffix().first == m[0].second);
assert(m.suffix().second == s + std::char_traits<char>::length(s));
assert(m.length(0) == 10);
assert(m.position(0) == 0);
assert(m.str(0) == "tournament");
}
{
std::cmatch m;
const char s[] = "ment";
assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
std::regex_constants::egrep)));
assert(m.size() == 1);
assert(!m.prefix().matched);
assert(m.prefix().first == s);
assert(m.prefix().second == m[0].first);
assert(m.suffix().matched);
assert(m.suffix().first == m[0].second);
assert(m.suffix().second == s + std::char_traits<char>::length(s));
assert(m.length(0) == 0);
assert(m.position(0) == 0);
assert(m.str(0) == "");
}
{
std::cmatch m;
const char s[] = "tournament";
assert(std::regex_search(s, m, std::regex("(tour|to|tournament)+\ntourna",
std::regex_constants::egrep)));
assert(m.size() == 2);
assert(!m.prefix().matched);
assert(m.prefix().first == s);
assert(m.prefix().second == m[0].first);
assert(!m.suffix().matched);
assert(m.suffix().first == m[0].second);
assert(m.suffix().second == s + std::char_traits<char>::length(s));
assert(m.length(0) == 10);
assert(m.position(0) == 0);
assert(m.str(0) == "tournament");
}
{
std::cmatch m;
const char s[] = "tourna";
assert(std::regex_search(s, m, std::regex("(tour|to|tournament)+\ntourna",
std::regex_constants::egrep)));
assert(m.size() == 2);
assert(!m.prefix().matched);
assert(m.prefix().first == s);
assert(m.prefix().second == m[0].first);
assert(!m.suffix().matched);
assert(m.suffix().first == m[0].second);
assert(m.suffix().second == s + std::char_traits<char>::length(s));
assert(m.length(0) == 6);
assert(m.position(0) == 0);
assert(m.str(0) == "tourna");
}
}

View File

@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class BidirectionalIterator, class Allocator, class charT, class traits>
// bool
// regex_search(BidirectionalIterator first, BidirectionalIterator last,
// match_results<BidirectionalIterator, Allocator>& m,
// const basic_regex<charT, traits>& e,
// regex_constants::match_flag_type flags = regex_constants::match_default);
#include <iostream>
#include <regex>
#include <cassert>
#include "../../iterators.h"
int main()
{
{
std::cmatch m;
const char s[] = "tournament";
assert(std::regex_search(s, m, std::regex("tour\nto\ntournament",
std::regex_constants::grep)));
assert(m.size() == 1);
assert(!m.prefix().matched);
assert(m.prefix().first == s);
assert(m.prefix().second == m[0].first);
assert(!m.suffix().matched);
assert(m.suffix().first == m[0].second);
assert(m.suffix().second == s + std::char_traits<char>::length(s));
assert(m.length(0) == 10);
assert(m.position(0) == 0);
assert(m.str(0) == "tournament");
}
{
std::cmatch m;
const char s[] = "ment";
assert(std::regex_search(s, m, std::regex("tour\n\ntournament",
std::regex_constants::grep)));
assert(m.size() == 1);
assert(!m.prefix().matched);
assert(m.prefix().first == s);
assert(m.prefix().second == m[0].first);
assert(m.suffix().matched);
assert(m.suffix().first == m[0].second);
assert(m.suffix().second == s + std::char_traits<char>::length(s));
assert(m.length(0) == 0);
assert(m.position(0) == 0);
assert(m.str(0) == "");
}
}