56e373cfdec96eabdcb42900040d5696485f5e17
[platform/upstream/libzypp.git] / devel / devel.ma / Parse.cc
1 //http://www.boost.org/libs/libraries.htm
2 #include <iostream>
3 #include <list>
4 #include <string>
5
6 #include <zypp/base/Logger.h>
7 #include <zypp/base/Exception.h>
8 #include <zypp/base/PtrTypes.h>
9 #include <zypp/parser/tagfile/Tags.h>
10
11 ///////////////////////////////////////////////////////////////////
12 namespace zypp
13 { /////////////////////////////////////////////////////////////////
14   ///////////////////////////////////////////////////////////////////
15   namespace parser
16   { /////////////////////////////////////////////////////////////////
17     ///////////////////////////////////////////////////////////////////
18     namespace tagfile
19     { /////////////////////////////////////////////////////////////////
20
21       void echoOn( std::ostream & str,
22                    iterator_t first, const iterator_t last,
23                    const char* s = "" )
24       {
25         //return;
26         str << first.get_position() << last.get_position();
27         str << s << ">>";
28         while ( first != last )
29           str << *first++;
30         str << "<< " << std::endl;
31       }
32
33       void echo( iterator_t first, const iterator_t last, const char* s = "" )
34       {
35         echoOn( DBG, first, last, s );
36       }
37       void mecho( iterator_t first, const iterator_t last, const char* s = "" )
38       {
39         echoOn( MIL, first, last, s );
40       }
41
42       /////////////////////////////////////////////////////////////////
43     } // namespace tagfile
44     ///////////////////////////////////////////////////////////////////
45     /////////////////////////////////////////////////////////////////
46   } // namespace parser
47   ///////////////////////////////////////////////////////////////////
48   /////////////////////////////////////////////////////////////////
49 } // namespace zypp
50 ///////////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////////////////////
52 //
53 //  Types
54 //
55 ////////////////////////////////////////////////////////////////////////////
56 using std::endl;
57 using std::list;
58 using std::string;
59 using namespace zypp;
60 using namespace zypp::parser::tagfile;
61 typedef scanner<iterator_t>            scanner_t;
62 typedef rule<scanner_t>                rule_t;
63 ////////////////////////////////////////////////////////////////////////////
64 //
65 //  Just for the stats
66 //
67 ////////////////////////////////////////////////////////////////////////////
68 struct Measure
69 {
70   time_t _begin;
71   Measure()
72   : _begin( time(NULL) )
73   {
74     USR << "START MEASURE..." << endl;
75   }
76   ~Measure()
77   {
78     USR << "DURATION: " << (time(NULL)-_begin) << " sec." << endl;
79   }
80 };
81 ////////////////////////////////////////////////////////////////////////////
82 //
83 //  Main
84 //
85 ////////////////////////////////////////////////////////////////////////////
86 int main( int argc, char* argv[] )
87 {
88   INT << "===[START]==========================================" << endl;
89   string infile( "p" );
90   if (argc >= 2 )
91     infile = argv[1];
92
93   // Create a file iterator for this file
94   fiterator_t first(infile);
95   if (!first)
96     {
97       ERR << "Unable to open file!\n";
98       return -1;
99     }
100   // Create an EOF iterator
101   fiterator_t last = first.make_end();
102
103   // Create position iterators
104   iterator_t begin( first, last, infile );
105   iterator_t end;
106
107   // Result var
108   SingleTag stag;
109   MultiTag  mtag;
110   STag      stagData;
111   MTag      mtagData;
112
113   rule_t file =   end_p
114                 | ( stag
115                   | mtag
116                   | ( *blank_p
117                       >> !( ch_p('#')
118                             >> *(anychar_p - eol_p)
119                           )
120                       >> (eol_p|end_p)
121                     | error_report_p( "neither empty nor comment" )
122                     )
123                   )
124                   >> file;
125
126   // Parse
127   shared_ptr<Measure> duration( new Measure );
128   parse_info<iterator_t> info
129     = parse( begin, end, file );
130   duration.reset();
131
132   // Check for fail...
133   if ( info.full )
134     USR << "Parse succeeded!\n";
135   else if ( info.hit )
136     {
137       ERR << "Parse partial!\n";
138       ERR << " at pos " << info.length << endl;
139     }
140   else
141     {
142       ERR << "Parse failed!\n";
143       ERR << " at pos " << info.length << endl;
144     }
145
146   INT << "===[END]============================================" << endl;
147   return 0;
148 }