Xmipp  v3.23.11-Nereus
argsparser.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: J.M.de la Rosa Trevin (jmdelarosa@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your param) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef CORE_ARGSPARSER_H_
27 #define CORE_ARGSPARSER_H_
28 
29 #include <map>
30 #include <iostream>
31 #include "xmipp_strings.h"
32 #include "comment_list.h"
33 
34 //TODO (MARIANA) Please give more documentation and in a good structure e.g. @name (see args.h as example)
35 /* MARIANA: I defined the name of this group. Please define it as you want. */
36 
43 typedef enum {
44  TOK_ID, //identifier
45  TOK_OPT, // -ID or --ID
46  TOK_INT, //integer number
47  TOK_FLOAT, //float number
48  TOK_STR, //string value
49  TOK_MINUS, // - or --
50  TOK_PLUS, // +
51  TOK_EQ, // =
52  TOK_COMM, // Comment, from : until end of line
53  TOK_LAN, // <
54  TOK_RAN, // >
55  TOK_LBRA, // [
56  TOK_RBRA, // ]
57  TOK_SEMI, // semicolor ;
58  TOK_COMMA, // ,
59  TOK_ETC, // ...
60  TOK_END, // end of input
62  TOK_WHERE, // 'WHERE' keyword
63  TOK_ALIAS, // 'ALIAS' keyword
64  TOK_OR, // 'OR' keyword
65  TOK_REQUIRES,// 'REQUIRES' keyword
66  TOK_SECTION // section defined by == Section ==
67 } ArgTokenType;
68 
69 class ProgramDef;
70 
72 class ArgToken
73 {
74 public:
77  int line;
78  int start, end;
79  int visibility;
85  bool starred;
86 
87  static const char * typeString(ArgTokenType type);
88 };
89 
94 class ArgLexer
95 {
96 private:
97  StringVector input;
98  size_t line;
99  size_t pos;
100  size_t offset;
101  ArgToken * pToken;
102  std::map<String, ArgTokenType> reservedWords;
104 
105  //Some utils functions
106  void readSpaces();
107  void readDigits();
108  void readId();
109  void setupToken(ArgTokenType type);
110  void checkVisibility();
111  void checkIndependent();
112  void nextLine();
113 
114 public:
116  ArgLexer();
118  ~ArgLexer();
120  void addLine(const String &line);
125  bool nextToken();
126  ArgToken * currentToken() const;
127  ArgTokenType lookahead() const;
128 
129 };
130 
140 class ASTNode
141 {
142 public:
143  ASTNode(ArgLexer *lexer = NULL, ASTNode * parent = NULL);
144  virtual ~ASTNode()
145  {
146  }
147  ;
148 
149  ASTNode * parent;
153  int visible;
154 
155  virtual bool parse() = 0; //abstract function
156  virtual void check(std::stringstream & errors) = 0; //abstract function
157  virtual bool consume(ArgTokenType type);
158  ArgTokenType lookahead() const;
159  bool lookahead(ArgTokenType type) const;
160  ArgToken * currentToken() const;
161  void nextToken();
162  bool parseCommentList(CommentList &comments);
163  void error(String msg);
164  void unexpectedToken(String msg = "");
165 };
166 
167 class ParamDef;
168 
169 class ArgumentDef: public ASTNode
170 {
171 public:
173  bool isList;
174  bool isType;
175  std::vector<ParamDef*> subParams;
177 
178  ArgumentDef(ArgLexer *lexer, ASTNode * parent);
179  ~ArgumentDef();
180  virtual bool parse();
181  virtual void check(std::stringstream & errors)
182  {
183  }
185  // to pass to this parameter and increase the index
186  bool acceptArguments(std::stringstream &errors, size_t &argIndex, std::vector<const char *> &cmdArguments);
187 };
188 
192 class ParamDef: public ASTNode
193 {
194 public:
195  bool notOptional; //contradictory param not paramal :)
196  bool orBefore;
198  std::vector<ArgumentDef*> arguments;
199  std::vector<const char *> cmdArguments;
200  std::vector<ParamDef*> *exclusiveGroup;
201  int counter;
202 
206 
207  //Empty constructor
208  ParamDef(ArgLexer *lexer, ASTNode * parent);
209  ~ParamDef();
210  bool parseParamList(ArgTokenType startToken, ProgramDef * prog, StringVector &paramList, bool addName);
211  bool parseArgumentList();
212  virtual bool parse();
213  bool checkRequires(std::stringstream & errors, ProgramDef * prog);
214  virtual void check(std::stringstream & errors);
215  bool containsArgument(const String & argName);
216  ArgumentDef * findArgument(const String & argName);
217  bool containsAlias(const String & alias);
218 };
219 
220 class SectionDef: public ASTNode
221 {
222 public:
224  std::vector<ParamDef*> params;
225 
226  SectionDef(ArgLexer * lexer, ASTNode * parent);
227  ~SectionDef();
228  virtual bool parse();
229  virtual void check(std::stringstream & errors)
230  {
231  }
233  void addParamDef(ParamDef * param);
234 };
235 
236 class ProgramDef: public ASTNode
237 {
238 private:
239  std::vector<ParamDef*> *exclusiveGroup;
240 public:
241  std::vector<SectionDef*> sections;
244  std::map<String, ParamDef*> paramsMap;
251 
252  ProgramDef();
253  ~ProgramDef();
254  virtual bool parse();
255  virtual void check(std::stringstream & errors);
256  ParamDef * findParam(const String &param);
258  ParamDef * findAndFillParam(const String &param);
259  const char * getParam(const char * paramName, size_t paramNumber = 0);
260  const char * getParam(const char * paramName, const char * subParam, size_t paramNumber = 0);
261  void addParamName(const String & name, ParamDef *param);
262  void addParamRequires(const String &name);
263  void addParamExclusiveGroup(ParamDef * param);
265  void clear();
267  void read(int argc, const char ** argv, bool reportErrors = true);
270  SectionDef * addSection(String sectionName, int visibility = 0);
271 
272 };
273 
275 #endif /* ARGSPARSER_H_ */
int end
Definition: argsparser.h:78
ArgTokenType type
Type of the token.
Definition: argsparser.h:75
Reserved words.
Definition: argsparser.h:62
std::vector< ParamDef * > * exclusiveGroup
Definition: argsparser.h:200
String name
Definition: argsparser.h:152
bool notOptional
Definition: argsparser.h:195
CommentList comments
Definition: argsparser.h:223
ASTNode * parent
Definition: argsparser.h:147
int visibility
Definition: argsparser.h:83
bool hasDefault
Definition: argsparser.h:176
bool singleOption
Definition: argsparser.h:250
CommentList comments
Definition: argsparser.h:203
std::vector< SectionDef * > sections
Definition: argsparser.h:241
static const char * typeString(ArgTokenType type)
Definition: argsparser.cpp:33
virtual ~ASTNode()
Definition: argsparser.h:144
std::vector< String > StringVector
Definition: xmipp_strings.h:35
int start
Definition: argsparser.h:78
bool starred
Some special mark to tokens.
Definition: argsparser.h:85
StringVector requires
Definition: argsparser.h:205
ProgTransformDimRed * prog
ArgLexer * pLexer
Definition: argsparser.h:150
ArgTokenType
Definition: argsparser.h:43
int visible
Definition: argsparser.h:153
String seeAlso
Definition: argsparser.h:247
std::vector< ParamDef * > subParams
Definition: argsparser.h:175
std::vector< const char * > cmdArguments
Definition: argsparser.h:199
std::vector< ArgumentDef * > arguments
Definition: argsparser.h:198
StringVector pendingRequires
This is for checking that requires names exists.
Definition: argsparser.h:245
bool independent
Definition: argsparser.h:197
virtual void check(std::stringstream &errors)
Definition: argsparser.h:181
virtual void check(std::stringstream &errors)
Definition: argsparser.h:229
String keywords
Definition: argsparser.h:246
String argDefault
Definition: argsparser.h:172
struct _parameter * param
void error(char *s)
Definition: tools.cpp:107
std::vector< ParamDef * > params
All params defined for the program.
Definition: argsparser.h:224
CommentList examples
examples of use
Definition: argsparser.h:243
CommentList usageComments
comments of usage
Definition: argsparser.h:242
std::string String
Definition: xmipp_strings.h:34
int line
line where token was found
Definition: argsparser.h:77
ArgToken token
Definition: argsparser.h:151
file read(std::istream &is)
Definition: pdb2cif.cpp:6200
int counter
for count the number of times it appears in command line
Definition: argsparser.h:201
String nextToken(const String &str, size_t &i)
bool orBefore
Definition: argsparser.h:196
String lexeme
the string literal value of the token
Definition: argsparser.h:76
std::map< String, ParamDef * > paramsMap
Dictionary with all params and alias names.
Definition: argsparser.h:244
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)
StringVector aliases
Definition: argsparser.h:204