Current Version: 1.0.10
Project Name: csspp
node.h
Go to the documentation of this file.
1 #ifndef CSSPP_NODE_H
2 #define CSSPP_NODE_H
3 // CSS Preprocessor
4 // Copyright (C) 2015-2016 Made to Order Software Corp.
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #include <csspp/color.h>
21 #include <csspp/error.h>
22 
23 #include <map>
24 #include <vector>
25 
26 namespace csspp
27 {
28 
30 {
34 };
35 
36 enum class node_type_t
37 {
38  UNKNOWN,
39 
40  // basic token
41  ADD, // for selectors: E + F, F is the next sibling of E
42  AND,
43  ASSIGNMENT,
44  AT_KEYWORD,
45  BOOLEAN,
46  CDC,
47  CDO,
51  COLON, // for selectors: pseudo-class, E:first-child
52  COLOR, // in expressions, #RGB or rgb(R,G,B)
53  COLUMN,
54  COMMA,
55  COMMENT,
57  DASH_MATCH, // for selectors: dash match E[land|="en"]
59  //DIMENSION, -- DECIMAL_NUMBER and INTEGER with a string are dimensions
60  DIVIDE,
61  DOLLAR,
62  EOF_TOKEN,
63  EQUAL, // for selectors: exact match E[foo="bar"]
65  FONT_METRICS, // 12px/14px (font-size/line-height)
66  FUNCTION,
68  GREATER_THAN, // for selectors: E > F, F is a child of E
69  HASH,
70  IDENTIFIER,
71  INCLUDE_MATCH, // for selectors: include match E[foo~="bar"]
72  INTEGER,
73  LESS_EQUAL,
74  LESS_THAN,
75  MODULO,
76  MULTIPLY, // for selectors: '*'
77  NOT_EQUAL,
78  NULL_TOKEN,
79  OPEN_CURLYBRACKET, // holds the children of '{'
80  OPEN_PARENTHESIS, // holds the children of '('
81  OPEN_SQUAREBRACKET, // holds the children of '['
82  PERCENT,
83  PERIOD, // for selectors: E.name, equivalent to E[class~='name']
84  PLACEHOLDER, // extended selectors: E %name or E%name
85  POWER,
86  PRECEDED, // for selectors: E ~ F, F is a sibling after E
87  PREFIX_MATCH, // for selectors: prefix match E[foo^="bar"]
88  REFERENCE,
89  SCOPE, // '|' used in 'ns|E'
90  SEMICOLON,
91  STRING,
92  SUBSTRING_MATCH, // for selectors: substring match E[foo*="bar"]
93  SUBTRACT,
94  SUFFIX_MATCH, // for selectors: suffix match E[foo$="bar"]
96  URL,
97  VARIABLE,
99  WHITESPACE,
100 
101  // composed tokens
102  AN_PLUS_B, // An+B for nth-child() functions
103  ARG, // broken up comma separated elements end up in lists of arguments (for functions and qualified rule selectors)
104  ARRAY, // "value value value ...", like a map, only just indexed with integers
105  COMPONENT_VALUE, // "token token token ..." representing a component-value-list
106  DECLARATION, // <id> ':' ...
107  LIST, // bare "token token token ..." until better qualified
108  MAP, // "index value index value ..." (a property list)
109 
110  max_type
111 };
112 
113 // useful for quick switchs
114 int32_t constexpr mix_node_types(node_type_t a, node_type_t b)
115 {
116  return static_cast<int32_t>(a) * 65536 + static_cast<int32_t>(b);
117 }
118 
119 class node : public std::enable_shared_from_this<node>
120 {
121 public:
122  typedef std::shared_ptr<node> pointer_t;
123  static size_t const npos = static_cast<size_t>(-1);
124 
125  static int const g_to_string_flag_show_quotes = 0x01;
126  static int const g_to_string_flag_add_spaces = 0x02;
127 
128  node(node_type_t const type, position const & pos);
129  ~node();
130 
131  pointer_t clone() const;
132 
133  node_type_t get_type() const;
134  bool is(node_type_t const type) const;
135  boolean_t to_boolean() const;
136 
137  position const & get_position() const;
138  std::string const & get_string() const;
139  void set_string(std::string const & str);
140  std::string const & get_lowercase_string() const;
141  void set_lowercase_string(std::string const & str);
142  integer_t get_integer() const;
143  void set_integer(integer_t integer);
144  bool get_boolean() const;
145  void set_boolean(bool integer);
147  void set_decimal_number(decimal_number_t decimal_number);
148  color get_color() const;
149  void set_color(color c);
151  void set_font_size(decimal_number_t font_size);
153  void set_line_height(decimal_number_t line_height);
154  std::string get_dim1() const;
155  void set_dim1(std::string const & font_size);
156  std::string get_dim2() const;
157  void set_dim2(std::string const & line_height);
158 
159  bool empty() const;
160  void clear();
161  size_t size() const;
162  size_t child_position(pointer_t child);
163  void add_child(pointer_t child);
164  void insert_child(size_t idx, pointer_t child);
165  void remove_child(pointer_t child);
166  void remove_child(size_t idx);
167  pointer_t get_child(size_t idx) const;
168  pointer_t get_last_child() const;
170  void replace_child(pointer_t o, pointer_t n);
171 
172  void clear_variables();
173  void set_variable(std::string const & name, pointer_t value);
174  pointer_t get_variable(std::string const & name);
175  void copy_variable(node::pointer_t source);
176 
177  void clear_flags();
178  void set_flag(std::string const & name, bool value);
179  bool get_flag(std::string const & name);
180 
181  std::string to_string(int flags) const;
182  void display(std::ostream & out, uint32_t indent) const;
183 
184  static void limit_nodes_to(uint32_t count);
185 
186 private:
187  typedef std::vector<pointer_t> list_t;
188  typedef std::map<std::string, node::pointer_t> variable_table_t;
189  typedef std::map<std::string, bool> flag_table_t;
190 
193  bool f_boolean = false;
196  std::string f_string;
197  std::string f_lowercase_string;
201 };
202 
203 typedef std::vector<node::pointer_t> node_vector_t;
204 
205 } // namespace csspp
206 
207 std::ostream & operator << (std::ostream & out, csspp::node_type_t const type);
208 std::ostream & operator << (std::ostream & out, csspp::node const & n);
209 
211 
212 #endif
213 // #ifndef CSSPP_NODE_H
214 
215 // Local Variables:
216 // mode: cpp
217 // indent-tabs-mode: nil
218 // c-basic-offset: 4
219 // tab-width: 4
220 // End:
221 
222 // vim: ts=4 sw=4 et
pointer_t clone() const
Definition: node.cpp:237
static size_t const npos
Definition: node.h:123
void add_child(pointer_t child)
Definition: node.cpp:580
std::map< std::string, bool > flag_table_t
Definition: node.h:189
void clear_flags()
Definition: node.cpp:725
std::vector< node::pointer_t > node_vector_t
Definition: node.h:203
bool empty() const
Definition: node.cpp:546
list_t f_children
Definition: node.h:198
position const & get_position() const
Definition: node.cpp:325
void insert_child(size_t idx, pointer_t child)
Definition: node.cpp:599
std::shared_ptr< node > pointer_t
Definition: node.h:122
std::string to_string(int flags) const
Definition: node.cpp:752
int64_t integer_t
Definition: csspp.h:52
bool f_boolean
Definition: node.h:193
variable_table_t f_variables
Definition: node.h:199
pointer_t get_variable(std::string const &name)
Definition: node.cpp:715
decimal_number_t get_line_height() const
Definition: node.cpp:443
std::string const & get_string() const
Definition: node.cpp:330
node_type_t
Definition: node.h:36
void set_flag(std::string const &name, bool value)
Definition: node.cpp:730
pointer_t get_last_child() const
Definition: node.cpp:659
void take_over_children_of(pointer_t n)
Definition: node.cpp:665
int32_t constexpr mix_node_types(node_type_t a, node_type_t b)
Definition: node.h:114
decimal_number_t get_font_size() const
Definition: node.cpp:429
void set_lowercase_string(std::string const &str)
Definition: node.cpp:348
std::map< std::string, node::pointer_t > variable_table_t
Definition: node.h:188
void set_dim1(std::string const &font_size)
Definition: node.cpp:476
void remove_child(pointer_t child)
Definition: node.cpp:622
color get_color() const
Definition: node.cpp:390
void set_line_height(decimal_number_t line_height)
Definition: node.cpp:452
bool get_flag(std::string const &name)
Definition: node.cpp:746
static int const g_to_string_flag_show_quotes
Definition: node.h:125
decimal_number_t f_decimal_number
Definition: node.h:195
bool is(node_type_t const type) const
Definition: node.cpp:264
void set_decimal_number(decimal_number_t decimal_number)
Definition: node.cpp:384
position f_position
Definition: node.h:192
void display(std::ostream &out, uint32_t indent) const
Definition: node.cpp:1263
void set_variable(std::string const &name, pointer_t value)
Definition: node.cpp:699
void clear()
Definition: node.cpp:553
void set_string(std::string const &str)
Definition: node.cpp:336
boolean_t
Definition: node.h:29
size_t size() const
Definition: node.cpp:560
node(node_type_t const type, position const &pos)
Definition: node.cpp:214
void set_font_size(decimal_number_t font_size)
Definition: node.cpp:436
void set_dim2(std::string const &line_height)
Definition: node.cpp:513
static int const g_to_string_flag_add_spaces
Definition: node.h:126
std::string get_dim1() const
Definition: node.cpp:461
pointer_t get_child(size_t idx) const
Definition: node.cpp:647
std::string get_dim2() const
Definition: node.cpp:498
void replace_child(pointer_t o, pointer_t n)
Definition: node.cpp:677
decimal_number_t get_decimal_number() const
Definition: node.cpp:378
double decimal_number_t
Definition: csspp.h:53
void set_integer(integer_t integer)
Definition: node.cpp:360
static void limit_nodes_to(uint32_t count)
Definition: node.cpp:1416
std::string f_string
Definition: node.h:196
std::string f_lowercase_string
Definition: node.h:197
node_type_t get_type() const
Definition: node.cpp:259
void set_color(color c)
Definition: node.cpp:411
void set_boolean(bool integer)
Definition: node.cpp:372
boolean_t to_boolean() const
Definition: node.cpp:269
integer_t f_integer
Definition: node.h:194
std::vector< pointer_t > list_t
Definition: node.h:187
bool get_boolean() const
Definition: node.cpp:366
std::string const & get_lowercase_string() const
Definition: node.cpp:342
std::ostream & operator<<(std::ostream &out, csspp::node_type_t const type)
Definition: node.cpp:1423
node_type_t f_type
Definition: node.h:191
void copy_variable(node::pointer_t source)
Definition: node.cpp:704
integer_t get_integer() const
Definition: node.cpp:354
flag_table_t f_flags
Definition: node.h:200
void clear_variables()
Definition: node.cpp:694
size_t child_position(pointer_t child)
Definition: node.cpp:567

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.