From: jbandela_at_[hidden]
Date: 2001-06-06 22:17:33


I have modified the Spirit example feat.cpp to use TYPEOF instead of
Rule<>. All the code compiled and ran except for the last test. The
last type generates more than 20 "entities/sizes" (see
implementation, especially Sizes. Right now Typeof implementation
supports only 20 (though that can be increased).
Typeof.h comes from the TYPEOF directectory in boost files
The following code compiles and runs under gcc 2.95.2
Let me know what you think

John R. Bandela

#include <Spirit.h>
#include <iostream>
#include<Typeof.h>
//////////////////////////////////////////////////////////////////////
///////////////////
using std::cout;
using std::cin;
using std::endl;

using Spirit::Scanner;
using Spirit::space;
using Spirit::Rule;
using Spirit::StrLit;
using Spirit::NCStrLit;
using Spirit::NCRange;
using Spirit::Range;
using Spirit::ChLit;
using Spirit::NCChLit;
using Spirit::anychar;
using Spirit::xdigit;
using Spirit::digit;
using Spirit::alpha;
using Spirit::more;
using Spirit::Longest;

using namespace Spirit;
//Typeofs
JRB_TYPE2(Scanner)
JRB_TYPE1(Parser)
JRB_TYPE1(Rule)
JRB_TYPE1(StrLit)
JRB_TYPE1(NCStrLit)
JRB_TYPE1(NCRange)
JRB_TYPE1(Range)
JRB_TYPE1(ChLit)
JRB_TYPE1(NCChLit)
JRB_TYPE0(AnyChar_)
JRB_TYPE0(Nothing_)
JRB_TYPE0(Epsilon_)
JRB_TYPE0(AlNum_)
JRB_TYPE0(Alpha_)
JRB_TYPE0(Cntrl_)
JRB_TYPE0(Digit_)
JRB_TYPE0(Graph_)
JRB_TYPE0(Lower_)
JRB_TYPE0(Print_)
JRB_TYPE0(Punct_)
JRB_TYPE0(Space_)
JRB_TYPE0(Upper_)
JRB_TYPE0(XDigit_)
JRB_TYPE2(Sequence)
JRB_TYPE1(Positive)
JRB_TYPE1(CharTest)
JRB_TYPE1(KleeneStar)
JRB_TYPE2(Alternative)
JRB_TYPE2(Difference)
JRB_TYPE1(Optional)
JRB_TYPE2(Intersection)
JRB_TYPE2(Xor)
JRB_TYPE1(FixedIterator)
JRB_TYPE1(FiniteIterator)
JRB_TYPE1(InfiniteIterator)
JRB_TYPE2(LongestAlt)

//////////////////////////////////////////////////////////////////////
///////////////////
static void
Parse(Rule<> const& rule, char const* expr)
{
        Scanner<> scan(expr, space);
        if (rule.Parse(scan) && !*scan)
        {
                cout << "\t\t" << expr << "\tPass\n";
        }
        else
        {
                cout << "\t\t" << expr << "\tFail\n";
                cout << "\t\t";
                for (int i = 0; i < (&*scan - expr); i++)
                        cout << " ";
                cout << "^\n";
        }
}

//////////////////////////////////////////////////////////////////////
///////////////////
int
main()
{
        cout
<< "/////////////////////////////////////////////////////////\n\n";
        cout << "\t\tPattern Test For Spirit...\n\n";
        cout
<< "/////////////////////////////////////////////////////////\n\n";

        cout << "strings\n\n";
// Rule<> str = StrLit<>("joel");
        
        TYPEOF( StrLit<>("joel") ) str = StrLit<>("joel") ;
        
        Parse(str, "joel");
        Parse(str, "joelo");
        Parse(str, "joal");
        Parse(str, "joe");
        cout << "\n";

        cout << "no case strings\n\n";
// Rule<> ncstr = NCStrLit<>("joel");
        
        TYPEOF(NCStrLit<>("joel")) ncstr = NCStrLit<>("joel");
        
        Parse(ncstr, "joel");
        Parse(ncstr, "JOEL");
        Parse(ncstr, "joAl");
        Parse(ncstr, "JoEl");
        cout << "\n";

        cout << "any >> string\n\n";
// Rule<> any = anychar >> StrLit<>("art");
        
        TYPEOF(anychar >> StrLit<>("art")) any = anychar >> StrLit<>
("art");
        
        Parse(any, "dart");
        Parse(any, "cart");
        Parse(any, "tart");
        Parse(any, "art");
        Parse(any, "hurt");
        Parse(any, "dark");
        cout << "\n";

        cout << "one or more\n\n";
// Rule<> onem = +ChLit<>('a') >> 'b';
        
        TYPEOF(+ChLit<>('a') >> 'b') onem = +ChLit<>('a') >> 'b';
        
        Parse(onem, "ab");
        Parse(onem, "aaab");
        Parse(onem, "b");
        Parse(onem, "baaa");
        cout << "\n";

        cout << "zero or more\n";
// Rule<> zerom = *ChLit<>('a') >> 'b';
        
        TYPEOF(*ChLit<>('a') >> 'b') zerom = *ChLit<>('a') >> 'b';
        
        Parse(zerom, "b");
        Parse(zerom, "ab");
        Parse(zerom, "aaab");
        Parse(zerom, "baaa");
        cout << "\n";

        cout << "repeat\n";
// Rule<> rep1 = +(NCRange<>('a','z') | Range<>
('0','9'));
        
        TYPEOF(+(NCRange<>('a','z') | Range<>('0','9'))) rep1
                = +(NCRange<>('a','z') | Range<>('0','9'));
        
// Rule<> rep2 = +(StrLit<>("abc"));
        
        TYPEOF(+(StrLit<>("abc"))) rep2 = +(StrLit<>("abc"));
        
        Parse(rep1, "aaaaaa");
        Parse(rep2, "abcabcabcabc");
        Parse(rep2, "abcabcabcabcc");
        cout << "\n";

        cout << "any and repeat\n";
        
        
// Rule<> anyrep = *StrLit<>("fo") >> *ChLit<>('o') >> *
(anychar - '\0');
        
        TYPEOF(*StrLit<>("fo") >> *ChLit<>('o') >> *(anychar - '\0'))
anyrep
                = *StrLit<>("fo") >> *ChLit<>('o') >> *(anychar -
 '\0');
        
        Parse(anyrep, "fo");
        Parse(anyrep, "foo");
        Parse(anyrep, "fooo");
        Parse(anyrep, "foobar");
        Parse(anyrep, "foox");
        cout << "\n";

        cout << "sequence\n";
// Rule<> seq = *NCStrLit<>("0x") >> +xdigit;
        
        TYPEOF(*NCStrLit<>("0x") >> +xdigit) seq = *NCStrLit<>("0x")
>> +xdigit;
        
        Parse(seq, "0x3FFF");
        Parse(seq, "0x3GFF");
        cout << "\n";

        cout << "optional\n";
        
// Rule<> opt = +digit >> '.' >> +digit >> !(NCChLit<>
('e') >> +digit);
        
        TYPEOF(+digit >> '.' >> +digit >> !(NCChLit<>('e') >>
+digit)) opt
                = +digit >> '.' >> +digit >> !(NCChLit<>('e') >>
+digit);
        
        Parse(opt, "3.666");
        Parse(opt, "3.666e25");
        Parse(opt, "3.6A6");
        cout << "\n";

        cout << "or\n";
// Rule<> alt = StrLit<>("joel") | StrLit<>("mariel");
        
        TYPEOF(StrLit<>("joel") | StrLit<>("mariel")) alt
                 = StrLit<>("joel") | StrLit<>("mariel");
        
        
        Parse(alt, "joel");
        Parse(alt, "mariel");
        Parse(alt, "tenji");
        cout << "\n";

        cout << "and\n";
// Rule<> and_ = +((anychar - '\0') & 'x');
        
        TYPEOF(+((anychar - '\0') & 'x')) and_ = +((anychar - '\0')
& 'x');
        
        
        Parse(and_, "xxxxxxxxxx");
        Parse(and_, "xxxxxaxxxx");
        Parse(and_, "aaaaaaa");
        cout << "\n";

        cout << "diff\n";
// Rule<> diff = +(alpha - 'a');
        TYPEOF(+(alpha - 'a')) diff = +(alpha - 'a');
        
        
        Parse(diff, "abcdefg");
        Parse(diff, "bcdefgh");
        cout << "\n";

        cout << "xor\n";
// Rule<> xor_ = alpha ^ xdigit;

        TYPEOF(alpha ^ xdigit) xor_ = alpha ^ xdigit;
        
        
        Parse(xor_, "0");
        Parse(xor_, "1");
        Parse(xor_, "2");
        Parse(xor_, "3");
        Parse(xor_, "4");
        Parse(xor_, "5");
        Parse(xor_, "6");
        Parse(xor_, "7");
        Parse(xor_, "8");
        Parse(xor_, "9");
        Parse(xor_, "a");
        Parse(xor_, "b");
        Parse(xor_, "c");
        Parse(xor_, "d");
        Parse(xor_, "e");
        Parse(xor_, "f");
        Parse(xor_, "g");
        Parse(xor_, "h");
        Parse(xor_, "i");
        cout << "\n";
        
        cout << "repeat exact 8\n";
// Rule<> rep8 = alpha.Repeat(8);
        
        TYPEOF(alpha.Repeat(8)) rep8 = alpha.Repeat(8);
        
        Parse(rep8, "abcdefg");
        Parse(rep8, "abcdefgh");
        Parse(rep8, "abcdefghi");
        Parse(rep8, "abcdefg");
        Parse(rep8, "a");

        cout << "repeat 2 to 8\n";
// Rule<> rep28 = alpha.Repeat(2, 8);
        
        TYPEOF(alpha.Repeat(2, 8)) rep28 = alpha.Repeat(2, 8);
        
        Parse(rep28, "abcdefg");
        Parse(rep28, "abcdefgh");
        Parse(rep28, "abcdefghi");
        Parse(rep28, "abcdefg");
        Parse(rep28, "a");

        cout << "repeat 2 or more\n";
// Rule<> rep2_ = alpha.Repeat(2, more);
        
        TYPEOF(alpha.Repeat(2, more)) rep2_ = alpha.Repeat(2, more);
        
        Parse(rep2_, "abcdefg");
        Parse(rep2_, "abcdefgh");
        Parse(rep2_, "abcdefghi");
        Parse(rep2_, "abcdefg");
        Parse(rep2_, "a");

        cout << "longest parse\n";
        
        
// Rule<> real = +digit >> '.' >> +digit;
        
        TYPEOF(+digit >> '.' >> +digit) real = +digit >> '.' >>
+digit;
        
// Rule<> integ = +digit;
        
        TYPEOF(+digit) integ = +digit;
        
// Rule<> num = Longest[ integ | real ];
        
        TYPEOF(Longest[ integ | real ]) num = Longest[ integ | real ];
        
        Parse(num, "123456");
        Parse(num, "456.1245");
        Parse(num, "123456A");

/* cout << "Back Tracking\n";
        Rule<> b = ChLit<>('B');
        
        TYPEOF(ChLit<>('B')) b_to = ChLit<>('B');
        
        Rule<> a = ChLit<>('A') >> ( ChLit<>('B') | 'D' ) >> 'E'
>> 'F' >> 'H'
                        | 'A' >> b >> 'E' >> 'G' >> 'H';

        TYPEOF(ChLit<>('A') >> ( ChLit<>('B') | 'D' ) >> 'E' >> 'F'
>> 'H'
                        | 'A' >> b >> 'E' >> 'G' >> 'H') a_to =
                        ChLit<>('A') >> ( ChLit<>('B') | 'D' ) >> 'E'
>> 'F' >> 'H'
                        | 'A' >> b >> 'E' >> 'G' >> 'H';
        
        cout << "\tShould parse OK...\n";
        Parse(a, "ABEFH");
        Parse(a, "ADEFH");
        Parse(a, "ABEGH");
        cout << "\n";

        cout << "\tShould fail parsing...\n";
        Parse(a, "AEFH");
        Parse(a, "ADEGH");
        
        */
}