Current Version: 1.0.33
Project Name: csspp
expression.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
36// self
37//
38#include "csspp/expression.h"
39
40#include "csspp/exception.h"
41#include "csspp/parser.h"
42#include "csspp/unicode_range.h"
43
44
45// C++
46//
47#include <algorithm>
48#include <cmath>
49#include <iostream>
50
51
52// last include
53//
54#include <snapdev/poison.h>
55
56
57
58namespace csspp
59{
60
62 : f_node(n)
63{
64 if(!f_node)
65 {
66 throw csspp_exception_logic("expression.cpp:expression(): contructor called with a null pointer.");
67 }
68}
69
74
81
82// basic state handling
84{
85 return f_pos >= f_node->size();
86}
87
92
94{
95 if(result)
96 {
97 if(f_start == static_cast<size_t>(-1))
98 {
99 throw csspp_exception_logic("expression.cpp:expression(): replace_with_result() cannot be called if mark_start() was never called."); // LCOV_EXCL_LINE
100 }
101
102 // f_pos may point to a tag right after the end of the previous
103 // expression; expressions may be separated by WHITESPACE tokens
104 // too so we have to restore them if they appear at the end of
105 // the epxression we just worked on (i.e. we cannot eat a WHITESPACE
106 // at the end of an expression.)
107 if(!f_current->is(node_type_t::EOF_TOKEN) && f_pos > 0)
108 {
109 while(f_pos > 0)
110 {
111 --f_pos;
112 if(f_node->get_child(f_pos) == f_current)
113 {
114 break;
115 }
116 }
117 if(f_pos > 0 && f_node->get_child(f_pos - 1)->is(node_type_t::WHITESPACE))
118 {
119 --f_pos;
120 }
121 }
122
123 // this "reduces" the expression with its result
124 while(f_pos > f_start)
125 {
126 --f_pos;
127 f_node->remove_child(f_pos);
128 }
129 f_node->insert_child(f_pos, result);
130 ++f_pos;
131 }
132
133 mark_start();
134
135 return result;
136}
137
139{
140 if(f_pos >= f_node->size())
141 {
142 if(!f_current
144 {
145 f_current.reset(new node(node_type_t::EOF_TOKEN, f_node->get_position()));
146 }
147 }
148 else
149 {
150 f_current = f_node->get_child(f_pos);
151 ++f_pos;
152 while(f_pos < f_node->size()
153 && f_node->get_child(f_pos)->is(node_type_t::WHITESPACE))
154 {
155 ++f_pos;
156 }
157 }
158}
159
160} // namespace csspp
161// vim: ts=4 sw=4 et
node::pointer_t conditional()
expression(node::pointer_t n)
node::pointer_t f_current
Definition expression.h:155
node::pointer_t compile()
expression_variables_interface * f_variable_handler
Definition expression.h:158
void set_variable_handler(expression_variables_interface *handler)
node::pointer_t replace_with_result(node::pointer_t result)
node::pointer_t f_node
Definition expression.h:152
std::shared_ptr< node > pointer_t
Definition node.h:132
The namespace of all the classes in the CSS Preprocessor.
Definition csspp.h:48

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.