summaryrefslogtreecommitdiff
path: root/src/parse/language.h
blob: 0f1985c390ae18c34be1bb9e7cf80de302da481e (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
/*
 * This file is part of LibCSS.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 */

#ifndef css_css__parse_language_h_
#define css_css__parse_language_h_

#include <parserutils/utils/stack.h>
#include <parserutils/utils/vector.h>

#include <libcss/functypes.h>
#include <libcss/types.h>

#include "lex/lex.h"
#include "parse/parse.h"
#include "parse/propstrings.h"

/**
 * CSS namespace mapping
 */
typedef struct css_namespace {
	lwc_string *prefix;		/**< Namespace prefix */
	lwc_string *uri;		/**< Namespace URI */
} css_namespace;

/**
 * Context for a CSS language parser
 */
typedef struct css_language {
	css_stylesheet *sheet;		/**< The stylesheet to parse for */

#define STACK_CHUNK 32
	parserutils_stack *context;	/**< Context stack */

	enum {
		CHARSET_PERMITTED,
		IMPORT_PERMITTED,
		NAMESPACE_PERMITTED,
		HAD_RULE
	} state;			/**< State flag, for at-rule handling */

	/** \todo These should be statically allocated */
	/** Interned strings */
	lwc_string *strings[LAST_KNOWN];

	lwc_string *default_namespace;	/**< Default namespace URI */
	css_namespace *namespaces;	/**< Array of namespace mappings */
	uint32_t num_namespaces;	/**< Number of namespace mappings */

	css_allocator_fn alloc;		/**< Memory (de)allocation function */
	void *pw;			/**< Client's private data */
} css_language;

css_error css__language_create(css_stylesheet *sheet, css_parser *parser,
		css_allocator_fn alloc, void *pw, void **language);
css_error css__language_destroy(css_language *language);

/******************************************************************************
 * Helper functions                                                           *
 ******************************************************************************/

/**
 * Consume all leading whitespace tokens
 *
 * \param vector  The vector to consume from
 * \param ctx     The vector's context
 */
static inline void consumeWhitespace(const parserutils_vector *vector, int *ctx)
{
	const css_token *token = NULL;

	while ((token = parserutils_vector_peek(vector, *ctx)) != NULL &&
			token->type == CSS_TOKEN_S)
		parserutils_vector_iterate(vector, ctx);
}

/**
 * Determine if a token is a character
 *
 * \param token  The token to consider
 * \param c      The character to match (lowercase ASCII only)
 * \return True if the token matches, false otherwise
 */
static inline bool tokenIsChar(const css_token *token, uint8_t c)
{
	bool result = false;

	if (token != NULL && token->type == CSS_TOKEN_CHAR && 
	                lwc_string_length(token->idata) == 1) {
		char d = lwc_string_data(token->idata)[0];

		/* Ensure lowercase comparison */
		if ('A' <= d && d <= 'Z')
			d += 'a' - 'A';

		result = (d == c);
	}

	return result;
}

#endif