Cerata
A library to generate structural hardware designs
template.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <string>
18 #include <vector>
19 #include <fstream>
20 #include <regex>
21 #include <map>
22 
23 namespace cerata::vhdl {
24 
26 struct trloc {
28  trloc(size_t line, size_t start) : line(line), start(start) {}
30  size_t line;
32  size_t start;
33 };
34 
36 class Template {
37  public:
39  explicit Template(std::istream *str);
41  static Template FromString(const std::string& str);
43  static Template FromFile(const std::string &filename);
45  void Analyze();
47  void Replace(const std::string &str, int with);
49  void Replace(const std::string &str, const std::string &with);
51  std::string ToString();
52 
53  private:
55  std::map<std::string, std::vector<trloc>> replace_list_;
57  std::vector<std::string> lines_;
58 };
59 
60 } // namespace cerata::vhdl
cerata::vhdl::Template
Class to hold and modify a VHDL template file.
Definition: template.h:36
cerata::vhdl::trloc::line
size_t line
The line.
Definition: template.h:30
cerata::vhdl::Template::Analyze
void Analyze()
Mark the locations of all replaceable template strings.
Definition: template.cc:72
cerata::vhdl::Template::ToString
std::string ToString()
Return the file as a string.
Definition: template.cc:63
cerata::vhdl::Template::FromString
static Template FromString(const std::string &str)
Construct a Template from an input string.
Definition: template.cc:31
cerata::vhdl::Template::FromFile
static Template FromFile(const std::string &filename)
Construct a Template from a file.
Definition: template.cc:38
cerata::vhdl::trloc::start
size_t start
The starting character index on the line.
Definition: template.h:32
cerata::vhdl::trloc
Structure to hold a template replacement string location.
Definition: template.h:26
cerata::vhdl::Template::Template
Template(std::istream *str)
Construct a Template from an input stream.
Definition: template.cc:23
cerata::vhdl::trloc::trloc
trloc(size_t line, size_t start)
Template replacement location constructor.
Definition: template.h:28
cerata::vhdl::Template::Replace
void Replace(const std::string &str, int with)
Replace a template replacement string with some number.
Definition: template.cc:50
cerata::vhdl
Contains everything related to the VHDL back-end.
Definition: architecture.cc:31