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

          Line data    Source code
       1             : /* node_value.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 Define a set of functions used to change the value of literals.
      43             :  *
      44             :  * The system supports a few direct literals:
      45             :  *
      46             :  * \li integers
      47             :  * \li floating points
      48             :  * \li strings
      49             :  * \li identifiers
      50             :  * \li labels
      51             :  * \li class
      52             :  *
      53             :  * Each one of these can be set a value representing the literal as read
      54             :  * in the source file. The functions below handle that value.
      55             :  */
      56             : 
      57             : 
      58             : namespace as2js
      59             : {
      60             : 
      61             : 
      62             : 
      63             : /**********************************************************************/
      64             : /**********************************************************************/
      65             : /***  NODE VALUE  *****************************************************/
      66             : /**********************************************************************/
      67             : /**********************************************************************/
      68             : 
      69             : /** \brief Set the Boolean value of this node.
      70             :  *
      71             :  * This function saves a Boolean value in this node.
      72             :  *
      73             :  * Note that only two types of nodes can be assigned a Boolean value:
      74             :  *
      75             :  * NODE_TRUE and NODE_FALSE
      76             :  *
      77             :  * This function converst the C++ Boolean value to either NODE_TRUE
      78             :  * or NODE_FALSE.
      79             :  *
      80             :  * \exception exception_internal_error
      81             :  * This exception is raised if the set_boolean() function is called on a
      82             :  * type of node that is not a Boolean node.
      83             :  *
      84             :  * \param[in] value  The C++ Boolean value to save in this node.
      85             :  */
      86        2398 : void Node::set_boolean(bool value)
      87             : {
      88             :     // only the corresponding node type accepts a set() call
      89        2398 :     switch(f_type)
      90             :     {
      91             :     case node_t::NODE_TRUE:
      92             :     case node_t::NODE_FALSE:
      93        2230 :         break;
      94             : 
      95             :     default:
      96         168 :         throw exception_internal_error("set_boolean() called with a non-Boolean node type");
      97             : 
      98             :     }
      99             : 
     100        2230 :     f_type = value ? node_t::NODE_TRUE : node_t::NODE_FALSE;
     101        2230 : }
     102             : 
     103             : 
     104             : /** \brief Set the Int64 of this node.
     105             :  *
     106             :  * This function saves an Int64 in this node.
     107             :  *
     108             :  * Note that only one type of node can be assigned an Int64:
     109             :  *
     110             :  * NODE_INT64
     111             :  *
     112             :  * \exception exception_internal_error
     113             :  * This exception is raised if the set_int64() function is called on a
     114             :  * type of node that does not support a string.
     115             :  *
     116             :  * \param[in] value  The Int64 to save in this node.
     117             :  */
     118     2642033 : void Node::set_int64(Int64 value)
     119             : {
     120             :     // only the corresponding node type accepts a set() call
     121     2642033 :     switch(f_type)
     122             :     {
     123             :     case node_t::NODE_INT64:
     124     2641864 :         break;
     125             : 
     126             :     default:
     127         169 :         throw exception_internal_error("set_int64() called with a non-int64 node type");
     128             : 
     129             :     }
     130             : 
     131     2641864 :     f_int = value;
     132     2641864 : }
     133             : 
     134             : 
     135             : /** \brief Set the Float64 of this node.
     136             :  *
     137             :  * This function saves a Float64 in this node.
     138             :  *
     139             :  * Note that only one type of node can be assigned a Float64:
     140             :  *
     141             :  * NODE_FLOAT64
     142             :  *
     143             :  * \exception exception_internal_error
     144             :  * This exception is raised if the set_float64() function is called on a
     145             :  * type of node that does not support a string.
     146             :  *
     147             :  * \param[in] value  The Float64 to save in this node.
     148             :  */
     149      311212 : void Node::set_float64(Float64 value)
     150             : {
     151             :     // only the corresponding node type accepts a set() call
     152      311212 :     switch(f_type)
     153             :     {
     154             :     case node_t::NODE_FLOAT64:
     155      310943 :         break;
     156             : 
     157             :     default:
     158         269 :         throw exception_internal_error("set_float64() called with a non-float64 node type");
     159             : 
     160             :     }
     161             : 
     162      310943 :     f_float = value;
     163      310943 : }
     164             : 
     165             : 
     166             : /** \brief Set the string of this node.
     167             :  *
     168             :  * This function saves a string in this node. The string is a
     169             :  * standard String object (full Unicode support.)
     170             :  *
     171             :  * Note that only a few types of nodes can be assigned a string:
     172             :  *
     173             :  * NODE_BREAK, NODE_CLASS, NODE_CONTINUE, NODE_ENUM, NODE_FUNCTION,
     174             :  * NODE_GOTO, NODE_IDENTIFIER, NODE_IMPORT, NODE_INTERFACE, NODE_LABEL,
     175             :  * NODE_NAME, NODE_NAMESPACE, NODE_PACKAGE, NODE_PARAM,
     176             :  * NODE_REGULAR_EXPRESSION, NODE_STRING, NODE_VARIABLE, NODE_VAR_ATTRIBUTES,
     177             :  * and NODE_VIDENTIFIER
     178             :  *
     179             :  * \exception exception_internal_error
     180             :  * This exception is raised if the set_string() function is called on a
     181             :  * type of node that does not support a string.
     182             :  *
     183             :  * \param[in] value  The String to save in this node.
     184             :  */
     185    19005431 : void Node::set_string(String const& value)
     186             : {
     187             :     // only the corresponding node type accepts a set() call
     188    19005431 :     switch(f_type)
     189             :     {
     190             :     case node_t::NODE_BREAK:                // name of label: BREAK [IDENTIFIER | DEFAULT];
     191             :     case node_t::NODE_CLASS:                // name of class: CLASS IDENTIFIER
     192             :     case node_t::NODE_CONTINUE:             // name of label: CONTINUE [IDENTIFIER | DEFAULT];
     193             :     case node_t::NODE_ENUM:                 // name of enumeration: ENUM [IDENTIFIER] ...;
     194             :     case node_t::NODE_FUNCTION:             // name of function: FUNCTION [[GET | SET] IDENTIFIER | STRING] ...;
     195             :     case node_t::NODE_GOTO:                 // name of label: GOTO IDENTIFIER;
     196             :     case node_t::NODE_IDENTIFIER:           // the identifier string: IDENTIFIER
     197             :     case node_t::NODE_IMPORT:               // name of package: IMPORT [IDENTIFIER ('.' IDENTIFIER)* | STRING] ...;
     198             :     case node_t::NODE_INTERFACE:            // name of interface: INTERFACE IDENTIFIER
     199             :     case node_t::NODE_LABEL:                // name of label: IDENTIFIER ':'
     200             :     case node_t::NODE_NAME:                 // name of object field: { IDENTIFIER ':' ... }
     201             :     case node_t::NODE_NAMESPACE:            // name of namespace: NAMESPACE IDENTIFIER
     202             :     case node_t::NODE_PACKAGE:              // name of package: PACKAGE [IDENTIFIER ('.' IDENTIFIER)* | STRING] ...;
     203             :     case node_t::NODE_PARAM:                // name of parameter: FUNCTION '(' IDENTIFIER ... ')' ...
     204             :     case node_t::NODE_REGULAR_EXPRESSION:   // name of parameter: `...` or /.../...
     205             :     case node_t::NODE_STRING:               // the string itself: STRING
     206             :     case node_t::NODE_VARIABLE:             // name of variable: VAR <name> [':' type_expr] ['=' expression], ...;
     207             :     case node_t::NODE_VAR_ATTRIBUTES:       // name of variable: VAR <name> [':' type_expr] ['=' expression], ...; (transformed to VAR_ATTRIBUTES)
     208             :     case node_t::NODE_VIDENTIFIER:          // the identifier string: IDENTIFIER (transformed to VIDENTIFIER)
     209    19005280 :         break;
     210             : 
     211             :     default:
     212         151 :         throw exception_internal_error("set_string() called with a non-string node type");
     213             : 
     214             :     }
     215             : 
     216    19005280 :     f_str = value;
     217    19005280 : }
     218             : 
     219             : 
     220             : /** \brief Get the Boolean value of this node.
     221             :  *
     222             :  * This function returns true or false depending on the node type:
     223             :  * NODE_TRUE or NODE_FALSE.
     224             :  *
     225             :  * \exception exception_internal_error
     226             :  * This exception is raised if the get_boolean() function is called on a
     227             :  * type of node which is not NODE_TRUE or NODE_FALSE.
     228             :  *
     229             :  * \return The Boolean value attached to this node.
     230             :  */
     231      709230 : bool Node::get_boolean() const
     232             : {
     233             :     // only the corresponding node type accepts a get() call
     234      709230 :     switch(f_type)
     235             :     {
     236             :     case node_t::NODE_TRUE:
     237        4113 :         return true;
     238             : 
     239             :     case node_t::NODE_FALSE:
     240        4131 :         return false;
     241             : 
     242             :     default:
     243      700986 :         throw exception_internal_error("get_boolean() called with a non-Boolean node type");
     244             : 
     245             :     }
     246             :     /*NOTREACHED*/
     247             : }
     248             : 
     249             : 
     250             : /** \brief Get the Int64 of this node.
     251             :  *
     252             :  * This function returns the Int64 of this node.
     253             :  *
     254             :  * Note that only one type of nodes can be assigned an Int64:
     255             :  *
     256             :  * NODE_INT64
     257             :  *
     258             :  * \exception exception_internal_error
     259             :  * This exception is raised if the get_int64() function is called on a
     260             :  * type of node that does not support a float.
     261             :  *
     262             :  * \return The integer attached to this node.
     263             :  */
     264    26538901 : Int64 Node::get_int64() const
     265             : {
     266             :     // only the corresponding node type accepts a get() call
     267    26538901 :     switch(f_type)
     268             :     {
     269             :     case node_t::NODE_INT64:
     270     3921011 :         break;
     271             : 
     272             :     default:
     273    22617890 :         throw exception_internal_error("get_int64() called with a non-int64 node type");
     274             : 
     275             :     }
     276             : 
     277     3921011 :     return f_int;
     278             : }
     279             : 
     280             : 
     281             : /** \brief Get the Float64 of this node.
     282             :  *
     283             :  * This function returns the Float64 of this node.
     284             :  *
     285             :  * Note that only one type of nodes can be assigned a Float64:
     286             :  *
     287             :  * NODE_FLOAT64
     288             :  *
     289             :  * \exception exception_internal_error
     290             :  * This exception is raised if the get_float64() function is called on a
     291             :  * type of node that does not support a float.
     292             :  *
     293             :  * \return The float attached to this node.
     294             :  */
     295    24203057 : Float64 Node::get_float64() const
     296             : {
     297             :     // only the corresponding node type accepts a get() call
     298    24203057 :     switch(f_type)
     299             :     {
     300             :     case node_t::NODE_FLOAT64:
     301      462893 :         break;
     302             : 
     303             :     default:
     304    23740164 :         throw exception_internal_error("get_float64() called with a non-float64 node type");
     305             : 
     306             :     }
     307             : 
     308      462893 :     return f_float;
     309             : }
     310             : 
     311             : 
     312             : /** \brief Get the string of this node.
     313             :  *
     314             :  * This function returns the string of this node. The string is a
     315             :  * standard String object (full Unicode support.)
     316             :  *
     317             :  * Note that only a few types of nodes can be assigned a string:
     318             :  *
     319             :  * NODE_BREAK, NODE_CLASS, NODE_CONTINUE, NODE_ENUM, NODE_FUNCTION,
     320             :  * NODE_GOTO, NODE_IDENTIFIER, NODE_IMPORT, NODE_INTERFACE, NODE_LABEL,
     321             :  * NODE_NAME, NODE_NAMESPACE, NODE_PACKAGE, NODE_PARAM,
     322             :  * NODE_REGULAR_EXPRESSION, NODE_STRING, NODE_VARIABLE, NODE_VAR_ATTRIBUTES,
     323             :  * and NODE_VIDENTIFIER
     324             :  *
     325             :  * \exception exception_internal_error
     326             :  * This exception is raised if the get_string() function is called on a
     327             :  * type of node that does not support a string.
     328             :  *
     329             :  * \return The string attached to this node.
     330             :  */
     331    39246193 : String const& Node::get_string() const
     332             : {
     333             :     // only the corresponding node type accepts a get() call
     334    39246193 :     switch(f_type)
     335             :     {
     336             :     case node_t::NODE_BREAK:                // name of label: BREAK [IDENTIFIER | DEFAULT];
     337             :     case node_t::NODE_CLASS:                // name of class: CLASS IDENTIFIER
     338             :     case node_t::NODE_CONTINUE:             // name of label: CONTINUE [IDENTIFIER | DEFAULT];
     339             :     case node_t::NODE_ENUM:                 // name of enumeration: ENUM [IDENTIFIER] ...;
     340             :     case node_t::NODE_FUNCTION:             // name of function: FUNCTION [[GET | SET] IDENTIFIER | STRING] ...;
     341             :     case node_t::NODE_GOTO:                 // name of label: GOTO IDENTIFIER;
     342             :     case node_t::NODE_IDENTIFIER:           // the identifier string: IDENTIFIER
     343             :     case node_t::NODE_IMPORT:               // name of package: IMPORT [IDENTIFIER ('.' IDENTIFIER)* | STRING] ...;
     344             :     case node_t::NODE_INTERFACE:            // name of interface: INTERFACE IDENTIFIER
     345             :     case node_t::NODE_LABEL:                // name of label: IDENTIFIER ':'
     346             :     case node_t::NODE_NAME:                 // name of object field: { IDENTIFIER ':' ... }
     347             :     case node_t::NODE_NAMESPACE:            // name of namespace: NAMESPACE IDENTIFIER
     348             :     case node_t::NODE_PACKAGE:              // name of package: PACKAGE [IDENTIFIER ('.' IDENTIFIER)* | STRING] ...;
     349             :     case node_t::NODE_PARAM:                // name of parameter: FUNCTION '(' IDENTIFIER ... ')' ...
     350             :     case node_t::NODE_REGULAR_EXPRESSION:   // name of parameter: `...` or /.../...
     351             :     case node_t::NODE_STRING:               // the string itself: "..." or '...'
     352             :     case node_t::NODE_VARIABLE:             // name of variable: VAR <name> [':' type_expr] ['=' expression], ...;
     353             :     case node_t::NODE_VAR_ATTRIBUTES:       // name of variable: VAR <name> [':' type_expr] ['=' expression], ...; (transformed to VAR_ATTRIBUTES)
     354             :     case node_t::NODE_VIDENTIFIER:          // the identifier string: IDENTIFIER (transformed to VIDENTIFIER)
     355    24292008 :         break;
     356             : 
     357             :     default:
     358    14954185 :         throw exception_internal_error("get_string() called with a non-string node type");
     359             : 
     360             :     }
     361             : 
     362    24292008 :     return f_str;
     363             : }
     364             : 
     365             : 
     366          63 : }
     367             : // namespace as2js
     368             : 
     369             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10