From 0111d34a31723757b1ee1ee7064c32f5cedc7ed5 Mon Sep 17 00:00:00 2001 From: Ulya Fokanova Date: Thu, 10 Apr 2014 01:15:40 +0300 Subject: [PATCH] Moved 'Range' stuff to separate file. --- re2c/Makefile.am | 2 +- re2c/actions.cc | 125 ------------------------------------------ re2c/range.cc | 137 +++++++++++++++++++++++++++++++++++++++++++++++ re2c/range.h | 51 ++++++++++++++++++ re2c/re.h | 35 +----------- 5 files changed, 190 insertions(+), 160 deletions(-) create mode 100644 re2c/range.cc create mode 100644 re2c/range.h diff --git a/re2c/Makefile.am b/re2c/Makefile.am index 170d25a3..74fdce0e 100755 --- a/re2c/Makefile.am +++ b/re2c/Makefile.am @@ -2,7 +2,7 @@ bin_PROGRAMS = re2c win_BINARIES = $(WINBUILDDIR)/re2c.exe -re2c_SOURCES = code.cc dfa.cc main.cc parser.cc actions.cc scanner.re substr.cc\ +re2c_SOURCES = code.cc dfa.cc main.cc parser.cc actions.cc scanner.re substr.cc range.cc \ translate.cc scanner.cc mbo_getopt.cc print.cc \ enc.cc utf8.cc utf8_range.cc utf8_regexp.cc utf16.cc utf16_range.cc utf16_regexp.cc range_suffix.cc \ basics.h dfa.h globals.h ins.h parser.h re.h scanner.h \ diff --git a/re2c/actions.cc b/re2c/actions.cc index 9a76681e..a9008b5c 100644 --- a/re2c/actions.cc +++ b/re2c/actions.cc @@ -120,131 +120,6 @@ void NullOp::split(CharSet&) ; } -std::ostream& operator<<(std::ostream &o, const Range &r) -{ - if ((r.ub - r.lb) == 1) - { - prtCh(o, r.lb); - } - else - { - prtCh(o, r.lb); - o << "-"; - prtCh(o, r.ub - 1); - } - - return o << r.next; -} - -Range *doUnion(Range *r1, Range *r2) -{ - Range *r, **rP = &r; - - for (;;) - { - Range *s; - - if (r1->lb <= r2->lb) - { - s = new Range(*r1); - } - else - { - s = new Range(*r2); - } - - *rP = s; - rP = &s->next; - - for (;;) - { - if (r1->lb <= r2->lb) - { - if (r1->lb > s->ub) - break; - - if (r1->ub > s->ub) - s->ub = r1->ub; - - if (!(r1 = r1->next)) - { - uint ub = 0; - - for (; r2 && r2->lb <= s->ub; r2 = r2->next) - ub = r2->ub; - - if (ub > s->ub) - s->ub = ub; - - *rP = r2; - - return r; - } - } - else - { - if (r2->lb > s->ub) - break; - - if (r2->ub > s->ub) - s->ub = r2->ub; - - if (!(r2 = r2->next)) - { - uint ub = 0; - - for (; r1 && r1->lb <= s->ub; r1 = r1->next) - ub = r1->ub; - - if (ub > s->ub) - s->ub = ub; - - *rP = r1; - - return r; - } - } - } - } - - *rP = NULL; - return r; -} - -Range *doDiff(Range *r1, Range *r2) -{ - Range *r, *s, **rP = &r; - - for (; r1; r1 = r1->next) - { - uint lb = r1->lb; - - for (; r2 && r2->ub <= r1->lb; r2 = r2->next) - - ; - for (; r2 && r2->lb < r1->ub; r2 = r2->next) - { - if (lb < r2->lb) - { - *rP = s = new Range(lb, r2->lb); - rP = &s->next; - } - - if ((lb = r2->ub) >= r1->ub) - goto noMore; - } - - *rP = s = new Range(lb, r1->ub); - rP = &s->next; - -noMore: - ; - } - - *rP = NULL; - return r; -} - MatchOp *merge(MatchOp *m1, MatchOp *m2) { if (!m1) diff --git a/re2c/range.cc b/re2c/range.cc new file mode 100644 index 00000000..2a9dfb7d --- /dev/null +++ b/re2c/range.cc @@ -0,0 +1,137 @@ +#include "print.h" +#include "range.h" + +namespace re2c +{ + +std::ostream& operator<<(std::ostream &o, const Range &r) +{ + if ((r.ub - r.lb) == 1) + { + prtCh(o, r.lb); + } + else + { + prtCh(o, r.lb); + o << "-"; + prtCh(o, r.ub - 1); + } + + return o << r.next; +} + +Range *doUnion(Range *r1, Range *r2) +{ + if (r1 == NULL) + return r2; + if (r2 == NULL) + return r1; + + Range *r, **rP = &r; + + for (;;) + { + Range *s; + + if (r1->lb <= r2->lb) + { + s = new Range(*r1); + } + else + { + s = new Range(*r2); + } + + *rP = s; + rP = &s->next; + + for (;;) + { + if (r1->lb <= r2->lb) + { + if (r1->lb > s->ub) + break; + + if (r1->ub > s->ub) + s->ub = r1->ub; + + if (!(r1 = r1->next)) + { + uint ub = 0; + + for (; r2 && r2->lb <= s->ub; r2 = r2->next) + ub = r2->ub; + + if (ub > s->ub) + s->ub = ub; + + *rP = r2; + + return r; + } + } + else + { + if (r2->lb > s->ub) + break; + + if (r2->ub > s->ub) + s->ub = r2->ub; + + if (!(r2 = r2->next)) + { + uint ub = 0; + + for (; r1 && r1->lb <= s->ub; r1 = r1->next) + ub = r1->ub; + + if (ub > s->ub) + s->ub = ub; + + *rP = r1; + + return r; + } + } + } + } + + *rP = NULL; + return r; +} + +Range *doDiff(Range *r1, Range *r2) +{ + Range *r, *s, **rP = &r; + + for (; r1; r1 = r1->next) + { + uint lb = r1->lb; + + for (; r2 && r2->ub <= r1->lb; r2 = r2->next) + + ; + for (; r2 && r2->lb < r1->ub; r2 = r2->next) + { + if (lb < r2->lb) + { + *rP = s = new Range(lb, r2->lb); + rP = &s->next; + } + + if ((lb = r2->ub) >= r1->ub) + goto noMore; + } + + *rP = s = new Range(lb, r1->ub); + rP = &s->next; + +noMore: + ; + } + + *rP = NULL; + return r; +} + +} // end namespace re2c diff --git a/re2c/range.h b/re2c/range.h new file mode 100644 index 00000000..a1f52842 --- /dev/null +++ b/re2c/range.h @@ -0,0 +1,51 @@ +#ifndef _range_h +#define _range_h + +#include + +#include "basics.h" +#include "free_list.h" + +namespace re2c +{ + +class Range +{ + +public: + Range *next; + uint lb, ub; // [lb,ub) + + static free_list vFreeList; + +public: + Range(uint l, uint u) : next(NULL), lb(l), ub(u) + { + vFreeList.insert(this); + } + + Range(Range &r) : next(NULL), lb(r.lb), ub(r.ub) + { + vFreeList.insert(this); + } + + ~Range() + { + vFreeList.erase(this); + } + + friend std::ostream& operator<<(std::ostream&, const Range&); + friend std::ostream& operator<<(std::ostream&, const Range*); +}; + +inline std::ostream& operator<<(std::ostream &o, const Range *r) +{ + return r ? o << *r : o; +} + +Range *doUnion(Range *r1, Range *r2); +Range *doDiff(Range *r1, Range *r2); + +} // end namespace re2c + +#endif diff --git a/re2c/re.h b/re2c/re.h index a164fece..a5874545 100644 --- a/re2c/re.h +++ b/re2c/re.h @@ -12,6 +12,7 @@ #include "ins.h" #include "free_list.h" #include "globals.h" +#include "range.h" #include "smart_ptr.h" namespace re2c @@ -46,40 +47,6 @@ struct CharSet CharPtn *ptn; }; -class Range -{ - -public: - Range *next; - uint lb, ub; // [lb,ub) - - static free_list vFreeList; - -public: - Range(uint l, uint u) : next(NULL), lb(l), ub(u) - { - vFreeList.insert(this); - } - - Range(Range &r) : next(NULL), lb(r.lb), ub(r.ub) - { - vFreeList.insert(this); - } - - ~Range() - { - vFreeList.erase(this); - } - - friend std::ostream& operator<<(std::ostream&, const Range&); - friend std::ostream& operator<<(std::ostream&, const Range*); -}; - -inline std::ostream& operator<<(std::ostream &o, const Range *r) -{ - return r ? o << *r : o; -} - class RegExp { -- 2.50.0