glr2.cc: get rid of the macros wrapping the lookahead
[bison.git] / data / skeletons / stack.hh
blob7ba9e5f9af2c0ae4ded1793a65f88699e1a241da
1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2015, 2018-2020 Free Software Foundation, Inc.
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.
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.
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/>.
19 # b4_stack_file
20 # -------------
21 # Name of the file containing the stack class, if we want this file.
22 b4_header_if([b4_required_version_if([30200], [],
23 [m4_define([b4_stack_file], [stack.hh])])])
26 # b4_stack_define
27 # ---------------
28 m4_define([b4_stack_define],
29 [[ /// A stack with random access from its top.
30 template <typename T, typename S = std::vector<T> >
31 class stack
33 public:
34 // Hide our reversed order.
35 typedef typename S::iterator iterator;
36 typedef typename S::const_iterator const_iterator;
37 typedef typename S::size_type size_type;
38 typedef typename std::ptrdiff_t index_type;
40 stack (size_type n = 200)
41 : seq_ (n)
44 #if 201103L <= YY_CPLUSPLUS
45 /// Non copyable.
46 stack (const stack&) = delete;
47 /// Non copyable.
48 stack& operator= (const stack&) = delete;
49 #endif
51 /// Random access.
52 ///
53 /// Index 0 returns the topmost element.
54 const T&
55 operator[] (index_type i) const
57 return seq_[size_type (size () - 1 - i)];
60 /// Random access.
61 ///
62 /// Index 0 returns the topmost element.
64 operator[] (index_type i)
66 return seq_[size_type (size () - 1 - i)];
69 /// Steal the contents of \a t.
70 ///
71 /// Close to move-semantics.
72 void
73 push (YY_MOVE_REF (T) t)
75 seq_.push_back (T ());
76 operator[] (0).move (t);
79 /// Pop elements from the stack.
80 void
81 pop (std::ptrdiff_t n = 1) YY_NOEXCEPT
83 for (; 0 < n; --n)
84 seq_.pop_back ();
87 /// Pop all elements from the stack.
88 void
89 clear () YY_NOEXCEPT
91 seq_.clear ();
94 /// Number of elements on the stack.
95 index_type
96 size () const YY_NOEXCEPT
98 return index_type (seq_.size ());
101 /// Iterator on top of the stack (going downwards).
102 const_iterator
103 begin () const YY_NOEXCEPT
105 return seq_.begin ();
108 /// Bottom of the stack.
109 const_iterator
110 end () const YY_NOEXCEPT
112 return seq_.end ();
115 /// Present a slice of the top of a stack.
116 class slice
118 public:
119 slice (const stack& stack, index_type range)
120 : stack_ (stack)
121 , range_ (range)
124 const T&
125 operator[] (index_type i) const
127 return stack_[range_ - i];
130 private:
131 const stack& stack_;
132 index_type range_;
135 private:
136 #if YY_CPLUSPLUS < 201103L
137 /// Non copyable.
138 stack (const stack&);
139 /// Non copyable.
140 stack& operator= (const stack&);
141 #endif
142 /// The wrapped container.
143 S seq_;
147 m4_ifdef([b4_stack_file],
148 [b4_output_begin([b4_dir_prefix], [b4_stack_file])[
149 ]b4_generated_by[
150 // Starting with Bison 3.2, this file is useless: the structure it
151 // used to define is now defined with the parser itself.
153 // To get rid of this file:
154 // 1. add '%require "3.2"' (or newer) to your grammar file
155 // 2. remove references to this file from your build system.
156 ]b4_output_end[