Current Version: 1.0.10
Project Name: csspp
expr_conditional.cpp
Go to the documentation of this file.
1 // CSS Preprocessor
2 // Copyright (C) 2015-2016 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 
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 
41 {
42  // conditional: logical_or
43  // | conditional '?' conditional ':' logical_or
44 
45  // note: we also support if(expr, expr, expr)
46 
47  node::pointer_t result(logical_or());
48  if(!result)
49  {
50  return node::pointer_t();
51  }
52 
54  {
55  bool const r(boolean(result));
56 
57  // skip the '?'
58  next();
59 
60  // TODO: avoid calculating both sides.
61  if(r)
62  {
63  result = conditional();
64  if(!result)
65  {
66  return node::pointer_t();
67  }
68  }
69  else
70  {
71  // we will ignore this entry, so ignore the eventual errors
72  csspp::safe_error_t safe_errors;
73  std::stringstream ignore;
74  safe_error_stream_t safe_output(ignore);
75 
76  static_cast<void>(conditional());
77  }
78 
80  {
81  error::instance() << f_current->get_position()
82  << "a mandatory ':' was expected after a '?' first expression."
84  return node::pointer_t();
85  }
86 
87  // skip the ':'
88  next();
89 
90  if(!r)
91  {
92  result = conditional();
93  if(!result)
94  {
95  return node::pointer_t();
96  }
97  }
98  else
99  {
100  // we will ignore this entry, so ignore the eventual errors
101  csspp::safe_error_t safe_errors;
102  std::stringstream ignore;
103  safe_error_stream_t safe_output(ignore);
104 
105  static_cast<void>(conditional());
106  }
107  }
108 
109  return result;
110 }
111 
112 } // 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
std::shared_ptr< node > pointer_t
Definition: node.h:122
node::pointer_t f_current
Definition: expression.h:151
node::pointer_t logical_or()
node::pointer_t conditional()
static error & instance()
Definition: error.cpp:78

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.