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

Generated by: LCOV version 1.9