This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
109 lines (96 loc) · 2.53 KB
/
parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright (C) 2020 Tristan. All Rights Reserved.
* This file is licensed under the BSD 2-Clause license.
* See the COPYING file for licensing information.
*/
#include "parser/css/parser.hpp"
#include "logger.hpp"
namespace CSS {
Stylesheet
Parser::ParseStylesheet() noexcept {
topLevelFlag = true;
Stylesheet sheet;
this->rules = &sheet.rules;
ConsumeListOfRules();
return sheet;
}
void
Parser::ConsumeListOfRules() noexcept {
this->it = std::begin(tokenizer.tokens);
this->endIterator = std::end(tokenizer.tokens);
for (; it != endIterator;) {
const auto &token = *it;
bool wasItMutatedManually = false;
// NOTE that the subroutines called from the following switch
// likely mutates the 'it'erator.
switch (token.type) {
case Token::Type::WHITESPACE:
break;
case Token::Type::CDC:
case Token::Type::CDO:
if (!topLevelFlag && !ConsumeQualifiedRule()) {
wasItMutatedManually = true;
}
break;
case Token::Type::AT_KEYWORD:
if (!ConsumeAtRule()) {
wasItMutatedManually = true;
}
break;
default:
if (!topLevelFlag && !ConsumeQualifiedRule()) {
wasItMutatedManually = true;
}
break;
}
if (!wasItMutatedManually) {
++it;
}
}
}
bool
Parser::ConsumeAtRule() noexcept {
return false;
}
bool
Parser::ConsumeQualifiedRule() noexcept {
for (; it != endIterator; ++it) {
}
// EOF is reached, consume all the tokens but we can't make a qualified
// rule out of it unfortunately :(.
Logger::Error(__FUNCTION__, "Parse error: EOF reached!");
return true;
}
void
Parser::ConsumeDeclaration() noexcept {
const std::vector<Unicode::CodePoint> &name = std::get_if<TokenCodePointsData>(&it->data)->codePoints;
std::vector<ComponentValue> values;
while (it != endIterator && it->type == Token::Type::WHITESPACE) {
++it;
}
if (it == endIterator || it->type != Token::Type::COLON) {
Logger::Error(__FUNCTION__, "Parse error: ident should be followed by a colon!");
return;
}
++it;
while (it != endIterator && it->type == Token::Type::WHITESPACE) {
++it;
}
if (it != endIterator) {
values.push_back(ConsumeComponentValue());
}
}
ComponentValue
Parser::ConsumeComponentValue() noexcept {
switch (it->type) {
case Token::Type::CURLY_OPEN:
case Token::Type::SQUARE_OPEN:
case Token::Type::PAREN_OPEN:
return {/* a simple block */ Block {}};
case Token::Type::FUNCTION:
return {/* a function */ Function {}};
default:
return {*it++};
}
}
} // namespace CSS