1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/parsinc.h,v 1.29 2017/01/12 14:44:05 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati	<masarati@aero.polimi.it>
9  * Paolo Mantegazza	<mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30  */
31 
32 /* Parser per l'ingresso dati - parte generale */
33 
34 /* Si compone di tre diverse strutture di scansione,
35  * piu' le strutture di memorizzazione di stringhe e variabili.
36  *
37  * La prima struttura e' data dal LowParser, che riconosce gli elementi
38  * della sintassi. E' data da:
39  * <statement_list>::=
40  *   <statement> ; <statement_list>
41  *   epsilon
42  * <statement>::=
43  *   <description>
44  *   <description> : <arg_list>
45  * <arg_list>::=
46  *   <arg>
47  *   <arg> , <arg_list>
48  * <arg>::=
49  *   <word>
50  *   <number>
51  * ecc. ecc.
52  *
53  * La seconda struttura e' data dall'HighParser, che riconosce la sintassi
54  * vera e propria. In alternativa al LowParser, qualora sia atteso un valore
55  * numerico esprimibile mediante un'espressione regolare
56  * (espressione matematica), e' possibile invocare la terza struttura,
57  * il MathParser. Questo analizza espressioni anche complesse e multiple,
58  * se correttamente racchiuse tra parentesi.
59  *
60  * L'HighParser deve necessariamente riconoscere una parola chiave nel campo
61  * <description>, mentre puo' trovare parole qualsiasi nel campo <arg>
62  * qualora sia attesa una stringa.
63  *
64  * Le parole chiave vengono fornite all'HighParser attraverso la KeyTable,
65  * ovvero una lista di parole impaccate (senza spazi). L'uso consigliato e':
66  *
67  *   const char sKeyWords[] = { "keyword0",
68  *                              "keyword1",
69  *                              "...",
70  *                              "keywordN"};
71  *
72  *   enum KeyWords { KEYWORD0 = 0,
73  *                   KEYWORD1,
74  *                   ...,
75  *                   KEYWORDN,
76  *                   LASTKEYWORD};
77  *
78  *   KeyTable K((int)LASTKEYWORD, sKeyWords);
79  *
80  * Il MathParser usa una tabella di simboli, ovvero nomi (dotati di tipo)
81  * a cui e' associato un valore. La tabella e' esterna e quindi puo' essere
82  * conservata ed utilizzata in seguito conservando in memoria i nomi
83  * definiti in precedenza.
84  *
85  * A questo punto si puo' generare la tabella dei simboli:
86  *
87  *   int iSymbolTableInitialSize = 10;
88  *   Table T(iSymbolTableInitialSize);
89  *
90  * Quindi si crea il MathParser:
91  *
92  *   MathParser Math(T);
93  *
94  * Infine si genera l'HighParser:
95  *
96  *   HighParser HP(Math, K, StreamIn);
97  *
98  * dove StreamIn e' l'istream da cui avviene la lettura.
99  */
100 
101 
102 #ifndef PARSINC_H
103 #define PARSINC_H
104 
105 #include <fstream>
106 #include <stack>
107 
108 #include "parser.h"
109 
110 /* IncludeParser - begin */
111 
112 class IncludeParser : public HighParser {
113 	friend struct IncludeDR;
114 protected:
115 
116 	/*
117 	 * Struttura e dati per la stack di flussi in ingresso, usata
118 	 * per consentire l'inclusione multipla di files
119 	 * ATTENZIONE: non impedisce l'inclusione ricorsiva, e quindi il loop
120 	 */
121 	struct MyInput {
122 		std::ifstream* pfile;
123 		InputStream* pis;
124 
125 #ifdef USE_INCLUDE_PARSER
126 		char* sPath;
127 		char* sFile;
128 
129 		MyInput(std::ifstream* pf = NULL, InputStream* pi = NULL,
130 			char* sp = NULL, char* sfile = NULL)
pfileMyInput131 		: pfile(pf), pis(pi), sPath(sp), sFile(sfile) {
132 			NO_OP;
133 		};
134 
135 #else /* !USE_INCLUDE_PARSER */
136 		MyInput(std::ifstream* pf = NULL, InputStream* pi = NULL)
pfileMyInput137 		: pfile(pf), pis(pi) {
138 			NO_OP;
139 		};
140 #endif /* !USE_INCLUDE_PARSER */
141 	};
142 
143 	std::stack<MyInput *> myinput;
144 	char* sCurrPath;
145 	char* sInitialPath;
146 	char* sCurrFile;
147 
148 	flag fCheckStack(void);
149 	bool Include_int(void);
150 	virtual void Eof(void);
151 
152 public:
153 	IncludeParser(MathParser& MP, InputStream& streamIn,
154 		const char *initial_file = "initial file");
155 	virtual ~IncludeParser(void);
156 
157 	virtual void Close(void);                  /* "Chiude" i flussi */
158 
159 	virtual const char* GetFileName(enum Delims Del = DEFAULTDELIM);
160 
161 	virtual HighParser::ErrOut GetLineData(void) const;
162 };
163 
164 /* Le funzioni:
165  *   ExpectDescription()
166  *   ExpectArg()
167  * informano il parser di cio' che e' atteso; di default il costruttore
168  * setta ExpectDescription().
169  *
170  * Le funzioni:
171  *   GetDescription()
172  *   IsKeyWord()
173  *   GetWord()
174  * restituiscono un intero che corrisponde alla posizione occupata nella
175  * KeyTable dalle parole corrispondenti, oppure -1 se la parola non e'
176  * trovata. Si noti che IsKeyWord(), in caso di esito negativo, ripristina
177  * l'istream. Tutte preparano poi il parser per la lettura successiva.
178  *
179  * La funzione
180  *   IsKeyWord(const char*)
181  * restituisce 0 se non trova la parola e ripristina l'istream, altrimenti
182  * restituisce 1 e prepara il parser alla lettura successiva.
183  *
184  * Le funzioni
185  *   GetInt(),
186  *   GetReal(),
187  *   GetString(),
188  *   GetStringWithDelims(enum Delims)
189  *   GatFileName(enum Delims)
190  * restituiscono i valori attesi e preparano il parser alla lettura successiva.
191  */
192 
193 /* IncludeParser - end */
194 
195 #endif /* PARSINC_H */
196 
197