42 #include <boost/algorithm/string.hpp>
48 using namespace Expressions;
76 std::string
name =
parseString(stat,
"Identifier or keyword expected.");
96 std::string objName =
parseString(stat,
"Object name expected.");
101 std::string name = tok.
getLex();
102 std::string hint =
getHint(name);
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.");
301 if(stat.
delimiter(
'=') ||
object->isTreeMember(realConstant)) {
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.");
414 macro->makeTemplate(macName, *
inputStack.back(), stat)) {
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);
645 return std::string();
648 std::string hint =
"the " + type +
" '" + name +
"' could belong to\n";
652 if (its.first != its.second) {
653 elements = (its.first)->second;
654 bool any = (its.first)->second ==
"Any";
655 for (
auto it = std::next(its.first);
it != its.second && !
any; ++
it) {
656 elements +=
", " +
it->second;
657 any =
it->second ==
"Any";
660 hint += std::string(
" - any element\n");
662 hint += std::string(
" - the element") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + elements +
"\n";
667 std::string commands;
669 if (its.first != its.second) {
670 commands = (its.first)->second;
671 for (
auto it = std::next(its.first);
it != its.second; ++
it) {
672 commands +=
", " +
it->second;
674 hint += std::string(
" - the command") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + commands +
"\n";
678 std::string sub_commands;
680 if (its.first != its.second) {
681 sub_commands = (its.first)->second;
682 for (
auto it = std::next(its.first);
it != its.second; ++
it) {
683 sub_commands +=
", " +
it->second;
685 hint += std::string(
" - the sub-command") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + sub_commands +
"\n";
689 std::string statements;
691 if (its.first != its.second) {
692 statements = (its.first)->second;
693 for (
auto it = std::next(its.first);
it != its.second; ++
it) {
694 statements +=
", " +
it->second;
696 hint += std::string(
" - the statement") + (std::distance(its.first, its.second) > 1?
"s ":
" ") + statements +
"\n";
700 hint +=
"but it's not present!";
static OpalData * getInstance()
virtual void parse(Statement &)
Parse the object.
virtual bool shouldUpdate() const =0
Update flag.
and give any other recipients of the Program a copy of this License along with the Program You may charge a fee for the physical act of transferring a copy
bool isEOF() const
Test for end of file.
The base class for all OPAL objects.
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
void update()
Update all objects.
static Token readToken()
Return next input token.
void append(const Token &)
Append a token.
virtual void parse(Statement &) const
Parse and execute current statement.
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
virtual const std::string & what() const
Return the message string for the exception.
void start()
Return to start.
virtual Token readToken()=0
Read single token from stream.
void define(Object *newObject)
Define a new object.
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
void makeDirty(Object *object)
Invalidate expressions.
b Accompany it with a written valid for at least three to give any third for a charge no more than your cost of physically performing source a complete machine readable copy of the corresponding source code
virtual void printHelp(const std::string &) const
Print help on named command.
virtual void print(std::ostream &os) const
Print statement.
bool atEnd() const
Test for end of command.
unsigned int position() const
Return current character number in line.
void mark()
Mark position in command.
void restore()
Return to marked position.
Inform & endl(Inform &inf)
void execute(Object *, const std::string &) const
Execute or check the current command.
virtual Statement * readStatement(TokenStream *) const
Read complete statement from a token stream.
const std::string & getLex() const
Return the lexeme.
static void parseBracketList(char close, Statement &)
Token & getCurrent()
Return current token and skip it.
virtual const std::string & where() const
Return the name of the method or function which detected the exception.
Abstract interface for a stream of input tokens.
virtual void setShared(bool)
Set/reset shared flag.
virtual void parseAction(Statement &) const
Parse executable command.
static std::vector< Pointer< TokenStream > > inputStack
Representation of a single input token.
Interface for statements.
bool delimiter(char c)
Test for delimiter.
A simple input statement in token form.
std::string parseString(Statement &, const char msg[])
Parse string value.
static void parseTokenList(Statement &)
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Macro handler function.
double parseRealConst(Statement &)
Parse real constant.
bool isKey(const char *key) const
Test for keyword.
virtual void printWhere(Inform &msg, bool withToken) const
Print position.
virtual void parseMacro(const std::string &name, Statement &) const
Parse macro definition or call.
b mention the algorithm in the References section The appropriate citation is
void stop() const
Set stop flag.
const std::string & getFile() const
Return the token's file name.
static std::multimap< OwnerType, std::string > getOwner(const std::string &att)
bool keyword(const char *s)
Test for keyword.
int getLine() const
Return the token's line number.
Object * find(const std::string &name)
Find entry.
bool isTreeMember(const Object *subTree) const
Test for tree membership.
bool isDel(char del) const
Test for delimiter.
virtual Attribute * findAttribute(const std::string &name)
Find an attribute by name.
bool isString() const
Test for string.
virtual void parseDefine(Statement &) const
Parse definition.
virtual void run() const
Read current stream.
virtual Object * find(const std::string &) const
Find object by name in the main directory.
bool isWord() const
Test for word.
static std::string getHint(const std::string &, const std::string &="attribute")
virtual bool shouldTrace() const =0
Trace flag.
virtual Object * clone(const std::string &name)=0
Return a clone.
virtual void parseEnd(Statement &) const
Check for end of statement.
virtual void parseAssign(Statement &) const
Parse assignment statement.
A representation of an Object attribute.