position_iterator.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <html>
  2. <head>
  3. <title>Position Iterator</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <link rel="stylesheet" href="theme/style.css" type="text/css">
  6. </head>
  7. <body>
  8. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  9. <tr>
  10. <td width="10">
  11. </td>
  12. <td width="85%"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Position
  13. Iterator</b></font> </td>
  14. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  15. </tr>
  16. </table>
  17. <br>
  18. <table border="0">
  19. <tr>
  20. <td width="10"></td>
  21. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  22. <td width="30"><a href="file_iterator.html"><img src="theme/l_arr.gif" border="0"></a></td>
  23. <td width="30"><a href="debugging.html"><img src="theme/r_arr.gif" border="0"></a></td>
  24. </tr>
  25. </table>
  26. <p>Often, when writing a parser that is able to detect errors in the format of
  27. the input stream, we want it to communicate to the user where the error happened
  28. within that input. The classic example is when writing a compiler or interpreter
  29. that detects syntactical errors in the parsed program, indicating the line number
  30. and maybe even the position within the line where the error was found.</p>
  31. <p> The class position_iterator is a tool provided within Spirit that allows parser
  32. writers to easily implement this functionality. The concept is quite simple:
  33. this class is an iterator wrapper that keeps track of the current position within
  34. the file, including current file, line and column. It requires a single template
  35. parameter, which should be the type of the iterator that is to be wrapped.</p>
  36. <p> To use it, you'll need to add the following include:</p>
  37. <pre>
  38. <code><span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>spirit</span><span class=special>/</span><span class=identifier>iterator</span><span class=special>/</span><span class=identifier>position_iterator</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span></code></pre>
  39. <p> Or include all the iterators in Spirit:</p>
  40. <pre>
  41. <code><span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>spirit</span><span class=special>/</span><span class=identifier>iterator</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span></code></pre>
  42. <p> To construct the wrapper, it needs both the begin and end iterators of the
  43. input sequence, and the file name of the input sequence. Optionally, you can
  44. also specify the starting line and column numbers, which default to 1. Default
  45. construction, with no parameters, creates a generic end-of-sequence iterator,
  46. in a similar manner as it's done in the stream operators of the standard C++
  47. library.</p>
  48. <p> The wrapped iterator must belong to the input or forward iterator category,
  49. and the position_iterator just inherits that category.</p>
  50. <p> For example, to create begin and end positional iterators from an input C-
  51. string, you'd use:</p>
  52. <pre>
  53. <code><span class=keyword>char </span><span class=keyword>const</span><span class=special>* </span><span class=identifier>inputstring </span><span class=special>= </span><span class=string>&quot;...&quot;</span><span class=special>;
  54. </span><span class=keyword>typedef </span><span class=identifier>position_iterator</span><span class=special>&lt;</span><span class=keyword>char </span><span class=keyword>const</span><span class=special>*&gt; </span><span class=identifier>iterator_t</span><span class=special>;
  55. </span><span class=identifier>iterator_t </span><span class=identifier>begin</span><span class=special>(</span><span class=identifier>inputstring</span><span class=special>, </span><span class=identifier>inputstring</span><span class=special>+</span><span class=identifier>strlen</span><span class=special>(</span><span class=identifier>inputstring</span><span class=special>));
  56. </span><span class=identifier>iterator_t </span><span class=identifier>end</span><span class=special>;</span></code></pre>
  57. <a name="operations"></a>
  58. <h2>Operations</h2>
  59. <pre>
  60. <code><span class=keyword>void </span><span class=identifier>set_position</span><span class=special>(</span><span class=identifier>file_position </span><span class=keyword>const</span><span class=special>&amp;);</span></code></pre>
  61. <p> Call this function when you need to change the current position stored in
  62. the iterator. For example, if parsing C-style #include directives, the included
  63. file's input must be marked by restarting the file and column to 1 and 1 and
  64. the name to the new file's name.<br>
  65. </p>
  66. <pre>
  67. <code><span class=identifier>file_position </span><span class=keyword>const</span><span class=special>&amp; </span><span class=identifier>get_position</span><span class=special>() </span><span class=keyword>const</span><span class=special>;</span></code></pre>
  68. <p> Call this function to retrieve the current position.</p>
  69. <pre>
  70. <code><span class=keyword>void </span><span class=identifier>set_tabchars</span><span class=special>(</span><span class=keyword>int</span><span class=special>);</span></code></pre>
  71. <p> Call this to set the number of tabs per character. This value is necessary
  72. to correctly track the column number.<br>
  73. </p>
  74. <p> <a name="file_position"></a> </p>
  75. <h2>file_position</h2>
  76. <p> file_position is a structure that holds the position within a file. Its fields
  77. are:</p>
  78. <table width="90%" border="0" align="center">
  79. <tr>
  80. <td class="table_title" colspan="2">file_position fields</td>
  81. </tr>
  82. <tr>
  83. <td class="table_cells" width="26%"><code><span class=identifier>std</span><span class=special>::</span><span class=identifier>string
  84. </span><span class=identifier>file</span><span class=special>;</span></code></td>
  85. <td class="table_cells" width="74%">Name of the file. Hopefully a full pathname</td>
  86. </tr>
  87. <tr>
  88. <td class="table_cells" width="26%"><code><span class=keyword>int</span><span class=identifier>
  89. line</span><span class=special>;</span></code></td>
  90. <td class="table_cells" width="74%">Line number within the file. By default,
  91. the first line is number 1</td>
  92. </tr>
  93. <tr>
  94. <td class="table_cells" width="26%"><code><span class=keyword>int </span><span class=identifier>column</span><span class=special>;</span></code></td>
  95. <td class="table_cells" width="74%">Column position within the file. The first
  96. column is 1</td>
  97. </tr>
  98. </table>
  99. <p><img src="theme/lens.gif" width="15" height="16"> See <a href="../example/fundamental/position_iterator/position_iterator.cpp">position_iterator.cpp</a> for a compilable example. This is part of the Spirit distribution.</p>
  100. <table border="0">
  101. <tr>
  102. <td width="10"></td>
  103. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  104. <td width="30"><a href="file_iterator.html"><img src="theme/l_arr.gif" border="0"></a></td>
  105. <td width="30"><a href="debugging.html"><img src="theme/r_arr.gif" border="0"></a></td>
  106. </tr>
  107. </table>
  108. <hr size="1">
  109. <p class="copyright">Copyright &copy; 2002 Juan Carlos Arevalo-Baeza<br>
  110. <br>
  111. <font size="2">Use, modification and distribution is subject to the Boost Software
  112. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  113. http://www.boost.org/LICENSE_1_0.txt) </font> </p>
  114. <p class="copyright">&nbsp; </p>
  115. <p>&nbsp;</p>
  116. </body>
  117. </html>