libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
error-test.cc
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2002,2003 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26// (c) COPYRIGHT URI/MIT 1996
27// Please read the full copyright statement in the file COPYRIGHT_URI.
28//
29// Authors:
30// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31
32// Test the Error object scanner, parser and class.
33//
34// jhrg 4/25/96
35
36#include "config.h"
37
38static char rcsid[] not_used = {"$Id$"};
39
40#include <GetOpt.h>
41
42#include "Error.h"
43#include "Error.tab.hh"
44#include "parser.h"
45
46void test_scanner();
47void test_parser(Error &err);
48#ifdef GUI
49void test_object(Error &err);
50#endif
51void usage();
52
53int Errorlex();
54// int Errorparse(parser_arg *);
55
56extern YYSTYPE Errorlval;
57extern int Errordebug;
58const char *prompt = "error-test: ";
59
60#ifdef WIN32
61void
62#else
63int
64#endif
65main(int argc, char *argv[])
66{
67#ifdef WIN32
68 GetOpt getopt(argc, argv, "spd");
69#else
70 GetOpt getopt(argc, argv, "spdo");
71#endif
72 int option_char;
73 bool scanner_test = false, parser_test = false, object_test = false;
74
75 // process options
76
77 while ((option_char = getopt()) != EOF)
78 switch (option_char) {
79 case 'd':
80 Errordebug = 1;
81 break;
82 case 's':
83 scanner_test = true;
84 break;
85 case 'p':
86 parser_test = true;
87 break;
88#ifndef WIN32
89 case 'o':
90 parser_test = object_test = true;
91 break;
92#endif
93 case '?':
94 default:
95 usage();
96 }
97
98#ifdef WIN32
99 if (!(scanner_test || parser_test))
100#else
101 if (!(scanner_test || parser_test || object_test))
102#endif
103 usage();
104
105 if (scanner_test)
106 test_scanner();
107
108 Error err;
109 if (parser_test)
110 test_parser(err);
111
112#ifdef GUI
113 if (object_test)
114 test_object(err);
115#endif
116
117#ifdef WIN32
118 exit(0); // Cygwin/Dejagu test suites require this to succeed.
119 return; // Visual C++ requires this.
120#endif
121}
122
123void usage() {
124#ifdef WIN32
125 fprintf(stderr, "usage: error-test: [d][sp] < filename ...\n");
126#else
127 fprintf(stderr, "usage: error-test: [d][spo] < filename ...\n");
128#endif
129 fprintf(stderr, " d: extra parser debugging information\n");
130 fprintf(stderr, " s: run the scanner\n");
131 fprintf(stderr, " p: run the parser\n");
132#ifdef WIN32
133 fprintf(stderr, " o: evaluate the object, runs the parser\n");
134#endif
135}
136
137void test_scanner() {
138 int tok;
139
140 fprintf(stdout, "%s", prompt); // first prompt
141 fflush(stdout);
142 while ((tok = Errorlex())) {
143 switch (tok) {
144 case SCAN_ERROR:
145 fprintf(stdout, "ERROR\n");
146 break;
147 case SCAN_CODE:
148 fprintf(stdout, "CODE\n");
149 break;
150 case SCAN_PTYPE:
151 fprintf(stdout, "PTYPE\n");
152 break;
153 case SCAN_MSG:
154 fprintf(stdout, "MSG\n");
155 break;
156 case SCAN_PROGRAM:
157 fprintf(stdout, "PROGRAM\n");
158 break;
159 case SCAN_STR:
160 fprintf(stdout, "%s\n", Errorlval.string);
161 break;
162 case SCAN_INT:
163 fprintf(stdout, "%d\n", Errorlval.integer);
164 break;
165 case '{':
166 fprintf(stdout, "Left Brace\n");
167 break;
168 case '}':
169 fprintf(stdout, "Right Brace\n");
170 break;
171 case ';':
172 fprintf(stdout, "Semicolon\n");
173 break;
174 case '=':
175 fprintf(stdout, "Assignment\n");
176 break;
177 default:
178 fprintf(stdout, "Error: Unrecognized input\n");
179 }
180 fprintf(stdout, "%s", prompt); // print prompt after output
181 fflush(stdout);
182 }
183}
184
185void test_parser(Error &err) {
186 int status = err.parse(stdin);
187 fprintf(stdout, "Status from parser: %d\n", status);
188
189 if (err.OK())
190 fprintf(stdout, "Error passed OK check\n");
191 else
192 fprintf(stdout, "Error failed OK check\n");
193
194 err.print(stdout);
195}