location.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // A Bison parser, made by GNU Bison 3.7.2.
  2. // Locations for Bison parsers in C++
  3. // Copyright (C) 2002-2015, 2018-2020 Free Software Foundation, Inc.
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License
  13. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. // As a special exception, you may create a larger work that contains
  15. // part or all of the Bison parser skeleton and distribute that work
  16. // under terms of your choice, so long as that work isn't itself a
  17. // parser generator using the skeleton or a modified version thereof
  18. // as a parser skeleton. Alternatively, if you modify or redistribute
  19. // the parser skeleton itself, you may (at your option) remove this
  20. // special exception, which will cause the skeleton and the resulting
  21. // Bison output files to be licensed under the GNU General Public
  22. // License without this special exception.
  23. // This special exception was added by the Free Software Foundation in
  24. // version 2.2 of Bison.
  25. /**
  26. ** \file pico_sdk/tools/pioasm/gen/location.h
  27. ** Define the yy::location class.
  28. */
  29. #ifndef YY_YY_HOME_GRAHAM_DEV_MU_PICO_SDK_TOOLS_PIOASM_GEN_LOCATION_H_INCLUDED
  30. # define YY_YY_HOME_GRAHAM_DEV_MU_PICO_SDK_TOOLS_PIOASM_GEN_LOCATION_H_INCLUDED
  31. # include <iostream>
  32. # include <string>
  33. # ifndef YY_NULLPTR
  34. # if defined __cplusplus
  35. # if 201103L <= __cplusplus
  36. # define YY_NULLPTR nullptr
  37. # else
  38. # define YY_NULLPTR 0
  39. # endif
  40. # else
  41. # define YY_NULLPTR ((void*)0)
  42. # endif
  43. # endif
  44. namespace yy {
  45. /// A point in a source file.
  46. class position
  47. {
  48. public:
  49. /// Type for file name.
  50. typedef const std::string filename_type;
  51. /// Type for line and column numbers.
  52. typedef int counter_type;
  53. /// Construct a position.
  54. explicit position (filename_type* f = YY_NULLPTR,
  55. counter_type l = 1,
  56. counter_type c = 1)
  57. : filename (f)
  58. , line (l)
  59. , column (c)
  60. {}
  61. /// Initialization.
  62. void initialize (filename_type* fn = YY_NULLPTR,
  63. counter_type l = 1,
  64. counter_type c = 1)
  65. {
  66. filename = fn;
  67. line = l;
  68. column = c;
  69. }
  70. /** \name Line and Column related manipulators
  71. ** \{ */
  72. /// (line related) Advance to the COUNT next lines.
  73. void lines (counter_type count = 1)
  74. {
  75. if (count)
  76. {
  77. column = 1;
  78. line = add_ (line, count, 1);
  79. }
  80. }
  81. /// (column related) Advance to the COUNT next columns.
  82. void columns (counter_type count = 1)
  83. {
  84. column = add_ (column, count, 1);
  85. }
  86. /** \} */
  87. /// File name to which this position refers.
  88. filename_type* filename;
  89. /// Current line number.
  90. counter_type line;
  91. /// Current column number.
  92. counter_type column;
  93. private:
  94. /// Compute max (min, lhs+rhs).
  95. static counter_type add_ (counter_type lhs, counter_type rhs, counter_type min)
  96. {
  97. return lhs + rhs < min ? min : lhs + rhs;
  98. }
  99. };
  100. /// Add \a width columns, in place.
  101. inline position&
  102. operator+= (position& res, position::counter_type width)
  103. {
  104. res.columns (width);
  105. return res;
  106. }
  107. /// Add \a width columns.
  108. inline position
  109. operator+ (position res, position::counter_type width)
  110. {
  111. return res += width;
  112. }
  113. /// Subtract \a width columns, in place.
  114. inline position&
  115. operator-= (position& res, position::counter_type width)
  116. {
  117. return res += -width;
  118. }
  119. /// Subtract \a width columns.
  120. inline position
  121. operator- (position res, position::counter_type width)
  122. {
  123. return res -= width;
  124. }
  125. /** \brief Intercept output stream redirection.
  126. ** \param ostr the destination output stream
  127. ** \param pos a reference to the position to redirect
  128. */
  129. template <typename YYChar>
  130. std::basic_ostream<YYChar>&
  131. operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
  132. {
  133. if (pos.filename)
  134. ostr << *pos.filename << ':';
  135. return ostr << pos.line << '.' << pos.column;
  136. }
  137. /// Two points in a source file.
  138. class location
  139. {
  140. public:
  141. /// Type for file name.
  142. typedef position::filename_type filename_type;
  143. /// Type for line and column numbers.
  144. typedef position::counter_type counter_type;
  145. /// Construct a location from \a b to \a e.
  146. location (const position& b, const position& e)
  147. : begin (b)
  148. , end (e)
  149. {}
  150. /// Construct a 0-width location in \a p.
  151. explicit location (const position& p = position ())
  152. : begin (p)
  153. , end (p)
  154. {}
  155. /// Construct a 0-width location in \a f, \a l, \a c.
  156. explicit location (filename_type* f,
  157. counter_type l = 1,
  158. counter_type c = 1)
  159. : begin (f, l, c)
  160. , end (f, l, c)
  161. {}
  162. /// Initialization.
  163. void initialize (filename_type* f = YY_NULLPTR,
  164. counter_type l = 1,
  165. counter_type c = 1)
  166. {
  167. begin.initialize (f, l, c);
  168. end = begin;
  169. }
  170. /** \name Line and Column related manipulators
  171. ** \{ */
  172. public:
  173. /// Reset initial location to final location.
  174. void step ()
  175. {
  176. begin = end;
  177. }
  178. /// Extend the current location to the COUNT next columns.
  179. void columns (counter_type count = 1)
  180. {
  181. end += count;
  182. }
  183. /// Extend the current location to the COUNT next lines.
  184. void lines (counter_type count = 1)
  185. {
  186. end.lines (count);
  187. }
  188. /** \} */
  189. public:
  190. /// Beginning of the located region.
  191. position begin;
  192. /// End of the located region.
  193. position end;
  194. };
  195. /// Join two locations, in place.
  196. inline location&
  197. operator+= (location& res, const location& end)
  198. {
  199. res.end = end.end;
  200. return res;
  201. }
  202. /// Join two locations.
  203. inline location
  204. operator+ (location res, const location& end)
  205. {
  206. return res += end;
  207. }
  208. /// Add \a width columns to the end position, in place.
  209. inline location&
  210. operator+= (location& res, location::counter_type width)
  211. {
  212. res.columns (width);
  213. return res;
  214. }
  215. /// Add \a width columns to the end position.
  216. inline location
  217. operator+ (location res, location::counter_type width)
  218. {
  219. return res += width;
  220. }
  221. /// Subtract \a width columns to the end position, in place.
  222. inline location&
  223. operator-= (location& res, location::counter_type width)
  224. {
  225. return res += -width;
  226. }
  227. /// Subtract \a width columns to the end position.
  228. inline location
  229. operator- (location res, location::counter_type width)
  230. {
  231. return res -= width;
  232. }
  233. /** \brief Intercept output stream redirection.
  234. ** \param ostr the destination output stream
  235. ** \param loc a reference to the location to redirect
  236. **
  237. ** Avoid duplicate information.
  238. */
  239. template <typename YYChar>
  240. std::basic_ostream<YYChar>&
  241. operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
  242. {
  243. location::counter_type end_col
  244. = 0 < loc.end.column ? loc.end.column - 1 : 0;
  245. ostr << loc.begin;
  246. if (loc.end.filename
  247. && (!loc.begin.filename
  248. || *loc.begin.filename != *loc.end.filename))
  249. ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col;
  250. else if (loc.begin.line < loc.end.line)
  251. ostr << '-' << loc.end.line << '.' << end_col;
  252. else if (loc.begin.column < end_col)
  253. ostr << '-' << end_col;
  254. return ostr;
  255. }
  256. } // yy
  257. #endif // !YY_YY_HOME_GRAHAM_DEV_MU_PICO_SDK_TOOLS_PIOASM_GEN_LOCATION_H_INCLUDED