]>
Commit | Line | Data |
---|---|---|
51bacae6 AD |
1 | # C++ skeleton for Bison |
2 | ||
3209eb1c | 3 | # Copyright (C) 2002-2015 Free Software Foundation, Inc. |
51bacae6 AD |
4 | |
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | m4_pushdef([b4_copyright_years], | |
3209eb1c | 19 | [2002-2015]) |
51bacae6 | 20 | |
5de5b987 AD |
21 | # b4_stack_define |
22 | # --------------- | |
23 | m4_define([b4_stack_define], | |
b809770e AD |
24 | [[ /// A stack with random access from its top. |
25 | template <class T, class S = std::vector<T> > | |
51bacae6 AD |
26 | class stack |
27 | { | |
28 | public: | |
29 | // Hide our reversed order. | |
30 | typedef typename S::reverse_iterator iterator; | |
31 | typedef typename S::const_reverse_iterator const_iterator; | |
32 | ||
33 | stack () | |
34 | : seq_ () | |
35 | { | |
573654ca | 36 | seq_.reserve (200); |
51bacae6 AD |
37 | } |
38 | ||
0d40b364 | 39 | stack (unsigned n) |
51bacae6 | 40 | : seq_ (n) |
573654ca | 41 | {} |
51bacae6 | 42 | |
b809770e AD |
43 | /// Random access. |
44 | /// | |
45 | /// Index 0 returns the topmost element. | |
51bacae6 | 46 | T& |
0d40b364 | 47 | operator[] (unsigned i) |
51bacae6 | 48 | { |
e7b26e94 | 49 | return seq_[seq_.size () - 1 - i]; |
51bacae6 AD |
50 | } |
51 | ||
b809770e AD |
52 | /// Random access. |
53 | /// | |
54 | /// Index 0 returns the topmost element. | |
51bacae6 | 55 | const T& |
0d40b364 | 56 | operator[] (unsigned i) const |
51bacae6 | 57 | { |
e7b26e94 | 58 | return seq_[seq_.size () - 1 - i]; |
51bacae6 AD |
59 | } |
60 | ||
97ae878e AD |
61 | /// Steal the contents of \a t. |
62 | /// | |
63 | /// Close to move-semantics. | |
51bacae6 | 64 | void |
97ae878e | 65 | push (T& t) |
51bacae6 | 66 | { |
97ae878e AD |
67 | seq_.push_back (T()); |
68 | operator[](0).move (t); | |
51bacae6 AD |
69 | } |
70 | ||
51bacae6 | 71 | void |
0d40b364 | 72 | pop (unsigned n = 1) |
51bacae6 AD |
73 | { |
74 | for (; n; --n) | |
e7b26e94 | 75 | seq_.pop_back (); |
51bacae6 AD |
76 | } |
77 | ||
e83be476 AD |
78 | void |
79 | clear () | |
80 | { | |
81 | seq_.clear (); | |
82 | } | |
83 | ||
51bacae6 AD |
84 | typename S::size_type |
85 | size () const | |
86 | { | |
87 | return seq_.size (); | |
88 | } | |
89 | ||
51bacae6 AD |
90 | const_iterator |
91 | begin () const | |
92 | { | |
93 | return seq_.rbegin (); | |
94 | } | |
95 | ||
51bacae6 AD |
96 | const_iterator |
97 | end () const | |
98 | { | |
99 | return seq_.rend (); | |
100 | } | |
101 | ||
102 | private: | |
e83be476 AD |
103 | stack (const stack&); |
104 | stack& operator= (const stack&); | |
51bacae6 AD |
105 | /// The wrapped container. |
106 | S seq_; | |
107 | }; | |
108 | ||
109 | /// Present a slice of the top of a stack. | |
110 | template <class T, class S = stack<T> > | |
111 | class slice | |
112 | { | |
113 | public: | |
0d40b364 | 114 | slice (const S& stack, unsigned range) |
a9b67b5b AD |
115 | : stack_ (stack) |
116 | , range_ (range) | |
573654ca | 117 | {} |
51bacae6 | 118 | |
51bacae6 | 119 | const T& |
0d40b364 | 120 | operator [] (unsigned i) const |
51bacae6 AD |
121 | { |
122 | return stack_[range_ - i]; | |
123 | } | |
124 | ||
125 | private: | |
126 | const S& stack_; | |
0d40b364 | 127 | unsigned range_; |
51bacae6 | 128 | }; |
5de5b987 AD |
129 | ]]) |
130 | ||
131 | b4_defines_if( | |
064e42b0 | 132 | [b4_output_begin([b4_dir_prefix[]stack.hh]) |
5de5b987 AD |
133 | b4_copyright([Stack handling for Bison parsers in C++])[ |
134 | ||
135 | /** | |
136 | ** \file ]b4_dir_prefix[stack.hh | |
137 | ** Define the ]b4_namespace_ref[::stack class. | |
138 | */ | |
139 | ||
140 | ]b4_cpp_guard_open([b4_dir_prefix[]stack.hh])[ | |
141 | ||
e7b26e94 | 142 | # include <vector> |
5de5b987 AD |
143 | |
144 | ]b4_namespace_open[ | |
145 | ]b4_stack_define[ | |
51bacae6 AD |
146 | ]b4_namespace_close[ |
147 | ||
22172d47 | 148 | ]b4_cpp_guard_close([b4_dir_prefix[]stack.hh]) |
1c7ec959 | 149 | b4_output_end() |
5de5b987 | 150 | ]) |
1c7ec959 AD |
151 | |
152 | m4_popdef([b4_copyright_years]) |