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
|
/***************************************************************************
* css.c
* Token-based parser for CSS definitions
* Author - Colomban Wendling <colomban@geany.org>
* License GPL-2
**************************************************************************/
#include "general.h"
#include <string.h>
#include <ctype.h>
#include "entry.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
#define isSelectorChar(c) \
/* attribute selectors are handled separately */ \
(isalnum (c) || \
(c) == '_' || /* allowed char */ \
(c) == '-' || /* allowed char */ \
(c) == '+' || /* allow all sibling in a single tag */ \
(c) == '>' || /* allow all child in a single tag */ \
(c) == '~' || /* allow general sibling combinator */ \
(c) == '|' || /* allow namespace separator */ \
(c) == '(' || /* allow pseudo-class arguments */ \
(c) == ')' || \
(c) == '.' || /* allow classes and selectors */ \
(c) == ':' || /* allow pseudo classes */ \
(c) == '*' || /* allow globs as P + * */ \
(c) == '#') /* allow ids */
typedef enum eCssKinds {
K_CLASS, K_SELECTOR, K_ID
} cssKind;
static kindDefinition CssKinds [] = {
{ true, 'c', "class", "classes" },
{ true, 's', "selector", "selectors" },
{ true, 'i', "id", "identities" }
};
typedef enum {
/* any ASCII */
TOKEN_EOF = 257,
TOKEN_SELECTOR,
TOKEN_STRING
} tokenType;
typedef struct {
tokenType type;
vString *string;
} tokenInfo;
static void parseSelector (vString *const string, const int firstChar)
{
int c = firstChar;
do
{
vStringPut (string, (char) c);
c = getcFromInputFile ();
} while (isSelectorChar (c));
ungetcToInputFile (c);
}
static void readToken (tokenInfo *const token)
{
int c;
vStringClear (token->string);
getNextChar:
c = getcFromInputFile ();
while (isspace (c))
c = getcFromInputFile ();
token->type = c;
switch (c)
{
case EOF: token->type = TOKEN_EOF; break;
case '\'':
case '"':
{
const int delimiter = c;
do
{
vStringPut (token->string, c);
c = getcFromInputFile ();
if (c == '\\')
c = getcFromInputFile ();
}
while (c != EOF && c != delimiter);
if (c != EOF)
vStringPut (token->string, c);
token->type = TOKEN_STRING;
break;
}
case '/': /* maybe comment start */
{
int d = getcFromInputFile ();
if (d != '*')
{
ungetcToInputFile (d);
vStringPut (token->string, c);
token->type = c;
}
else
{
d = getcFromInputFile ();
do
{
c = d;
d = getcFromInputFile ();
}
while (d != EOF && ! (c == '*' && d == '/'));
goto getNextChar;
}
break;
}
default:
if (! isSelectorChar (c))
{
vStringPut (token->string, c);
token->type = c;
}
else
{
parseSelector (token->string, c);
token->type = TOKEN_SELECTOR;
}
break;
}
}
/* sets selector kind in @p kind if found, otherwise don't touches @p kind */
static cssKind classifySelector (const vString *const selector)
{
size_t i;
for (i = vStringLength (selector); i > 0; --i)
{
char c = vStringChar (selector, i - 1);
if (c == '.')
return K_CLASS;
else if (c == '#')
return K_ID;
}
return K_SELECTOR;
}
static void findCssTags (void)
{
bool readNextToken = true;
tokenInfo token;
token.string = vStringNew ();
do
{
if (readNextToken)
readToken (&token);
readNextToken = true;
if (token.type == '@')
{ /* At-rules, from the "@" to the next block or semicolon */
bool useContents;
readToken (&token);
useContents = (strcmp (vStringValue (token.string), "media") == 0 ||
strcmp (vStringValue (token.string), "supports") == 0);
while (token.type != TOKEN_EOF &&
token.type != ';' && token.type != '{')
{
readToken (&token);
}
/* HACK: we *eat* the opening '{' for medias and the like so that
* the content is parsed as if it was at the root */
readNextToken = useContents && token.type == '{';
}
else if (token.type == TOKEN_SELECTOR)
{ /* collect selectors and make a tag */
cssKind kind = K_SELECTOR;
MIOPos filePosition;
unsigned long lineNumber;
vString *selector = vStringNew ();
do
{
if (vStringLength (selector) > 0)
vStringPut (selector, ' ');
vStringCat (selector, token.string);
kind = classifySelector (token.string);
lineNumber = getInputLineNumber ();
filePosition = getInputFilePosition ();
readToken (&token);
/* handle attribute selectors */
if (token.type == '[')
{
int depth = 1;
while (depth > 0 && token.type != TOKEN_EOF)
{
vStringCat (selector, token.string);
readToken (&token);
if (token.type == '[')
depth++;
else if (token.type == ']')
depth--;
}
if (token.type != TOKEN_EOF)
vStringCat (selector, token.string);
readToken (&token);
}
}
while (token.type == TOKEN_SELECTOR);
/* we already consumed the next token, don't read it twice */
readNextToken = false;
if (CssKinds[kind].enabled)
{
tagEntryInfo e;
initTagEntry (&e, vStringValue (selector), kind);
e.lineNumber = lineNumber;
e.filePosition = filePosition;
makeTagEntry (&e);
}
vStringDelete (selector);
}
else if (token.type == '{')
{ /* skip over { ... } */
int depth = 1;
while (depth > 0 && token.type != TOKEN_EOF)
{
readToken (&token);
if (token.type == '{')
depth++;
else if (token.type == '}')
depth--;
}
}
}
while (token.type != TOKEN_EOF);
vStringDelete (token.string);
}
/* parser definition */
extern parserDefinition* CssParser (void)
{
static const char *const extensions [] = { "css", NULL };
parserDefinition* def = parserNew ("CSS");
def->kindTable = CssKinds;
def->kindCount = ARRAY_SIZE (CssKinds);
def->extensions = extensions;
def->parser = findCssTags;
return def;
}
|