Cerata
A library to generate structural hardware designs
block.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 <vector>
18 #include <string>
19 #include <algorithm>
20 #include <optional>
21 
22 namespace cerata::vhdl {
23 
25 struct Line {
26  Line() = default;
28  explicit Line(const std::string &str) {
29  parts.push_back(str);
30  }
32  [[nodiscard]] bool IsBlank() const {
33  if (parts.empty()) {
34  return true;
35  }
36  if (parts.size() == 1) {
37  if (parts[0] == "\n") {
38  return true;
39  }
40  }
41  return false;
42  }
44  [[nodiscard]] std::string ToString() const;
46  std::vector<std::string> parts;
47 };
48 
50 struct Block {
52  explicit Block(int indent = 0) : indent(indent) {}
54  [[nodiscard]] std::vector<size_t> GetAlignments() const;
56  Block &Reverse();
59 
65  Block &Sort(std::optional<char> c = std::nullopt);
66 
68  [[nodiscard]] std::string ToString() const;
69 
71  std::vector<Line> lines;
73  int indent = 0;
74 };
75 
77 struct MultiBlock {
79  explicit MultiBlock(int indent = 0) : indent(indent) {}
81  [[nodiscard]] std::string ToString() const;
83  std::vector<Block> blocks;
85  int indent = 0;
86 };
87 
89 Line &operator+=(Line &lhs, const std::string &str); //NOLINT
90 
92 Line &operator<<(Line &lhs, const std::string &str);
93 
95 Line &operator<<(Line &lhs, const Line &rhs);
96 
98 Block &operator<<(Block &lhs, const Line &line);
99 
101 Block &operator<<(Block &lhs, const Block &rhs);
102 
104 Block &operator<<(Block &lhs, const std::string &rhs);
105 
107 Block &operator<<=(Block &lhs, const std::string &rhs); //NOLINT
108 
110 Block &Prepend(const std::string &lhs, Block *rhs, const std::string &sep = "_");
111 
113 MultiBlock &operator<<(MultiBlock &lhs, const Block &rhs);
114 
116 MultiBlock &operator<<(MultiBlock &lhs, const MultiBlock &rhs);
117 
119 MultiBlock &operator<<(MultiBlock &lhs, const Line &rhs);
120 
122 std::string ToString(const std::vector<Block> &blocks);
123 
124 } // namespace cerata::vhdl
cerata::vhdl::Line::parts
std::vector< std::string > parts
The parts of the line of code.
Definition: block.h:46
cerata::vhdl::MultiBlock
A structure to hold multiple blocks.
Definition: block.h:77
cerata::vhdl::Block::lines
std::vector< Line > lines
Lines in the blocks.
Definition: block.h:71
cerata::vhdl::MultiBlock::MultiBlock
MultiBlock(int indent=0)
Multiblock constructor.
Definition: block.h:79
cerata::vhdl::Block::ToString
std::string ToString() const
Return this block as a single string.
Definition: block.cc:57
cerata::vhdl::MultiBlock::blocks
std::vector< Block > blocks
The blocks in this multiblock.
Definition: block.h:83
cerata::vhdl::Line
A line of code.
Definition: block.h:25
cerata::vhdl::Line::ToString
std::string ToString() const
Return the line as a single string.
Definition: block.cc:194
cerata::vhdl::MultiBlock::indent
int indent
Indent level.
Definition: block.h:85
cerata::vhdl::operator<<=
Block & operator<<=(Block &lhs, const std::string &rhs)
Append a string to the last parts of all lines in a block, except the last one.
Definition: block.cc:150
cerata::vhdl::Prepend
Block & Prepend(const std::string &lhs, Block *rhs, const std::string &sep)
Prepend a string to every line of a block.
Definition: block.cc:118
cerata::vhdl::operator+=
Line & operator+=(Line &lhs, const std::string &str)
Append a string to the last line part.
Definition: block.cc:38
cerata::vhdl::Line::Line
Line(const std::string &str)
Line constructor.
Definition: block.h:28
cerata::vhdl::Block::Block
Block(int indent=0)
Block constructor.
Definition: block.h:52
cerata::vhdl::ToString
std::string ToString(const std::vector< Block > &blocks)
Return a vector of blocks as a single string.
Definition: block.cc:186
cerata::vhdl::MultiBlock::ToString
std::string ToString() const
Return this multiblock as a single string.
Definition: block.cc:178
cerata::vhdl::Block
A block of code.
Definition: block.h:50
cerata::vhdl::operator<<
Line & operator<<(Line &lhs, const std::string &str)
Append a part to a line.
Definition: block.cc:28
cerata::vhdl::Block::Sort
Block & Sort(std::optional< char > c=std::nullopt)
Sort the lines in the block. Supply a character to stop sorting per line after encountering the chara...
Definition: block.cc:85
cerata::vhdl::Block::indent
int indent
Indenting level of the block.
Definition: block.h:73
cerata::vhdl::Block::Reverse
Block & Reverse()
Return the block in reverse.
Definition: block.cc:80
cerata::vhdl::Block::GetAlignments
std::vector< size_t > GetAlignments() const
Return the alignment for each line.
Definition: block.cc:43
cerata::vhdl::Line::IsBlank
bool IsBlank() const
Return true if line is blank.
Definition: block.h:32
cerata::vhdl::Block::AppendBlankLineIfNotEmpty
Block & AppendBlankLineIfNotEmpty()
Append a blank line if the block is not empty.
Definition: block.cc:99
cerata::vhdl
Contains everything related to the VHDL back-end.
Definition: architecture.cc:31