diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/BufferedCharReader.cpp | 2 | ||||
-rw-r--r-- | src/core/CSS.hpp | 93 | ||||
-rw-r--r-- | src/core/CodeTokenizer.cpp | 26 | ||||
-rw-r--r-- | src/core/CodeTokenizer.hpp | 4 | ||||
-rw-r--r-- | src/plugins/css/CSSParser.cpp | 71 | ||||
-rw-r--r-- | src/plugins/css/CSSParser.hpp | 32 |
6 files changed, 164 insertions, 64 deletions
diff --git a/src/core/BufferedCharReader.cpp b/src/core/BufferedCharReader.cpp index 0821a5d..aeedf12 100644 --- a/src/core/BufferedCharReader.cpp +++ b/src/core/BufferedCharReader.cpp @@ -158,7 +158,7 @@ bool BufferedCharReader::readCharacterAtCursor(ReadCursor &cursor, char *c) // If data has been read, append it to the input buffer and try // again if (cnt > 0) { - buffer.emplace_back(buf.data()); + buffer.emplace_back(std::string(buf.data(), cnt)); continue; } diff --git a/src/core/CSS.hpp b/src/core/CSS.hpp index 1c0ed17..4cf15be 100644 --- a/src/core/CSS.hpp +++ b/src/core/CSS.hpp @@ -23,6 +23,8 @@ #include <vector> #include <tuple> +#include <core/variant/Variant.hpp> + #include "Managed.hpp" #include "Node.hpp" @@ -44,51 +46,50 @@ struct Specificity { int d; Specificity(int b, int c, int d) : b(b), c(c), d(d) {} -}; -// TODO: Is this inline usage correct? I used this to prevent "multiple -// definition" errors -inline 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); -} + friend 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); + } -inline 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); -} + friend 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); + } -inline 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); -} + friend 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); + } +}; /** * The RuleSet class serves as a container class for key-value * pairs. The values are TypeInstances. The proper type is * implicitly defined by the keyword. - * - * TODO: This code is currently commented out until the TypeSystem works. */ -/*class RuleSet : public Managed { +class RuleSet : public Managed { private: - const std::map<std::string, std::string> values; - const Specificity specificity; + std::map<std::string, variant::Variant> rules; public: - RuleSet(Manager &mgr, std::map<std::string, std::string> values, - Specificity specificity) - : Managed(mgr), values(std::move(values)), specificity(specificity) - { - } + /** + * Initializes an empty RuleSet. + */ + RuleSet(Manager &mgr) : Managed(mgr), rules() {} - const std::map<std::string, std::string> &getValues() const - { - return values; - } + std::map<std::string, variant::Variant> &getRules() { return rules; } - const Specificity &getSpecificity() const { return specificity; } -};*/ + const std::map<std::string, variant::Variant> &getRules() const + { + return rules; + } + + void merge(Rooted<RuleSet> other) + { + rules.insert(other->rules.begin(), other->rules.end()); + } +}; /** * PseudoSelectors are functions that change the behaviour of Selectors. @@ -249,8 +250,8 @@ public: private: const PseudoSelector pseudoSelector; ManagedVector<SelectorEdge> edges; - // TODO: This is temporarily commented out until the TypeSystem works. - // Owned<RuleSet> ruleSets; + Owned<RuleSet> ruleSet; + bool accepting = false; /** * This is an internal method all getChildren variants refer to. @@ -260,21 +261,27 @@ private: const PseudoSelector *select); public: + /** + * This initializes an empty SelectorNode with the given name and the + * given PseudoSelector. + */ SelectorNode(Manager &mgr, std::string name, PseudoSelector pseudoSelector) : Node(mgr, std::move(name)), pseudoSelector(std::move(pseudoSelector)), - edges(this) //, - // TODO: This is temporarily commented out until the TypeSystem works. - // ruleSets(acquire(ruleSets)) + edges(this), + ruleSet(new RuleSet(mgr), this) { } + /** + * This initializes an empty SelectorNode with the given name and the + * trivial PseudoSelector "true". + */ SelectorNode(Manager &mgr, std::string name) : Node(mgr, std::move(name)), pseudoSelector("true", false), - edges(this) //, - // TODO: This is temporarily commented out until the TypeSystem works. - // ruleSets(acquire(ruleSets)) + edges(this), + ruleSet(new RuleSet(mgr), this) { } @@ -282,9 +289,7 @@ public: ManagedVector<SelectorEdge> &getEdges() { return edges; } - // TODO: This is temporarily commented out until the TypeSystem works. - // const std::vector<Owned<RuleSet>> &getRuleSets() const { return - // ruleSets; } + Rooted<RuleSet> getRuleSet() const { return ruleSet; } /** * This returns the child of this SelectorNode that is connected by @@ -386,6 +391,10 @@ public: * automatically using the DESCENDANT SelectionOperator. */ std::vector<Rooted<SelectorNode>> append(Rooted<SelectorNode> node); + + bool isAccepting() { return accepting; } + + void setAccepting(bool accepting) { this->accepting = accepting; } }; } #endif diff --git a/src/core/CodeTokenizer.cpp b/src/core/CodeTokenizer.cpp index ce4afe5..e9c1bbf 100644 --- a/src/core/CodeTokenizer.cpp +++ b/src/core/CodeTokenizer.cpp @@ -59,10 +59,14 @@ bool CodeTokenizer::doPrepare(const Token &t, std::deque<Token> &peeked) state = CodeTokenizerState::IN_LINE_COMMENT; break; case CodeTokenMode::LINEBREAK: - peeked.push_back({it->second.id, t.content, t.startColumn, - t.startLine, t.endColumn, t.endLine}); - return true; + if (!ignoreLinebreaks) { + peeked.push_back({it->second.id, t.content, + t.startColumn, t.startLine, + t.endColumn, t.endLine}); + } + return !ignoreLinebreaks; default: + bool empty = true; if (t.tokenId == TOKEN_TEXT) { int begin = -1; for (size_t c = 0; c < t.content.length(); c++) { @@ -86,20 +90,22 @@ bool CodeTokenizer::doPrepare(const Token &t, std::deque<Token> &peeked) t.startColumn + begin, t.startLine, t.startColumn + (int)c, t.endLine}); begin = -1; + empty = false; } } } - if(begin >= 0){ - peeked.push_back(Token{ - TOKEN_TEXT, - t.content.substr(begin), - t.startColumn + begin, t.startLine, - t.endColumn, t.endLine}); + if (begin >= 0) { + peeked.push_back( + Token{TOKEN_TEXT, t.content.substr(begin), + t.startColumn + begin, t.startLine, + t.endColumn, t.endLine}); + empty = false; } } else { + empty = false; peeked.push_back(t); } - return true; + return !empty; } startToken = t; returnTokenId = it->second.id; diff --git a/src/core/CodeTokenizer.hpp b/src/core/CodeTokenizer.hpp index 247918e..43c7abb 100644 --- a/src/core/CodeTokenizer.hpp +++ b/src/core/CodeTokenizer.hpp @@ -101,6 +101,10 @@ public: * true. */ bool ignoreComments = false; + /** + * If you do not want linebreaks to be returned you can set this to true. + */ + bool ignoreLinebreaks = false; /** * diff --git a/src/plugins/css/CSSParser.cpp b/src/plugins/css/CSSParser.cpp index 6653d9e..82ed7e1 100644 --- a/src/plugins/css/CSSParser.cpp +++ b/src/plugins/css/CSSParser.cpp @@ -64,12 +64,7 @@ static const TokenTreeNode CSS_ROOT{{{"{", CURLY_OPEN}, {"*/", COMMENT_CLOSE}, {"\"", DOUBLE_QUOTE}, {"\\", ESCAPE}, - // linux linebreak - {"\n", LINEBREAK}, - // windows linebreak - {"\r\n", LINEBREAK}, - // Mac OS linebreak - {"\r", LINEBREAK}}}; + {"\n", LINEBREAK}}}; static const std::map<int, CodeTokenDescriptor> CSS_DESCRIPTORS = { {COMMENT_OPEN, {CodeTokenMode::BLOCK_COMMENT_START, COMMENT}}, @@ -83,6 +78,7 @@ Rooted<Node> CSSParser::parse(std::istream &is, ParserContext &ctx) BufferedCharReader input{is}; CodeTokenizer tokenizer{input, CSS_ROOT, CSS_DESCRIPTORS}; tokenizer.ignoreComments = true; + tokenizer.ignoreLinebreaks = true; Rooted<SelectorNode> root = {new SelectorNode{ctx.manager, "root"}}; parseDocument(root, tokenizer, ctx); return root; @@ -97,8 +93,23 @@ void CSSParser::parseDocument(Rooted<SelectorNode> root, } tokenizer.resetPeek(); std::vector<Rooted<SelectorNode>> leafList; + // parse the SelectorTree for this ruleSet. parseSelectors(root, tokenizer, leafList, ctx); - // TODO: Parse Ruleset + // parse the RuleSet itself. + Rooted<RuleSet> ruleSet = parseRuleSet(tokenizer, ctx); + for (auto &leaf : leafList) { + /* + * every leaf is an accepting node, if one considers the SelectorTree + * to be a finite state machine. This is relevant, if users do not use + * the CSS Parser to parse actual Ruleset content but to construct a + * SelectorTree just to identify a part of the DocumentTree. + */ + leaf->setAccepting(true); + /* + * similarly we append the found rules to all leafs. + */ + leaf->getRuleSet()->merge(ruleSet); + } parseDocument(root, tokenizer, ctx); } @@ -280,7 +291,51 @@ Rooted<SelectorNode> CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, } } -// TODO: Add RuleSet parsing methods. +Rooted<RuleSet> CSSParser::parseRuleSet(CodeTokenizer &tokenizer, + ParserContext &ctx) +{ + Rooted<RuleSet> ruleSet{new RuleSet(ctx.manager)}; + // if we have no ruleset content, we return an empty ruleset. + Token t; + if (!expect(CURLY_OPEN, tokenizer, t, false, ctx)) { + return ruleSet; + } + // otherwise we parse the rules. + parseRules(tokenizer, ruleSet, ctx); + // and we expect closing curly braces. + expect(CURLY_CLOSE, tokenizer, t, true, ctx); + return ruleSet; +} + +void CSSParser::parseRules(CodeTokenizer &tokenizer, Rooted<RuleSet> ruleSet, + ParserContext &ctx) +{ + std::string key; + variant::Variant value; + while (parseRule(tokenizer, ctx, key, value)) { + ruleSet->getRules().insert({key, value}); + } +} + +bool CSSParser::parseRule(CodeTokenizer &tokenizer, ParserContext &ctx, + std::string &key, variant::Variant &value) +{ + Token t; + if (!expect(TOKEN_TEXT, tokenizer, t, false, ctx)) { + return false; + } + // if we find text that is the key first. + key = t.content; + // then we expect a : + expect(COLON, tokenizer, t, true, ctx); + // then the value + // TODO: Resolve key for appropriate parsing function here. + expect(STRING, tokenizer, t, true, ctx); + value = variant::Variant(t.content.c_str()); + // and a ; + expect(SEMICOLON, tokenizer, t, true, ctx); + return true; +} bool CSSParser::expect(int expectedType, CodeTokenizer &tokenizer, Token &t, bool force, ParserContext &ctx) diff --git a/src/plugins/css/CSSParser.hpp b/src/plugins/css/CSSParser.hpp index eb51dfa..27a483d 100644 --- a/src/plugins/css/CSSParser.hpp +++ b/src/plugins/css/CSSParser.hpp @@ -89,7 +89,30 @@ private: Rooted<SelectorNode> parsePrimitiveSelector(CodeTokenizer &tokenizer, ParserContext &ctx); - // TODO: Add RuleSet parsing methods. + /** + * Implements the RULESET Nonterminal, which parses an entire RuleSet. Note + * that we do not require RuleSets to be parsed. It is permitted to just + * insert Selector expressions. + */ + Rooted<RuleSet> parseRuleSet(CodeTokenizer &tokenizer, ParserContext &ctx); + + /** + * Implements the RULES Nonterminal, which parses CSSRules inside a RuleSet. + */ + void parseRules(CodeTokenizer &tokenizer, Rooted<RuleSet> ruleSet, + ParserContext &ctx); + + /** + * Implements the RULE Nonterminal, which parses one single CSSRule. Key + * and value are stored in the input references. + * + * @param key is a (possibly empty) string reference for the key found. + * @param value is a (possibly empty) Variant reference for the value found. + * + * @return true if a rule was found. + */ + bool parseRule(CodeTokenizer &tokenizer, ParserContext &ctx, + std::string &key, variant::Variant &value); /** * A convenience function to wrap around the tokenizer peek() function that @@ -112,9 +135,12 @@ public: /** * This parses the given input as CSS content as specified by the grammar * seen above. The return value is a Rooted reference to the root of the - * SelectorTree. - * TODO: The RuleSet at the respective node at the tree lists all CSS Style + * SelectorTree. SelectorTrees are documented in detail in the CSS.hpp + * The RuleSet at the respective node at the tree lists all CSS Style * rules that apply. + * Note that you are not required to insert CSS code containing actual + * rules. You are permitted to just insert a CSS Selector expression + * specifying some part of a DocumentTree you want to refer to. * * @param is is a reference to the input stream that should be parsed. * @param ctx is a reference to the context that should be used while |