Current Version: 1.0.33
Project Name: csspp
expr_relational.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-2025 Made to Order Software Corp. All Rights Reserved
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License along
14// with this program; if not, write to the Free Software Foundation, Inc.,
15// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
26#include "csspp/expression.h"
27
28#include "csspp/exception.h"
29#include "csspp/parser.h"
30#include "csspp/unicode_range.h"
31
32#include <algorithm>
33#include <cmath>
34#include <iostream>
35
36namespace csspp
37{
38
40{
41 switch(mix_node_types(lhs->get_type(), rhs->get_type()))
42 {
44 return lhs->get_boolean() < rhs->get_boolean();
45
47 // TBD: should we generate an error if these are not
48 // equivalent dimensions?
49 return lhs->get_integer() < rhs->get_integer();
50
52 // TBD: should we generate an error if these are not
53 // equivalent dimensions?
54 return lhs->get_integer() < rhs->get_decimal_number();
55
57 // TBD: should we generate an error if these are not
58 // equivalent dimensions?
59 return lhs->get_decimal_number() < rhs->get_integer();
60
62 // TBD: should we generate an error if these are not
63 // equivalent dimensions?
64 return lhs->get_decimal_number() < rhs->get_decimal_number();
65
67 return lhs->get_decimal_number() < rhs->get_decimal_number();
68
70 return lhs->get_string() < rhs->get_string();
71
72 }
73
74 // at this time this only really applies to 'COLOR op COLOR'
75 error::instance() << lhs->get_position()
76 << "incompatible types between "
77 << lhs->get_type()
78 << " and "
79 << rhs->get_type()
80 << " for operator '<', '<=', '>', or '>='."
82
83 return false;
84}
85
87{
88 switch(n->get_type())
89 {
94 return n->get_type();
95
96 default:
98
99 }
100}
101
103{
104 // relational: additive
105 // | relational '<' additive
106 // | relational '<=' additive
107 // | relational '>' additive
108 // | relational '>=' additive
109
110 node::pointer_t result(additive());
111 if(!result)
112 {
113 return node::pointer_t();
114 }
115
117 while(op != node_type_t::UNKNOWN)
118 {
119 position pos(f_current->get_position());
120
121 // skip the relational operator
122 next();
123
125 if(!rhs)
126 {
127 return node::pointer_t();
128 }
129
130 // if not comparable, go on, although we already generated an
131 // error; but at least the rest of the expression can be
132 // parsed properly
133 if(is_comparable(result, rhs))
134 {
135 // apply the equality operation
136 bool boolean_result(false);
137 switch(op)
138 {
140 boolean_result = is_less_than(result, rhs);
141 break;
142
144 boolean_result = is_less_than(result, rhs) || is_equal(result, rhs);
145 break;
146
148 boolean_result = !is_less_than(result, rhs) && !is_equal(result, rhs);
149 break;
150
152 boolean_result = !is_less_than(result, rhs);
153 break;
154
155 default:
156 throw csspp_exception_logic("expression.cpp:relational(): unexpected operator in 'op'."); // LCOV_EXCL_LINE
157
158 }
159 result.reset(new node(node_type_t::BOOLEAN, pos));
160 result->set_boolean(boolean_result);
161 }
162
164 }
165
166 return result;
167}
168
169} // namespace csspp
170
171// Local Variables:
172// mode: cpp
173// indent-tabs-mode: nil
174// c-basic-offset: 4
175// tab-width: 4
176// End:
177
178// vim: ts=4 sw=4 et
static error & instance()
Definition error.cpp:77
node::pointer_t f_current
Definition expression.h:155
bool is_less_than(node::pointer_t lhs, node::pointer_t rhs)
node::pointer_t additive()
node::pointer_t relational()
bool is_comparable(node::pointer_t lhs, node::pointer_t rhs)
bool is_equal(node::pointer_t lhs, node::pointer_t rhs)
std::shared_ptr< node > pointer_t
Definition node.h:132
The namespace of all the classes in the CSS Preprocessor.
Definition csspp.h:48
node_type_t relational_operator(node::pointer_t n)
node_type_t
Definition node.h:41
int32_t constexpr mix_node_types(node_type_t a, node_type_t b)
Definition node.h:119

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.