Current Version: 1.0.20
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-2018 Made to Order Software Corp. All Rights Reserved
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  FRAME, // @keyframes <name> { frame { ... } frame { ... } ... };
110 
111  max_type
112 };
113 
114 // useful for quick switchs
115 int32_t constexpr mix_node_types(node_type_t a, node_type_t b)
116 {
117  return static_cast<int32_t>(a) * 65536 + static_cast<int32_t>(b);
118 }
119 
120 // the std::enable_shared_from_this<>() has no virtual dtor
121 #pragma GCC diagnostic push
122 #pragma GCC diagnostic ignored "-Weffc++"
123 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
124 class node
125  : public std::enable_shared_from_this<node>
126 {
127 public:
128  typedef std::shared_ptr<node> pointer_t;
129  static size_t const npos = static_cast<size_t>(-1);
130 
131  static int const g_to_string_flag_show_quotes = 0x01;
132  static int const g_to_string_flag_add_spaces = 0x02;
133 
134  node(node_type_t const type, position const & pos);
135  ~node();
136 
137  pointer_t clone() const;
138 
139  node_type_t get_type() const;
140  bool is(node_type_t const type) const;
141  boolean_t to_boolean() const;
142 
143  position const & get_position() const;
144  std::string const & get_string() const;
145  void set_string(std::string const & str);
146  std::string const & get_lowercase_string() const;
147  void set_lowercase_string(std::string const & str);
148  integer_t get_integer() const;
149  void set_integer(integer_t integer);
150  bool get_boolean() const;
151  void set_boolean(bool integer);
152  decimal_number_t get_decimal_number() const;
153  void set_decimal_number(decimal_number_t decimal_number);
154  color get_color() const;
155  void set_color(color c);
156  decimal_number_t get_font_size() const;
157  void set_font_size(decimal_number_t font_size);
158  decimal_number_t get_line_height() const;
159  void set_line_height(decimal_number_t line_height);
160  std::string get_dim1() const;
161  void set_dim1(std::string const & font_size);
162  std::string get_dim2() const;
163  void set_dim2(std::string const & line_height);
164 
165  bool empty() const;
166  void clear();
167  size_t size() const;
168  size_t child_position(pointer_t child);
169  void add_child(pointer_t child);
170  void insert_child(size_t idx, pointer_t child);
171  void remove_child(pointer_t child);
172  void remove_child(size_t idx);
173  pointer_t get_child(size_t idx) const;
174  pointer_t get_last_child() const;
175  void take_over_children_of(pointer_t n);
176  void replace_child(pointer_t o, pointer_t n);
177 
178  void clear_variables();
179  void set_variable(std::string const & name, pointer_t value);
180  pointer_t get_variable(std::string const & name);
181  void copy_variable(node::pointer_t source);
182 
183  void clear_flags();
184  void set_flag(std::string const & name, bool value);
185  bool get_flag(std::string const & name);
186 
187  std::string to_string(int flags) const;
188  void display(std::ostream & out, uint32_t indent) const;
189 
190  static void limit_nodes_to(uint32_t count);
191 
192 private:
193  typedef std::vector<pointer_t> list_t;
194  typedef std::map<std::string, node::pointer_t> variable_table_t;
195  typedef std::map<std::string, bool> flag_table_t;
196 
199  bool f_boolean = false;
200  integer_t f_integer = 0;
201  decimal_number_t f_decimal_number = 0.0;
202  std::string f_string = std::string();
203  std::string f_lowercase_string = std::string();
204  list_t f_children = list_t();
205  variable_table_t f_variables = variable_table_t();
206  flag_table_t f_flags = flag_table_t();
207 };
208 #pragma GCC diagnostic pop
209 
210 typedef std::vector<node::pointer_t> node_vector_t;
211 
212 } // namespace csspp
213 
214 std::ostream & operator << (std::ostream & out, csspp::node_type_t const type);
215 std::ostream & operator << (std::ostream & out, csspp::node const & n);
216 
218 
219 #endif
220 // #ifndef CSSPP_NODE_H
221 
222 // Local Variables:
223 // mode: cpp
224 // indent-tabs-mode: nil
225 // c-basic-offset: 4
226 // tab-width: 4
227 // End:
228 
229 // vim: ts=4 sw=4 et
std::map< std::string, bool > flag_table_t
Definition: node.h:195
std::vector< node::pointer_t > node_vector_t
Definition: node.h:210
std::shared_ptr< node > pointer_t
Definition: node.h:128
int64_t integer_t
Definition: csspp.h:54
The namespace of all the classes in the CSS Preprocessor.
Definition: assembler.cpp:40
node_type_t
Definition: node.h:36
int32_t constexpr mix_node_types(node_type_t a, node_type_t b)
Definition: node.h:115
std::map< std::string, node::pointer_t > variable_table_t
Definition: node.h:194
position f_position
Definition: node.h:198
boolean_t
Definition: node.h:29
double decimal_number_t
Definition: csspp.h:55
std::vector< pointer_t > list_t
Definition: node.h:193
std::ostream & operator<<(std::ostream &out, csspp::node_type_t const type)
Definition: node.cpp:1452

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.