LCOV - code coverage report
Current view: top level - lib - expr_conditional.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 31 100.0 %
Date: 2017-03-23 22:48:20 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // CSS Preprocessor
       2             : // Copyright (C) 2015-2017  Made to Order Software Corp.
       3             : //
       4             : // This program is free software; you can redistribute it and/or modify
       5             : // it under the terms of the GNU General Public License as published by
       6             : // the Free Software Foundation; either version 2 of the License, or
       7             : // (at your option) any later version.
       8             : //
       9             : // This program is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             : // GNU General Public License for more details.
      13             : //
      14             : // You should have received a copy of the GNU General Public License
      15             : // along with this program; if not, write to the Free Software
      16             : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      17             : 
      18             : /** \file
      19             :  * \brief Implementation of the CSS Preprocessor expression.
      20             :  *
      21             :  * The CSS Preprocessor expression class is used to reduce a list
      22             :  * of nodes by applying expressions to the various values.
      23             :  *
      24             :  * \sa \ref expression_rules
      25             :  */
      26             : 
      27             : #include "csspp/expression.h"
      28             : 
      29             : #include "csspp/exceptions.h"
      30             : #include "csspp/parser.h"
      31             : #include "csspp/unicode_range.h"
      32             : 
      33             : #include <algorithm>
      34             : #include <cmath>
      35             : #include <iostream>
      36             : 
      37             : namespace csspp
      38             : {
      39             : 
      40      562903 : node::pointer_t expression::conditional()
      41             : {
      42             :     // conditional: logical_or
      43             :     //            | conditional '?' conditional ':' logical_or
      44             : 
      45             :     // note: we also support if(expr, expr, expr)
      46             : 
      47     1125806 :     node::pointer_t result(logical_or());
      48      562903 :     if(!result)
      49             :     {
      50         568 :         return node::pointer_t();
      51             :     }
      52             : 
      53      565795 :     while(f_current->is(node_type_t::CONDITIONAL))
      54             :     {
      55        1734 :         bool const r(boolean(result));
      56             : 
      57             :         // skip the '?'
      58        1734 :         next();
      59             : 
      60             :         // TODO: avoid calculating both sides.
      61        1734 :         if(r)
      62             :         {
      63         937 :             result = conditional();
      64         937 :             if(!result)
      65             :             {
      66           2 :                 return node::pointer_t();
      67             :             }
      68             :         }
      69             :         else
      70             :         {
      71             :             // we will ignore this entry, so ignore the eventual errors
      72        1594 :             csspp::safe_error_t safe_errors;
      73        1594 :             std::stringstream ignore;
      74        1594 :             safe_error_stream_t safe_output(ignore);
      75             : 
      76         797 :             static_cast<void>(conditional());
      77             :         }
      78             : 
      79        1732 :         if(!f_current->is(node_type_t::COLON))
      80             :         {
      81           1 :             error::instance() << f_current->get_position()
      82           1 :                     << "a mandatory ':' was expected after a '?' first expression."
      83           1 :                     << error_mode_t::ERROR_ERROR;
      84           1 :             return node::pointer_t();
      85             :         }
      86             : 
      87             :         // skip the ':'
      88        1731 :         next();
      89             : 
      90        1731 :         if(!r)
      91             :         {
      92         797 :             result = conditional();
      93         797 :             if(!result)
      94             :             {
      95           1 :                 return node::pointer_t();
      96             :             }
      97             :         }
      98             :         else
      99             :         {
     100             :             // we will ignore this entry, so ignore the eventual errors
     101        1868 :             csspp::safe_error_t safe_errors;
     102        1868 :             std::stringstream ignore;
     103        1868 :             safe_error_stream_t safe_output(ignore);
     104             : 
     105         934 :             static_cast<void>(conditional());
     106             :         }
     107             :     }
     108             : 
     109      562331 :     return result;
     110             : }
     111             : 
     112           9 : } // namespace csspp
     113             : 
     114             : // Local Variables:
     115             : // mode: cpp
     116             : // indent-tabs-mode: nil
     117             : // c-basic-offset: 4
     118             : // tab-width: 4
     119             : // End:
     120             : 
     121             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12