LCOV - code coverage report
Current view: top level - as2js/lib - position.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 44 100.0 %
Date: 2014-08-10 Functions: 15 15 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* position.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include "as2js/position.h"
      37             : 
      38             : #include "as2js/exceptions.h"
      39             : 
      40             : 
      41             : namespace as2js
      42             : {
      43             : 
      44             : 
      45             : /** \brief Set the filename being read.
      46             :  *
      47             :  * This function saves the name of the file being read if available.
      48             :  *
      49             :  * \todo
      50             :  * Test that the filename is valid (no '\0', mainly.)
      51             :  *
      52             :  * \param[in] filename  The name of the file being compiled.
      53             :  */
      54     8701412 : void Position::set_filename(String const& filename)
      55             : {
      56     8701412 :     f_filename = filename;
      57     8701412 : }
      58             : 
      59             : 
      60             : /** \brief Set the function being read.
      61             :  *
      62             :  * This function saves the name of the function being read if available.
      63             :  * The compile is capable of detecting which function is being read and
      64             :  * keeps a stack of such (since a function can be declared within another.)
      65             :  * Functions without a name are given a system name for the purpose of
      66             :  * displaying errors that give us as much information as possible.
      67             :  *
      68             :  * \param[in] function  The name of the function being compiled.
      69             :  */
      70    10497013 : void Position::set_function(String const& function)
      71             : {
      72    10497013 :     f_function = function;
      73    10497013 : }
      74             : 
      75             : 
      76             : /** \brief Reset the counter.
      77             :  *
      78             :  * This function resets all the counters to 1 except for the line which
      79             :  * is set to the specified \p line parameter (which defaults to 1.)
      80             :  *
      81             :  * \param[in] line  The line number to start with. Defaults to 1.
      82             :  */
      83     5817323 : void Position::reset_counters(counter_t line)
      84             : {
      85     5817323 :     if(line < 1)
      86             :     {
      87          19 :         throw exception_internal_error("the line parameter of the position object cannot be less than 1");
      88             :     }
      89             : 
      90     5817304 :     f_page = 1;
      91     5817304 :     f_page_line = 1;
      92     5817304 :     f_paragraph = 1;
      93     5817304 :     f_line = line;
      94     5817304 : }
      95             : 
      96             : 
      97             : /** \brief Increment the page counter by 1.
      98             :  *
      99             :  * This function increments the page counter by one, resets the page
     100             :  * line to 1 and the paragraph to 1.
     101             :  */
     102        5753 : void Position::new_page()
     103             : {
     104        5753 :     ++f_page;
     105        5753 :     f_page_line = 1;
     106        5753 :     f_paragraph = 1;
     107        5753 : }
     108             : 
     109             : 
     110             : /** \brief Increments the paragraph counter by 1.
     111             :  *
     112             :  * When the compiler detects the end of a paragraph, it calls this function
     113             :  * to increment that counter by one. Paragraphs are counted within one page.
     114             :  */
     115       95352 : void Position::new_paragraph()
     116             : {
     117       95352 :     ++f_paragraph;
     118       95352 : }
     119             : 
     120             : 
     121             : /** \brief Increment the line counter by 1.
     122             :  *
     123             :  * This function increases the file as a whole line counter by 1. It also
     124             :  * increments the page line counter by 1.
     125             :  */
     126    26786291 : void Position::new_line()
     127             : {
     128    26786291 :     ++f_page_line;
     129    26786291 :     ++f_line;
     130    26786291 : }
     131             : 
     132             : 
     133             : /** \brief Retrieve the filename.
     134             :  *
     135             :  * This function returns the filename as set by the set_filename() function.
     136             :  *
     137             :  * It is possible for the filename to be empty (in case you are compiling
     138             :  * a function from memory.)
     139             :  *
     140             :  * \return The current filename.
     141             :  */
     142    19052878 : String Position::get_filename() const
     143             : {
     144    19052878 :     return f_filename;
     145             : }
     146             : 
     147             : 
     148             : /** \brief Retrieve the function name.
     149             :  *
     150             :  * This function returns the function name as set by the set_function()
     151             :  * function.
     152             :  *
     153             :  * It is possible for the function name to be empty (before it was ever set.)
     154             :  *
     155             :  * \return The current function name.
     156             :  */
     157    17669280 : String Position::get_function() const
     158             : {
     159    17669280 :     return f_function;
     160             : }
     161             : 
     162             : 
     163             : /** \brief Retrieve the current page counter.
     164             :  *
     165             :  * The page counter is incremented by one after X number of lines or when
     166             :  * a Ctrl-L character is found in the input stream.
     167             :  *
     168             :  * \return The page number.
     169             :  */
     170    12504577 : Position::counter_t Position::get_page() const
     171             : {
     172    12504577 :     return f_page;
     173             : }
     174             : 
     175             : 
     176             : /** \brief Retrieve the current page line counter.
     177             :  *
     178             :  * The page line counter is incremented by one every time a new line
     179             :  * character is found. It starts at 1. It is reset back to one each
     180             :  * time a new page is found.
     181             :  *
     182             :  * \return The page line number.
     183             :  */
     184    12504577 : Position::counter_t Position::get_page_line() const
     185             : {
     186    12504577 :     return f_page_line;
     187             : }
     188             : 
     189             : 
     190             : /** \brief Retrieve the current paragraph counter.
     191             :  *
     192             :  * The paragraph counter is incremented by one every time empty
     193             :  * lines are found between blocks of non empty lines. It starts at 1.
     194             :  * It is reset back to one each time a new page is found.
     195             :  *
     196             :  * \return The paragraph number.
     197             :  */
     198    12504577 : Position::counter_t Position::get_paragraph() const
     199             : {
     200    12504577 :     return f_paragraph;
     201             : }
     202             : 
     203             : 
     204             : /** \brief Retrieve the current line counter.
     205             :  *
     206             :  * The line counter is reset to 1 (or some other value) at the start and
     207             :  * then it increases by 1 each time a new line character is found. It
     208             :  * does not get reset on anything. It is generally useful when using a
     209             :  * text editor as it represents the line number in such an editor.
     210             :  *
     211             :  * \return The current line number.
     212             :  */
     213    13159956 : Position::counter_t Position::get_line() const
     214             : {
     215    13159956 :     return f_line;
     216             : }
     217             : 
     218             : 
     219             : /** \brief Print this position in the \p out stream.
     220             :  *
     221             :  * This function prints out this position in the \p out stream. We limit
     222             :  * the printing to the filename and the line number as most compilers
     223             :  * do. The other information is available for you to print additional
     224             :  * data if required.
     225             :  *
     226             :  * \code
     227             :  * <filename>:<line>:
     228             :  * \endcode
     229             :  *
     230             :  * \param[in,out] out  The stream to print to.
     231             :  * \param[in] pos  The position to print in the output.
     232             :  *
     233             :  * \return A reference to this stream.
     234             :  */
     235      650449 : std::ostream& operator << (std::ostream& out, Position const& pos)
     236             : {
     237      650449 :     if(pos.get_filename().empty())
     238             :     {
     239       24074 :         out << "line " << pos.get_line() << ":";
     240             :     }
     241             :     else
     242             :     {
     243      626375 :         out << pos.get_filename() << ":" << pos.get_line() << ":";
     244             :     }
     245             : 
     246      650449 :     return out;
     247             : }
     248             : 
     249             : 
     250          20 : }
     251             : // namespace as2js
     252             : 
     253             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.9