42 #include <boost/algorithm/string.hpp>
76 std::string
name =
parseString(stat,
"Identifier or keyword expected.");
96 std::string objName =
parseString(stat,
"Object name expected.");
103 unsigned int position = stat.
position();
104 std::string positionIndicator = std::string(position,
' ') +
"^\n";
105 std::ostringstream statStr;
111 "Syntax error, either the keyword REAL is missing or\n" +
117 "Syntax error, the keyword REAL is missing\n");
129 double time = double(clock()) / double(CLOCKS_PER_SEC);
130 *
gmsg <<
"\nBegin execution: \"" <<
name
131 <<
"\", CPU time = " << time <<
" seconds.\n" <<
endl;
144 double time = double(clock()) / double(CLOCKS_PER_SEC);
145 *
gmsg <<
"\nEnd execution: \"" <<
name
146 <<
"\", CPU time = " << time <<
" seconds.\n" <<
endl;
158 std::string cmdName =
parseString(stat,
"Command name expected");
160 if(cmdName ==
"STOP") {
162 }
else if(cmdName ==
"QUIT") {
164 }
else if(cmdName ==
"HELP" && stat.
delimiter(
',')) {
165 cmdName =
parseString(stat,
"Object name expected");
170 copy =
object->
clone(
"");
180 std::string hint =
getHint(cmdName,
"command");
183 "Syntax error, " + hint);
187 "Command \"" + cmdName +
"\" is unknown.");
212 }
else if(stat.
keyword(
"BOOL")) {
214 }
else if(stat.
keyword(
"REAL")) {
216 }
else if(stat.
keyword(
"STRING")) {
218 }
else if(stat.
keyword(
"VECTOR")) {
225 std::string objName =
parseString(stat,
"Object name expected.");
228 std::string attrName;
232 attrName =
parseString(stat,
"Attribute name expected.");
236 "Invalid type specification for this value.");
239 "The object \"" + objName +
"\" is unknown.");
248 model = realConstant;
252 model = boolConstant;
256 model = realVariable;
266 model = stringConstant;
273 object = model->
clone(objName);
276 throw ParseError(
"OpalParser::parseAssign()",
"Invalid <type> field.");
280 "You cannot redefine the constant \"" + objName +
"\".");
294 throw ParseError(
"Expressions::parseReference()",
295 "Index must be positive.");
303 attr->parseComponent(stat,
true, index);
305 attr->parse(stat,
true);
309 attr->parseComponent(stat,
false, index);
311 attr->parse(stat,
false);
316 "Object \"" + objName +
"\" has no attribute \"" +
328 bool isShared = stat.
keyword(
"SHARED");
329 std::string objName =
parseString(stat,
"Object name expected.");
332 std::string clsName =
parseString(stat,
"Class name expected.");
335 if(classObject == 0) {
336 if (clsName ==
"SURFACEPHYSICS")
338 "The object \"" + clsName +
"\" is changed to \"PARTICLEMATTERINTERACTION\".");
341 "The object \"" + clsName +
"\" is unknown.");
351 copy = classObject->
clone(objName);
376 unsigned int position = stat.
position();
377 std::string positionIndicator = std::string(position + 1,
' ') +
"^\n";
378 std::ostringstream statStr;
384 "Syntax error (maybe missing comma or semicolon ? )");
399 if(--par_level == 0)
break;
407 std::string className =
parseString(stat,
"Class name expected.");
417 throw ParseError(
"OpalParser::parseMacro()",
"Command \"" +
418 macName +
"\" cannot be defined with arguments.");
422 "Object \"" + className +
"\" is unknown.");
439 "Macro \"" + macName +
"\" is unknown.");
449 *
gmsg <<
"\nOpalParser::printHelp(): Unknown object \""
450 << cmdName <<
"\".\n" <<
endl;
452 object->printHelp(std::cerr);
460 while(! token.
isEOF()) {
463 if(token.
isDel(
'(')) {
465 }
else if(token.
isDel(
'[')) {
467 }
else if(token.
isDel(
'{')) {
469 }
else if(token.
isDel(close)) {
481 while(! token.
isEOF()) {
483 if(token.
isDel(
';'))
break;
486 if(token.
isDel(
'(')) {
488 }
else if(token.
isDel(
'[')) {
490 }
else if(token.
isDel(
'{')) {
513 if(token.
isDel(
'{')) {
517 }
else if(token.
isKey(
"IF")) {
521 }
else if(token.
isKey(
"WHILE")) {
531 if(! token.
isEOF()) {
532 if(token.
isDel(
'(')) {
543 if(! token.
isEOF()) {
545 if(token.
isKey(
"MACRO")) {
552 throw ParseError(
"OpalParser::readStatement()",
553 "MACRO definition lacks \"{...}\".");
559 }
else if(! token.
isDel(
';')) {
560 throw ParseError(
"OpalParser::readStatement()",
561 "MACRO call is not terminated by ';'.");
563 }
else if(! token.
isDel(
';')) {
569 }
else if(token.
isDel(
';')) {
572 }
else if(token.
isDel(
'?')) {
574 *
gmsg <<
"\ntry typing \"HELP\" for help.\n" <<
endl;
576 }
else if(! token.
isEOF()) {
581 throw ParseError(
"OpalParser::readStatement()",
582 "Command should begin with a <name>.");
585 ERRORMSG(
"\n*** Parse error detected by function \""
586 <<
"OpalParser::readStatement()" <<
"\"\n");
589 std::string what = ex.
what();
590 boost::replace_all(what,
"\n",
"\n ");
608 stat->execute(*
this);
610 Inform errorMsg(
"Error", std::cerr);
611 errorMsg <<
"\n*** Parse error detected by function \""
612 << ex.
where() <<
"\"\n";
613 stat->printWhere(errorMsg,
true);
614 std::string what = ex.
what();
615 size_t pos = what.find_first_of(
'\n');
617 errorMsg <<
" " << what.substr(0, pos) <<
endl;
618 what = what.substr(pos + 1, std::string::npos);
619 pos = what.find_first_of(
'\n');
620 }
while (pos != std::string::npos);
621 errorMsg <<
" " << what <<
endl;
623 MPI_Abort(MPI_COMM_WORLD, -100);
644 if (owner.size() > 0) {
645 std::string hint =
"the " +
type +
" '" +
name +
"' could belong to\n";
649 if (its.first != its.second) {
651 bool any = (its.first)->second ==
"Any";
652 for (
auto it = std::next(its.first); it != its.second && !
any; ++ it) {
654 any = it->second ==
"Any";
657 hint += std::string(
" - any element\n");
659 hint += std::string(
" - the element") + (std::distance(its.first, its.second) > 1?
"s ":
" ") +
elements +
"\n";
664 std::string commands =
"";
666 if (its.first != its.second) {
667 commands = (its.first)->second;
668 for (
auto it = std::next(its.first); it != its.second; ++ it) {
669 commands +=
", " + it->second;
671 hint += std::string(
" - the command") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + commands +
"\n";
675 std::string sub_commands =
"";
677 if (its.first != its.second) {
678 sub_commands = (its.first)->second;
679 for (
auto it = std::next(its.first); it != its.second; ++ it) {
680 sub_commands +=
", " + it->second;
682 hint += std::string(
" - the sub-command") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + sub_commands +
"\n";
686 std::string statements =
"";
688 if (its.first != its.second) {
689 statements = (its.first)->second;
690 for (
auto it = std::next(its.first); it != its.second; ++ it) {
691 statements +=
", " + it->second;
693 hint += std::string(
" - the statement") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + statements +
"\n";
697 hint +=
"but it's not present!";
bool any(const PETE_Expr< T1 > &expr, T2 val)
Inform & endl(Inform &inf)
Representation objects and parsers for attribute expressions.
std::string parseString(Statement &, const char msg[])
Parse string value.
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
double parseRealConst(Statement &)
Parse real constant.
boost::function< boost::tuple< double, bool >arguments_t)> type
A representation of an Object attribute.
static std::multimap< OwnerType, std::string > getOwner(const std::string &att)
The base class for all OPAL objects.
virtual bool shouldTrace() const =0
Trace flag.
virtual Object * makeTemplate(const std::string &, TokenStream &, Statement &)
Macro handler function.
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Macro handler function.
virtual Object * clone(const std::string &name)=0
Return a clone.
virtual void setShared(bool)
Set/reset shared flag.
virtual Attribute * findAttribute(const std::string &name)
Find an attribute by name.
virtual bool shouldUpdate() const =0
Update flag.
bool isTreeMember(const Object *subTree) const
Test for tree membership.
virtual void parse(Statement &)
Parse the object.
void makeDirty(Object *object)
Invalidate expressions.
void update()
Update all objects.
Object * find(const std::string &name)
Find entry.
static OpalData * getInstance()
void define(Object *newObject)
Define a new object.
A simple input statement in token form.
Interface for statements.
Token & getCurrent()
Return current token and skip it.
void append(const Token &)
Append a token.
unsigned int position() const
Return current character number in line.
void restore()
Return to marked position.
virtual void printWhere(Inform &msg, bool withToken) const
Print position.
bool keyword(const char *s)
Test for keyword.
void mark()
Mark position in command.
virtual void print(std::ostream &os) const
Print statement.
bool atEnd() const
Test for end of command.
bool delimiter(char c)
Test for delimiter.
void start()
Return to start.
Representation of a single input token.
bool isString() const
Test for string.
bool isDel(char del) const
Test for delimiter.
bool isWord() const
Test for word.
bool isEOF() const
Test for end of file.
const std::string & getLex() const
Return the lexeme.
bool isKey(const char *key) const
Test for keyword.
int getLine() const
Return the token's line number.
const std::string & getFile() const
Return the token's file name.
Abstract interface for a stream of input tokens.
virtual Token readToken()=0
Read single token from stream.
virtual const std::string & what() const
Return the message string for the exception.
virtual const std::string & where() const
Return the name of the method or function which detected the exception.
virtual void parseAssign(Statement &) const
Parse assignment statement.
static Token readToken()
Return next input token.
static void parseTokenList(Statement &)
virtual void parseEnd(Statement &) const
Check for end of statement.
virtual void run() const
Read current stream.
virtual Object * find(const std::string &) const
Find object by name in the main directory.
virtual void parseMacro(const std::string &name, Statement &) const
Parse macro definition or call.
static void parseBracketList(char close, Statement &)
static std::vector< Pointer< TokenStream > > inputStack
virtual Statement * readStatement(TokenStream *) const
Read complete statement from a token stream.
virtual void printHelp(const std::string &) const
Print help on named command.
void execute(Object *, const std::string &) const
Execute or check the current command.
static std::string getHint(const std::string &, const std::string &="attribute")
virtual void parse(Statement &) const
Parse and execute current statement.
void stop() const
Set stop flag.
virtual void parseDefine(Statement &) const
Parse definition.
virtual void parseAction(Statement &) const
Parse executable command.