OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
LineTemplate.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: LineTemplate.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: LineTemplate
10 // The class for storage of OPAL beam lines with arguments.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2000/03/29 10:41:39 $
15 // $Author: opal $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Lines/LineTemplate.h"
21 #include "Lines/Line.h"
22 #include "Parser/SimpleStatement.h"
23 #include "Utilities/ParseError.h"
24 #include <vector>
25 #include <cassert>
26 
27 // Class LineTemplate
28 // ------------------------------------------------------------------------
29 
31  Macro(0, "LINE",
32  "This object defines a beamline list with arguments.\n"
33  "\t<name>(<args>);"),
34  body("LINE")
35 {}
36 
37 
38 LineTemplate::LineTemplate(const std::string &name, Object *parent):
39  Macro(name, parent), body(name)
40 {}
41 
42 
44 {}
45 
46 
47 LineTemplate *LineTemplate::clone(const std::string &name) {
48  throw ParseError("LineTemplate::clone()",
49  "You cannot use this object without attributes.");
50 }
51 
52 
54 (const std::string &name, Statement &statement, const Parser *) {
55  Line *instance = 0;
56 
57  try {
58  // Parse actuals and check their number.
59  parseActuals(statement);
60  if(formals.size() != actuals.size()) {
61  throw ParseError("LineTemplate::makeInstance()",
62  "Inconsistent number of macro arguments.");
63  }
64 
65  // Expand the LINE macro in token form.
66  body.start();
67  Token token = body.readToken();
68  SimpleStatement expansion(getOpalName(), 1);
69  while(! token.isEOF()) {
70  bool found = false;
71  if(token.isWord()) {
72  std::string word = token.getWord();
73 
74  for(std::vector<std::string>::size_type i = 0;
75  i < formals.size(); i++) {
76  if(word == formals[i]) {
77  std::vector<Token> act = actuals[i];
78  for(Token t : act) {
79  expansion.append(t);
80  }
81  found = true;
82  break;
83  }
84  }
85  }
86  if(! found) expansion.append(token);
87  token = body.readToken();
88  }
89 
90  // Parse the modified line.
91  Line *model = dynamic_cast<Line *>(OpalData::getInstance()->find("LINE"));
92  instance = model->clone(name);
93  instance->copyAttributes(*this);
94  expansion.start();
95  instance->parse(expansion);
96  } catch(...) {
97  delete instance;
98  throw;
99  }
100 
101  return instance;
102 }
103 
104 
106  // Should not be called.
107  return 0;
108 }
109 
110 
112  parseFormals(statement);
113  assert(statement.keyword("LINE"));
114 
115  // Store the template list.
116  Token token = statement.getCurrent();
117  if(token.isDel('=')) {
118  body.append(token);
119  int level = 0;
120  while(! statement.atEnd()) {
121  token = statement.getCurrent();
122  if(token.isDel('(')) {
123  level++;
124  } else if(token.isDel(')')) {
125  body.append(token);
126  if(--level == 0) break;
127  }
128  body.append(token);
129  }
130  } else {
131  throw ParseError("Line::makeTemplate()",
132  "Equals sign '=' expected.");
133  }
134 
135 }
virtual ~LineTemplate()
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
void copyAttributes(const Object &)
Copy attributes from another object.
Definition: Object.cpp:57
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
virtual void parseActuals(Statement &)
Parse actual arguments.
Definition: Macro.cpp:64
bool isWord() const
Test for word.
Definition: Token.cpp:127
bool atEnd() const
Test for end of command.
Definition: Statement.cpp:52
virtual Object * makeInstance(const std::string &name, Statement &stat, const Parser *)
Make line instance.
std::vector< std::vector< Token > > actuals
The actual argument list.
Definition: Macro.h:82
virtual Line * clone(const std::string &name)
Make clone.
Definition: Line.cpp:119
Interface for abstract language parser.
Definition: Parser.h:31
static OpalData * getInstance()
Definition: OpalData.cpp:209
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
virtual Token readToken()
Read a token from the stream.
Definition: MacroStream.cpp:41
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
void start()
Reset stream to start.
Definition: MacroStream.cpp:53
void start()
Return to start.
Definition: Statement.cpp:182
void append(Token &)
Append a token to the stream.
Definition: MacroStream.cpp:36
virtual Object * makeTemplate(const std::string &, TokenStream &, Statement &)
Make a line template.
void append(const Token &)
Append a token.
Definition: Statement.cpp:47
void parseTemplate(TokenStream &is, Statement &stat)
Parse the line template.
Abstract base class for macros.
Definition: Macro.h:34
An ``archetype&#39;&#39; for a OPAL beam line with arguments.
Definition: LineTemplate.h:36
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
The base class for all OPAL objects.
Definition: Object.h:48
A simple input statement in token form.
const std::string name
virtual void parse(Statement &stat)
Parse the line object.
Definition: Line.cpp:152
Token & getCurrent()
Return current token and skip it.
Definition: Statement.cpp:76
MacroStream body
Definition: LineTemplate.h:73
The LINE definition.
Definition: Line.h:36
virtual LineTemplate * clone(const std::string &name)
Make clone.
std::vector< std::string > formals
The formal argument list.
Definition: Macro.h:78
std::string getWord() const
Return word value.
Definition: Token.cpp:190