summaryrefslogtreecommitdiffstats
path: root/src/parser/css_parser_base.cpp
blob: 128e5d4fb3820197efc8191a55795aa4130dc753 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <orcus/css_parser_base.hpp>
#include <orcus/parser_global.hpp>

#include "utf8.hpp"

#include <cstring>
#include <cassert>
#include <cmath>
#include <limits>

namespace orcus { namespace css {

parser_base::parser_base(std::string_view content) :
    orcus::parser_base(content.data(), content.size()),
    m_simple_selector_count(0),
    m_combinator(combinator_t::descendant) {}

void parser_base::identifier(const char*& p, size_t& len, std::string_view extra)
{
    p = mp_char;
    len = 1;
    for (next(); has_char(); next(), ++len)
    {
        char c = cur_char();
        if (is_alpha(c) || is_numeric(c) || is_in(c, "-_"))
            continue;

        if (!extra.empty())
        {
            // See if the character is one of the extra allowed characters.
            if (is_in(c, extra))
                continue;
        }
        return;
    }
}

uint8_t parser_base::parse_uint8()
{
    // 0 - 255
    int val = 0;
    size_t len = 0;
    for (; has_char() && len <= 3; next())
    {
        char c = cur_char();
        if (!is_numeric(c))
            break;

        ++len;
        val *= 10;
        val += c - '0';
    }

    if (!len)
        throw parse_error("parse_uint8: no digit encountered.", offset());

    int maxval = std::numeric_limits<uint8_t>::max();
    if (val > maxval)
        val = maxval;

    return static_cast<uint8_t>(val);
}

std::string_view parser_base::parse_value()
{
    auto throw_invalid = [this](uint8_t n_bytes)
    {
        std::ostringstream os;
        os << "parse_value: invalid utf-8 byte length (" << int(n_bytes) << ")";
        throw parse_error(os.str(), offset());
    };

    auto check_byte_length_or_throw = [this](uint8_t n_bytes, std::size_t max_size)
    {
        if (std::size_t(n_bytes) > max_size)
        {
            std::ostringstream os;
            os << "parse_value: utf-8 byte length is " << int(n_bytes) << " but only " << max_size << " bytes remaining.";
            throw parse_error(os.str(), offset());
        }
    };

    std::size_t max_size = available_size();
    if (!max_size)
        return {};

    const char* p0 = mp_char;
    std::size_t len = 0;

    char c = cur_char();
    uint8_t n_bytes = calc_utf8_byte_length(c);

    // any of '-+.#' is allowed as first character, while any of '-_.%' is
    // allowed as second characters.

    switch (n_bytes)
    {
        case 1:
        {
            if (!is_alpha(c) && !is_numeric(c) && !is_in(c, "-+.#"))
                parse_error::throw_with("parse_value: illegal first character of a value '", c, "'", offset());
            break;
        }
        case 2:
        case 3:
        case 4:
        {
            check_byte_length_or_throw(n_bytes, max_size);
            break;
        }
        default:
            throw_invalid(n_bytes);
    }

    len += n_bytes;
    next(n_bytes);

    while (has_char())
    {
        c = cur_char();
        max_size = available_size();
        n_bytes = calc_utf8_byte_length(c);

        switch (n_bytes)
        {
            case 1:
            {
                if (!is_alpha(c) && !is_numeric(c) && !is_in(c, "-_.%"))
                    return {p0, len};
                break;
            }
            case 2:
            case 3:
            case 4:
            {
                check_byte_length_or_throw(n_bytes, max_size);
                break;
            }
            default:
                throw_invalid(n_bytes);
        }

        len += n_bytes;
        next(n_bytes);
    }

    return {p0, len};
}

double parser_base::parse_percent()
{
    double v = parse_double_or_throw();

    if (*mp_char != '%')
        parse_error::throw_with(
            "parse_percent: '%' expected after the numeric value, but '", *mp_char, "' found.", offset());

    next(); // skip the '%'.
    return v;
}

double parser_base::parse_double_or_throw()
{
    double v = parse_double();
    if (std::isnan(v))
        throw parse_error("parse_double: failed to parse double precision value.", offset());
    return v;
}

void parser_base::literal(const char*& p, size_t& len, char quote)
{
    assert(cur_char() == quote);
    next();
    skip_to(p, len, quote);

    if (cur_char() != quote)
        throw parse_error("literal: end quote has never been reached.", offset());
}

void parser_base::skip_to(const char*&p, size_t& len, char c)
{
    p = mp_char;
    len = 0;
    for (; has_char(); next(), ++len)
    {
        if (cur_char() == c)
            return;
    }
}

void parser_base::skip_to_or_blank(const char*&p, size_t& len, std::string_view chars)
{
    p = mp_char;
    len = 0;
    for (; has_char(); next(), ++len)
    {
        if (is_blank(*mp_char) || is_in(*mp_char, chars))
            return;
    }
}

void parser_base::skip_blanks()
{
    skip(" \t\r\n");
}

void parser_base::skip_blanks_reverse()
{
    const char* p = mp_char + remaining_size();
    for (; p != mp_char; --p, --mp_end)
    {
        if (!is_blank(*p))
            break;
    }
}

void parser_base::shrink_stream()
{
    // Skip any leading blanks.
    skip_blanks();

    if (!remaining_size())
        return;

    // Skip any trailing blanks.
    skip_blanks_reverse();

    // Skip leading <!-- if present.

    const char* com_open = "<!--";
    size_t com_open_len = std::strlen(com_open);
    if (remaining_size() < com_open_len)
        // Not enough stream left.  Bail out.
        return;

    const char* p = mp_char;
    for (size_t i = 0; i < com_open_len; ++i, ++p)
    {
        if (*p != com_open[i])
            return;
        next();
    }
    mp_char = p;

    // Skip leading blanks once again.
    skip_blanks();

    // Skip trailing --> if present.
    const char* com_close = "-->";
    size_t com_close_len = std::strlen(com_close);
    size_t n = remaining_size();
    if (n < com_close_len)
        // Not enough stream left.  Bail out.
        return;

    p = mp_char + n; // move to the last char.
    for (size_t i = com_close_len; i > 0; --i, --p)
    {
        if (*p != com_close[i-1])
            return;
    }
    mp_end -= com_close_len;

    skip_blanks_reverse();
}

bool parser_base::skip_comment()
{
    char c = cur_char();
    if (c != '/')
        return false;

    if (remaining_size() > 2 && peek_char() == '*')
    {
        next();
        comment();
        skip_blanks();
        return true;
    }

    return false;
}

void parser_base::comment()
{
    assert(cur_char() == '*');

    // Parse until we reach either EOF or '*/'.
    bool has_star = false;
    for (next(); has_char(); next())
    {
        char c = cur_char();
        if (has_star && c == '/')
        {
            next();
            return;
        }
        has_star = (c == '*');
    }

    // EOF reached.
}

void parser_base::skip_comments_and_blanks()
{
    skip_blanks();
    while (skip_comment())
        ;
}

void parser_base::set_combinator(char c, css::combinator_t combinator)
{
    if (!m_simple_selector_count)
        parse_error::throw_with(
            "set_combinator: combinator '", c, "' encountered without parent element.", offset());

    m_combinator = combinator;
    next();
    skip_comments_and_blanks();
}

void parser_base::reset_before_block()
{
    m_simple_selector_count = 0;
    m_combinator = css::combinator_t::descendant;
}

}}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */