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
|
#ifndef SASS_LEXER_H
#define SASS_LEXER_H
#include <cstring>
namespace Sass {
namespace Prelexer {
//####################################
// BASIC CHARACTER MATCHERS
//####################################
// Match standard control chars
const char* kwd_at(const char* src);
const char* kwd_dot(const char* src);
const char* kwd_comma(const char* src);
const char* kwd_colon(const char* src);
const char* kwd_star(const char* src);
const char* kwd_plus(const char* src);
const char* kwd_minus(const char* src);
const char* kwd_slash(const char* src);
//####################################
// BASIC CLASS MATCHERS
//####################################
// These are locale independant
bool is_space(const char& src);
bool is_alpha(const char& src);
bool is_punct(const char& src);
bool is_digit(const char& src);
bool is_number(const char& src);
bool is_alnum(const char& src);
bool is_xdigit(const char& src);
bool is_unicode(const char& src);
bool is_nonascii(const char& src);
bool is_character(const char& src);
bool is_uri_character(const char& src);
bool escapable_character(const char& src);
// Match a single ctype predicate.
const char* space(const char* src);
const char* alpha(const char* src);
const char* digit(const char* src);
const char* xdigit(const char* src);
const char* alnum(const char* src);
const char* punct(const char* src);
const char* hyphen(const char* src);
const char* unicode(const char* src);
const char* nonascii(const char* src);
const char* character(const char* src);
const char* uri_character(const char* src);
const char* escapable_character(const char* src);
// Match multiple ctype characters.
const char* spaces(const char* src);
const char* digits(const char* src);
const char* hyphens(const char* src);
// Whitespace handling.
const char* no_spaces(const char* src);
const char* optional_spaces(const char* src);
// Match any single character (/./).
const char* any_char(const char* src);
// Assert word boundary (/\b/)
// Is a zero-width positive lookaheads
const char* word_boundary(const char* src);
// Match a single linebreak (/(?:\n|\r\n?)/).
const char* re_linebreak(const char* src);
// Assert string boundaries (/\Z|\z|\A/)
// There are zero-width positive lookaheads
const char* end_of_line(const char* src);
// Assert end_of_file boundary (/\z/)
const char* end_of_file(const char* src);
// const char* start_of_string(const char* src);
// Type definition for prelexer functions
typedef const char* (*prelexer)(const char*);
//####################################
// BASIC "REGEX" CONSTRUCTORS
//####################################
// Match a single character literal.
// Regex equivalent: /(?:x)/
template <char chr>
const char* exactly(const char* src) {
return *src == chr ? src + 1 : 0;
}
// Match the full string literal.
// Regex equivalent: /(?:literal)/
template <const char* str>
const char* exactly(const char* src) {
if (str == NULL) return 0;
const char* pre = str;
if (src == NULL) return 0;
// there is a small chance that the search string
// is longer than the rest of the string to look at
while (*pre && *src == *pre) {
++src, ++pre;
}
// did the matcher finish?
return *pre == 0 ? src : 0;
}
// Match a single character literal.
// Regex equivalent: /(?:x)/i
// only define lower case alpha chars
template <char chr>
const char* insensitive(const char* src) {
return *src == chr || *src+32 == chr ? src + 1 : 0;
}
// Match the full string literal.
// Regex equivalent: /(?:literal)/i
// only define lower case alpha chars
template <const char* str>
const char* insensitive(const char* src) {
if (str == NULL) return 0;
const char* pre = str;
if (src == NULL) return 0;
// there is a small chance that the search string
// is longer than the rest of the string to look at
while (*pre && (*src == *pre || *src+32 == *pre)) {
++src, ++pre;
}
// did the matcher finish?
return *pre == 0 ? src : 0;
}
// Match for members of char class.
// Regex equivalent: /[axy]/
template <const char* char_class>
const char* class_char(const char* src) {
const char* cc = char_class;
while (*cc && *src != *cc) ++cc;
return *cc ? src + 1 : 0;
}
// Match for members of char class.
// Regex equivalent: /[axy]+/
template <const char* char_class>
const char* class_chars(const char* src) {
const char* p = src;
while (class_char<char_class>(p)) ++p;
return p == src ? 0 : p;
}
// Match for members of char class.
// Regex equivalent: /[^axy]/
template <const char* neg_char_class>
const char* neg_class_char(const char* src) {
if (*src == 0) return 0;
const char* cc = neg_char_class;
while (*cc && *src != *cc) ++cc;
return *cc ? 0 : src + 1;
}
// Match for members of char class.
// Regex equivalent: /[^axy]+/
template <const char* neg_char_class>
const char* neg_class_chars(const char* src) {
const char* p = src;
while (neg_class_char<neg_char_class>(p)) ++p;
return p == src ? 0 : p;
}
// Match all except the supplied one.
// Regex equivalent: /[^x]/
template <const char chr>
const char* any_char_but(const char* src) {
return (*src && *src != chr) ? src + 1 : 0;
}
// Succeeds if the matcher fails.
// Aka. zero-width negative lookahead.
// Regex equivalent: /(?!literal)/
template <prelexer mx>
const char* negate(const char* src) {
return mx(src) ? 0 : src;
}
// Succeeds if the matcher succeeds.
// Aka. zero-width positive lookahead.
// Regex equivalent: /(?=literal)/
// just hangs around until we need it
template <prelexer mx>
const char* lookahead(const char* src) {
return mx(src) ? src : 0;
}
// Tries supplied matchers in order.
// Succeeds if one of them succeeds.
// Regex equivalent: /(?:FOO|BAR)/
template <const prelexer mx>
const char* alternatives(const char* src) {
const char* rslt;
if ((rslt = mx(src))) return rslt;
return 0;
}
template <const prelexer mx1, const prelexer mx2, const prelexer... mxs>
const char* alternatives(const char* src) {
const char* rslt;
if ((rslt = mx1(src))) return rslt;
return alternatives<mx2, mxs...>(src);
}
// Tries supplied matchers in order.
// Succeeds if all of them succeeds.
// Regex equivalent: /(?:FOO)(?:BAR)/
template <const prelexer mx1>
const char* sequence(const char* src) {
const char* rslt = src;
if (!(rslt = mx1(rslt))) return 0;
return rslt;
}
template <const prelexer mx1, const prelexer mx2, const prelexer... mxs>
const char* sequence(const char* src) {
const char* rslt = src;
if (!(rslt = mx1(rslt))) return 0;
return sequence<mx2, mxs...>(rslt);
}
// Match a pattern or not. Always succeeds.
// Regex equivalent: /(?:literal)?/
template <prelexer mx>
const char* optional(const char* src) {
const char* p = mx(src);
return p ? p : src;
}
// Match zero or more of the patterns.
// Regex equivalent: /(?:literal)*/
template <prelexer mx>
const char* zero_plus(const char* src) {
const char* p = mx(src);
while (p) src = p, p = mx(src);
return src;
}
// Match one or more of the patterns.
// Regex equivalent: /(?:literal)+/
template <prelexer mx>
const char* one_plus(const char* src) {
const char* p = mx(src);
if (!p) return 0;
while (p) src = p, p = mx(src);
return src;
}
// Match mx non-greedy until delimiter.
// Other prelexers are greedy by default.
// Regex equivalent: /(?:$mx)*?(?=$delim)\b/
template <prelexer mx, prelexer delim>
const char* non_greedy(const char* src) {
while (!delim(src)) {
const char* p = mx(src);
if (p == src) return 0;
if (p == 0) return 0;
src = p;
}
return src;
}
//####################################
// ADVANCED "REGEX" CONSTRUCTORS
//####################################
// Match with word boundary rule.
// Regex equivalent: /(?:$mx)\b/i
template <const char* str>
const char* keyword(const char* src) {
return sequence <
insensitive < str >,
word_boundary
>(src);
}
// Match with word boundary rule.
// Regex equivalent: /(?:$mx)\b/
template <const char* str>
const char* word(const char* src) {
return sequence <
exactly < str >,
word_boundary
>(src);
}
template <char chr>
const char* loosely(const char* src) {
return sequence <
optional_spaces,
exactly < chr >
>(src);
}
template <const char* str>
const char* loosely(const char* src) {
return sequence <
optional_spaces,
exactly < str >
>(src);
}
}
}
#endif
|