Modify perl colorizer for consistency with other languages (#30)

This commit is contained in:
Charlie Gordon 2024-05-20 11:03:29 +02:00 • committed by GitHub
commit e2116cbdae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -144,32 +144,32 @@ static int perl_string(const char32_t *str, char32_t delim, int j, int n) {
static void perl_colorize_line(QEColorizeContext *cp,
char32_t *str, int n, ModeDef *syn)
{
int i = 0, j = i, s1, s2, delim = 0;
int i = 0, start = i, j, s1, s2, delim = 0;
char32_t c, c1, c2;
int colstate = cp->colorize_state;
if (colstate & (IN_PERL_STRING1 | IN_PERL_STRING2)) {
delim = (colstate & IN_PERL_STRING1) ? '\'' : '\"';
i = perl_string(str, delim, j, n);
i = perl_string(str, delim, start, n);
if (i < n) {
i++;
colstate &= ~(IN_PERL_STRING1 | IN_PERL_STRING2);
}
SET_COLOR(str, j, i, PERL_STYLE_STRING);
SET_COLOR(str, start, i, PERL_STYLE_STRING);
} else
if (colstate & IN_PERL_FORMAT) {
i = n;
if (n == 1 && str[0] == '.')
colstate &= ~IN_PERL_FORMAT;
SET_COLOR(str, j, i, PERL_STYLE_STRING);
SET_COLOR(str, start, i, PERL_STYLE_STRING);
}
if (colstate & IN_PERL_HEREDOC) {
i = n;
if (n == perl_eos_len && !umemcmp(perl_eos, str, n)) {
colstate &= ~IN_PERL_HEREDOC;
SET_COLOR(str, j, i, PERL_STYLE_KEYWORD);
SET_COLOR(str, start, i, PERL_STYLE_KEYWORD);
} else {
SET_COLOR(str, j, i, PERL_STYLE_STRING);
SET_COLOR(str, start, i, PERL_STYLE_STRING);
}
}
if (str[i] == '=' && qe_isalpha(str[i + 1])) {
@ -181,28 +181,29 @@ static void perl_colorize_line(QEColorizeContext *cp,
}
if (str[i] == '=' && qe_isalpha(str[i + 1])) {
i = n;
SET_COLOR(str, j, i, PERL_STYLE_KEYWORD);
SET_COLOR(str, start, i, PERL_STYLE_KEYWORD);
} else {
i = n;
SET_COLOR(str, j, i, PERL_STYLE_COMMENT);
SET_COLOR(str, start, i, PERL_STYLE_COMMENT);
}
}
while (i < n) {
j = i + 1;
c1 = str[j];
switch (c = str[i]) {
start = i;
c = str[i++];
c1 = str[i];
switch (c) {
case '$':
if (c1 == '^' && qe_isalpha(str[i + 2])) {
j = i + 3;
if (c1 == '^' && qe_isalpha(str[i + 1])) {
i += 2;
goto keyword;
}
if (c1 == '#' && qe_isalpha_(str[i + 2]))
j++;
if (c1 == '#' && qe_isalpha_(str[i + 1]))
i += 1;
else
if (memchr("|%=-~^123456789&`'+_./\\,\"#$?*0[];!@", c1, 35)) {
if (qe_findchar("|%=-~^123456789&`'+_./\\,\"#$?*0[];!@", c1)) {
/* Special variable */
j = i + 2;
i += 1;
goto keyword;
}
fallthrough;
@ -210,33 +211,33 @@ static void perl_colorize_line(QEColorizeContext *cp,
case '@': /* arrays */
case '%': /* associative arrays */
case '&':
if (j >= n)
if (i >= n)
break;
s1 = perl_var(str, j, n);
if (s1 > j) {
SET_COLOR(str, i, s1, PERL_STYLE_VAR);
s1 = perl_var(str, i, n);
if (s1 > i) {
i = s1;
SET_COLOR(str, start, i, PERL_STYLE_VAR);
continue;
}
break;
case '-':
if (c1 == '-') {
i += 2;
i += 1;
continue;
}
if (qe_isalpha(c1) && !qe_isalnum(str[i + 2])) {
j = i + 2;
if (qe_isalpha(c1) && !qe_isalnum(str[i + 1])) {
i += 1;
goto keyword;
}
break;
case '#':
SET_COLOR(str, i, n, PERL_STYLE_COMMENT);
i = n;
SET_COLOR(str, start, i, PERL_STYLE_COMMENT);
continue;
case '<':
if (c1 == '<') {
/* Should check for unary context */
s1 = i + 2;
s1 = i + 1;
while (qe_isblank(str[s1]))
s1++;
c2 = str[s1];
@ -251,7 +252,7 @@ static void perl_colorize_line(QEColorizeContext *cp,
perl_eos[perl_eos_len] = '\0';
colstate |= IN_PERL_HEREDOC;
}
i += 2;
i += 1;
continue;
}
delim = '>';
@ -260,15 +261,16 @@ static void perl_colorize_line(QEColorizeContext *cp,
case '?':
/* Should check for unary context */
/* parse regex */
s1 = perl_string(str, c, j, n);
s1 = perl_string(str, c, i, n);
if (s1 >= n)
break;
SET_COLOR1(str, i, PERL_STYLE_DELIM);
SET_COLOR(str, i + 1, s1, PERL_STYLE_REGEX);
SET_COLOR1(str, start, PERL_STYLE_DELIM);
i = s1;
SET_COLOR(str, start + 1, i, PERL_STYLE_REGEX);
start = i;
while (++i < n && qe_isalpha(str[i]))
continue;
SET_COLOR(str, s1, i, PERL_STYLE_DELIM);
SET_COLOR(str, start, i, PERL_STYLE_DELIM);
continue;
case '\'':
case '`':
@ -276,18 +278,18 @@ static void perl_colorize_line(QEColorizeContext *cp,
delim = c;
string:
/* parse string const */
s1 = perl_string(str, delim, j, n);
s1 = perl_string(str, delim, i, n);
if (s1 >= n) {
if (c == '\'') {
SET_COLOR(str, i, n, PERL_STYLE_STRING);
i = n;
colstate |= IN_PERL_STRING1;
i = n;
SET_COLOR(str, start, i, PERL_STYLE_STRING);
continue;
}
if (c == '\"') {
SET_COLOR(str, i, n, PERL_STYLE_STRING);
i = n;
colstate |= IN_PERL_STRING2;
i = n;
SET_COLOR(str, start, i, PERL_STYLE_STRING);
continue;
}
/* ` string spanning more than one line treated as
@ -296,8 +298,8 @@ static void perl_colorize_line(QEColorizeContext *cp,
break;
}
s1++;
SET_COLOR(str, i, s1, PERL_STYLE_STRING);
i = s1;
SET_COLOR(str, start, i, PERL_STYLE_STRING);
continue;
case '.':
if (qe_isdigit(c1))
@ -307,45 +309,44 @@ static void perl_colorize_line(QEColorizeContext *cp,
default:
if (qe_isdigit(c)) {
number:
j = perl_number(str, i, n);
SET_COLOR(str, i, j, PERL_STYLE_NUMBER);
i = j;
i = perl_number(str, start, n);
SET_COLOR(str, start, i, PERL_STYLE_NUMBER);
continue;
}
if (!qe_isalpha_(c))
break;
j = perl_var(str, i, n);
if (j == i)
j = perl_var(str, start, n);
if (j == start)
break;
if (j >= n)
goto keyword;
/* Should check for context */
if ((j == i + 1 && (c == 'm' || c == 'q'))
|| (j == i + 2 && c == 'q' && (c1 == 'q' || c1 == 'x'))) {
if ((j == start + 1 && (c == 'm' || c == 'q'))
|| (j == start + 2 && c == 'q' && (c1 == 'q' || c1 == 'x'))) {
s1 = perl_string(str, str[j], j + 1, n);
if (s1 >= n)
goto keyword;
SET_COLOR(str, i, j + 1, PERL_STYLE_DELIM);
SET_COLOR(str, j + 1, s1, PERL_STYLE_REGEX);
SET_COLOR(str, start, j + 1, PERL_STYLE_DELIM);
i = s1;
SET_COLOR(str, j + 1, i, PERL_STYLE_REGEX);
while (++i < n && qe_isalpha(str[i]))
continue;
SET_COLOR(str, s1, i, PERL_STYLE_DELIM);
continue;
}
/* Should check for context */
if ((j == i + 1 && (c == 's' /* || c == 'y' */))
|| (j == i + 2 && c == 't' && c1 == 'r')) {
if ((j == start + 1 && (c == 's' /* || c == 'y' */))
|| (j == start + 2 && c == 't' && c1 == 'r')) {
s1 = perl_string(str, str[j], j + 1, n);
if (s1 >= n)
goto keyword;
s2 = perl_string(str, str[j], s1 + 1, n);
if (s2 >= n)
goto keyword;
SET_COLOR(str, i, j + 1, PERL_STYLE_DELIM);
SET_COLOR(str, start, j + 1, PERL_STYLE_DELIM);
SET_COLOR(str, j + 1, s1, PERL_STYLE_REGEX);
SET_COLOR1(str, s1, PERL_STYLE_DELIM);
SET_COLOR(str, s1 + 1, s2, PERL_STYLE_REGEX);
@ -356,22 +357,19 @@ static void perl_colorize_line(QEColorizeContext *cp,
continue;
}
keyword:
if (j - i == 6 && ustristart(str + i, "format", NULL)) {
for (s1 = 0; s1 < i; s1++) {
if (i - start == 6 && ustristart(str + start, "format", NULL)) {
for (s1 = 0; s1 < start; s1++) {
if (!qe_isblank(str[s1]))
break;
}
if (s1 == i) {
if (s1 == start) {
/* keyword is first on line */
colstate |= IN_PERL_FORMAT;
}
}
SET_COLOR(str, i, j, PERL_STYLE_KEYWORD);
i = j;
SET_COLOR(str, start, i, PERL_STYLE_KEYWORD);
continue;
}
i++;
continue;
}
cp->colorize_state = colstate;
}