Aprepro  5.0x
aprepro.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2017 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 //
17 // * Neither the name of NTESS nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 
34 // Might be good to add a callback function which would be called
35 // when there was output -- In LexerOutput for example. Default
36 // could be to just write to std::cout or to resultsOutput stringstream...
37 
38 #ifndef SEAMS_DRIVER_H
39 #define SEAMS_DRIVER_H
40 
41 #include <cstdlib>
42 #include <iostream>
43 #include <ostream>
44 #include <sstream>
45 #include <stack>
46 #include <string>
47 #include <vector>
48 
49 #include <cmath>
50 #ifndef math_errhandling
51 #define math_errhandling MATH_ERRNO
52 #endif
53 
54 /** The SEAMS namespace is used to encapsulate the three parser classes
55  * SEAMS::Parser, SEAMS::Scanner and SEAMS::Aprepro */
56 namespace SEAMS {
57 
58  struct array
59  {
60  std::vector<double> data;
61  int rows{0};
62  int cols{0};
63 
64  array(int r, int c) : rows(r), cols(c) { data.resize(r * c); }
65  array() = default;
66  ~array() = default;
67  };
68 
69  struct symrec
70  {
71  std::string name;
72  std::string syntax;
73  std::string info;
74  int type;
75  bool isInternal;
76  struct value
77  {
78  double var{0};
79  double (*fnctptr)(){nullptr};
80  double (*fnctptr_d)(double){nullptr};
81  double (*fnctptr_c)(char *){nullptr};
82  double (*fnctptr_dc)(double, char *){nullptr};
83  double (*fnctptr_cd)(char *, double){nullptr};
84  double (*fnctptr_cc)(char *, char *){nullptr};
85  double (*fnctptr_dd)(double, double){nullptr};
86  double (*fnctptr_ddd)(double, double, double){nullptr};
87  double (*fnctptr_ccc)(char *, char *, char *){nullptr};
88  double (*fnctptr_ccd)(char *, char *, double){nullptr};
89  double (*fnctptr_dddd)(double, double, double, double){nullptr};
90  double (*fnctptr_ddddc)(double, double, double, double, char *){nullptr};
91  double (*fnctptr_dddddd)(double, double, double, double, double, double){nullptr};
92  double (*fnctptr_a)(const array *){nullptr};
93  std::string svar;
94  const char *(*strfnct)(){nullptr};
95  const char *(*strfnct_c)(char *){nullptr};
96  const char *(*strfnct_d)(double){nullptr};
97  const char *(*strfnct_a)(const array *){nullptr};
98  const char *(*strfnct_dd)(double, double){nullptr};
99  const char *(*strfnct_cc)(char *, char *){nullptr};
100  const char *(*strfnct_ccc)(char *, char *, char *){nullptr};
101  const char *(*strfnct_dcc)(double, char *, char *){nullptr};
102  const char *(*strfnct_dcccc)(double, char *, char *, char *, char *){nullptr};
103  array *avar{nullptr}; /* Array Variable */
104  array *(*arrfnct_c)(const char *){nullptr};
105  array *(*arrfnct_cc)(const char *, const char *){nullptr};
106  array *(*arrfnct_cd)(const char *, double){nullptr};
107  array *(*arrfnct_ddd)(double, double, double){nullptr};
108  array *(*arrfnct_dd)(double, double){nullptr};
109  array *(*arrfnct_d)(double){nullptr};
110  array *(*arrfnct_a)(const array *){nullptr};
111 
112  value() = default;
113  } value;
115 
116  symrec(const char *my_name, int my_type, bool is_internal = false)
117  : name(my_name), syntax(my_name), info("UNDEFINED"), type(my_type), isInternal(is_internal),
118  next(nullptr)
119  {
120  value.var = 0;
121  }
122 
123  symrec(const std::string &my_name, int my_type, bool is_internal = false)
124  : name(my_name), syntax(my_name), info("UNDEFINED"), type(my_type), isInternal(is_internal),
125  next(nullptr)
126  {
127  value.var = 0;
128  }
129  };
130 
131  /* Global options */
133  {
134  std::string include_path;
135  std::string include_file;
136  bool end_on_exit{false};
137  bool warning_msg{true};
138  bool info_msg{false};
139  bool debugging{false};
140  bool dumpvars{false};
141  bool interactive{false};
142  bool immutable{false};
143  bool trace_parsing{false}; // enable debug output in the bison parser
144  bool one_based_index{false};
145  bool keep_history{false}; // Flag to keep a history of Aprepro substitutions
146  aprepro_options() = default;
147  };
148 
149  /* Structure for holding file names and line counters */
150  struct file_rec
151  {
152  std::string name{"STDIN"};
153  int lineno{0};
154  int loop_count{0};
155  bool tmp_file{false};
156 
157  file_rec(const char *my_name, int line_num, bool is_temp, int loop_cnt)
158  : name(my_name), lineno(line_num), loop_count(loop_cnt), tmp_file(is_temp)
159  {
160  }
161  file_rec() = default;
162  };
163 
164  /* Structure for holding aprepro substitution info */
166  {
167  std::string original;
168  std::string substitution;
169  std::streampos index; // Character index in the output where the substitution begins.
170  };
171 
172  /** The Aprepro class brings together all components. It creates an instance of
173  * the Parser and Scanner classes and connects them. Then the input stream is
174  * fed into the scanner object and the parser gets it's token
175  * sequence. Furthermore the aprepro object is available in the grammar rules as
176  * a parameter. Therefore the aprepro class contains a reference to the
177  * structure into which the parsed data is saved. */
178  class Aprepro
179  {
180  public:
181  /// construct a new parser aprepro context
182  Aprepro();
183  ~Aprepro();
184 
185  enum class SYMBOL_TYPE {
186  VARIABLE = 1,
187  STRING_VARIABLE = 2,
188  UNDEFINED_VARIABLE = 5,
189  FUNCTION = 3,
190  STRING_FUNCTION = 4,
191  ARRAY_FUNCTION = 6,
192  ARRAY_VARIABLE = 7,
193  IMMUTABLE_VARIABLE = 11,
195  };
196 
197  bool state_is_immutable() const { return stateImmutable; }
198 
199  /** Return an std::ostringstream reference to get the results of
200  the parse_* call (* = stream, file, or string).
201  */
202  const std::ostringstream &parsing_results() const { return parsingResults; }
203  void clear_results();
204 
205  /** Return string representation of current version of aprepro. */
206  std::string version() const;
207 
208  /** Invoke the scanner and parser for a stream.
209  * @param in input stream
210  * @param in_name stream name for error messages
211  * @return true if successfully parsed
212  */
213  bool parse_stream(std::istream &in, const std::string &in_name = "stream input");
214 
215  /** Invoke the scanner and parser on an input string.
216  * @param input input string
217  * @param sname stream name for error messages
218  * @return true if successfully parsed
219  */
220  bool parse_string(const std::string &input, const std::string &sname = "string stream");
221 
222  /** Invoke the scanner and parser on a vector of strings.
223  * @param input vector of input strings
224  * @param sname stream name for error messages
225  * @return true if successfully parsed
226  */
227  bool parse_strings(const std::vector<std::string> &input, const std::string &sname);
228 
229  /** Invoke the scanner and parser on an input string in an
230  * interactive manner.
231  * @param input input stringInput
232  * @return true if successfully parsed
233  */
234  bool parse_string_interactive(const std::string &input);
235 
236  /** Get the string interactive flag, which indicates if we are in
237  * the middle of parsing a string in an interactive manner.
238  */
240 
241  /** Invoke the scanner and parser on a file. Use parse_stream with a
242  * std::ifstream if detection of file reading errors is required.
243  * @param filename input file name
244  * @return true if successfully parsed
245  */
246  bool parse_file(const std::string &filename);
247 
248  void statistics(std::ostream *out = nullptr) const;
249 
251  std::stack<file_rec> ap_file_list;
252 
253  std::stack<std::ostream *> outputStream;
254 
255  SEAMS::symrec *getsym(const char *sym_name) const;
256  SEAMS::symrec *getsym(const std::string &sym_name) const;
257  SEAMS::symrec *putsym(const std::string &sym_name, SYMBOL_TYPE sym_type, bool is_internal);
258 
259  void add_variable(const std::string &sym_name, const std::string &sym_value,
260  bool immutable = false, bool internal = false);
261  void add_variable(const std::string &sym_name, double sym_value, bool immutable = false,
262  bool internal = false);
263  void add_variable(const std::string &sym_name, array *value);
264 
265  std::vector<std::string> get_variable_names(bool doInternal = false);
266  void remove_variable(const std::string &sym_name);
267 
268  int set_option(const std::string &option, const std::string &optional_value = std::string(""));
269 
270  std::fstream *open_file(const std::string &file, const char *mode);
271  std::fstream *check_open_file(const std::string &file, const char *mode);
272 
273  /** Pointer to the current lexer instance, this is used to connect the
274  * parser to the scanner. It is used in the yylex macro. */
275  class Scanner *lexer{nullptr};
276 
277  /** Error handling. */
278  int get_error_count() const
279  {
280  return parseErrorCount;
281  } /** Return number of errors reported during parse */
282  void error(const std::string &msg, bool line_info = true, bool prefix = true) const;
283  void warning(const std::string &msg, bool line_info = true, bool prefix = true) const;
284  void info(const std::string &msg, bool line_info = false, bool prefix = true) const;
285 
286  // The info stream. To only print out info messages if the -M option was
287  // specified, use info(...) instead.
288  std::ostream *infoStream{&std::cout};
289 
290  void set_error_streams(std::ostream *c_error, std::ostream *c_warning, std::ostream *c_info);
291 
292  void dumpsym(const char *type, bool doInternal) const;
293  void dumpsym(int type, bool doInternal) const;
294  void dumpsym(int type, const char *pre, bool doInternal) const;
295 
296  private:
297  void init_table(const char *comment);
298  std::vector<symrec *> sym_table;
299  std::ostringstream parsingResults;
300 
301  // Input stream used with parse_string_interactive
302  std::istringstream stringInput;
303 
304  bool stringInteractive{false};
305  class Scanner *stringScanner{nullptr};
306 
307  // For error handling
308  std::ostream *errorStream{&std::cerr};
309  std::ostream *warningStream{&std::cerr};
310 
311  // For substitution history.
312  std::vector<history_data> history;
313 
314  mutable int parseErrorCount{0};
315 
316  public:
317  bool stateImmutable{false};
318 
319  // Flag to do Aprepro substitutions within loops. Default value is true. If set to
320  // false, content within the loop will be treated as verbatim text.
321  bool doLoopSubstitution{true};
322 
323  // Flag to do Aprepro substitutions when including a file. Default value is true.
324  // If set to false, content within the file will be treated as verbatim text that
325  // needs to be sent through Aprepro again later.
327 
328  // Flag to inidicate whether Aprepro is in the middle of collecting lines for a
329  // loop.
330  bool isCollectingLoop{false};
331 
332  // Record the substitution of the current Aprepro statement. This function will also
333  // reset the historyString and add an entry to the substitution map.
334  void add_history(const std::string &original, const std::string &substitution);
335 
336  // Used to avoid undefined variable warnings in old ifdef/ifndef construct
337  mutable bool inIfdefGetvar{false};
338 
339  const std::vector<history_data> &get_history();
340  void clear_history();
341  };
342 
343 } // namespace SEAMS
344 
345 #endif // SEAMS_DRIVER_H
symrec * next
Definition: aprepro.h:114
int parseErrorCount
Definition: aprepro.h:314
bool parse_strings(const std::vector< std::string > &input, const std::string &sname)
Definition: apr_aprepro.cc:145
int lineno
Definition: aprepro.h:153
void init_table(const char *comment)
Definition: apr_init.cc:387
Definition: aprepro.h:178
bool doLoopSubstitution
Definition: aprepro.h:321
std::string svar
Definition: aprepro.h:92
void set_error_streams(std::ostream *c_error, std::ostream *c_warning, std::ostream *c_info)
Definition: apr_aprepro.cc:269
std::ostream * infoStream
Definition: aprepro.h:288
bool parse_string_interactive(const std::string &input)
Definition: apr_aprepro.cc:154
std::stack< std::ostream * > outputStream
Definition: aprepro.h:253
std::string substitution
Definition: aprepro.h:168
symrec(const char *my_name, int my_type, bool is_internal=false)
Definition: aprepro.h:116
std::string include_path
Definition: aprepro.h:134
std::vector< std::string > get_variable_names(bool doInternal=false)
Definition: apr_aprepro.cc:593
Definition: aprepro.h:150
double(* fnctptr_dd)(double, double)
Definition: aprepro.h:85
const std::ostringstream & parsing_results() const
Definition: aprepro.h:202
bool immutable
Definition: aprepro.h:142
Definition: aprepro.h:69
~Aprepro()
Definition: apr_aprepro.cc:80
std::string original
Definition: aprepro.h:167
double(* fnctptr_ccd)(char *, char *, double)
Definition: aprepro.h:88
void clear_history()
Definition: apr_aprepro.cc:872
int type
Definition: aprepro.h:74
array()=default
void error(const std::string &msg, bool line_info=true, bool prefix=true) const
Definition: apr_aprepro.cc:184
void dumpsym(const char *type, bool doInternal) const
Definition: apr_aprepro.cc:682
bool warning_msg
Definition: aprepro.h:137
std::fstream * open_file(const std::string &file, const char *mode)
Definition: apr_aprepro.cc:291
bool interactive
Definition: aprepro.h:141
bool trace_parsing
Definition: aprepro.h:143
bool parse_file(const std::string &filename)
Definition: apr_aprepro.cc:130
aprepro_options ap_options
Definition: aprepro.h:250
bool isInternal
Definition: aprepro.h:75
bool parse_stream(std::istream &in, const std::string &in_name="stream input")
Definition: apr_aprepro.cc:111
struct SEAMS::symrec::value value
double(* fnctptr_d)(double)
Definition: aprepro.h:80
double(* fnctptr)()
Definition: aprepro.h:79
bool state_is_immutable() const
Definition: aprepro.h:197
int get_error_count() const
Definition: aprepro.h:278
void clear_results()
Definition: apr_aprepro.cc:105
bool one_based_index
Definition: aprepro.h:144
SYMBOL_TYPE
Definition: aprepro.h:185
std::string info
Definition: aprepro.h:73
~array()=default
std::istringstream stringInput
Definition: aprepro.h:302
void info(const std::string &msg, bool line_info=false, bool prefix=true) const
Definition: apr_aprepro.cc:240
double(* fnctptr_ddd)(double, double, double)
Definition: aprepro.h:86
double(* fnctptr_c)(char *)
Definition: aprepro.h:81
int rows
Definition: aprepro.h:61
bool isCollectingLoop
Definition: aprepro.h:330
bool inIfdefGetvar
Definition: aprepro.h:337
bool tmp_file
Definition: aprepro.h:155
const std::vector< history_data > & get_history()
Definition: apr_aprepro.cc:870
void statistics(std::ostream *out=nullptr) const
Definition: apr_aprepro.cc:799
std::streampos index
Definition: aprepro.h:169
array(int r, int c)
Definition: aprepro.h:64
bool stringInteractive
Definition: aprepro.h:304
bool stateImmutable
Definition: aprepro.h:317
SEAMS::symrec * getsym(const char *sym_name) const
Definition: apr_aprepro.cc:669
double(* fnctptr_cc)(char *, char *)
Definition: aprepro.h:84
double(* fnctptr_cd)(char *, double)
Definition: aprepro.h:83
std::string & comment()
Definition: apr_units.cc:50
std::ostream * warningStream
Definition: aprepro.h:309
bool parse_string(const std::string &input, const std::string &sname="string stream")
Definition: apr_aprepro.cc:139
class Scanner * lexer
Definition: aprepro.h:275
bool string_interactive()
Definition: aprepro.h:239
void add_variable(const std::string &sym_name, const std::string &sym_value, bool immutable=false, bool internal=false)
Definition: apr_aprepro.cc:536
void warning(const std::string &msg, bool line_info=true, bool prefix=true) const
Definition: apr_aprepro.cc:210
symrec(const std::string &my_name, int my_type, bool is_internal=false)
Definition: aprepro.h:123
std::ostringstream parsingResults
Definition: aprepro.h:299
SEAMS::symrec * putsym(const std::string &sym_name, SYMBOL_TYPE sym_type, bool is_internal)
Definition: apr_aprepro.cc:345
std::ostream * errorStream
Definition: aprepro.h:308
double(* fnctptr_ddddc)(double, double, double, double, char *)
Definition: aprepro.h:90
array * avar
Definition: aprepro.h:103
std::fstream * check_open_file(const std::string &file, const char *mode)
Definition: apr_aprepro.cc:324
std::string name
Definition: aprepro.h:152
Definition: aprepro.h:165
std::string include_file
Definition: aprepro.h:135
double(* fnctptr_dc)(double, char *)
Definition: aprepro.h:82
int loop_count
Definition: aprepro.h:154
int cols
Definition: aprepro.h:62
double(* fnctptr_dddd)(double, double, double, double)
Definition: aprepro.h:89
Definition: aprepro.h:132
bool doIncludeSubstitution
Definition: aprepro.h:326
std::string version() const
Definition: apr_aprepro.cc:103
Definition: aprepro.h:76
std::string syntax
Definition: aprepro.h:72
int set_option(const std::string &option, const std::string &optional_value=std::string(""))
Definition: apr_aprepro.cc:408
Definition: apr_scanner.h:27
std::vector< double > data
Definition: aprepro.h:60
Definition: apr_aprepro.cc:68
double(* fnctptr_a)(const array *)
Definition: aprepro.h:92
file_rec()=default
double var
Definition: aprepro.h:78
bool keep_history
Definition: aprepro.h:145
bool info_msg
Definition: aprepro.h:138
void remove_variable(const std::string &sym_name)
Definition: apr_aprepro.cc:623
bool end_on_exit
Definition: aprepro.h:136
Aprepro()
construct a new parser aprepro context
Definition: apr_aprepro.cc:72
double(* fnctptr_ccc)(char *, char *, char *)
Definition: aprepro.h:87
bool dumpvars
Definition: aprepro.h:140
class Scanner * stringScanner
Definition: aprepro.h:305
bool debugging
Definition: aprepro.h:139
double(* fnctptr_dddddd)(double, double, double, double, double, double)
Definition: aprepro.h:91
std::string name
Definition: aprepro.h:71
Definition: aprepro.h:58
std::vector< symrec * > sym_table
Definition: aprepro.h:298
void add_history(const std::string &original, const std::string &substitution)
Definition: apr_aprepro.cc:854
std::stack< file_rec > ap_file_list
Definition: aprepro.h:251
file_rec(const char *my_name, int line_num, bool is_temp, int loop_cnt)
Definition: aprepro.h:157
std::vector< history_data > history
Definition: aprepro.h:312