OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SequenceTemplate.cpp
Go to the documentation of this file.
1//
2// Class SequenceTemplate
3//
4// An ``archetype'' for a SEQUENCE with arguments.
5// The model is stored in form of a MacroStream. A call to the macro
6// sequence is expanded by first replacing the arguments, and then parsing
7// the resulting stream as a SEQUENCE definition.
8//
9// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
10//
11// All rights reserved
12//
13// This file is part of OPAL.
14//
15// OPAL is free software: you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation, either version 3 of the License, or
18// (at your option) any later version.
19//
20// You should have received a copy of the GNU General Public License
21// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
22//
23
25
26#include "Utility/PAssert.h"
27
28#include <vector>
30#include "Lines/Sequence.h"
36
37// Class SequenceTemplate
38// ------------------------------------------------------------------------
39
41 : Macro(0, "SEQUENCE",
42 "This object defines a beamsequence list with arguments.\n"
43 "\t<name>(<args>);"),
44 body("SEQUENCE") {}
45
46SequenceTemplate::SequenceTemplate(const std::string& name, Object* parent)
47 : Macro(name, parent), body(name) {}
48
50
51SequenceTemplate* SequenceTemplate::clone(const std::string& /*name*/) {
52 throw ParseError("SequenceTemplate::clone()", "You cannot use this object without attributes.");
53}
54
56 const std::string& name, Statement& statement, const Parser*) {
57 MacroStream* expansion = 0;
58 Sequence* instance = 0;
59
60 try {
61 // Parse actuals and check their number.
62 parseActuals(statement);
63 if (formals.size() != actuals.size()) {
64 throw ParseError("MacroCmd::makeInstance()", "Inconsistent number of macro arguments.");
65 }
66
67 // Expand the SEQUENCE macro in token form.
68 body.start();
69 Token token = body.readToken();
70 expansion = new MacroStream(getOpalName());
71 while (!token.isEOF()) {
72 bool found = false;
73 if (token.isWord()) {
74 std::string word = token.getWord();
75 for (std::vector<std::string>::size_type i = 0; 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 // Make the instance and parse it.
91 Sequence* model = dynamic_cast<Sequence*>(OpalData::getInstance()->find("SEQUENCE"));
92 instance = model->clone(name);
93 instance->copyAttributes(*this);
94 expansion->start();
95 SequenceParser parser(instance);
96 parser.run(&*expansion);
97 } catch (...) {
98 delete expansion;
99 delete instance;
100 throw;
101 }
102
103 return instance;
104}
105
107 // Should not be called.
108 return 0;
109}
110
112 // Save the formals.
113 parseFormals(statement);
114 bool isSequence = statement.keyword("SEQUENCE");
115 PAssert(isSequence);
116
117 // Parse the sequence header.
118 Object::parse(statement);
119
120 // Save the sequence body.
121 Token token = is.readToken();
122
123 // Read through ENDSEQUENCE.
124 while (!token.isEOF()) {
125 body.append(token);
126 if (token.isKey("ENDSEQUENCE")) {
127 // Read remainder up to ';'.
128 token = is.readToken();
129 while (!token.isEOF()) {
130 body.append(token);
131 if (token.isDel(';')) break;
132 token = is.readToken();
133 }
134 break;
135 }
136 token = is.readToken();
137 }
138}
An input buffer for macro commands.
Definition MacroStream.h:30
void append(Token &)
Append a token to the stream.
void start()
Reset stream to start.
virtual Token readToken()
Read a token from the stream.
Abstract base class for macros.
Definition Macro.h:33
std::vector< std::vector< Token > > actuals
The actual argument list.
Definition Macro.h:77
virtual void parseFormals(Statement &)
Parse formal arguments.
Definition Macro.cpp:82
std::vector< std::string > formals
The formal argument list.
Definition Macro.h:73
virtual void parseActuals(Statement &)
Parse actual arguments.
Definition Macro.cpp:47
The base class for all OPAL objects.
Definition Object.h:45
const std::string & getOpalName() const
Return object name.
Definition Object.cpp:267
void copyAttributes(const Object &)
Copy attributes from another object.
Definition Object.cpp:50
virtual void parse(Statement &)
Parse the object.
Definition Object.cpp:82
Object * find(const std::string &name)
Find entry.
Definition OpalData.cpp:477
static OpalData * getInstance()
Definition OpalData.cpp:193
virtual void run() const
Read current stream.
Parse exception.
Definition ParseError.h:31
Interface for abstract language parser.
Definition Parser.h:30
The parser for SEQUENCE members.
virtual SequenceTemplate * clone(const std::string &name)
Make clone.
void parseTemplate(TokenStream &, Statement &)
Parse the sequence template.
virtual Object * makeInstance(const std::string &name, Statement &, const Parser *)
Make line instance.
virtual Object * makeTemplate(const std::string &name, TokenStream &, Statement &)
Make a sequence template.
virtual Sequence * clone(const std::string &name)
Make clone.
Definition Sequence.cpp:93
Interface for statements.
Definition Statement.h:37
bool keyword(const char *s)
Test for keyword.
Abstract interface for a stream of input tokens.
Definition TokenStream.h:30
virtual Token readToken()=0
Read single token from stream.
Representation of a single input token.
Definition Token.h:32
bool isDel(char del) const
Test for delimiter.
Definition Token.cpp:81
bool isWord() const
Test for word.
Definition Token.cpp:97
bool isEOF() const
Test for end of file.
Definition Token.cpp:89
std::string getWord() const
Return word value.
Definition Token.cpp:147
bool isKey(const char *key) const
Test for keyword.
Definition Token.cpp:101