| 1 |
// Copyright(C) 1999-2021, 2023 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 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include <fstream> |
| 8 |
#include <iostream> |
| 9 |
|
| 10 |
#include "aprepro.h" |
| 11 |
|
| 12 |
// This function is used below in the example showing how an |
| 13 |
// application can add its own functions to an aprepro instance. |
| 14 |
double succ(double i) { return ++i; } |
| 15 |
|
| 16 |
int main(int argc, char *argv[]) |
| 17 |
{ |
| 18 |
bool readfile = false; |
| 19 |
|
| 20 |
std::string output_file; |
| 21 |
|
| 22 |
SEAMS::Aprepro aprepro; |
| 23 |
|
| 24 |
// EXAMPLE: Add a function to aprepro... |
| 25 |
SEAMS::symrec *ptr = aprepro.putsym("succ", SEAMS::Aprepro::SYMBOL_TYPE::FUNCTION, false); |
| 26 |
ptr->value.fnctptr_d = succ; |
| 27 |
ptr->info = "Return the successor to d"; |
| 28 |
ptr->syntax = "succ(d)"; |
| 29 |
|
| 30 |
// EXAMPLE: Add a couple variables... |
| 31 |
aprepro.add_variable("Greg", "Is the author of this code", true); // Make it immutable |
| 32 |
aprepro.add_variable("BirthYear", 1958); |
| 33 |
|
| 34 |
for (int ai = 1; ai < argc; ++ai) { |
| 35 |
std::string arg = argv[ai]; |
| 36 |
if (arg == "-o") { |
| 37 |
output_file = argv[++ai]; |
| 38 |
} |
| 39 |
else if (arg == "-i") { |
| 40 |
// Read from cin and echo each line to cout All results will |
| 41 |
// also be stored in Aprepro::parsing_results() stream if needed |
| 42 |
// at end of file. |
| 43 |
aprepro.ap_options.interactive = true; |
| 44 |
bool result = aprepro.parse_stream(std::cin, "standard input"); |
| 45 |
if (result) { |
| 46 |
if (!output_file.empty()) { |
| 47 |
std::ofstream ofile(output_file); |
| 48 |
ofile << aprepro.parsing_results().str(); |
| 49 |
} |
| 50 |
else { |
| 51 |
std::cout << aprepro.parsing_results().str(); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
else if (arg[0] == '-') { |
| 56 |
aprepro.set_option(argv[ai]); |
| 57 |
} |
| 58 |
else { |
| 59 |
// Read and parse a file. The entire file will be parsed and |
| 60 |
// then the output can be obtained in an std::ostringstream via |
| 61 |
// Aprepro::parsing_results() |
| 62 |
std::fstream infile(argv[ai]); |
| 63 |
if (!infile.good()) { |
| 64 |
if (!aprepro.ap_options.include_path.empty() && argv[ai][0] != '/') { |
| 65 |
std::string filename = aprepro.ap_options.include_path + "/" + argv[ai]; |
| 66 |
infile.open(filename, std::fstream::in); |
| 67 |
} |
| 68 |
} |
| 69 |
if (!infile.good()) { |
| 70 |
std::cerr << "APREPRO: Could not open file: " << argv[ai] << '\n'; |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
bool result = aprepro.parse_stream(infile, argv[ai]); |
| 75 |
if (result) { |
| 76 |
if (!output_file.empty()) { |
| 77 |
std::ofstream ofile(output_file); |
| 78 |
ofile << aprepro.parsing_results().str(); |
| 79 |
} |
| 80 |
else { |
| 81 |
std::cout << aprepro.parsing_results().str(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
readfile = true; |
| 86 |
} |
| 87 |
} |
| 88 |
if (readfile) { |
| 89 |
std::cerr << "Aprepro: There were " << aprepro.get_error_count() |
| 90 |
<< " errors detected during parsing.\n"; |
| 91 |
return aprepro.get_error_count() == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 92 |
} |
| 93 |
|
| 94 |
// Read and parse a string's worth of data at a time. |
| 95 |
// Cannot use looping/ifs/... with this method. |
| 96 |
std::string line, tmp; |
| 97 |
while (std::cout << "\nexpression: " && std::getline(std::cin, tmp) && !tmp.empty()) { |
| 98 |
|
| 99 |
line += tmp; |
| 100 |
|
| 101 |
if (*tmp.rbegin() == '\\') { |
| 102 |
line.erase(line.length() - 1); |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
line += "\n"; |
| 107 |
|
| 108 |
bool result = aprepro.parse_string_interactive(line); |
| 109 |
|
| 110 |
if (result) { |
| 111 |
std::string res_str = aprepro.parsing_results().str(); |
| 112 |
std::cout << " : " << res_str; |
| 113 |
|
| 114 |
// Example showing how to get the substitution history for the current line. |
| 115 |
if (aprepro.ap_options.keep_history) { |
| 116 |
std::vector<SEAMS::history_data> hist = aprepro.get_history(); |
| 117 |
for (const auto &curr_history : hist) { |
| 118 |
|
| 119 |
auto substitution = curr_history.substitution; |
| 120 |
if (substitution == "\n") { |
| 121 |
substitution = "<not echoed>"; |
| 122 |
} |
| 123 |
std::cout << "\t'" << curr_history.original << "' was substituted with '" << substitution |
| 124 |
<< "' at index " << curr_history.index << '\n'; |
| 125 |
} |
| 126 |
|
| 127 |
aprepro.clear_history(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
aprepro.clear_results(); |
| 132 |
|
| 133 |
line.clear(); |
| 134 |
} |
| 135 |
std::cerr << "Aprepro: There were " << aprepro.get_error_count() |
| 136 |
<< " errors detected during parsing.\n"; |
| 137 |
return aprepro.get_error_count() == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 138 |
} |