Current Version: 1.0.10
Project Name: csspp
expression.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  : f_node(n)
42 {
43  if(!f_node)
44  {
45  throw csspp_exception_logic("expression.cpp:expression(): contructor called with a null pointer.");
46  }
47 }
48 
50 {
51  f_variable_handler = handler;
52 }
53 
55 {
56  mark_start();
57  next();
59 }
60 
61 // basic state handling
63 {
64  return f_pos >= f_node->size();
65 }
66 
68 {
69  f_start = f_pos;
70 }
71 
73 {
74  if(result)
75  {
76  if(f_start == static_cast<size_t>(-1))
77  {
78  throw csspp_exception_logic("expression.cpp:expression(): replace_with_result() cannot be called if mark_start() was never called."); // LCOV_EXCL_LINE
79  }
80 
81  // f_pos may point to a tag right after the end of the previous
82  // expression; expressions may be separated by WHITESPACE tokens
83  // too so we have to restore them if they appear at the end of
84  // the epxression we just worked on (i.e. we cannot eat a WHITESPACE
85  // at the end of an expression.)
86  if(!f_current->is(node_type_t::EOF_TOKEN) && f_pos > 0)
87  {
88  while(f_pos > 0)
89  {
90  --f_pos;
91  if(f_node->get_child(f_pos) == f_current)
92  {
93  break;
94  }
95  }
96  if(f_pos > 0 && f_node->get_child(f_pos - 1)->is(node_type_t::WHITESPACE))
97  {
98  --f_pos;
99  }
100  }
101 
102  // this "reduces" the expression with its result
103  while(f_pos > f_start)
104  {
105  --f_pos;
106  f_node->remove_child(f_pos);
107  }
108  f_node->insert_child(f_pos, result);
109  ++f_pos;
110  }
111 
112  mark_start();
113 
114  return result;
115 }
116 
118 {
119  if(f_pos >= f_node->size())
120  {
121  if(!f_current
123  {
124  f_current.reset(new node(node_type_t::EOF_TOKEN, f_node->get_position()));
125  }
126  }
127  else
128  {
129  f_current = f_node->get_child(f_pos);
130  ++f_pos;
131  while(f_pos < f_node->size()
132  && f_node->get_child(f_pos)->is(node_type_t::WHITESPACE))
133  {
134  ++f_pos;
135  }
136  }
137 }
138 
139 } // namespace csspp
140 
141 // Local Variables:
142 // mode: cpp
143 // indent-tabs-mode: nil
144 // c-basic-offset: 4
145 // tab-width: 4
146 // End:
147 
148 // vim: ts=4 sw=4 et
std::shared_ptr< node > pointer_t
Definition: node.h:122
expression(node::pointer_t n)
Definition: expression.cpp:40
node::pointer_t replace_with_result(node::pointer_t result)
Definition: expression.cpp:72
expression_variables_interface * f_variable_handler
Definition: expression.h:154
node::pointer_t f_current
Definition: expression.h:151
void set_variable_handler(expression_variables_interface *handler)
Definition: expression.cpp:49
node::pointer_t conditional()
node::pointer_t compile()
Definition: expression.cpp:54
node::pointer_t f_node
Definition: expression.h:148

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.