cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parser.h
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2022 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 
4 #ifndef PARSER_H_
5 #define PARSER_H_
6 
13 #include <cstdio>
14 #include <map>
15 #include "count_ptr.h"
16 #include "input.h"
17 
18 const char * nWord(const char *chKey,
19  const char *chCard);
20 
21 class Parser;
22 class DepthTable;
23 class LineID;
24 
25 typedef void (*OptionParser)(Parser &);
26 
27 struct CloudyCommand {
28  const char *name;
30 };
31 
32 bool isBoundaryChar(char c);
33 
34 class Symbol {
35 public:
38  string value;
39  Symbol(enum tokens t, const string& v) : toktype(t), value(v) {}
40 };
41 
43 class Parser
44 {
45  char m_card[INPUT_LINE_LENGTH]; // all-caps version of m_card_raw
46  char m_card_raw[INPUT_LINE_LENGTH]; // raw input line with all comments stripped
47  char m_card_comment[INPUT_LINE_LENGTH]; // input line with visible comments included, used for output
48  long int m_len;
49  long int m_off;
50  bool m_lgEOL;
51  const CloudyCommand * const m_Commands;
52  std::map<string,double> m_symtab;
53 public:
54  long int m_nqh;
56 
57  explicit Parser(void) : m_Commands(NULL)
58  {
59  init();
60  }
61  explicit Parser(const CloudyCommand *commands) : m_Commands(commands)
62  {
63  init();
64  }
65 private:
66  void init()
67  {
68  m_nqh = 0;
69  m_lgDSet = m_lgEOF = false;
70  setline("");
71  }
72  void newlineProcess();
73  bool at_end() const;
74  char current( void ) const
75  {
76  return m_card[m_off];
77  }
78  char current_raw() const;
79  void skip_whitespace();
80 public:
81  bool getline();
82  void setline(const char * const card)
83  {
85  ASSERT(strlen(card) < (unsigned) INPUT_LINE_LENGTH);
86  strncpy(m_card_comment,card,INPUT_LINE_LENGTH);
87  string m_card_local( card );
88  StripComment( m_card_local, true );
89  // the ASSERTs above guarantee that this will not overflow buffer
90  // as StripComment() can only make the string shorter
91  strcpy(m_card_raw,m_card_local.c_str());
93  }
94 
95  void set_point(long int ipnt)
96  {
97  m_off = ipnt;
98  }
99  const char * nWord(const char *chKey) const;
100  bool lgReachedEnd();
101  void showLocation(FILE *io = ioQQQ) const;
102 public:
103  long int GetElem( void ) const;
104  double FFmtRead( void );
105  double getNumberPlain( const char *chDesc );
106  double getNumberCheck( const char *chDesc );
107  double getNumberDefault( const char *chDesc, double fdef );
108  double getNumberCheckLogLinNegImplLog( const char *chDesc );
109  double getNumberCheckAlwaysLog( const char *chDesc );
110  double getNumberCheckAlwaysLogLim( const char *chDesc, double flim );
111  double getNumberDefaultAlwaysLog( const char *chDesc, double fdef );
112  double getNumberDefaultNegImplLog( const char *chDesc, double fdef );
113  bool lgEOL(void) const
114  {
115  return m_lgEOL;
116  }
117  void setEOL(bool val)
118  {
119  m_lgEOL = val;
120  }
121  NORETURN void NoNumb(const char *chDesc) const;
122 private:
123  int nMatch1(const char *chKey) const
124  {
125  const char *p=chKey;
126 
127  while (isspace(*p))
128  ++p;
129 
130  for (const char *q=p; *q; ++q)
131  ASSERT(!islower(*q));
132 
133  if ( !isBoundaryChar(*p))
134  {
135  const char *q = ::nWord(p, m_card);
136  if (NULL == q)
137  return 0;
138  else
139  return q-m_card+1;
140  }
141  else
142  {
143  // If the keyword starts with a member of the boundary character
144  // set, can't require it to be preceded by one so revert to explicit
145  // matching
146  return ::nMatch(chKey, m_card);
147  }
148  }
149 public:
150  bool nMatch(const char *chKey) const
151  {
152  return nMatch1(chKey) != 0;
153  }
154  bool GetParam(const char *chKey, double *val)
155  {
156  int i = nMatch1(chKey);
157  if (i > 0) {
158  m_off = i-1;
159  *val = FFmtRead();
160  }
161  return i>0;
162  }
163  bool GetRange(const char *chKey, double *val1, double *val2)
164  {
165  int i = nMatch1(chKey);
166  if (i > 0) {
167  m_off = i-1;
168  *val1 = FFmtRead();
169  *val2 = FFmtRead();
170  }
171  return i>0;
172  }
173  bool nMatchErase(const char *chKey)
174  {
175  const char *p=chKey;
176  while (isspace(*p))
177  ++p;
178  int i = nMatch1(p);
179  bool found = (i != 0);
180  if(found)
181  {
182  char *ptr = m_card+i-1;
183  const long len = strlen(p);
184  /* erase this keyword, it upsets FFmtRead */
185  for (long i=0; i<len; ++i)
186  {
187  ptr[i] = ' ';
188  }
189  }
190  return found;
191  }
192  bool hasCommand(const char *s2);
193  bool Command(const char *name, OptionParser doOpts)
194  {
195  bool lgFound = hasCommand(name);
196  if ( lgFound )
197  (*doOpts)(*this);
198  return lgFound;
199  }
200  bool isComment(void) const;
201  bool isVar(void) const;
202  std::string getVarName(void);
203  void doSetVar(void);
204  void echo(void) const;
205  bool last(void) const;
206  int PrintLine(FILE *fp) const
207  {
208  return fprintf( fp, " ==%-.80s==\n", m_card_raw);
209  }
210  NORETURN void CommandError( void ) const;
211  NORETURN void Error( const char *msg ) const;
212  NORETURN void StringError( ) const;
213  int GetQuote( string& chLabel );
214  const char *StandardEnergyUnit(void) const;
215  string StandardFluxUnit(void) const;
216  string getFirstChunk(long i);
217  string getRawTail()
218  {
219  return string(m_card_raw+m_off);
220  }
221  void help(FILE *fp) const;
222  double getWave();
223  double getWaveOpt();
224  LineID getLineID();
225  Symbol getSymbol();
226  void getPairs(vector<double>& a, vector<double> & b);
227  void readList(vector<string>& list, const char *chName);
228  void readLaw(DepthTable& table);
229 };
230 
232 template <typename V>
233 class KeyAction {
234  const char * const m_keyword;
236 public:
237  KeyAction(const char *keyword, const V &action) :
238  m_keyword(keyword), m_action(action) {}
239 
240  const char *key(void) const
241  {
242  return m_keyword;
243  }
244  void operator()(realnum *v) const
245  {
246  m_action(v);
247  }
248 };
249 
251 template <typename V>
252 inline KeyAction<V> MakeKeyAction(const char *keyword, const V &action)
253 {
254  return KeyAction<V>(keyword, action);
255 }
256 
259 {
261 public:
262  UnitConverter ( double unit ) : m_unit((realnum)unit) {}
263 
264  void operator()( realnum *t ) const
265  {
266  *t *= m_unit;
267  }
268 };
269 
272 template <typename T, typename V>
273 bool parserProcess(Parser &p, T *list, unsigned long nlist, V *value)
274 {
275  bool lgFound = false;
276  for (unsigned long option=0; option < nlist; ++option)
277  {
278  if( p.nWord( list[option].key() ) )
279  {
280  list[option]( value );
281  lgFound = true;
282  break;
283  }
284  }
285  return lgFound;
286 }
287 
291 void ParseCosmicRays( Parser &p );
292 
296 void ParseCosmology( Parser &p );
297 
303 
304 void ParseAbundances(Parser &p);
305 
307 void ParseDont(Parser &p);
308 
312 void ParseSave(Parser &p);
313 
314 void parse_save_line(Parser &p,
315  /* true, return rel intensity, false, log of luminosity or intensity I */
316  bool lgLog3,
317  ostringstream& chHeader,
318  long int ipPun
319 );
320 
321 void parse_save_average(Parser &p,
322  /* the file we will write to */
323  long int ipPun,
324  ostringstream& chHeader);
325 
326 void parse_save_colden(Parser &p,
327  /* the header for the file, a list of identifications */
328  ostringstream& chHeader);
329 
330 void Parse_Save_Line_RT(Parser &p);
331 
333 void ParseAge(Parser &p);
334 
338 void ParseAgn(Parser &p);
339 
343 void ParseState(Parser &p);
344 
350 void ParseBlackbody(Parser &p);
351 
355 void ParseCompile(Parser &p );
356 
358 void ParseConstant(Parser &p);
359 
363 void ParseDLaw(Parser &p );
364 
368 void ParseTLaw(Parser &p);
369 
373 void ParseDrive(Parser &p );
374 
379 void ParseGrain(Parser &p);
380 
382 void ParseFluc(Parser &p);
383 
385 void ParseHDEN(Parser &p);
386 
391 void ParseDatabaseISO(long ipISO, Parser &p);
392 
396 void ParseDatabaseH2(Parser &p );
397 
401 void ParseGrid(Parser &p);
402 
404 void ParseInit(Parser &p);
405 
410 void ParseInterp(Parser &p);
411 
417 void ParseIonParI(Parser &p);
418 
425 void ParseIonParX(Parser &p);
431 void ParseIonPar(Parser &p,
432  char chType);
433 
437 void ParseNorm(Parser &p);
438 
442 void ParseOptimize(Parser &p);
443 
447 void ParsePrint(Parser &p );
448 
450 void ParseRadius(Parser &p);
451 
453 void ParseSet(Parser &p);
454 
460 void ParseTable(Parser &p);
461 
463 void ParseTrace(Parser &p);
464 
465 /*ParseExtinguish parse the extinguish command */
466 void ParseExtinguish( Parser &p );
467 
468 /*ParseIlluminate parse the illuminate command */
469 void ParseIlluminate( Parser &p );
470 
471 /*ParseCaseB - parse the Case B command */
472 void ParseCaseB(Parser &p );
473 
475 void ParseTest(Parser &p);
476 
478 void ParseAbsMag(Parser &p);
479 
481 void ParseBackgrd(Parser &p);
482 
484 void ParseCoronal(Parser &p);
485 
487 void ParseElement(Parser &p);
488 
494 void ParseCMB(double z,
495  long int *nqh);
496 
504 void ParseF_nu(
505  Parser &p,
506  const char *chType,
507  bool lgNU2);
508 
512 void ParseGlobule(Parser &p);
513 
515 void ParseRangeOption(Parser &p);
516 
518 void ParseMap(Parser &p);
519 
521 void ParseMetal(Parser &p);
522 
523 void ParsePrtLineSum(Parser &p);
524 
526 void ParsePlot(Parser &p);
527 
530 
532 void ParseRatio(Parser &p);
533 
535 void ParseSphere(Parser &p);
536 
538 void ParseStop(Parser &p);
539 
543 void ParseCrashDo(Parser &p);
544 
545 class Option {
546 public:
549  union {
550  bool l;
551  long i;
552  double r;
553  };
554  string s;
555  Option(bool val)
556  {
557  opttype=BOOL;
558  l = val;
559  }
560  Option(long val)
561  {
562  opttype=LONG;
563  i = val;
564  }
565  Option(double val)
566  {
567  opttype=REAL;
568  r = val;
569  }
570  Option(const string& val, enum Quoted q)
571  {
572  if (q == QUOTED)
573  opttype=STRING;
574  else
575  opttype=OPTION;
576  s = val;
577  }
578 };
579 
581 {
582  bool m_lgDone;
583 public:
584  vector< pair<string, count_ptr<Option> > > p;
585  Properties() : m_lgDone(false) {}
586  void setDone()
587  {
588  m_lgDone = true;
589  }
590  bool isDone() const
591  {
592  return m_lgDone;
593  }
594 };
595 
596 #endif // _PARSER_H_
void ParseState(Parser &p)
Definition: parse_state.cpp:9
void Parse_Save_Line_RT(Parser &p)
Definition: save_line.cpp:274
void setEOL(bool val)
Definition: parser.h:117
bool nMatch(const char *chKey) const
Definition: parser.h:150
void ParseHDEN(Parser &p)
Definition: parse_hden.cpp:10
void parse_save_average(Parser &p, long int ipPun, ostringstream &chHeader)
void ParseAgn(Parser &p)
Definition: parse_agn.cpp:10
bool hasCommand(const char *s2)
Definition: parser.cpp:746
const realnum m_unit
Definition: parser.h:260
void(* OptionParser)(Parser &)
Definition: parser.h:25
void ParseDont(Parser &p)
Definition: parse_dont.cpp:28
vector< pair< string, count_ptr< Option > > > p
Definition: parser.h:584
STATIC long int ipPun
Definition: save_do.cpp:367
void echo(void) const
Definition: parser.cpp:189
double FFmtRead(void)
Definition: parser.cpp:472
void ParseAge(Parser &p)
Definition: parse_age.cpp:38
#define NORETURN
Definition: cpu.h:455
void ParsePlot(Parser &p)
Definition: parse_plot.cpp:16
void ParseDatabaseISO(long ipISO, Parser &p)
void ParseBlackbody(Parser &p)
void setline(const char *const card)
Definition: parser.h:82
Option(double val)
Definition: parser.h:565
double getNumberCheckLogLinNegImplLog(const char *chDesc)
Definition: parser.cpp:410
Parser(void)
Definition: parser.h:57
bool m_lgEOL
Definition: parser.h:50
void ParseBackgrd(Parser &p)
bool Command(const char *name, OptionParser doOpts)
Definition: parser.h:193
long int m_nqh
Definition: parser.h:54
void operator()(realnum *t) const
Definition: parser.h:264
void readList(vector< string > &list, const char *chName)
Definition: parser.cpp:949
Properties()
Definition: parser.h:585
void ParseDatabaseH2(Parser &p)
bool lgReachedEnd()
Definition: parser.cpp:109
void ParseExtinguish(Parser &p)
void ParseTable(Parser &p)
Definition: parse_table.cpp:94
long nMatch(const char *chKey, const char *chCard)
Definition: service.cpp:525
void ParseSet(Parser &p)
Definition: parse_set.cpp:38
void skip_whitespace()
Definition: parser.cpp:45
int GetQuote(string &chLabel)
Definition: parser.cpp:213
char m_card[INPUT_LINE_LENGTH]
Definition: parser.h:45
bool at_end() const
Definition: parser.cpp:37
void ParseRatio(Parser &p)
Definition: parse_ratio.cpp:10
void ParseConstant(Parser &p)
Symbol(enum tokens t, const string &v)
Definition: parser.h:39
void newlineProcess()
Definition: parser.cpp:50
double getNumberDefaultAlwaysLog(const char *chDesc, double fdef)
Definition: parser.cpp:446
bool nMatchErase(const char *chKey)
Definition: parser.h:173
void ParseIonPar(Parser &p, char chType)
long int m_off
Definition: parser.h:49
Definition: lines.h:14
void ParseCrashDo(Parser &p)
bool parserProcess(Parser &p, T *list, unsigned long nlist, V *value)
Definition: parser.h:273
FILE * ioQQQ
Definition: cddefines.cpp:7
void parse_save_line(Parser &p, bool lgLog3, ostringstream &chHeader, long int ipPun)
void ParseStop(Parser &p)
Definition: parse_stop.cpp:17
double getWave()
Definition: parser.cpp:379
void ParseSave(Parser &p)
Definition: parse_save.cpp:85
string s
Definition: parser.h:554
void ParseGrid(Parser &p)
Definition: parse_grid.cpp:10
const char * name
Definition: parser.h:28
bool isBoundaryChar(char c)
Definition: parser.cpp:130
void ParseDLaw(Parser &p)
Definition: parse_dlaw.cpp:10
Definition: parser.h:43
Symbol getSymbol()
Definition: parser.cpp:824
void ParseRangeOption(Parser &p)
void ParsePrint(Parser &p)
NORETURN void StringError() const
Definition: parser.cpp:203
double getNumberPlain(const char *chDesc)
Definition: parser.cpp:388
void ParseSphere(Parser &p)
Definition: parse_sphere.cpp:9
bool m_lgDone
Definition: parser.h:582
void StripComment(string &s, bool lgStripVisible)
Definition: input.cpp:98
void ParseMap(Parser &p)
Definition: parse_map.cpp:9
char m_card_raw[INPUT_LINE_LENGTH]
Definition: parser.h:46
void ParseIlluminate(Parser &p)
bool isDone() const
Definition: parser.h:590
void ParseCosmology(Parser &p)
LineID getLineID()
Definition: parser.cpp:569
void ParseCompile(Parser &p)
OptionParser action
Definition: parser.h:29
void ParseTest(Parser &p)
Definition: parse_test.cpp:9
string StandardFluxUnit(void) const
Definition: parser.cpp:290
Option(long val)
Definition: parser.h:560
void ParseGlobule(Parser &p)
void ParseCaseB(Parser &p)
Definition: parse_caseb.cpp:9
string getRawTail()
Definition: parser.h:217
Quoted
Definition: parser.h:548
bool m_lgDSet
Definition: parser.h:55
void set_point(long int ipnt)
Definition: parser.h:95
void ParseF_nu(Parser &p, const char *chType, bool lgNU2)
Definition: parse_f_nu.cpp:9
Opts
Definition: parser.h:547
void ParseIonParX(Parser &p)
void init()
Definition: parser.h:66
void operator()(realnum *v) const
Definition: parser.h:244
Option(bool val)
Definition: parser.h:555
void showLocation(FILE *io=ioQQQ) const
Definition: parser.cpp:115
void ParseAbsMag(Parser &p)
Definition: parse_absmag.cpp:9
float realnum
Definition: cddefines.h:124
const char * StandardEnergyUnit(void) const
Definition: parser.cpp:286
char m_card_comment[INPUT_LINE_LENGTH]
Definition: parser.h:47
bool last(void) const
Definition: parser.cpp:196
const int INPUT_LINE_LENGTH
Definition: cddefines.h:301
void parse_save_colden(Parser &p, ostringstream &chHeader)
long i
Definition: parser.h:551
void ParseFluc(Parser &p)
Definition: parse_fluc.cpp:9
void ParseNorm(Parser &p)
Definition: parse_norm.cpp:10
void ParseCMB(double z, long int *nqh)
Definition: parse_CMB.cpp:10
NORETURN void NoNumb(const char *chDesc) const
Definition: parser.cpp:345
long int GetElem(void) const
Definition: parser.cpp:321
tokens
Definition: parser.h:36
const char * nWord(const char *chKey) const
Definition: parser.cpp:32
int nMatch1(const char *chKey) const
Definition: parser.h:123
string getFirstChunk(long i)
Definition: parser.cpp:554
void ParseDrive(Parser &p)
Definition: parse_drive.cpp:28
bool isVar(void) const
Definition: parser.cpp:144
const char * nWord(const char *chKey, const char *chCard)
Definition: parser.cpp:62
void ParseTrace(Parser &p)
Definition: parse_trace.cpp:11
long int m_len
Definition: parser.h:48
void ParseAbundancesNonSolar(Parser &p)
void ParseInit(Parser &p)
Definition: parse_init.cpp:10
void ParseGrain(Parser &p)
Definition: parse_grain.cpp:12
void ParsePrtLineSum(Parser &p)
Definition: prt_linesum.cpp:18
void setDone()
Definition: parser.h:586
void ParsePowerlawContinuum(Parser &p)
Definition: parser.h:545
double getNumberCheckAlwaysLogLim(const char *chDesc, double flim)
Definition: parser.cpp:433
void help(FILE *fp) const
Definition: parser.cpp:294
bool l
Definition: parser.h:550
void ParseIonParI(Parser &p)
#define ASSERT(exp)
Definition: cddefines.h:613
KeyAction< V > MakeKeyAction(const char *keyword, const V &action)
Definition: parser.h:252
bool getline()
Definition: parser.cpp:273
KeyAction(const char *keyword, const V &action)
Definition: parser.h:237
NORETURN void Error(const char *msg) const
Definition: parser.cpp:249
bool GetParam(const char *chKey, double *val)
Definition: parser.h:154
double getNumberCheckAlwaysLog(const char *chDesc)
Definition: parser.cpp:427
Option(const string &val, enum Quoted q)
Definition: parser.h:570
void ParseRadius(Parser &p)
Parser(const CloudyCommand *commands)
Definition: parser.h:61
const CloudyCommand *const m_Commands
Definition: parser.h:51
double getNumberDefault(const char *chDesc, double fdef)
Definition: parser.cpp:401
void ParseTLaw(Parser &p)
Definition: parse_tlaw.cpp:13
char current(void) const
Definition: parser.h:74
bool lgEOL(void) const
Definition: parser.h:113
string value
Definition: parser.h:38
void ParseCosmicRays(Parser &p)
int fprintf(const Output &stream, const char *format,...)
Definition: service.cpp:1121
NORETURN void CommandError(void) const
Definition: parser.cpp:260
void getPairs(vector< double > &a, vector< double > &b)
Definition: parser.cpp:768
enum tokens toktype
Definition: parser.h:37
const char *const m_keyword
Definition: parser.h:234
char current_raw() const
Definition: parser.cpp:41
void ParseOptimize(Parser &p)
int PrintLine(FILE *fp) const
Definition: parser.h:206
std::string getVarName(void)
Definition: parser.cpp:148
enum Option::Opts opttype
bool m_lgEOF
Definition: parser.h:55
Definition: parser.h:34
void readLaw(DepthTable &table)
Definition: parser.cpp:976
void ParseMetal(Parser &p)
Definition: parse_metal.cpp:12
bool GetRange(const char *chKey, double *val1, double *val2)
Definition: parser.h:163
UnitConverter(double unit)
Definition: parser.h:262
void doSetVar(void)
Definition: parser.cpp:161
double getNumberDefaultNegImplLog(const char *chDesc, double fdef)
Definition: parser.cpp:455
double getWaveOpt()
Definition: parser.cpp:356
std::map< string, double > m_symtab
Definition: parser.h:52
void ParseCoronal(Parser &p)
const char * key(void) const
Definition: parser.h:240
void ParseElement(Parser &p)
V m_action
Definition: parser.h:235
bool isComment(void) const
Definition: parser.cpp:140
void ParseInterp(Parser &p)
double r
Definition: parser.h:552
void ParseAbundances(Parser &p)
double getNumberCheck(const char *chDesc)
Definition: parser.cpp:392