LCOV - code coverage report
Current view: top level - lib - node_param.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 28 100.0 %
Date: 2014-11-22 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* node_param.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/node.h"
      37             : 
      38             : #include    "as2js/exceptions.h"
      39             : 
      40             : 
      41             : /** \file
      42             :  * \brief Handle nodes of type parameter.
      43             :  *
      44             :  * This file represents the implementation of the various
      45             :  * parameter functions applying to nodes.
      46             :  *
      47             :  * Parameters are used to call functions. The list of
      48             :  * parameters defined in this file represents such.
      49             :  */
      50             : 
      51             : 
      52             : namespace as2js
      53             : {
      54             : 
      55             : 
      56             : /**********************************************************************/
      57             : /**********************************************************************/
      58             : /***  NODE PARAM  *****************************************************/
      59             : /**********************************************************************/
      60             : /**********************************************************************/
      61             : 
      62             : 
      63             : /** \brief Define the size of the parameter index and depth vectors.
      64             :  *
      65             :  * This function defines the size of the depth and index parameter
      66             :  * vectors. Until this function is called, trying to set a depth
      67             :  * or index parameter will fail.
      68             :  *
      69             :  * Also, the function cannot be called more than once and the size
      70             :  * parameter cannot be zero.
      71             :  *
      72             :  * \exception exception_internal_error
      73             :  * If this node is not of type NODE_PARAM_MATCH, if the function
      74             :  * had been called before, or if the \p size parameter is zero,
      75             :  * this exception is raised.
      76             :  *
      77             :  * \param[in] size  The number of parameters (size > 0 must be true).
      78             :  *
      79             :  * \sa get_param_size()
      80             :  * \sa get_param_depth()
      81             :  * \sa get_param_index()
      82             :  */
      83         175 : void Node::set_param_size(size_t size)
      84             : {
      85         175 :     if(f_type != node_t::NODE_PARAM_MATCH)
      86             :     {
      87         169 :         throw exception_internal_error("INTERNAL ERROR: set_param_size() called with a node other than a NODE_PARAM_MATCH.");
      88             :     }
      89           6 :     if(f_param_depth.size() != 0)
      90             :     {
      91           2 :         throw exception_internal_error("INTERNAL ERROR: set_param_size() called twice.");
      92             :     }
      93           4 :     if(size == 0)
      94             :     {
      95           2 :         throw exception_internal_error("INTERNAL ERROR: set_param_size() was called with a size of zero.");
      96             :     }
      97           2 :     f_param_depth.resize(size);
      98           2 :     f_param_index.resize(size);
      99           2 : }
     100             : 
     101             : 
     102             : /** \brief Return the size of the parameter index and depth vectors.
     103             :  *
     104             :  * This function returns zero until set_param_size() is successfully
     105             :  * called with a valid size.
     106             :  *
     107             :  * \return The current size of the parameter index and depth vectors.
     108             :  *
     109             :  * \sa set_param_size()
     110             :  */
     111      709013 : size_t Node::get_param_size() const
     112             : {
     113      709013 :     return f_param_depth.size();
     114             : }
     115             : 
     116             : 
     117             : /** \brief Get the depth at the specified index.
     118             :  *
     119             :  * This function returns the depth parameter at the specified index.
     120             :  *
     121             :  * This function cannot be called until the set_param_size() gets
     122             :  * called with a valid size.
     123             :  *
     124             :  * \note
     125             :  * The index here is named 'j' because it represents the final
     126             :  * index in the function being called and not the index of the
     127             :  * parameter being matched. See the set_param_index() to see
     128             :  * the difference between the 'idx' and 'j' indexes.
     129             :  *
     130             :  * \exception std::out_of_range
     131             :  * The function throws an exception if the \p j parameter is out
     132             :  * of range. It should be defined between 0 and get_param_size() - 1.
     133             :  *
     134             :  * \param[in] j  The index of the depth to retrieve.
     135             :  *
     136             :  * \return The depth of the type of this parameter.
     137             :  *
     138             :  * \sa set_param_size()
     139             :  * \sa get_param_size()
     140             :  * \sa set_param_depth()
     141             :  */
     142          15 : Node::depth_t Node::get_param_depth(size_t j) const
     143             : {
     144          15 :     return f_param_depth.at(j);
     145             : }
     146             : 
     147             : 
     148             : /** \brief Set the depth of a parameter.
     149             :  *
     150             :  * When we search for a match of a function call, we check its parameters.
     151             :  * If a parameter has a higher class type definition, then it wins over
     152             :  * the others. This depth value represents that information.
     153             :  *
     154             :  * \note
     155             :  * The index here is named 'j' because it represents the final
     156             :  * index in the function being called and not the index of the
     157             :  * parameter being matched. See the set_param_index() to see
     158             :  * the difference between the 'idx' and 'j' indexes.
     159             :  *
     160             :  * \exception std::out_of_range
     161             :  * The function throws an exception if the \p j parameter is out
     162             :  * of range. It should be defined between 0 and get_param_size() - 1.
     163             :  *
     164             :  * \param[in] j  The index of the parameter for which we define the depth.
     165             :  *               (The order is the function being called order.)
     166             :  * \param[in] depth  The new depth.
     167             :  */
     168          16 : void Node::set_param_depth(size_t j, depth_t depth)
     169             : {
     170          16 :     if(j >= f_param_depth.size())
     171             :     {
     172          11 :         throw std::out_of_range("set_param_depth() called with an index out of range");
     173             :     }
     174           5 :     f_param_depth[j] = depth;
     175           5 : }
     176             : 
     177             : 
     178             : /** \brief Get the index of the parameter.
     179             :  *
     180             :  * When a user writes a function call, he can spell out the parameter
     181             :  * names as in:
     182             :  *
     183             :  * \code
     184             :  * pos = find(size => 123, characer => 'c', haystack => str);
     185             :  * \endcode
     186             :  *
     187             :  * The parameters, in the function declaration, may not be in the
     188             :  * same order:
     189             :  *
     190             :  * \code
     191             :  * function find(haystack: string, character: string, size: number = -1);
     192             :  * \endcode
     193             :  *
     194             :  * The parameter index vector holds the indices so we can reorganize the
     195             :  * call as in:
     196             :  *
     197             :  * \code
     198             :  * pos = find(str, 'c', 123);
     199             :  * \endcode
     200             :  *
     201             :  * The really cool thing is that you could call a function with
     202             :  * multiple definitions and still get the parameters in the right
     203             :  * order even though both functions define their parameters
     204             :  * in a different order.
     205             :  *
     206             :  * \exception std::out_of_range
     207             :  * The function throws an exception if the \p idx parameter is out
     208             :  * of range. It should be defined between 0 and get_param_size() - 1.
     209             :  *
     210             :  * \param[in] idx  The index of the parameter in the function being called.
     211             :  *
     212             :  * \return The index in the function definition.
     213             :  *
     214             :  * \sa set_param_size()
     215             :  * \sa get_param_size()
     216             :  * \sa set_param_index()
     217             :  */
     218          15 : size_t Node::get_param_index(size_t idx) const
     219             : {
     220          15 :     return f_param_index.at(idx);
     221             : }
     222             : 
     223             : 
     224             : /** \brief Set the parameter index.
     225             :  *
     226             :  * Save the index of the parameter in the function being called, opposed
     227             :  * to the index of the parameter in the function call.
     228             :  *
     229             :  * See function get_param_index() for more details about the indexes.
     230             :  *
     231             :  * \exception std::out_of_range
     232             :  * The function throws an exception if the \p idx or \p j parameters are
     233             :  * out of range. They should both be defined between 0 and
     234             :  * get_param_size() - 1.
     235             :  *
     236             :  * \param[in] idx  The index in the function call.
     237             :  * \param[in] j  The index in the function being called.
     238             :  *
     239             :  * \sa set_param_size()
     240             :  * \sa get_param_size()
     241             :  * \sa get_param_index()
     242             :  */
     243          77 : void Node::set_param_index(size_t idx, size_t j)
     244             : {
     245         154 :     if(idx >= f_param_index.size()
     246          77 :     || j >= f_param_index.size())
     247             :     {
     248          72 :         throw std::out_of_range("set_param_index() called with an index out of range");
     249             :     }
     250           5 :     f_param_index[idx] = j;
     251           5 : }
     252             : 
     253             : 
     254             : 
     255          63 : }
     256             : // namespace as2js
     257             : 
     258             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10