OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
MacroCmd.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: MacroCmd.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: MacroCmd
10 //
11 // ------------------------------------------------------------------------
12 //
13 // $Date: 2000/03/27 09:33:43 $
14 // $Author: Andreas Adelmann $
15 //
16 // ------------------------------------------------------------------------
17 
18 #include "OpalParser/MacroCmd.h"
20 #include "OpalParser/OpalParser.h"
21 #include "Parser/Statement.h"
22 #include "Utilities/ParseError.h"
23 #include <vector>
24 #include <cassert>
25 
26 // Class MacroCmd
27 // ------------------------------------------------------------------------
28 
30  Macro(0u, "MACRO",
31  "A \"MACRO\" command defines a subroutine:\n"
32  "\t<name>(<arguments>):MACRO{<body>}"),
33  body(0)
34 {}
35 
36 
37 MacroCmd::MacroCmd(const std::string &name, MacroCmd *parent):
38  Macro(name, parent), body() {
39  body = new MacroStream(name);
40 }
41 
42 
44 {}
45 
46 
48  body->start();
49  itsParser->run(&*body);
50 }
51 
52 
54 (const std::string &name, Statement &statement, const Parser *parser) {
55  parseActuals(statement);
56 
57  // Check for consistency in argument number.
58  if(formals.size() != actuals.size()) {
59  throw ParseError("MacroCmd::makeInstance()",
60  "Inconsistent number of macro arguments.");
61  }
62 
63  // Substitute the actual arguments.
64  MacroCmd *macro = new MacroCmd(name, this);
65  macro->itsParser = parser;
66  body->start();
67  Token token = body->readToken();
68 
69  while(! token.isEOF()) {
70  bool found = false;
71 
72  if(token.isWord()) {
73  std::string word = token.getWord();
74 
75  for(std::vector<std::string>::size_type i = 0;
76  i < formals.size(); i++) {
77  if(word == formals[i]) {
78  std::vector<Token> act = actuals[i];
79  for(Token t : act) {
80  macro->body->append(t);
81  }
82  found = true;
83  break;
84  }
85  }
86  }
87 
88  if(! found) macro->body->append(token);
89  token = body->readToken();
90  }
91 
92  return macro;
93 }
94 
95 
97 (const std::string &name, TokenStream &, Statement &statement) {
98  MacroCmd *macro = new MacroCmd(name, this);
99  macro->parseFormals(statement);
100 
101  // Parse macro body->
102  assert(statement.keyword("MACRO"));
103  Token token;
104 
105  if(statement.delimiter('{')) {
106  int level = 1;
107  while(true) {
108  if(statement.atEnd()) {
109  throw ParseError("MacroCmd::makeTemplate()",
110  "MACRO body is not closed.");
111  } else {
112  token = statement.getCurrent();
113 
114  if(token.isDel('{')) {
115  ++level;
116  } else if(token.isDel('}')) {
117  if(--level == 0) break;
118  }
119 
120  macro->body->append(token);
121  }
122  }
123  } else {
124  throw ParseError("MacroCmd::makeTemplate()",
125  "Missing MACRO body, should be \"{...}\".");
126  }
127 
128  return macro;
129 }
MacroCmd()
Definition: MacroCmd.cpp:29
bool isEOF() const
Test for end of file.
Definition: Token.cpp:107
bool keyword(const char *s)
Test for keyword.
Definition: Statement.cpp:123
Parse exception.
Definition: ParseError.h:32
virtual void run(TokenStream *ts) const =0
Read statements and parse.
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
bool isWord() const
Test for word.
Definition: Token.cpp:127
bool atEnd() const
Test for end of command.
Definition: Statement.cpp:52
virtual void execute()
Execute the macro command.
Definition: MacroCmd.cpp:47
Interface for abstract language parser.
Definition: Parser.h:31
virtual void parseFormals(Statement &)
Parse formal arguments.
Definition: Macro.cpp:100
const Parser * itsParser
Definition: MacroCmd.h:65
Encapsulate the buffer for the ``archetypes&#39;&#39; of all macros.
Definition: MacroCmd.h:33
Interface for statements.
Definition: Statement.h:38
Representation of a single input token.
Definition: Token.h:33
virtual ~MacroCmd()
Definition: MacroCmd.cpp:43
void start()
Reset stream to start.
Definition: MacroStream.cpp:53
void append(Token &)
Append a token to the stream.
Definition: MacroStream.cpp:36
virtual Object * makeTemplate(const std::string &, TokenStream &, Statement &)
Make a macro template.
Definition: MacroCmd.cpp:97
An input buffer for macro commands.
Definition: MacroStream.h:31
Abstract base class for macros.
Definition: Macro.h:34
Pointer< MacroStream > body
Definition: MacroCmd.h:62
The base class for all OPAL objects.
Definition: Object.h:48
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Make a macro instance.
Definition: MacroCmd.cpp:54
const std::string name
Token & getCurrent()
Return current token and skip it.
Definition: Statement.cpp:76
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:103
std::string getWord() const
Return word value.
Definition: Token.cpp:190