diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/utils/CSSParser.cpp | 29 | ||||
-rw-r--r-- | src/core/utils/CSSParser.hpp | 119 |
2 files changed, 137 insertions, 11 deletions
diff --git a/src/core/utils/CSSParser.cpp b/src/core/utils/CSSParser.cpp index 6ac98fa..e66eb34 100644 --- a/src/core/utils/CSSParser.cpp +++ b/src/core/utils/CSSParser.cpp @@ -16,11 +16,40 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "BufferedCharReader.hpp" +#include "Tokenizer.hpp" + #include "CSSParser.hpp" namespace ousia { namespace utils { +static const int CURLY_OPEN = 1; +static const int CURLY_CLOSE = 2; +static const int COLON = 3; +static const int SEMICOLON = 4; +static const int HASH = 5; +static const int BRACKET_OPEN = 6; +static const int BRACKET_CLOSE = 7; +static const int COMMENT_OPEN = 8; +static const int COMMENT_CLOSE = 9; + +static const TokenTreeNode CSS_ROOT{{{"{", CURLY_OPEN}, + {"}", CURLY_CLOSE}, + {":", COLON}, + {";", SEMICOLON}, + {"#", HASH}, + {"[", BRACKET_OPEN}, + {"]", BRACKET_CLOSE}, + {"/*", COMMENT_OPEN}, + {"*/", COMMENT_CLOSE}}}; + +StyleNode CSSParser::parse(BufferedCharReader &input) { + Tokenizer tokenizer {input, CSS_ROOT}; + //TODO: implement + +} + } diff --git a/src/core/utils/CSSParser.hpp b/src/core/utils/CSSParser.hpp index 4e82371..8db5cdd 100644 --- a/src/core/utils/CSSParser.hpp +++ b/src/core/utils/CSSParser.hpp @@ -20,8 +20,12 @@ #define _OUSIA_UTILS_CSS_PARSER_HPP_ #include <istream> +#include <map> +#include <vector> #include <tuple> +#include "BufferedCharReader.hpp" + namespace ousia { namespace utils { @@ -36,29 +40,122 @@ namespace utils { * local style definitions for single nodes. */ struct Specificity { + int b; + int c; + int d; -int b; -int c; -int d; - -Specificity(int b, int c, int d): b(b), c(c), d(d) {} - + Specificity(int b, int c, int d) : b(b), c(c), d(d) {} }; - -bool operator< (const Specificity &x, const Specificity &y){ +bool operator<(const Specificity &x, const Specificity &y) +{ return std::tie(x.b, x.c, x.d) < std::tie(y.b, y.c, y.d); } -bool operator> (const Specificity &x, const Specificity &y){ +bool operator>(const Specificity &x, const Specificity &y) +{ return std::tie(x.b, x.c, x.d) > std::tie(y.b, y.c, y.d); } -bool operator== (const Specificity &x, const Specificity &y){ +bool operator==(const Specificity &x, const Specificity &y) +{ return std::tie(x.b, x.c, x.d) == std::tie(y.b, y.c, y.d); } +class RuleSet { +private: + const std::map<std::string, std::string> values; + const Specificity specificity; + +public: + RuleSet(std::map<std::string, std::string> values, Specificity specificity) + : values(values), specificity(specificity) + { + } + + const std::map<std::string, std::string> &getValues() const + { + return values; + } + + const Specificity &getSpecificity() const { return specificity; } +}; + +class PseudoSelector { +private: + const std::string name; + const std::vector<std::string> args; + const bool generative; + +public: + PseudoSelector(std::string name, std::vector<std::string> args, + bool generative) + : name(name), args(args), generative(generative) + { + } + + const std::string &getName() const { return name; } + + const std::vector<std::string> &getArgs() const { return args; } + + const bool &isGenerative() const { return generative; } +}; + +enum class SelectionOperator { DESCENDANT, DIRECT_DESCENDANT }; + +// TODO: Subclass of Andreas' Node class +class StyleEdge { +private: + // TODO: This is wrong! Here we want to have a managed pointer as Andreas + // mentioned! + // const StyleNode target; + const SelectionOperator selectionOperator; + +public: + StyleEdge(/*StyleNode target,*/ SelectionOperator selectionOperator) + : /*target(target),*/ selectionOperator(selectionOperator) + { + } + + // const StyleNode &getTarget() const { return target; } + + const SelectionOperator &getSelectionOperator() const + { + return selectionOperator; + } +}; + +// TODO: Subclass of Andreas' Node class +class StyleNode { +private: + const std::string className; + const PseudoSelector pseudoSelector; + const std::vector<StyleEdge> edges; + const std::vector<RuleSet> ruleSets; + +public: + StyleNode(std::string className, PseudoSelector pseudoSelector, + std::vector<StyleEdge> edges, std::vector<RuleSet> ruleSets) + : className(className), + pseudoSelector(pseudoSelector), + edges(edges), + ruleSets(ruleSets) + { + } + + const std::string &getClassName() const { return className; } + + const PseudoSelector &getPseudoSelector() const { return pseudoSelector; } + + const std::vector<StyleEdge> &getEdges() const { return edges; } + + const std::vector<RuleSet> &getRuleSets() const { return ruleSets; } +}; + +class CSSParser { +public: + StyleNode parse(BufferedCharReader &input); +}; } } - #endif |