OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Macro.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Macro.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Macro/
10 // Abstract base class for OPAL macro-like commands.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2000/03/27 09:33:43 $
15 // $Author: Andreas Adelmann $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "OpalParser/Macro.h"
21 #include "Parser/Statement.h"
22 #include "Utilities/ParseError.h"
23 
24 
25 // Class Macro
26 // ------------------------------------------------------------------------
27 
28 Macro::Macro(int size, const char *name, const char *help):
29  Object(size, name, help), formals(), actuals()
30 {}
31 
32 
33 Macro::Macro(const std::string &name, Object *parent):
34  Object(name, parent), formals(), actuals()
35 {}
36 
37 
39 {}
40 
41 
42 Macro *Macro::clone(const std::string &) {
43  throw ParseError("Macro::clone()",
44  "You cannot execute the command \"" + getOpalName() +
45  "\" without parameters.");
46 }
47 
48 
49 const std::string Macro::getCategory() const {
50  return "MACRO";
51 }
52 
53 
54 bool Macro::shouldTrace() const {
55  return false;
56 }
57 
58 
59 bool Macro::shouldUpdate() const {
60  return false;
61 }
62 
63 
65  // We start after the opening '('.
66  actuals.clear();
67 
68  if(! stat.delimiter(')')) {
69  int level = 1;
70  std::vector<Token> act;
71 
72  while(true) {
73  Token token = stat.getCurrent();
74 
75  if(token.isDel(',')) {
76  // Comma at level=1 terminates an actual argument.
77  if(level == 1) {
78  actuals.push_back(act);
79  act.clear();
80  continue;
81  }
82  } else if(token.isDel('(')) {
83  level++;
84  } else if(token.isDel(')')) {
85  // Closing parenthesis at level=1 terminates the actuals list.
86  if(--level == 0) {
87  actuals.push_back(act);
88  act.clear();
89  break;
90  }
91  }
92 
93  // In all other casses append token to current actual.
94  act.push_back(token);
95  }
96  }
97 }
98 
99 
101  // We start after the opening '('.
102  formals.clear();
103  if(! stat.delimiter(')')) {
104  do {
105  std::string form =
106  Expressions::parseString(stat, "Expected formal argument name.");
107  formals.push_back(form);
108  } while(stat.delimiter(','));
109 
110  Expressions::parseDelimiter(stat, ')');
111  }
112 
113  Expressions::parseDelimiter(stat, ':');
114 }
Parse exception.
Definition: ParseError.h:32
virtual bool shouldTrace() const
Trace flag.
Definition: Macro.cpp:54
virtual void parseActuals(Statement &)
Parse actual arguments.
Definition: Macro.cpp:64
std::vector< std::vector< Token > > actuals
The actual argument list.
Definition: Macro.h:82
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
virtual bool shouldUpdate() const
Update flag.
Definition: Macro.cpp:59
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
virtual void parseFormals(Statement &)
Parse formal arguments.
Definition: Macro.cpp:100
Interface for statements.
Definition: Statement.h:38
Representation of a single input token.
Definition: Token.h:33
virtual const std::string getCategory() const
Return the object category as a string.
Definition: Macro.cpp:49
virtual ~Macro()
Definition: Macro.cpp:38
virtual Macro * clone(const std::string &name)
Make clone.
Definition: Macro.cpp:42
Abstract base class for macros.
Definition: Macro.h:34
The base class for all OPAL objects.
Definition: Object.h:48
const std::string name
Token & getCurrent()
Return current token and skip it.
Definition: Statement.cpp:76
bool isDel(char del) const
Test for delimiter.
Definition: Token.cpp:92
std::string parseString(Statement &, const char msg[])
Parse string value.
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:103
std::vector< std::string > formals
The formal argument list.
Definition: Macro.h:78