1 /*===========================================================================
2 *
3 * PUBLIC DOMAIN NOTICE
4 * National Center for Biotechnology Information
5 *
6 * This software/database is a "United States Government Work" under the
7 * terms of the United States Copyright Act. It was written as part of
8 * the author's official duties as a United States Government employee and
9 * thus cannot be copyrighted. This software/database is freely available
10 * to the public for use. The National Library of Medicine and the U.S.
11 * Government have not placed any restriction on its use or reproduction.
12 *
13 * Although all reasonable efforts have been taken to ensure the accuracy
14 * and reliability of the software and data, the NLM and the U.S.
15 * Government do not and cannot warrant the performance or results that
16 * may be obtained by using this software or data. The NLM and the U.S.
17 * Government disclaim all warranties, express or implied, including
18 * warranties of performance, merchantability or fitness for any particular
19 * purpose.
20 *
21 * Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26
27 /**
28 * Command line test for schema parser.
29 * Parses the input files and reports errors
30 */
31
32 #include "../../libs/schema/SchemaParser.hpp"
33 #include "../../libs/schema/ParseTree.hpp"
34
35 using namespace std;
36 using namespace ncbi::SchemaParser;
37
38 //////////////////////////////////////////// Main
39 #include <kapp/args.h>
40 #include <kfg/config.h>
41 #include <klib/out.h>
42
43 #include <iostream>
44 #include <fstream>
45 #include <sstream>
46 #include <stdexcept>
47
48 static
49 void
PrintParseTree(const ParseTree * p_t,ostream & p_out)50 PrintParseTree ( const ParseTree * p_t, ostream& p_out )
51 {
52 if ( p_t == 0 )
53 {
54 p_out << " NULL";
55 return;
56 }
57
58 p_out << p_t -> GetToken () . GetLeadingWhitespace () << p_t -> GetToken () . GetValue ();
59
60 for ( uint32_t i = 0 ; i < p_t -> ChildrenCount (); ++ i )
61 {
62 PrintParseTree ( p_t -> GetChild ( i ), p_out );
63 }
64 }
65
66 static
67 bool
MatchStrings(const string & p_source,const string p_print)68 MatchStrings ( const string& p_source, const string p_print )
69 {
70 const size_t Context = 20;
71 for ( size_t i = 0; i < p_source . length (); ++ i )
72 {
73 if ( i >= p_print . length () )
74 {
75 cout << "premature end of print after '" << p_source . substr ( i > Context ? i - Context : 0 ) << "', " << ( i - p_print . length () + 1 ) << " character(s) missing" << endl;
76 return false;
77 }
78 if ( p_source [ i ] != p_print [ i ] )
79 {
80 cout << "mismatch at " << i << ", after " << p_source . substr ( i > Context ? i - Context : 0, Context ) << endl;
81 cout << "source: '" << p_source . substr ( i, Context ) << "'" << endl;
82 cout << "print : '" << p_print . substr ( i, Context ) << "'" << endl;
83 return false;
84 }
85 }
86 if ( p_print . length () > p_source . length () )
87 {
88 cout << "extra characters printed: " << p_print . substr ( p_source . length () ) << endl;
89 return false;
90 }
91 return true;
92 }
93
94 extern "C"
95 {
96
KAppVersion(void)97 ver_t CC KAppVersion ( void )
98 {
99 return 0x1000000;
100 }
101
102 const char UsageDefaultName[] = "test-schema-parse";
103
UsageSummary(const char * progname)104 rc_t CC UsageSummary (const char * progname)
105 {
106 return KOutMsg ( "Usage:\n" "\t%s [options] schema-file ... \n\n", progname );
107 }
108
Usage(const Args * args)109 rc_t CC Usage( const Args* args )
110 {
111 return 0;
112 }
113
KMain(int argc,char * argv[])114 rc_t CC KMain ( int argc, char *argv [] )
115 {
116 int failed = 0;
117 if ( argc < 2 )
118 {
119 cout << "Usage:\n\t" << argv[0] << " schema-file" << endl;
120 return 1;
121 }
122 try
123 {
124 cout << "Parsing " << argc - 1 << " schema-files" << endl;
125 for ( int i = 0 ; i < argc - 1; ++i )
126 {
127 stringstream buffer;
128 ifstream in ( argv [ i + 1 ] );
129 if ( ! in . good () )
130 {
131 throw runtime_error ( string ( "Invalid file " ) + argv [ i + 1 ] );
132 }
133 buffer << in.rdbuf();
134 SchemaParser parser;
135 if ( ! parser . ParseString ( buffer . str () . c_str () ) )
136 {
137 cout << string ( "Parsing failed: " ) + argv [ i + 1 ] << endl;
138 ++ failed;
139 }
140
141 ostringstream out;
142 PrintParseTree ( parser . GetParseTree (), out );
143 if ( ! MatchStrings ( buffer . str (), out . str () ) )
144 {
145 cout << string ( "Printout mismatch: " ) + argv [ i + 1 ] << endl;
146 ++ failed;
147 }
148 }
149 cout << "Failed: " << failed << endl;
150 }
151 catch ( exception& ex)
152 {
153 cerr << " Exception: " << ex . what () << endl;
154 return 2;
155 }
156 catch ( ... )
157 {
158 cerr << " Unknown exception" << endl;
159 return 3;
160 }
161 return failed == 0 ? 0 : 4;
162 }
163
164 }
165
166