libdap  Updated for version 3.20.6
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 
38 static char rcsid[] not_used =
39  {"$Id$"
40  };
41 
42 #include <GetOpt.h>
43 
44 #include "Error.h"
45 #include "parser.h"
46 #include "Error.tab.hh"
47 
48 void test_scanner();
49 void test_parser(Error &err);
50 #ifdef GUI
51 void test_object(Error &err);
52 #endif
53 void usage();
54 
55 int Errorlex();
56 //int Errorparse(parser_arg *);
57 
58 extern YYSTYPE Errorlval;
59 extern int Errordebug;
60 const char *prompt = "error-test: ";
61 
62 #ifdef WIN32
63 void
64 #else
65 int
66 #endif
67 main(int argc, char *argv[])
68 {
69 #ifdef WIN32
70  GetOpt getopt(argc, argv, "spd");
71 #else
72  GetOpt getopt(argc, argv, "spdo");
73 #endif
74  int option_char;
75  bool scanner_test = false, parser_test = false, object_test = false;
76 
77  // process options
78 
79  while ((option_char = getopt()) != EOF)
80  switch (option_char) {
81  case 'd':
82  Errordebug = 1;
83  break;
84  case 's':
85  scanner_test = true;
86  break;
87  case 'p':
88  parser_test = true;
89  break;
90 #ifndef WIN32
91  case 'o':
92  parser_test = object_test = true;
93  break;
94 #endif
95  case '?':
96  default:
97  usage();
98  }
99 
100 #ifdef WIN32
101  if (!(scanner_test || parser_test))
102 #else
103  if (!(scanner_test || parser_test || object_test))
104 #endif
105  usage();
106 
107  if (scanner_test)
108  test_scanner();
109 
110  Error err;
111  if (parser_test)
112  test_parser(err);
113 
114 #ifdef GUI
115  if (object_test)
116  test_object(err);
117 #endif
118 
119 #ifdef WIN32
120  exit(0); // Cygwin/Dejagu test suites require this to succeed.
121  return; // Visual C++ requires this.
122 #endif
123 }
124 
125 void
126 usage()
127 {
128 #ifdef WIN32
129  fprintf(stderr, "usage: error-test: [d][sp] < filename ...\n") ;
130 #else
131  fprintf(stderr, "usage: error-test: [d][spo] < filename ...\n") ;
132 #endif
133  fprintf(stderr, " d: extra parser debugging information\n") ;
134  fprintf(stderr, " s: run the scanner\n") ;
135  fprintf(stderr, " p: run the parser\n") ;
136 #ifdef WIN32
137  fprintf(stderr, " o: evaluate the object, runs the parser\n") ;
138 #endif
139 }
140 
141 void
142 test_scanner()
143 {
144  int tok;
145 
146  fprintf(stdout, "%s", prompt) ; // first prompt
147  fflush(stdout) ;
148  while ((tok = Errorlex())) {
149  switch (tok) {
150  case SCAN_ERROR:
151  fprintf(stdout, "ERROR\n") ;
152  break;
153  case SCAN_CODE:
154  fprintf(stdout, "CODE\n") ;
155  break;
156  case SCAN_PTYPE:
157  fprintf(stdout, "PTYPE\n") ;
158  break;
159  case SCAN_MSG:
160  fprintf(stdout, "MSG\n") ;
161  break;
162  case SCAN_PROGRAM:
163  fprintf(stdout, "PROGRAM\n") ;
164  break;
165  case SCAN_STR:
166  fprintf(stdout, "%s\n", Errorlval.string) ;
167  break;
168  case SCAN_INT:
169  fprintf(stdout, "%d\n", Errorlval.integer) ;
170  break;
171  case '{':
172  fprintf(stdout, "Left Brace\n") ;
173  break;
174  case '}':
175  fprintf(stdout, "Right Brace\n") ;
176  break;
177  case ';':
178  fprintf(stdout, "Semicolon\n") ;
179  break;
180  case '=':
181  fprintf(stdout, "Assignment\n") ;
182  break;
183  default:
184  fprintf(stdout, "Error: Unrecognized input\n") ;
185  }
186  fprintf(stdout, "%s", prompt) ; // print prompt after output
187  fflush(stdout) ;
188  }
189 }
190 
191 void
192 test_parser(Error &err)
193 {
194  int status = err.parse(stdin);
195  fprintf(stdout, "Status from parser: %d\n", status) ;
196 
197  if (err.OK())
198  fprintf(stdout, "Error passed OK check\n") ;
199  else
200  fprintf(stdout, "Error failed OK check\n") ;
201 
202  err.print(stdout);
203 }
Definition: GetOpt.h:38