| 1 |
// Copyright(C) 1999-2020, 2023, 2024 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 "aprepro.h" // for Aprepro |
| 8 |
#include <iostream> // for cout, ostream, etc |
| 9 |
#include <string> // for string, operator<< |
| 10 |
#include <vector> // for vector |
| 11 |
|
| 12 |
std::vector<std::string> build_strings(); |
| 13 |
|
| 14 |
int main(int, char **) |
| 15 |
{ |
| 16 |
SEAMS::Aprepro aprepro; |
| 17 |
|
| 18 |
aprepro.ap_options.warning_msg = false; |
| 19 |
|
| 20 |
std::vector<std::string> strings = build_strings(); |
| 21 |
bool result = aprepro.parse_strings(strings, "My list of strings"); |
| 22 |
if (result) { |
| 23 |
std::string res_str = aprepro.parsing_results().str(); |
| 24 |
std::cout << res_str; |
| 25 |
} |
| 26 |
aprepro.clear_results(); |
| 27 |
} |
| 28 |
|
| 29 |
std::vector<std::string> build_strings() |
| 30 |
{ |
| 31 |
std::vector<std::string> strings; |
| 32 |
|
| 33 |
strings.emplace_back(R"($ Test program for Aprepro)"); |
| 34 |
strings.emplace_back(R"($)"); |
| 35 |
strings.emplace_back(R"(Test number representations)"); |
| 36 |
strings.emplace_back(R"({1} {10e-1} {10.e-1} {.1e+1} {.1e1})"); |
| 37 |
strings.emplace_back(R"({1} {10E-1} {10.E-1} {.1E+1} {.1E1})"); |
| 38 |
strings.emplace_back(R"()"); |
| 39 |
strings.emplace_back(R"(Test assign statements:)"); |
| 40 |
strings.emplace_back(R"({_a = 5} {b=_a} $ Should print 5 5)"); |
| 41 |
strings.emplace_back(R"({_a +=b} {_a} $ Should print 10 10)"); |
| 42 |
strings.emplace_back(R"({_a -=b} {_a} $ Should print 5 5)"); |
| 43 |
strings.emplace_back(R"({_a *=b} {_a} $ Should print 25 25)"); |
| 44 |
strings.emplace_back(R"({_a /=b} {_a} $ Should print 5 5)"); |
| 45 |
strings.emplace_back(R"({_a ^=b} {_a} $ Should print 3125 3125)"); |
| 46 |
strings.emplace_back(R"({_a = b} {_a**=b} {_a} $ Should print 5 3125 3125)"); |
| 47 |
strings.emplace_back(R"()"); |
| 48 |
strings.emplace_back(R"(Test trigonometric functions (radians))"); |
| 49 |
strings.emplace_back(R"({pi = d2r(180)} {atan2(0,-1)} {4*atan(1.0)} $ Three values of pi)"); |
| 50 |
strings.emplace_back(R"({_a = sin(pi/4)} {pi-4*asin(_a)} $ sin(pi/4))"); |
| 51 |
strings.emplace_back(R"({_b = cos(pi/4)} {pi-4*acos(_b)} $ cos(pi/4))"); |
| 52 |
strings.emplace_back(R"({_c = tan(pi/4)} {pi-4*atan(_c)} $ tan(pi/4))"); |
| 53 |
strings.emplace_back(R"()"); |
| 54 |
strings.emplace_back(R"(Test trigonometric functions (degrees))"); |
| 55 |
strings.emplace_back(R"({r2d(pi)} {pid = atan2d(0,-1)} {4 * atand(1.0)})"); |
| 56 |
strings.emplace_back(R"({ad = sind(180/4)} {180-4*asind(ad)} $ sin(180/4))"); |
| 57 |
strings.emplace_back(R"({bd = cosd(180/4)} {180-4*acosd(bd)} $ cos(180/4))"); |
| 58 |
strings.emplace_back(R"({cd = tand(180/4)} {180-4*atand(cd)} $ tan(180/4))"); |
| 59 |
strings.emplace_back(R"()"); |
| 60 |
strings.emplace_back(R"(Test max, min, sign, dim, abs)"); |
| 61 |
strings.emplace_back( |
| 62 |
R"({pmin = min(0.5, 1.0)} {nmin = min(-0.5, -1.0)} $ Should be 0.5, -1)"); |
| 63 |
strings.emplace_back( |
| 64 |
R"({pmax = max(0.5, 1.0)} {nmax = max(-0.5, -1.0)} $ Should be 1.0, -0.5)"); |
| 65 |
strings.emplace_back(R"({zero = 0} {sign(0.5, zero) + sign(0.5, -zero)} $ Should be 0 1)"); |
| 66 |
strings.emplace_back( |
| 67 |
R"({nonzero = 1} {sign(0.5, nonzero) + sign(0.5, -nonzero)} $ Should be 1 0)"); |
| 68 |
strings.emplace_back(R"({dim(5.5, 4.5)} {dim(4.5, 5.5)} $ Should be 1 0)"); |
| 69 |
strings.emplace_back(R"()"); |
| 70 |
strings.emplace_back(R"({ifyes = 1} {ifno = 0})"); |
| 71 |
strings.emplace_back(R"($ Test ternary...)"); |
| 72 |
strings.emplace_back(R"({ifyes == 1 ? "correct" : "incorrect"})"); |
| 73 |
strings.emplace_back(R"({ifno == 1 ? "incorrect" : "correct"})"); |
| 74 |
strings.emplace_back(R"()"); |
| 75 |
strings.emplace_back(R"($ Test ifdef lines)"); |
| 76 |
strings.emplace_back(R"( {Ifdef(ifyes)})"); |
| 77 |
strings.emplace_back(R"(This line should be echoed. (a))"); |
| 78 |
strings.emplace_back(R"( {Endif})"); |
| 79 |
strings.emplace_back(R"(This line should be echoed. (b))"); |
| 80 |
strings.emplace_back(R"( {Ifdef(ifno)})"); |
| 81 |
strings.emplace_back(R"(This line should not be echoed)"); |
| 82 |
strings.emplace_back(R"( {Endif})"); |
| 83 |
strings.emplace_back(R"(This line should be echoed. (c))"); |
| 84 |
strings.emplace_back(R"( {Ifdef(ifundefined)})"); |
| 85 |
strings.emplace_back(R"(This line should not be echoed)"); |
| 86 |
strings.emplace_back(R"( {Endif})"); |
| 87 |
strings.emplace_back(R"(This line should be echoed. (d))"); |
| 88 |
strings.emplace_back(R"()"); |
| 89 |
strings.emplace_back(R"($ Test ifdef - else lines)"); |
| 90 |
strings.emplace_back(R"( {Ifdef(ifyes)})"); |
| 91 |
strings.emplace_back(R"(This line should be echoed. (1))"); |
| 92 |
strings.emplace_back(R"( {Else})"); |
| 93 |
strings.emplace_back(R"(This line should not be echoed (2))"); |
| 94 |
strings.emplace_back(R"( {Endif})"); |
| 95 |
strings.emplace_back(R"( {Ifdef(ifno)})"); |
| 96 |
strings.emplace_back(R"(This line should not be echoed. (3))"); |
| 97 |
strings.emplace_back(R"( {Else})"); |
| 98 |
strings.emplace_back(R"(This line should be echoed (4))"); |
| 99 |
strings.emplace_back(R"( {Endif})"); |
| 100 |
strings.emplace_back(R"()"); |
| 101 |
strings.emplace_back(R"($ Test ifndef - else lines)"); |
| 102 |
strings.emplace_back(R"( {Ifndef(ifyes)})"); |
| 103 |
strings.emplace_back(R"(This line should not be echoed. (5))"); |
| 104 |
strings.emplace_back(R"( {Else})"); |
| 105 |
strings.emplace_back(R"(This line should be echoed (6))"); |
| 106 |
strings.emplace_back(R"( {Endif})"); |
| 107 |
strings.emplace_back(R"( {Ifndef(ifno)})"); |
| 108 |
strings.emplace_back(R"(This line should be echoed. (7))"); |
| 109 |
strings.emplace_back(R"( {Else})"); |
| 110 |
strings.emplace_back(R"(This line should not be echoed (8))"); |
| 111 |
strings.emplace_back(R"( {Endif})"); |
| 112 |
strings.emplace_back(R"($ Lines a, b, c, d, 1, 4, 6, 7 should be echoed)"); |
| 113 |
strings.emplace_back(R"($ Check line counting -- should be on line 78: )"); |
| 114 |
strings.emplace_back(R"( )"); |
| 115 |
strings.emplace_back( |
| 116 |
R"($ ========================================================================)"); |
| 117 |
strings.emplace_back(R"($ Test string if lines)"); |
| 118 |
strings.emplace_back(R"({if("Greg")})"); |
| 119 |
strings.emplace_back(R"( This line should be echoed ("greg"))"); |
| 120 |
strings.emplace_back(R"({else})"); |
| 121 |
strings.emplace_back(R"( This line should not be echoed ("greg"))"); |
| 122 |
strings.emplace_back(R"({endif})"); |
| 123 |
strings.emplace_back(R"()"); |
| 124 |
strings.emplace_back(R"({empty=""})"); |
| 125 |
strings.emplace_back(R"({if(empty)})"); |
| 126 |
strings.emplace_back(R"( This line should not be echoed (empty))"); |
| 127 |
strings.emplace_back(R"({else})"); |
| 128 |
strings.emplace_back(R"( This line should be echoed (empty))"); |
| 129 |
strings.emplace_back(R"({endif})"); |
| 130 |
strings.emplace_back(R"()"); |
| 131 |
strings.emplace_back(R"()"); |
| 132 |
strings.emplace_back( |
| 133 |
R"($ ========================================================================)"); |
| 134 |
strings.emplace_back(R"($ Test if lines)"); |
| 135 |
strings.emplace_back(R"({if(sqrt(4) == 2)})"); |
| 136 |
strings.emplace_back(R"( This line should be echoed. (a))"); |
| 137 |
strings.emplace_back(R"({endif})"); |
| 138 |
strings.emplace_back(R"( This line should be echoed. (b))"); |
| 139 |
strings.emplace_back(R"({if(sqrt(2) == 2 || sqrt(3) == 2)})"); |
| 140 |
strings.emplace_back(R"( This line should not be echoed)"); |
| 141 |
strings.emplace_back(R"({endif})"); |
| 142 |
strings.emplace_back(R"(This line should be echoed. (c))"); |
| 143 |
strings.emplace_back(R"()"); |
| 144 |
strings.emplace_back(R"({diff = sqrt(3)*sqrt(3) - 3})"); |
| 145 |
strings.emplace_back(R"($ Test if - else lines)"); |
| 146 |
strings.emplace_back(R"({if(sqrt(3)*sqrt(3) - 3 == diff)})"); |
| 147 |
strings.emplace_back(R"( complex if works)"); |
| 148 |
strings.emplace_back(R"({else})"); |
| 149 |
strings.emplace_back(R"( complex if does not work)"); |
| 150 |
strings.emplace_back(R"({endif})"); |
| 151 |
strings.emplace_back(R"()"); |
| 152 |
strings.emplace_back(R"({if (sqrt(4) == 2)})"); |
| 153 |
strings.emplace_back(R"( {if (sqrt(9) == 3)})"); |
| 154 |
strings.emplace_back(R"( {if (sqrt(16) == 4)})"); |
| 155 |
strings.emplace_back(R"( square roots work)"); |
| 156 |
strings.emplace_back(R"( {else})"); |
| 157 |
strings.emplace_back(R"( sqrt(16) does not work)"); |
| 158 |
strings.emplace_back(R"( {endif})"); |
| 159 |
strings.emplace_back(R"( {else})"); |
| 160 |
strings.emplace_back(R"( sqrt(9) does not work)"); |
| 161 |
strings.emplace_back(R"( {endif})"); |
| 162 |
strings.emplace_back(R"({else})"); |
| 163 |
strings.emplace_back(R"( sqrt(4) does not work)"); |
| 164 |
strings.emplace_back(R"({endif})"); |
| 165 |
strings.emplace_back(R"()"); |
| 166 |
strings.emplace_back(R"({v1 = 1} {v2 = 2})"); |
| 167 |
strings.emplace_back(R"({if (v1 == v2)})"); |
| 168 |
strings.emplace_back(R"( Bad if)"); |
| 169 |
strings.emplace_back(R"( {if (v1 != v2)})"); |
| 170 |
strings.emplace_back(R"( should not see (1))"); |
| 171 |
strings.emplace_back(R"( {else})"); |
| 172 |
strings.emplace_back(R"( should not see (2))"); |
| 173 |
strings.emplace_back(R"( {endif})"); |
| 174 |
strings.emplace_back(R"( should not see (3))"); |
| 175 |
strings.emplace_back(R"({else})"); |
| 176 |
strings.emplace_back(R"( {if (v1 != v2)})"); |
| 177 |
strings.emplace_back(R"( good nested if)"); |
| 178 |
strings.emplace_back(R"( {else})"); |
| 179 |
strings.emplace_back(R"( bad nested if)"); |
| 180 |
strings.emplace_back(R"( {endif})"); |
| 181 |
strings.emplace_back(R"( good)"); |
| 182 |
strings.emplace_back(R"( make sure it is still good)"); |
| 183 |
strings.emplace_back(R"({endif})"); |
| 184 |
strings.emplace_back( |
| 185 |
R"($ ========================================================================)"); |
| 186 |
strings.emplace_back(R"($ Test switch)"); |
| 187 |
strings.emplace_back(R"({switch(PI)})"); |
| 188 |
strings.emplace_back(R"(This is in a switch, but prior to any case, it should not run)"); |
| 189 |
strings.emplace_back(R"({a = 0.5} Should not execute)"); |
| 190 |
strings.emplace_back(R"()"); |
| 191 |
strings.emplace_back(R"({case (1)})"); |
| 192 |
strings.emplace_back(R"(This should not echo)"); |
| 193 |
strings.emplace_back(R"({a = 1})"); |
| 194 |
strings.emplace_back(R"()"); |
| 195 |
strings.emplace_back(R"({case (2)})"); |
| 196 |
strings.emplace_back(R"(This should not echo)"); |
| 197 |
strings.emplace_back(R"({a = 2})"); |
| 198 |
strings.emplace_back(R"()"); |
| 199 |
strings.emplace_back(R"({case (PI)})"); |
| 200 |
strings.emplace_back(R"(This should echo)"); |
| 201 |
strings.emplace_back(R"({a = PI})"); |
| 202 |
strings.emplace_back(R"()"); |
| 203 |
strings.emplace_back(R"({case (PI)})"); |
| 204 |
strings.emplace_back(R"(This should not echo since a previous case matched.)"); |
| 205 |
strings.emplace_back(R"({a = 2})"); |
| 206 |
strings.emplace_back(R"()"); |
| 207 |
strings.emplace_back(R"({default})"); |
| 208 |
strings.emplace_back(R"({a=4})"); |
| 209 |
strings.emplace_back(R"()"); |
| 210 |
strings.emplace_back(R"({endswitch})"); |
| 211 |
strings.emplace_back(R"()"); |
| 212 |
strings.emplace_back(R"(This should be equal to PI -- {PI})"); |
| 213 |
strings.emplace_back(R"($ Test int and [] (shortcut for int))"); |
| 214 |
strings.emplace_back(R"({int(5.01)} {int(-5.01)})"); |
| 215 |
strings.emplace_back(R"({[5.01]} {[-5.01]})"); |
| 216 |
strings.emplace_back(R"()"); |
| 217 |
strings.emplace_back(R"($ Test looping - print sin, cos from 0 to 90 by 5)"); |
| 218 |
strings.emplace_back(R"({Loop(19, _angle, 0, 5)})"); |
| 219 |
strings.emplace_back( |
| 220 |
R"({_angle} {_sa=sind(_angle)} {_ca=cosd(_angle)} {hypot(_sa, _ca)} )"); |
| 221 |
strings.emplace_back(R"({EndLoop})"); |
| 222 |
strings.emplace_back(R"()"); |
| 223 |
strings.emplace_back(R"($$$$ Test formatting and string concatenation)"); |
| 224 |
strings.emplace_back(R"({_SAVE = _FORMAT})"); |
| 225 |
strings.emplace_back(R"({loop(20)})"); |
| 226 |
strings.emplace_back( |
| 227 |
R"({IO(__loop_1+1)} Using the format {_FORMAT = "%." // tostring(__loop_1+1) // "g"}, PI = {PI})"); |
| 228 |
strings.emplace_back(R"({endloop})"); |
| 229 |
strings.emplace_back(R"(Reset format to default: {_FORMAT = _SAVE})"); |
| 230 |
strings.emplace_back(R"()"); |
| 231 |
strings.emplace_back( |
| 232 |
R"($$$$ Test formatting using the `format` function. _FORMAT is not modified)"); |
| 233 |
strings.emplace_back(R"({loop(20)}")"); |
| 234 |
strings.emplace_back( |
| 235 |
R"({__loop_1+1} Using the format {_f = "%." // tostring(__loop_1+1) // "f"}, PI = {format(PI,_f)})"); |
| 236 |
strings.emplace_back(R"({endloop})"); |
| 237 |
strings.emplace_back(R"()"); |
| 238 |
strings.emplace_back(R"($$$$ Test string rescanning and executing)"); |
| 239 |
strings.emplace_back(R"({ECHO(OFF)})"); |
| 240 |
strings.emplace_back(R"({Test = ' This is line 1: {a = atan2(0,-1)})"); |
| 241 |
strings.emplace_back(R"( This is line 2: {sin(a/4)})"); |
| 242 |
strings.emplace_back(R"( This is line 3: {cos(a/4)}'})"); |
| 243 |
strings.emplace_back(R"({Test2 = 'This has an embedded string: {T = "This is a string"}'})"); |
| 244 |
strings.emplace_back(R"({ECHO(ON)})"); |
| 245 |
strings.emplace_back(R"(Original String:)"); |
| 246 |
strings.emplace_back(R"({Test})"); |
| 247 |
strings.emplace_back(R"(Rescanned String:)"); |
| 248 |
strings.emplace_back(R"({rescan(Test)} )"); |
| 249 |
strings.emplace_back(R"(Original String:)"); |
| 250 |
strings.emplace_back(R"({Test2})"); |
| 251 |
strings.emplace_back(R"(Print Value of variable T = {T})"); |
| 252 |
strings.emplace_back(R"(Rescanned String:)"); |
| 253 |
strings.emplace_back(R"({rescan(Test2)} )"); |
| 254 |
strings.emplace_back(R"(Print Value of variable T = {T})"); |
| 255 |
strings.emplace_back(R"()"); |
| 256 |
strings.emplace_back(R"d(Original String: {t1 = "atan2(0,-1)"})d"); |
| 257 |
strings.emplace_back(R"(Executed String: {execute(t1)})"); |
| 258 |
strings.emplace_back(R"()"); |
| 259 |
strings.emplace_back(R"(string = {_string = " one two, three"})"); |
| 260 |
strings.emplace_back(R"(delimiter "{_delm = " ,"}")"); |
| 261 |
strings.emplace_back(R"(word count = {word_count(_string,_delm)})"); |
| 262 |
strings.emplace_back(R"(second word = "{get_word(2,_string,_delm)}")"); |
| 263 |
strings.emplace_back(R"()"); |
| 264 |
strings.emplace_back(R"(string = {_string = " (one two, three * four - five"})"); |
| 265 |
strings.emplace_back(R"(delimiter "{_delm = " ,(*-"}")"); |
| 266 |
strings.emplace_back(R"(word count = {word_count(_string,_delm)})"); |
| 267 |
strings.emplace_back(R"(second word = "{get_word(2,_string,_delm)}")"); |
| 268 |
strings.emplace_back(R"()"); |
| 269 |
strings.emplace_back(R"()"); |
| 270 |
strings.emplace_back(R"(string = {_string = " one two, three"})"); |
| 271 |
strings.emplace_back(R"(delimiter "{_delm = " ,"}")"); |
| 272 |
strings.emplace_back(R"(word count = { iwords = word_count(_string,_delm)})"); |
| 273 |
strings.emplace_back(R"()"); |
| 274 |
strings.emplace_back(R"( {loop(iwords, _n, 1)})"); |
| 275 |
strings.emplace_back(R"(word {_n} = "{get_word(_n,_string,_delm)}")"); |
| 276 |
strings.emplace_back(R"( {endloop})"); |
| 277 |
strings.emplace_back(R"()"); |
| 278 |
strings.emplace_back(R"($ Check parsing of escaped braces...)"); |
| 279 |
strings.emplace_back(R"(\{ int a = b + {PI/2} \})"); |
| 280 |
strings.emplace_back(R"(\{ \})"); |
| 281 |
strings.emplace_back(R"()"); |
| 282 |
strings.emplace_back(R"($ Test variable deletion)"); |
| 283 |
strings.emplace_back(R"({new_var = sqrt(2) * sqrt(3)})"); |
| 284 |
strings.emplace_back(R"({new_var})"); |
| 285 |
strings.emplace_back(R"({delete("new_var")})"); |
| 286 |
strings.emplace_back(R"({new_var} This should print warning about undefined variable)"); |
| 287 |
strings.emplace_back(R"()"); |
| 288 |
strings.emplace_back(R"($ Test extract)"); |
| 289 |
strings.emplace_back(R"({ex_found = extract("The test string is found", "test", "")})"); |
| 290 |
strings.emplace_back(R"({ex_null = extract("The test string is not found", "xxxx", "yyyy")})"); |
| 291 |
strings.emplace_back(R"()"); |
| 292 |
strings.emplace_back(R"($ Test string tokenization optimization)"); |
| 293 |
strings.emplace_back(R"({list1 ='51,52,53,54,61,62,63,64'})"); |
| 294 |
strings.emplace_back(R"({list2 ='71,72,73,74,81,82,83,84'})"); |
| 295 |
strings.emplace_back(R"({loop(8, _i, 1)})"); |
| 296 |
strings.emplace_back( |
| 297 |
R"(Word {_i} of list1 and list2 are {get_word(_i,list1,',')} and {get_word(_i,list2,',')})"); |
| 298 |
strings.emplace_back(R"({endloop})"); |
| 299 |
strings.emplace_back(R"()"); |
| 300 |
strings.emplace_back(R"($$$$ Test double brace echo off/on)"); |
| 301 |
strings.emplace_back(R"(Nothing further on line: {{"not echoed"}})"); |
| 302 |
strings.emplace_back( |
| 303 |
R"(Noecho followed by non-parsing output: {{"not echoed"}}This should be echoed)"); |
| 304 |
strings.emplace_back( |
| 305 |
R"(Echo, noecho setting variable, then echo that variable: {e="echo"}+{{d="echo"}}+{d})"); |
| 306 |
strings.emplace_back(R"($End of test file)"); |
| 307 |
|
| 308 |
return strings; |
| 309 |
} |