Current Version: 1.0.33
Project Name: csspp
node.h
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#pragma once
17
18// self
19//
20#include "csspp/color.h"
21#include "csspp/error.h"
22
23
24// C++
25//
26#include <map>
27#include <vector>
28
29
30namespace csspp
31{
32
39
40enum class node_type_t
41{
42 UNKNOWN,
43
44 // basic token
45 ADD, // for selectors: E + F, F is the next sibling of E
46 AND,
49 BOOLEAN,
50 CDC,
51 CDO,
55 COLON, // for selectors: pseudo-class, E:first-child
56 COLOR, // in expressions, #RGB or rgb(R,G,B)
57 COLUMN,
58 COMMA,
59 COMMENT,
61 DASH_MATCH, // for selectors: dash match E[land|="en"]
63 //DIMENSION, -- DECIMAL_NUMBER and INTEGER with a string are dimensions
64 DIVIDE,
65 DOLLAR,
67 EQUAL, // for selectors: exact match E[foo="bar"]
69 FONT_METRICS, // 12px/14px (font-size/line-height)
72 GREATER_THAN, // for selectors: E > F, F is a child of E
73 HASH,
75 INCLUDE_MATCH, // for selectors: include match E[foo~="bar"]
76 INTEGER,
79 MODULO,
80 MULTIPLY, // for selectors: '*'
83 OPEN_CURLYBRACKET, // holds the children of '{'
84 OPEN_PARENTHESIS, // holds the children of '('
85 OPEN_SQUAREBRACKET, // holds the children of '['
86 PERCENT,
87 PERIOD, // for selectors: E.name, equivalent to E[class~='name']
88 PLACEHOLDER, // extended selectors: E %name or E%name
89 POWER,
90 PRECEDED, // for selectors: E ~ F, F is a sibling after E
91 PREFIX_MATCH, // for selectors: prefix match E[foo^="bar"]
93 SCOPE, // '|' used in 'ns|E'
95 STRING,
96 SUBSTRING_MATCH, // for selectors: substring match E[foo*="bar"]
98 SUFFIX_MATCH, // for selectors: suffix match E[foo$="bar"]
100 URL,
101 VARIABLE,
104
105 // composed tokens
106 AN_PLUS_B, // An+B for nth-child() functions
107 ARG, // broken up comma separated elements end up in lists of arguments (for functions and qualified rule selectors)
108 ARRAY, // "value value value ...", like a map, only just indexed with integers
109 COMPONENT_VALUE, // "token token token ..." representing a component-value-list
110 DECLARATION, // <id> ':' ...
111 LIST, // bare "token token token ..." until better qualified
112 MAP, // "index value index value ..." (a property list)
113 FRAME, // @keyframes <name> { frame { ... } frame { ... } ... };
114
116};
117
118// useful for quick switchs
120{
121 return static_cast<int32_t>(a) * 65536 + static_cast<int32_t>(b);
122}
123
124// the std::enable_shared_from_this<>() has no virtual dtor
125#pragma GCC diagnostic push
126#pragma GCC diagnostic ignored "-Weffc++"
127#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
128class node
129 : public std::enable_shared_from_this<node>
130{
131public:
132 typedef std::shared_ptr<node> pointer_t;
133 static size_t const npos = static_cast<size_t>(-1);
134
135 static int const g_to_string_flag_show_quotes = 0x01;
136 static int const g_to_string_flag_add_spaces = 0x02;
137
138 node(node_type_t const type, position const & pos);
139 ~node();
140
141 pointer_t clone() const;
142
143 node_type_t get_type() const;
144 bool is(node_type_t const type) const;
145 boolean_t to_boolean() const;
146
147 position const & get_position() const;
148 std::string const & get_string() const;
149 void set_string(std::string const & str);
150 std::string const & get_lowercase_string() const;
151 void set_lowercase_string(std::string const & str);
152 integer_t get_integer() const;
153 void set_integer(integer_t integer);
154 bool get_boolean() const;
155 void set_boolean(bool integer);
157 void set_decimal_number(decimal_number_t decimal_number);
158 color get_color() const;
159 void set_color(color c);
161 void set_font_size(decimal_number_t font_size);
163 void set_line_height(decimal_number_t line_height);
164 std::string get_dim1() const;
165 void set_dim1(std::string const & font_size);
166 std::string get_dim2() const;
167 void set_dim2(std::string const & line_height);
168
169 bool empty() const;
170 void clear();
171 size_t size() const;
172 size_t child_position(pointer_t child);
173 void add_child(pointer_t child);
174 void insert_child(size_t idx, pointer_t child);
175 void remove_child(pointer_t child);
176 void remove_child(size_t idx);
177 pointer_t get_child(size_t idx) const;
181
182 void clear_variables();
183 void set_variable(std::string const & name, pointer_t value);
184 pointer_t get_variable(std::string const & name);
185 void copy_variable(node::pointer_t source);
186
187 void clear_flags();
188 void set_flag(std::string const & name, bool value);
189 bool get_flag(std::string const & name);
190
191 std::string to_string(int flags) const;
192 void display(std::ostream & out, uint32_t indent) const;
193
194 static void limit_nodes_to(uint32_t count);
195
196private:
197 typedef std::vector<pointer_t> list_t;
198 typedef std::map<std::string, node::pointer_t> variable_table_t;
199 typedef std::map<std::string, bool> flag_table_t;
200
203 bool f_boolean = false;
206 std::string f_string = std::string();
207 std::string f_lowercase_string = std::string();
211};
212#pragma GCC diagnostic pop
213
214typedef std::vector<node::pointer_t> node_vector_t;
215
216} // namespace csspp
217
218std::ostream & operator << (std::ostream & out, csspp::node_type_t const type);
219std::ostream & operator << (std::ostream & out, csspp::node const & n);
220
222
223// vim: ts=4 sw=4 et
void add_child(pointer_t child)
Definition node.cpp:593
integer_t get_integer() const
Definition node.cpp:367
std::map< std::string, bool > flag_table_t
Definition node.h:199
std::string f_string
Definition node.h:206
void copy_variable(node::pointer_t source)
Definition node.cpp:717
color get_color() const
Definition node.cpp:403
variable_table_t f_variables
Definition node.h:209
bool f_boolean
Definition node.h:203
void take_over_children_of(pointer_t n)
Definition node.cpp:678
std::map< std::string, node::pointer_t > variable_table_t
Definition node.h:198
void remove_child(pointer_t child)
Definition node.cpp:635
void set_flag(std::string const &name, bool value)
Definition node.cpp:743
pointer_t get_last_child() const
Definition node.cpp:672
position const & get_position() const
Definition node.cpp:338
void set_line_height(decimal_number_t line_height)
Definition node.cpp:465
pointer_t get_child(size_t idx) const
Definition node.cpp:660
node_type_t f_type
Definition node.h:201
bool get_flag(std::string const &name)
Definition node.cpp:759
void clear_variables()
Definition node.cpp:707
void display(std::ostream &out, uint32_t indent) const
Definition node.cpp:1303
void clear()
Definition node.cpp:566
pointer_t get_variable(std::string const &name)
Definition node.cpp:728
void set_dim1(std::string const &font_size)
Definition node.cpp:489
static int const g_to_string_flag_add_spaces
Definition node.h:136
bool get_boolean() const
Definition node.cpp:379
bool is(node_type_t const type) const
Definition node.cpp:277
void set_boolean(bool integer)
Definition node.cpp:385
node_type_t get_type() const
Definition node.cpp:272
std::string const & get_lowercase_string() const
Definition node.cpp:355
void clear_flags()
Definition node.cpp:738
flag_table_t f_flags
Definition node.h:210
void set_string(std::string const &str)
Definition node.cpp:349
void set_dim2(std::string const &line_height)
Definition node.cpp:526
list_t f_children
Definition node.h:208
decimal_number_t get_font_size() const
Definition node.cpp:442
std::string get_dim1() const
Definition node.cpp:474
decimal_number_t get_decimal_number() const
Definition node.cpp:391
pointer_t clone() const
Definition node.cpp:250
void insert_child(size_t idx, pointer_t child)
Definition node.cpp:612
position f_position
Definition node.h:202
size_t child_position(pointer_t child)
Definition node.cpp:580
std::string to_string(int flags) const
Definition node.cpp:765
std::vector< pointer_t > list_t
Definition node.h:197
void set_decimal_number(decimal_number_t decimal_number)
Definition node.cpp:397
integer_t f_integer
Definition node.h:204
std::string f_lowercase_string
Definition node.h:207
size_t size() const
Definition node.cpp:573
static size_t const npos
Definition node.h:133
std::string get_dim2() const
Definition node.cpp:511
void set_color(color c)
Definition node.cpp:424
std::string const & get_string() const
Definition node.cpp:343
boolean_t to_boolean() const
Definition node.cpp:282
static int const g_to_string_flag_show_quotes
Definition node.h:135
static void limit_nodes_to(uint32_t count)
Definition node.cpp:1458
void replace_child(pointer_t o, pointer_t n)
Definition node.cpp:690
bool empty() const
Definition node.cpp:559
void set_font_size(decimal_number_t font_size)
Definition node.cpp:449
void set_integer(integer_t integer)
Definition node.cpp:373
decimal_number_t get_line_height() const
Definition node.cpp:456
void set_lowercase_string(std::string const &str)
Definition node.cpp:361
std::shared_ptr< node > pointer_t
Definition node.h:132
decimal_number_t f_decimal_number
Definition node.h:205
void set_variable(std::string const &name, pointer_t value)
Definition node.cpp:712
The namespace of all the classes in the CSS Preprocessor.
Definition csspp.h:48
node_type_t
Definition node.h:41
int64_t integer_t
Definition csspp.h:58
std::vector< node::pointer_t > node_vector_t
Definition node.h:214
int32_t constexpr mix_node_types(node_type_t a, node_type_t b)
Definition node.h:119
boolean_t
Definition node.h:34
@ BOOLEAN_FALSE
Definition node.h:36
@ BOOLEAN_TRUE
Definition node.h:37
@ BOOLEAN_INVALID
Definition node.h:35
double decimal_number_t
Definition csspp.h:59
std::ostream & operator<<(std::ostream &out, csspp::node_type_t const type)
Definition node.cpp:1465

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.