69 std::string
name =
parseString(stat,
"Identifier or keyword expected.");
89 std::string objName =
parseString(stat,
"Object name expected.");
96 unsigned int position = stat.
position();
97 std::string positionIndicator = std::string(position,
' ') +
"^\n";
98 std::ostringstream statStr;
104 "Syntax error, either the keyword REAL is missing or\n" +
110 "Syntax error, the keyword REAL is missing\n");
121 double time = double(clock()) / double(CLOCKS_PER_SEC);
122 *
gmsg <<
"\nBegin execution: \"" <<
name
123 <<
"\", CPU time = " << time <<
" seconds.\n" <<
endl;
136 double time = double(clock()) / double(CLOCKS_PER_SEC);
137 *
gmsg <<
"\nEnd execution: \"" <<
name
138 <<
"\", CPU time = " << time <<
" seconds.\n" <<
endl;
148 std::string cmdName =
parseString(stat,
"Command name expected");
150 if (cmdName ==
"STOP") {
152 }
else if (cmdName ==
"QUIT") {
154 }
else if (cmdName ==
"HELP" && stat.
delimiter(
',')) {
155 cmdName =
parseString(stat,
"Object name expected");
160 copy =
object->
clone(
"");
170 std::string hint =
getHint(cmdName,
"command");
173 "Syntax error, " + hint);
177 "Command \"" + cmdName +
"\" is unknown.");
201 }
else if (stat.
keyword(
"BOOL")) {
203 }
else if (stat.
keyword(
"REAL")) {
205 }
else if (stat.
keyword(
"STRING")) {
207 }
else if (stat.
keyword(
"VECTOR")) {
214 std::string objName =
parseString(stat,
"Object name expected.");
217 std::string attrName;
221 attrName =
parseString(stat,
"Attribute name expected.");
225 "Invalid type specification for this value.");
228 "The object \"" + objName +
"\" is unknown.");
237 model = realConstant;
241 model = boolConstant;
245 model = realVariable;
255 model = stringConstant;
262 object = model->
clone(objName);
265 throw ParseError(
"OpalParser::parseAssign()",
"Invalid <type> field.");
269 "You cannot redefine the constant \"" + objName +
"\".");
283 throw ParseError(
"Expressions::parseReference()",
284 "Index must be positive.");
292 attr->parseComponent(stat,
true, index);
294 attr->parse(stat,
true);
298 attr->parseComponent(stat,
false, index);
300 attr->parse(stat,
false);
305 "Object \"" + objName +
"\" has no attribute \"" +
316 bool isShared = stat.
keyword(
"SHARED");
317 std::string objName =
parseString(stat,
"Object name expected.");
320 std::string clsName =
parseString(stat,
"Class name expected.");
323 if (classObject == 0) {
324 if (clsName ==
"SURFACEPHYSICS")
326 "The object \"" + clsName +
"\" is changed to \"PARTICLEMATTERINTERACTION\".");
329 "The object \"" + clsName +
"\" is unknown.");
339 copy = classObject->
clone(objName);
363 unsigned int position = stat.
position();
364 std::string positionIndicator = std::string(position + 1,
' ') +
"^\n";
365 std::ostringstream statStr;
371 "Syntax error (maybe missing comma or semicolon ? )");
385 if (--par_level == 0)
break;
393 std::string className =
parseString(stat,
"Class name expected.");
403 throw ParseError(
"OpalParser::parseMacro()",
"Command \"" +
404 macName +
"\" cannot be defined with arguments.");
408 "Object \"" + className +
"\" is unknown.");
425 "Macro \"" + macName +
"\" is unknown.");
434 *
gmsg <<
"\nOpalParser::printHelp(): Unknown object \""
435 << cmdName <<
"\".\n" <<
endl;
437 object->printHelp(std::cerr);
444 while (! token.
isEOF()) {
447 if (token.
isDel(
'(')) {
449 }
else if (token.
isDel(
'[')) {
451 }
else if (token.
isDel(
'{')) {
453 }
else if (token.
isDel(close)) {
464 while (! token.
isEOF()) {
466 if (token.
isDel(
';'))
break;
469 if (token.
isDel(
'(')) {
471 }
else if (token.
isDel(
'[')) {
473 }
else if (token.
isDel(
'{')) {
494 if (token.
isDel(
'{')) {
498 }
else if (token.
isKey(
"IF")) {
502 }
else if (token.
isKey(
"WHILE")) {
512 if (! token.
isEOF()) {
513 if (token.
isDel(
'(')) {
524 if (! token.
isEOF()) {
526 if (token.
isKey(
"MACRO")) {
533 throw ParseError(
"OpalParser::readStatement()",
534 "MACRO definition lacks \"{...}\".");
540 }
else if (! token.
isDel(
';')) {
541 throw ParseError(
"OpalParser::readStatement()",
542 "MACRO call is not terminated by ';'.");
544 }
else if (! token.
isDel(
';')) {
550 }
else if (token.
isDel(
';')) {
553 }
else if (token.
isDel(
'?')) {
555 *
gmsg <<
"\ntry typing \"HELP\" for help.\n" <<
endl;
557 }
else if (! token.
isEOF()) {
562 throw ParseError(
"OpalParser::readStatement()",
563 "Command should begin with a <name>.");
566 ERRORMSG(
"\n*** Parse error detected by function \""
567 <<
"OpalParser::readStatement()" <<
"\"\n");
570 std::string what = ex.
what();
590 Inform errorMsg(
"Error", std::cerr);
591 errorMsg <<
"\n*** Parse error detected by function \""
592 << ex.
where() <<
"\"\n";
593 stat->printWhere(errorMsg,
true);
594 std::string what = ex.
what();
595 size_t pos = what.find_first_of(
'\n');
597 errorMsg <<
" " << what.substr(0, pos) <<
endl;
598 what = what.substr(pos + 1, std::string::npos);
599 pos = what.find_first_of(
'\n');
600 }
while (pos != std::string::npos);
601 errorMsg <<
" " << what <<
endl;
603 MPI_Abort(MPI_COMM_WORLD, -100);
624 return std::string();
627 std::string hint =
"the " + type +
" '" +
name +
"' could belong to\n";
631 if (its.first != its.second) {
633 bool any = (its.first)->second ==
"Any";
634 for (
auto it = std::next(its.first); it != its.second && !
any; ++ it) {
636 any = it->second ==
"Any";
639 hint += std::string(
" - any element\n");
641 hint += std::string(
" - the element") + (std::distance(its.first, its.second) > 1?
"s ":
" ") +
elements +
"\n";
646 std::string commands;
648 if (its.first != its.second) {
649 commands = (its.first)->second;
650 for (
auto it = std::next(its.first); it != its.second; ++ it) {
651 commands +=
", " + it->second;
653 hint += std::string(
" - the command") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + commands +
"\n";
657 std::string sub_commands;
659 if (its.first != its.second) {
660 sub_commands = (its.first)->second;
661 for (
auto it = std::next(its.first); it != its.second; ++ it) {
662 sub_commands +=
", " + it->second;
664 hint += std::string(
" - the sub-command") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + sub_commands +
"\n";
668 std::string statements;
670 if (its.first != its.second) {
671 statements = (its.first)->second;
672 for (
auto it = std::next(its.first); it != its.second; ++ it) {
673 statements +=
", " + it->second;
675 hint += std::string(
" - the statement") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + statements +
"\n";
679 hint +=
"but it's not present!";
Inform & endl(Inform &inf)
bool any(const PETE_Expr< T1 > &expr, T2 val)
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.
std::string replaceAll(const std::string &str, const std::string &from, const std::string &to)
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 * clone(const std::string &name)=0
Return a clone.
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Macro handler function.
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.
virtual void execute(const Parser &)=0
Execute.
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.