/usr/include/lexertl/parser/parser.hpp is in libpuma-dev 1:1.2+svn20150823-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 | // parser.hpp
// Copyright (c) 2005-2011 Ben Hanson (http://www.benhanson.net/)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef LEXERTL_PARSER_HPP
#define LEXERTL_PARSER_HPP
#include <assert.h>
#include <algorithm>
#include "../bool.hpp"
#include "tree/end_node.hpp"
#include "tree/iteration_node.hpp"
#include "tree/leaf_node.hpp"
#include <map>
#include "../containers/ptr_stack.hpp"
#include "tokeniser/re_tokeniser.hpp"
#include "../runtime_error.hpp"
#include "tree/selection_node.hpp"
#include "tree/sequence_node.hpp"
#include "../size_t.hpp"
#include <vector>
namespace lexertl
{
namespace detail
{
/*
General principles of regex parsing:
- Every regex is a sequence of sub-regexes.
- Regexes consist of operands and operators
- All operators decompose to sequence, selection ('|') and iteration ('*')
- Regex tokens are stored on a stack.
- When a complete sequence of regex tokens is on the stack it is processed.
Grammar:
<REGEX> -> <OREXP>
<OREXP> -> <SEQUENCE> | <OREXP>'|'<SEQUENCE>
<SEQUENCE> -> <SUB>
<SUB> -> <EXPRESSION> | <SUB><EXPRESSION>
<EXPRESSION> -> <REPEAT>
<REPEAT> -> charset | macro | '('<REGEX>')' | <REPEAT><DUPLICATE>
<DUPLICATE> -> '?' | '??' | '*' | '*?' | '+' | '+?' | '{n[,[m]]}' |
'{n[,[m]]}?'
*/
template<typename rules_char_type, typename sm_traits>
class basic_parser
{
public:
enum {char_24_bit = sm_traits::char_24_bit};
typedef typename sm_traits::char_type char_type;
typedef typename sm_traits::id_type id_type;
typedef basic_end_node<id_type> end_node;
typedef typename sm_traits::input_char_type input_char_type;
typedef basic_string_token<input_char_type> input_string_token;
typedef basic_iteration_node<id_type> iteration_node;
typedef basic_leaf_node<id_type> leaf_node;
typedef basic_re_tokeniser<rules_char_type, input_char_type, id_type>
tokeniser;
typedef basic_node<id_type> node;
typedef typename node::node_ptr_vector node_ptr_vector;
typedef std::basic_string<rules_char_type> string;
typedef basic_string_token<char_type> string_token;
typedef std::map<string, const node *> macro_map;
typedef basic_selection_node<id_type> selection_node;
typedef basic_sequence_node<id_type> sequence_node;
typedef std::map<string_token, std::size_t> charset_map;
typedef std::pair<string_token, std::size_t> charset_pair;
typedef bool_<sm_traits::compressed> compressed;
basic_parser (const std::locale &locale_,
node_ptr_vector &node_ptr_vector_, const macro_map ¯o_map_,
charset_map &charset_map_, const id_type eoi_) :
_locale (locale_),
_node_ptr_vector (node_ptr_vector_),
_macro_map (macro_map_),
_charset_map (charset_map_),
_eoi (eoi_)
{
}
node *parse (const rules_char_type *start_,
const rules_char_type * const end_, const id_type id_,
const id_type user_id_, const id_type next_dfa_,
const id_type push_dfa_, const bool pop_dfa_,
const std::size_t flags_, id_type &eol_id_, const bool seen_bol_,
const bool macro_)
{
node *root_ = 0;
state re_state_ (start_, end_, flags_, _locale, id_ == _eoi);
token *lhs_token_ = 0;
std::auto_ptr<token> rhs_token_ (new token);
char action_ = 0;
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = rhs_token_.release ();
rhs_token_.reset (new token);
tokeniser::next (re_state_, rhs_token_.get ());
do
{
lhs_token_ = _token_stack->top ();
action_ = lhs_token_->precedence (rhs_token_->_type);
switch (action_)
{
case '<':
case '=':
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = rhs_token_.release ();
rhs_token_.reset (new token);
tokeniser::next (re_state_, rhs_token_.get ());
break;
case '>':
reduce (re_state_);
break;
default:
std::ostringstream ss_;
ss_ << "A syntax error occurred: '" <<
lhs_token_->precedence_string () <<
"' against '" << rhs_token_->precedence_string () <<
"' at index " << re_state_.index () << ".";
throw runtime_error (ss_.str ().c_str ());
break;
}
} while (!_token_stack->empty ());
if (_tree_node_stack.empty ())
{
throw runtime_error ("Empty rules are not allowed.");
}
assert (_tree_node_stack.size () == 1);
node *lhs_node_ = _tree_node_stack.top ();
_tree_node_stack.pop ();
if (macro_)
{
// Macros have no end state...
root_ = lhs_node_;
}
else
{
_node_ptr_vector->push_back (static_cast<end_node *>(0));
node *rhs_node_ = new end_node (id_, user_id_, next_dfa_,
push_dfa_, pop_dfa_);
_node_ptr_vector->back () = rhs_node_;
_node_ptr_vector->push_back (static_cast<sequence_node *>(0));
_node_ptr_vector->back () = new sequence_node
(lhs_node_, rhs_node_);
root_ = _node_ptr_vector->back ();
}
if (seen_bol_)
{
fixup_bol (root_);
}
if (re_state_._eol_id != static_cast<id_type>(~0))
{
eol_id_ = re_state_._eol_id;
}
return root_;
}
static id_type bol_token ()
{
return static_cast<id_type>(~1);
}
static id_type eol_token ()
{
return static_cast<id_type>(~2);
}
private:
typedef typename input_string_token::range input_range;
typedef typename tokeniser::state state;
typedef basic_re_token<rules_char_type, input_char_type> token;
typedef typename string_token::range range;
typedef ptr_vector<string_token> string_token_vector;
typedef ptr_stack<token> token_stack;
typedef typename node::node_stack tree_node_stack;
const std::locale &_locale;
node_ptr_vector &_node_ptr_vector;
const macro_map &_macro_map;
charset_map &_charset_map;
id_type _eoi;
bool _compression;
token_stack _token_stack;
tree_node_stack _tree_node_stack;
struct find_functor
{
// Pointer to stop warning about cannot create assignment operator.
const string_token *_token;
find_functor (const string_token &token_) :
_token (&token_)
{
}
bool operator () (const string_token *rhs_)
{
return *_token == *rhs_;
}
};
void reduce (state &re_state_)
{
token *lhs_ = 0;
token *rhs_ = 0;
token_stack handle_;
char action_ = 0;
do
{
rhs_ = _token_stack->top ();
handle_->push (static_cast<token *>(0));
_token_stack->pop ();
handle_->top () = rhs_;
if (!_token_stack->empty ())
{
lhs_ = _token_stack->top ();
action_ = lhs_->precedence (rhs_->_type);
}
} while (!_token_stack->empty () && action_ == '=');
assert (_token_stack->empty () || action_ == '<');
switch (rhs_->_type)
{
case BEGIN:
// finished processing so exit
break;
case REGEX:
// finished parsing, nothing to do
break;
case OREXP:
orexp (handle_);
break;
case SEQUENCE:
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (OREXP);
break;
case SUB:
sub (handle_);
break;
case EXPRESSION:
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (SUB);
break;
case REPEAT:
repeat (handle_);
break;
case BOL:
bol (handle_);
break;
case EOL:
eol (handle_, re_state_);
break;
case CHARSET:
charset (handle_, compressed ());
break;
case MACRO:
macro (handle_);
break;
case OPENPAREN:
openparen (handle_);
break;
case OPT:
case AOPT:
optional (rhs_->_type == OPT);
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (DUP);
break;
case ZEROORMORE:
case AZEROORMORE:
zero_or_more (rhs_->_type == ZEROORMORE);
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (DUP);
break;
case ONEORMORE:
case AONEORMORE:
one_or_more (rhs_->_type == ONEORMORE);
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (DUP);
break;
case REPEATN:
case AREPEATN:
repeatn (rhs_->_type == REPEATN, handle_->top ());
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (DUP);
break;
default:
throw runtime_error
("Internal error regex_parser::reduce");
break;
}
}
void orexp (token_stack &handle_)
{
assert (handle_->top ()->_type == OREXP &&
(handle_->size () == 1 || handle_->size () == 3));
if (handle_->size () == 1)
{
std::auto_ptr<token> token_ (new token (REGEX));
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = token_.release ();
}
else
{
token *token_ = handle_->top ();
handle_->pop ();
delete token_;
token_ = 0;
assert (handle_->top ()->_type == OR);
token_ = handle_->top ();
handle_->pop ();
delete token_;
token_ = 0;
assert (handle_->top ()->_type == SEQUENCE);
perform_or ();
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (OREXP);
}
}
void perform_or ()
{
// perform or
node *rhs_ = _tree_node_stack.top ();
_tree_node_stack.pop ();
node *lhs_ = _tree_node_stack.top ();
_node_ptr_vector->push_back (static_cast<selection_node *>(0));
_node_ptr_vector->back () = new selection_node (lhs_, rhs_);
_tree_node_stack.top () = _node_ptr_vector->back ();
}
void sub (token_stack &handle_)
{
assert ((handle_->top ()->_type == SUB &&
handle_->size () == 1) || handle_->size () == 2);
if (handle_->size () == 1)
{
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (SEQUENCE);
}
else
{
token *token_ = handle_->top ();
handle_->pop ();
delete token_;
token_ = 0;
assert (handle_->top ()->_type == EXPRESSION);
// perform join
sequence ();
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (SUB);
}
}
void repeat (token_stack &handle_)
{
assert (handle_->top ()->_type == REPEAT &&
handle_->size () >= 1 && handle_->size () <= 3);
if (handle_->size () == 1)
{
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (EXPRESSION);
}
else
{
token *token_ = handle_->top ();
handle_->pop ();
delete token_;
token_ = 0;
assert (handle_->top ()->_type == DUP);
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (REPEAT);
}
}
#ifndef NDEBUG
void bol (token_stack &handle_)
#else
void bol (token_stack &)
#endif
{
assert (handle_->top ()->_type == BOL &&
handle_->size () == 1);
// store charset
_node_ptr_vector->push_back (static_cast<leaf_node *>(0));
_node_ptr_vector->back () = new leaf_node (bol_token (), true);
_tree_node_stack.push (_node_ptr_vector->back ());
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (REPEAT);
}
#ifndef NDEBUG
void eol (token_stack &handle_, state &re_state_)
#else
void eol (token_stack &, state &re_state_)
#endif
{
// Done in two parts for VC6.
const string_token eol_ ('\n');
assert (handle_->top ()->_type == EOL &&
handle_->size () == 1);
re_state_._eol_id = lookup (eol_, compressed ());
// store charset
_node_ptr_vector->push_back (static_cast<leaf_node *>(0));
_node_ptr_vector->back () = new leaf_node (eol_token (), true);
_tree_node_stack.push (_node_ptr_vector->back ());
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (REPEAT);
}
// Uncompressed
void charset (token_stack &handle_, const false_ &)
{
assert (handle_->top ()->_type == CHARSET &&
handle_->size () == 1);
const id_type id_ = lookup (handle_->top ()->_str, compressed ());
// store charset
_node_ptr_vector->push_back (static_cast<leaf_node *>(0));
_node_ptr_vector->back () = new leaf_node (id_, true);
_tree_node_stack.push (_node_ptr_vector->back ());
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (REPEAT);
}
// Compressed
void charset (token_stack &handle_, const true_ &)
{
assert (handle_->top ()->_type == CHARSET &&
handle_->size () == 1);
std::auto_ptr<token> token_ (handle_->top ());
handle_->pop ();
create_sequence (token_);
}
// Slice wchar_t into sequence of char.
void create_sequence (std::auto_ptr<token> &token_)
{
typename token::string_token::range_vector::iterator iter_ =
token_->_str._ranges.begin ();
typename token::string_token::range_vector::const_iterator end_ =
token_->_str._ranges.end ();
string_token_vector data_[char_24_bit ? 3 : 2];
for (; iter_ != end_; ++iter_)
{
slice_range (*iter_, data_, bool_<char_24_bit> ());
}
push_ranges (data_, bool_<char_24_bit> ());
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (OPENPAREN);
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (REGEX);
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (CLOSEPAREN);
}
// 16 bit unicode
void slice_range (const input_range &range_, string_token_vector data_[2],
const false_ &)
{
const unsigned char first_msb_ = static_cast<unsigned char>
((range_.first >> 8) & 0xff);
const unsigned char first_lsb_ = static_cast<unsigned char>
(range_.first & 0xff);
const unsigned char second_msb_ = static_cast<unsigned char>
((range_.second >> 8) & 0xff);
const unsigned char second_lsb_ = static_cast<unsigned char>
(range_.second & 0xff);
if (first_msb_ == second_msb_)
{
insert_range (first_msb_, first_msb_, first_lsb_,
second_lsb_, data_);
}
else
{
insert_range (first_msb_, first_msb_, first_lsb_, 0xff, data_);
if (second_msb_ > first_msb_ + 1)
{
insert_range (first_msb_ + 1, second_msb_ - 1, 0, 0xff, data_);
}
insert_range (second_msb_, second_msb_, 0, second_lsb_, data_);
}
}
// 24 bit unicode
void slice_range (const input_range &range_, string_token_vector data_[3],
const true_ &)
{
const unsigned char first_msb_ = static_cast<unsigned char>
((range_.first >> 16) & 0xff);
const unsigned char first_mid_ = static_cast<unsigned char>
((range_.first >> 8) & 0xff);
const unsigned char first_lsb_ = static_cast<unsigned char>
(range_.first & 0xff);
const unsigned char second_msb_ = static_cast<unsigned char>
((range_.second >> 16) & 0xff);
const unsigned char second_mid_ = static_cast<unsigned char>
((range_.second >> 8) & 0xff);
const unsigned char second_lsb_ = static_cast<unsigned char>
(range_.second & 0xff);
if (first_msb_ == second_msb_)
{
string_token_vector data2_[2];
// Re-use 16 bit slice function
slice_range (range_, data2_, false_ ());
for (std::size_t i_ = 0, size_ = data2_[0]->size ();
i_ < size_; ++i_)
{
insert_range (string_token (first_msb_, first_msb_),
*(*data2_[0])[i_], *(*data2_[1])[i_], data_);
}
}
else
{
insert_range (first_msb_, first_msb_,
first_mid_, first_mid_,
first_lsb_, 0xff, data_);
if (first_mid_ != 0xff)
{
insert_range (first_msb_, first_msb_,
first_mid_ + 1, 0xff,
0, 0xff, data_);
}
if (second_msb_ > first_msb_ + 1)
{
insert_range (first_mid_ + 1, second_mid_ - 1,
0, 0xff,
0, 0xff, data_);
}
if (second_mid_ != 0)
{
insert_range (second_msb_, second_msb_,
0, second_mid_ - 1,
0, 0xff, data_);
insert_range (second_msb_, second_msb_,
second_mid_, second_mid_,
0, second_lsb_, data_);
}
else
{
insert_range (second_msb_, second_msb_,
0, second_mid_,
0, second_lsb_, data_);
}
}
}
// 16 bit unicode
void insert_range (const unsigned char first_, const unsigned char second_,
const unsigned char first2_, const unsigned char second2_,
string_token_vector data_[2])
{
const string_token token_ (first_ > second_ ? second_ : first_,
first_ > second_ ? first_ : second_);
const string_token token2_ (first2_ > second2_ ? second2_ : first2_,
first2_ > second2_ ? first2_ : second2_);
insert_range (token_, token2_, data_);
}
void insert_range (const string_token &token_, const string_token &token2_,
string_token_vector data_[2])
{
typename string_token_vector::vector::const_iterator iter_ =
std::find_if (data_[0]->begin (), data_[0]->end (),
find_functor (token_));
if (iter_ == data_[0]->end ())
{
data_[0]->push_back (0);
data_[0]->back () = new string_token (token_);
data_[1]->push_back (0);
data_[1]->back () = new string_token (token2_);
}
else
{
const std::size_t index_ = iter_ - data_[0]->begin ();
(*data_[1])[index_]->insert (token2_);
}
}
// 24 bit unicode
void insert_range (const unsigned char first_, const unsigned char second_,
const unsigned char first2_, const unsigned char second2_,
const unsigned char first3_, const unsigned char second3_,
string_token_vector data_[3])
{
const string_token token_ (first_ > second_ ? second_ : first_,
first_ > second_ ? first_ : second_);
const string_token token2_ (first2_ > second2_ ? second2_ : first2_,
first2_ > second2_ ? first2_ : second2_);
const string_token token3_ (first3_ > second3_ ? second3_ : first3_,
first3_ > second3_ ? first3_ : second3_);
insert_range (token_, token2_, token3_, data_);
}
void insert_range (const string_token &token_, const string_token &token2_,
const string_token &token3_, string_token_vector data_[3])
{
typename string_token_vector::vector::const_iterator iter_ =
data_[0]->begin ();
typename string_token_vector::vector::const_iterator end_ =
data_[0]->end ();
bool finished_ = false;
do
{
iter_ = std::find_if (iter_, end_, find_functor (token_));
if (iter_ == end_)
{
data_[0]->push_back (0);
data_[0]->back () = new string_token (token_);
data_[1]->push_back (0);
data_[1]->back () = new string_token (token2_);
data_[2]->push_back (0);
data_[2]->back () = new string_token (token3_);
finished_ = true;
}
else
{
const std::size_t index_ = iter_ - data_[0]->begin ();
if (*(*data_[1])[index_] == token2_)
{
(*data_[2])[index_]->insert (token3_);
finished_ = true;
}
else
{
++iter_;
}
}
} while (!finished_);
}
// 16 bit unicode
void push_ranges (string_token_vector data_[2], const false_ &)
{
typename string_token_vector::vector::const_iterator viter_ =
data_[0]->begin ();
typename string_token_vector::vector::const_iterator vend_ =
data_[0]->end ();
typename string_token_vector::vector::const_iterator viter2_ =
data_[1]->begin ();
push_range (*viter_++);
push_range (*viter2_++);
sequence ();
while (viter_ != vend_)
{
push_range (*viter_++);
push_range (*viter2_++);
sequence ();
perform_or ();
}
}
// 24 bit unicode
void push_ranges (string_token_vector data_[3], const true_ &)
{
typename string_token_vector::vector::const_iterator viter_ =
data_[0]->begin ();
typename string_token_vector::vector::const_iterator vend_ =
data_[0]->end ();
typename string_token_vector::vector::const_iterator viter2_ =
data_[1]->begin ();
typename string_token_vector::vector::const_iterator viter3_ =
data_[2]->begin ();
push_range (*viter_++);
push_range (*viter2_++);
sequence ();
push_range (*viter3_++);
sequence ();
while (viter_ != vend_)
{
push_range (*viter_++);
push_range (*viter2_++);
sequence ();
push_range (*viter3_++);
sequence ();
perform_or ();
}
}
void push_range (const string_token *token_)
{
const id_type id_ = lookup (*token_, compressed ());
_node_ptr_vector->push_back (static_cast<leaf_node *>(0));
_node_ptr_vector->back () = new leaf_node (id_, true);
_tree_node_stack.push (_node_ptr_vector->back ());
}
// Uncompressed
id_type lookup (const input_string_token &charset_, const false_ &)
{
std::size_t id_ = sm_traits::npos ();
typename charset_map::const_iterator iter_ =
_charset_map.find (charset_);
if (iter_ == _charset_map.end ())
{
id_ = _charset_map.size ();
_charset_map.insert (charset_pair (charset_, id_));
}
else
{
id_ = iter_->second;
}
if (static_cast<id_type>(id_) < id_)
{
throw runtime_error ("IdType is not large enough "
"to hold all ids.");
}
return static_cast<id_type>(id_);
}
// Compressed
id_type lookup (const string_token &charset_, const true_ &)
{
// Converted to id_type below.
std::size_t id_ = sm_traits::npos ();
typename charset_map::const_iterator iter_;
iter_ = _charset_map.find (charset_);
if (iter_ == _charset_map.end ())
{
id_ = _charset_map.size ();
_charset_map.insert (charset_pair (charset_, id_));
}
else
{
id_ = iter_->second;
}
if (static_cast<id_type>(id_) < id_)
{
throw runtime_error ("IdType is not large enough "
"to hold all ids.");
}
return static_cast<id_type>(id_);
}
void macro (token_stack &handle_)
{
const token *top_ = handle_->top ();
assert (top_->_type == MACRO && handle_->size () == 1);
typename macro_map::const_iterator iter_ =
_macro_map.find (top_->_extra);
if (iter_ == _macro_map.end ())
{
const rules_char_type *name_ = top_->_extra.c_str ();
std::basic_stringstream<input_char_type> ss_;
std::ostringstream os_;
os_ << "Unknown MACRO name '";
while (*name_)
{
os_ << ss_.narrow (*name_++, ' ');
}
os_ << "'.";
throw runtime_error (os_.str ());
}
_tree_node_stack.push (iter_->second->copy (_node_ptr_vector));
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (REPEAT);
}
void openparen (token_stack &handle_)
{
token *token_ = handle_->top ();
assert (token_->_type == OPENPAREN &&
handle_->size () == 3);
handle_->pop ();
delete token_;
token_ = handle_->top ();
assert (token_->_type == REGEX);
handle_->pop ();
delete token_;
token_ = 0;
assert (handle_->top ()->_type == CLOSEPAREN);
_token_stack->push (static_cast<token *>(0));
_token_stack->top () = new token (REPEAT);
}
void sequence ()
{
node *rhs_ = _tree_node_stack.top ();
_tree_node_stack.pop ();
node *lhs_ = _tree_node_stack.top ();
_node_ptr_vector->push_back (static_cast<sequence_node *>(0));
_node_ptr_vector->back () = new sequence_node (lhs_, rhs_);
_tree_node_stack.top () = _node_ptr_vector->back ();
}
void optional (const bool greedy_)
{
// perform ?
node *lhs_ = _tree_node_stack.top ();
// Don't know if lhs_ is a leaf_node, so get firstpos.
typename node::node_vector &firstpos_ = lhs_->firstpos ();
for (typename node::node_vector::iterator iter_ = firstpos_.begin (),
end_ = firstpos_.end (); iter_ != end_; ++iter_)
{
// These are leaf_nodes!
(*iter_)->greedy (greedy_);
}
_node_ptr_vector->push_back (static_cast<leaf_node *>(0));
node *rhs_ = new leaf_node (node::null_token (), greedy_);
_node_ptr_vector->back () = rhs_;
_node_ptr_vector->push_back (static_cast<selection_node *>(0));
_node_ptr_vector->back () = new selection_node (lhs_, rhs_);
_tree_node_stack.top () = _node_ptr_vector->back ();
}
void zero_or_more (const bool greedy_)
{
// perform *
node *ptr_ = _tree_node_stack.top ();
_node_ptr_vector->push_back (static_cast<iteration_node *>(0));
_node_ptr_vector->back () = new iteration_node (ptr_, greedy_);
_tree_node_stack.top () = _node_ptr_vector->back ();
}
void one_or_more (const bool greedy_)
{
// perform +
node *lhs_ = _tree_node_stack.top ();
node *copy_ = lhs_->copy (_node_ptr_vector);
_node_ptr_vector->push_back (static_cast<iteration_node *>(0));
node *rhs_ = new iteration_node (copy_, greedy_);
_node_ptr_vector->back () = rhs_;
_node_ptr_vector->push_back (static_cast<sequence_node *>(0));
_node_ptr_vector->back () = new sequence_node (lhs_, rhs_);
_tree_node_stack.top () = _node_ptr_vector->back ();
}
// perform {n[,[m]]}
// Semantic checks have already been performed.
// {0,} = *
// {0,1} = ?
// {1,} = +
// therefore we do not check for these cases.
void repeatn (const bool greedy_, const token *token_)
{
const rules_char_type *str_ = token_->_extra.c_str ();
std::size_t min_ = 0;
bool comma_ = false;
std::size_t max_ = 0;
while (*str_>= '0' && *str_ <= '9')
{
min_ *= 10;
min_ += *str_ - '0';
++str_;
}
comma_ = *str_ == ',';
if (comma_) ++str_;
while (*str_>= '0' && *str_ <= '9')
{
max_ *= 10;
max_ += *str_ - '0';
++str_;
}
if (!(min_ == 1 && !comma_))
{
const std::size_t top_ = min_ > 0 ? min_ : max_;
if (min_ == 0)
{
optional (greedy_);
}
node *prev_ = _tree_node_stack.top ()->
copy (_node_ptr_vector);
node *curr_ = 0;
for (std::size_t i_ = 2; i_ < top_; ++i_)
{
node *temp_ = prev_->copy (_node_ptr_vector);
curr_ = temp_;
_tree_node_stack.push (static_cast<node *>(0));
_tree_node_stack.top () = prev_;
sequence ();
prev_ = curr_;
}
if (comma_ && min_ > 0)
{
if (min_ > 1)
{
node *temp_ = prev_->copy (_node_ptr_vector);
curr_ = temp_;
_tree_node_stack.push (static_cast<node *>(0));
_tree_node_stack.top () = prev_;
sequence ();
prev_ = curr_;
}
if (comma_ && max_)
{
_tree_node_stack.push (static_cast<node *>(0));
_tree_node_stack.top () = prev_;
optional (greedy_);
node *temp_ = _tree_node_stack.top ();
_tree_node_stack.pop ();
prev_ = temp_;
const std::size_t count_ = max_ - min_;
for (std::size_t i_ = 1; i_ < count_; ++i_)
{
node *temp_ = prev_->copy (_node_ptr_vector);
curr_ = temp_;
_tree_node_stack.push (static_cast<node *>(0));
_tree_node_stack.top () = prev_;
sequence ();
prev_ = curr_;
}
}
else
{
_tree_node_stack.push (static_cast<node *>(0));
_tree_node_stack.top () = prev_;
zero_or_more (greedy_);
node *temp_ = _tree_node_stack.top ();
prev_ = temp_;
_tree_node_stack.pop ();
}
}
_tree_node_stack.push (static_cast<node *>(0));
_tree_node_stack.top () = prev_;
sequence ();
}
}
void fixup_bol (node * &root_)const
{
typename node::node_vector *first_ = &root_->firstpos ();
bool found_ = false;
typename node::node_vector::const_iterator iter_ =
first_->begin ();
typename node::node_vector::const_iterator end_ =
first_->end ();
for (; iter_ != end_; ++iter_)
{
const node *node_ = *iter_;
found_ = !node_->end_state () && node_->token () == bol_token ();
if (found_) break;
}
if (!found_)
{
_node_ptr_vector->push_back (static_cast<leaf_node *>(0));
_node_ptr_vector->back () = new leaf_node (bol_token (), true);
node *lhs_ = _node_ptr_vector->back ();
_node_ptr_vector->push_back (static_cast<leaf_node *>(0));
_node_ptr_vector->back () = new leaf_node
(node::null_token (), true);
node *rhs_ = _node_ptr_vector->back ();
_node_ptr_vector->push_back (static_cast<selection_node *>(0));
_node_ptr_vector->back () = new selection_node (lhs_, rhs_);
lhs_ = _node_ptr_vector->back ();
_node_ptr_vector->push_back (static_cast<sequence_node *>(0));
_node_ptr_vector->back () = new sequence_node (lhs_, root_);
root_ = _node_ptr_vector->back ();
}
}
};
}
}
#endif
|