Skip to content

Commit

Permalink
Merge pull request sass#1464 from mgreter/feature/unicode-sequence-pa…
Browse files Browse the repository at this point in the history
…rser

Add unicode sequence parsing
  • Loading branch information
xzyfer committed Aug 24, 2015
2 parents d67f8da + 4ccdb25 commit 2be979c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "sass2scss.h"
#include "prelexer.hpp"
#include "emitter.hpp"
#include "debugger.hpp"

namespace Sass {
using namespace Constants;
Expand Down
58 changes: 57 additions & 1 deletion src/prelexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,23 @@ namespace Sass {
{
return sequence<
exactly<'\\'>,
any_char
alternatives <
minmax_range<
3, 3, xdigit
>,
any_char
>,
optional <
exactly <' '>
>
>(src);
}

// Match identifier start
const char* identifier_alpha(const char* src)
{
return alternatives<
unicode_seq,
alpha,
unicode,
exactly<'-'>,
Expand All @@ -90,6 +99,7 @@ namespace Sass {
const char* identifier_alnum(const char* src)
{
return alternatives<
unicode_seq,
alnum,
unicode,
exactly<'-'>,
Expand Down Expand Up @@ -173,6 +183,7 @@ namespace Sass {
re_linebreak
>,
escape_seq,
unicode_seq,
// skip interpolants
interpolant,
// skip non delimiters
Expand All @@ -196,6 +207,7 @@ namespace Sass {
re_linebreak
>,
escape_seq,
unicode_seq,
// skip interpolants
interpolant,
// skip non delimiters
Expand Down Expand Up @@ -880,6 +892,20 @@ namespace Sass {
return (p == 0) ? t.end : 0;
}

const char* unicode_seq(const char* src) {
return sequence <
alternatives <
exactly< 'U' >,
exactly< 'u' >
>,
exactly< '+' >,
padded_token <
6, xdigit,
exactly < '?' >
>
>(src);
}

const char* static_component(const char* src) {
return alternatives< identifier,
static_string,
Expand Down Expand Up @@ -974,5 +1000,35 @@ namespace Sass {
return sequence< number, optional_spaces, exactly<'/'>, optional_spaces, number >(src);
}

template <size_t size, prelexer mx, prelexer pad>
const char* padded_token(const char* src)
{
size_t got = 0;
const char* pos = src;
while (got < size) {
if (!mx(pos)) break;
++ pos; ++ got;
}
while (got < size) {
if (!pad(pos)) break;
++ pos; ++ got;
}
return got ? pos : 0;
}

template <size_t min, size_t max, prelexer mx>
const char* minmax_range(const char* src)
{
size_t got = 0;
const char* pos = src;
while (got < max) {
if (!mx(pos)) break;
++ pos; ++ got;
}
if (got < min) return 0;
if (got > min) return 0;
return pos;
}

}
}
8 changes: 8 additions & 0 deletions src/prelexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ namespace Sass {
const char* kwd_charset_directive(const char* src);
const char* kwd_extend(const char* src);

const char* unicode_seq(const char* src);

const char* kwd_if_directive(const char* src);
const char* kwd_else_directive(const char* src);
const char* elseif_directive(const char* src);
Expand Down Expand Up @@ -388,6 +390,12 @@ namespace Sass {
return counter;
}

template <size_t size, prelexer mx, prelexer pad>
const char* padded_token(const char* src);

template <size_t min, size_t max, prelexer mx>
const char* minmax_range(const char* src);

}
}

Expand Down
2 changes: 2 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ namespace Sass {
// ToDo: Maybe we could do this without creating a substring
uint32_t cp = strtol(s.substr (i + 1, len - 1).c_str(), nullptr, 16);

if (s[i + len] == ' ') ++ len;

// assert invalid code points
if (cp == 0) cp = 0xFFFD;
// replace bell character
Expand Down

0 comments on commit 2be979c

Please sign in to comment.