viml/parser/expressions: Add support for string parsing

This commit is contained in:
ZyX 2017-10-08 21:52:38 +03:00
parent c484613ce0
commit af38cea133
8 changed files with 1938 additions and 13 deletions

View File

@ -73,6 +73,8 @@ typedef struct {
extern const uint8_t utf8len_tab_zero[256];
extern const uint8_t utf8len_tab[256];
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "mbyte.h.generated.h"
#endif

View File

@ -915,8 +915,6 @@ static inline void viml_pexpr_debug_print_token(
//
// NVimIdentifierKey -> Identifier
//
// NVimFigureBrace -> NVimInternalError
//
// NVimUnaryPlus -> NVimUnaryOperator
// NVimBinaryPlus -> NVimBinaryOperator
// NVimConcat -> NVimBinaryOperator
@ -929,6 +927,18 @@ static inline void viml_pexpr_debug_print_token(
// NVimNestingParenthesis -> NVimParenthesis
// NVimCallingParenthesis -> NVimParenthesis
//
// NVimString -> String
// NVimStringSpecial -> SpecialChar
// NVimSingleQuote -> NVimString
// NVimSingleQuotedBody -> NVimString
// NVimSingleQuotedQuote -> NVimStringSpecial
// NVimDoubleQuote -> NVimString
// NVimDoubleQuotedBody -> NVimString
// NVimDoubleQuotedEscape -> NVimStringSpecial
// NVimDoubleQuotedUnknownEscape -> NVimInvalid
//
// " Note: NVimDoubleQuotedUnknownEscape is not actually invalid
//
// NVimInvalidComma -> NVimInvalidDelimiter
// NVimInvalidSpacing -> NVimInvalid
// NVimInvalidTernary -> NVimInvalidOperator
@ -952,6 +962,19 @@ static inline void viml_pexpr_debug_print_token(
// NVimInvalidList -> NVimInvalidDelimiter
// NVimInvalidSubscript -> NVimInvalidDelimiter
// NVimInvalidSubscriptColon -> NVimInvalidSubscript
// NVimInvalidString -> NVimInvalidValue
// NVimInvalidStringSpecial -> NVimInvalidString
// NVimInvalidSingleQuote -> NVimInvalidString
// NVimInvalidSingleQuotedBody -> NVimInvalidString
// NVimInvalidSingleQuotedQuote -> NVimInvalidStringSpecial
// NVimInvalidDoubleQuote -> NVimInvalidString
// NVimInvalidDoubleQuotedBody -> NVimInvalidString
// NVimInvalidDoubleQuotedEscape -> NVimInvalidStringSpecial
// NVimInvalidDoubleQuotedUnknownEscape -> NVimInvalid
//
// NVimFigureBrace -> NVimInternalError
// NVimInvalidSingleQuotedUnknownEscape -> NVimInternalError
// NVimSingleQuotedUnknownEscape -> NVimInternalError
/// Allocate a new node and set some of the values
///
@ -1402,6 +1425,318 @@ static inline void east_set_error(const ParserState *const pstate,
} \
} while (0)
/// Structure used to define “string shifts” necessary to map string
/// highlighting to actual strings.
typedef struct {
size_t start; ///< Where special character starts in original string.
size_t orig_len; ///< Length of orininal string (e.g. 4 for "\x80").
size_t act_len; ///< Length of resulting character(s) (e.g. 1 for "\x80").
bool escape_not_known; ///< True if escape sequence in original is not known.
} StringShift;
/// Parse and highlight single- or double-quoted string
///
/// Function is supposed to detect and highlight regular expressions (but does
/// not do now).
///
/// @param[out] pstate Parser state which also contains a place where
/// highlighting is saved.
/// @param[out] node Node where string parsing results are saved.
/// @param[in] token Token to highlight.
/// @param[in] ast_stack Parser AST stack, used to detect whether current
/// string is a regex.
/// @param[in] is_invalid Whether currently processed token is not valid.
static void parse_quoted_string(ParserState *const pstate,
ExprASTNode *const node,
const LexExprToken token,
const ExprASTStack ast_stack,
const bool is_invalid)
FUNC_ATTR_NONNULL_ALL
{
const ParserLine pline = pstate->reader.lines.items[token.start.line];
const char *const s = pline.data + token.start.col;
const char *const e = s + token.len - token.data.str.closed;
const char *p = s + 1;
const bool is_double = (token.type == kExprLexDoubleQuotedString);
size_t size = token.len - token.data.str.closed - 1;
kvec_withinit_t(StringShift, 16) shifts;
kvi_init(shifts);
if (!is_double) {
viml_parser_highlight(pstate, token.start, 1, HL(SingleQuotedString));
while (p < e) {
const char *const chunk_e = memchr(p, '\'', (size_t)(e - p));
if (chunk_e == NULL) {
break;
}
size--;
p = chunk_e + 2;
if (pstate->colors) {
kvi_push(shifts, ((StringShift) {
.start = token.start.col + (size_t)(chunk_e - s),
.orig_len = 2,
.act_len = 1,
.escape_not_known = false,
}));
}
}
node->data.str.size = size;
if (size == 0) {
node->data.str.value = NULL;
} else {
char *v_p;
v_p = node->data.str.value = xmalloc(size);
p = s + 1;
while (p < e) {
const char *const chunk_e = memchr(p, '\'', (size_t)(e - p));
if (chunk_e == NULL) {
memcpy(v_p, p, (size_t)(e - p));
break;
}
memcpy(v_p, p, (size_t)(chunk_e - p));
v_p += (size_t)(chunk_e - p) + 1;
v_p[-1] = '\'';
p = chunk_e + 2;
}
}
} else {
viml_parser_highlight(pstate, token.start, 1, HL(DoubleQuotedString));
for (p = s + 1; p < e; p++) {
if (*p == '\\' && p + 1 < e) {
p++;
if (p + 1 == e) {
size--;
break;
}
switch (*p) {
// A "\<x>" form occupies at least 4 characters, and produces up to
// 6 characters: reserve space for 2 extra, but do not compute actual
// length just now, it would be costy.
case '<': {
size += 2;
break;
}
// Hexadecimal, always single byte, but at least three bytes each.
case 'x': case 'X': {
size--;
if (ascii_isxdigit(p[1])) {
size--;
if (p + 2 < e && ascii_isxdigit(p[2])) {
size--;
}
}
break;
}
// Unicode
//
// \uF takes 1 byte which is 2 bytes less then escape sequence.
// \uFF: 2 bytes, 2 bytes less.
// \uFFF: 3 bytes, 2 bytes less.
// \uFFFF: 3 bytes, 3 bytes less.
// \UFFFFF: 4 bytes, 3 bytes less.
// \UFFFFFF: 5 bytes, 3 bytes less.
// \UFFFFFFF: 6 bytes, 3 bytes less.
// \U7FFFFFFF: 6 bytes, 4 bytes less.
case 'u': case 'U': {
const char *const esc_start = p;
size_t n = (*p == 'u' ? 4 : 8);
int nr = 0;
p++;
while (n-- && ascii_isxdigit(p[1])) {
p++;
nr = (nr << 4) + hex2nr(*p);
}
// Escape length: (esc_start - 1) points to "\\", esc_start to "u"
// or "U", p to the byte after last byte. So escape sequence
// occupies p - (esc_start - 1), but it stands for a utf_char2len
// bytes.
size -= (size_t)((p - (esc_start - 1)) - utf_char2len(nr));
p--;
break;
}
// Octal, always single byte, but at least two bytes each.
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
case '7': {
size--;
p++;
if (*p >= '0' && *p <= '7') {
size--;
p++;
if (*p >= '0' && *p <= '7') {
size--;
p++;
}
}
break;
}
default: {
size--;
break;
}
}
}
}
if (size == 0) {
node->data.str.value = NULL;
node->data.str.size = 0;
} else {
char *v_p;
v_p = node->data.str.value = xmalloc(size);
p = s + 1;
while (p < e) {
const char *const chunk_e = memchr(p, '\\', (size_t)(e - p));
if (chunk_e == NULL) {
memcpy(v_p, p, (size_t)(e - p));
v_p += e - p;
break;
}
memcpy(v_p, p, (size_t)(chunk_e - p));
v_p += (size_t)(chunk_e - p);
p = chunk_e + 1;
if (p == e) {
*v_p++ = '\\';
break;
}
bool is_unknown = false;
const char *const v_p_start = v_p;
switch (*p) {
#define SINGLE_CHAR_ESC(ch, real_ch) \
case ch: { \
*v_p++ = real_ch; \
p++; \
break; \
}
SINGLE_CHAR_ESC('b', BS)
SINGLE_CHAR_ESC('e', ESC)
SINGLE_CHAR_ESC('f', FF)
SINGLE_CHAR_ESC('n', NL)
SINGLE_CHAR_ESC('r', CAR)
SINGLE_CHAR_ESC('t', TAB)
SINGLE_CHAR_ESC('"', '"')
SINGLE_CHAR_ESC('\\', '\\')
#undef SINGLE_CHAR_ESC
// Hexadecimal or unicode.
case 'X': case 'x': case 'u': case 'U': {
if (ascii_isxdigit(p[1])) {
size_t n;
int nr;
bool is_hex = (*p == 'x' || *p == 'X');
if (is_hex) {
n = 2;
} else if (*p == 'u') {
n = 4;
} else {
n = 8;
}
nr = 0;
while (n-- && ascii_isxdigit(p[1])) {
p++;
nr = (nr << 4) + hex2nr(*p);
}
p++;
if (is_hex) {
*v_p++ = (char)nr;
} else {
v_p += utf_char2bytes(nr, (char_u *)v_p);
}
} else {
is_unknown = true;
*v_p++ = *p;
p++;
}
break;
}
// Octal: "\1", "\12", "\123".
case '0': case '1': case '2': case '3': case '4': case '5': case '6':
case '7': {
uint8_t ch = (uint8_t)(*p++ - '0');
if (*p >= '0' && *p <= '7') {
ch = (uint8_t)((ch << 3) + *p++ - '0');
if (*p >= '0' && *p <= '7') {
ch = (uint8_t)((ch << 3) + *p++ - '0');
}
}
*v_p++ = (char)ch;
break;
}
// Special key, e.g.: "\<C-W>"
case '<': {
const size_t special_len = (
trans_special((const char_u **)&p, (size_t)(e - p),
(char_u *)v_p, true, true));
if (special_len != 0) {
v_p += special_len;
} else {
is_unknown = true;
mb_copy_char((const char_u **)&p, (char_u **)&v_p);
}
break;
}
default: {
is_unknown = true;
mb_copy_char((const char_u **)&p, (char_u **)&v_p);
break;
}
}
if (pstate->colors) {
kvi_push(shifts, ((StringShift) {
.start = token.start.col + (size_t)(chunk_e - s),
.orig_len = (size_t)(p - chunk_e),
.act_len = (size_t)(v_p - (char *)v_p_start),
.escape_not_known = is_unknown,
}));
}
}
node->data.str.size = (size_t)(v_p - node->data.str.value);
}
}
if (pstate->colors) {
// TODO(ZyX-I): use ast_stack to determine and highlight regular expressions
// TODO(ZyX-I): use ast_stack to determine and highlight printf format str
// TODO(ZyX-I): use ast_stack to determine and highlight expression strings
size_t next_col = 1;
const char *const body_str = (is_double
? HL(DoubleQuotedBody)
: HL(SingleQuotedBody));
const char *const esc_str = (is_double
? HL(DoubleQuotedEscape)
: HL(SingleQuotedQuote));
const char *const ukn_esc_str = (is_double
? HL(DoubleQuotedUnknownEscape)
: HL(SingleQuotedUnknownEscape));
for (size_t i = 0; i < kv_size(shifts); i++) {
const StringShift cur_shift = kv_A(shifts, i);
if (cur_shift.start > next_col) {
viml_parser_highlight(pstate, shifted_pos(token.start, next_col),
cur_shift.start - next_col,
body_str);
}
viml_parser_highlight(pstate, shifted_pos(token.start, cur_shift.start),
cur_shift.orig_len,
(cur_shift.escape_not_known
? ukn_esc_str
: esc_str));
next_col = cur_shift.start + cur_shift.orig_len;
}
if (next_col < token.len - token.data.str.closed) {
viml_parser_highlight(pstate, shifted_pos(token.start, next_col),
token.len - token.data.str.closed - next_col,
body_str);
}
}
if (token.data.str.closed) {
if (is_double) {
viml_parser_highlight(pstate, shifted_pos(token.start, token.len - 1),
1, HL(DoubleQuotedString));
} else {
viml_parser_highlight(pstate, shifted_pos(token.start, token.len - 1),
1, HL(SingleQuotedString));
}
}
kvi_destroy(shifts);
}
/// Parse one VimL expression
///
/// @param pstate Parser state.
@ -1714,12 +2049,7 @@ viml_pexpr_parse_invalid_comma:
} else if (eastnode_lvl >= kEOpLvlComma) {
can_be_ternary = false;
} else {
viml_pexpr_parse_invalid_colon:
ERROR_FROM_TOKEN_AND_MSG(
cur_token,
_("E15: Colon outside of dictionary or ternary operator: "
"%.*s"));
break;
goto viml_pexpr_parse_invalid_colon;
}
if (i == kv_size(ast_stack) - 1) {
goto viml_pexpr_parse_invalid_colon;
@ -1741,6 +2071,12 @@ viml_pexpr_parse_invalid_colon:
ADD_OP_NODE(cur_node);
HL_CUR_TOKEN(SubscriptColon);
} else {
goto viml_pexpr_parse_valid_colon;
viml_pexpr_parse_invalid_colon:
ERROR_FROM_TOKEN_AND_MSG(
cur_token,
_("E15: Colon outside of dictionary or ternary operator: %.*s"));
viml_pexpr_parse_valid_colon:
ADD_VALUE_IF_MISSING(_(EXP_VAL_COLON));
NEW_NODE_WITH_CUR_POS(cur_node, kExprNodeColon);
if (is_ternary) {
@ -2201,6 +2537,30 @@ viml_pexpr_parse_no_paren_closing_error: {}
kvi_push(ast_stack, &ter_val_node->children);
break;
}
case kExprLexDoubleQuotedString:
case kExprLexSingleQuotedString: {
const bool is_double = (tok_type == kExprLexDoubleQuotedString);
if (!cur_token.data.str.closed) {
// It is weird, but Vim has two identical errors messages with
// different error numbers: "E114: Missing quote" and
// "E115: Missing quote".
ERROR_FROM_TOKEN_AND_MSG(
cur_token, (is_double
? _("E114: Missing double quote: %.*s")
: _("E115: Missing single quote: %.*s")));
}
if (want_node == kENodeOperator) {
OP_MISSING;
}
NEW_NODE_WITH_CUR_POS(
cur_node, (is_double
? kExprNodeDoubleQuotedString
: kExprNodeSingleQuotedString));
*top_node_p = cur_node;
parse_quoted_string(pstate, cur_node, cur_token, ast_stack, is_invalid);
want_node = kENodeOperator;
break;
}
}
viml_pexpr_parse_cycle_end:
prev_token = cur_token;

View File

@ -195,6 +195,8 @@ typedef enum {
kExprNodeConcatOrSubscript = 'S',
kExprNodeInteger = '0', ///< Integral number.
kExprNodeFloat = '1', ///< Floating-point number.
kExprNodeSingleQuotedString = '\'',
kExprNodeDoubleQuotedString = '"',
} ExprASTNodeType;
typedef struct expr_ast_node ExprASTNode;
@ -249,6 +251,11 @@ struct expr_ast_node {
struct {
float_T value;
} flt; ///< For kExprNodeFloat.
struct {
char *value;
size_t size;
} str; ///< For kExprNodeSingleQuotedString and
///< kExprNodeDoubleQuotedString.
} data;
};

View File

@ -330,9 +330,10 @@ format_luav = function(v, indent)
end
local ret = ''
if type(v) == 'string' then
ret = '\'' .. tostring(v):gsub('[\'\\]', '\\%0'):gsub('[%z\1-\31]', function(match)
return SUBTBL[match:byte()]
end) .. '\''
ret = tostring(v):gsub('[\'\\]', '\\%0'):gsub('[%z\1-\31]', function(match)
return SUBTBL[match:byte() + 1]
end)
ret = '\'' .. ret .. '\''
elseif type(v) == 'table' then
local processed_keys = {}
ret = '{' .. linesep

View File

@ -0,0 +1,540 @@
#include <stdbool.h>
#include "nvim/types.h"
#include "nvim/keymap.h"
#include "nvim/eval/typval.h"
#define MOD_KEYS_ENTRY_SIZE 5
static char_u modifier_keys_table[] =
{
MOD_MASK_SHIFT, '&', '9', '@', '1',
MOD_MASK_SHIFT, '&', '0', '@', '2',
MOD_MASK_SHIFT, '*', '1', '@', '4',
MOD_MASK_SHIFT, '*', '2', '@', '5',
MOD_MASK_SHIFT, '*', '3', '@', '6',
MOD_MASK_SHIFT, '*', '4', 'k', 'D',
MOD_MASK_SHIFT, '*', '5', 'k', 'L',
MOD_MASK_SHIFT, '*', '7', '@', '7',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7',
MOD_MASK_SHIFT, '*', '9', '@', '9',
MOD_MASK_SHIFT, '*', '0', '@', '0',
MOD_MASK_SHIFT, '#', '1', '%', '1',
MOD_MASK_SHIFT, '#', '2', 'k', 'h',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h',
MOD_MASK_SHIFT, '#', '3', 'k', 'I',
MOD_MASK_SHIFT, '#', '4', 'k', 'l',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l',
MOD_MASK_SHIFT, '%', 'a', '%', '3',
MOD_MASK_SHIFT, '%', 'b', '%', '4',
MOD_MASK_SHIFT, '%', 'c', '%', '5',
MOD_MASK_SHIFT, '%', 'd', '%', '7',
MOD_MASK_SHIFT, '%', 'e', '%', '8',
MOD_MASK_SHIFT, '%', 'f', '%', '9',
MOD_MASK_SHIFT, '%', 'g', '%', '0',
MOD_MASK_SHIFT, '%', 'h', '&', '3',
MOD_MASK_SHIFT, '%', 'i', 'k', 'r',
MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r',
MOD_MASK_SHIFT, '%', 'j', '&', '5',
MOD_MASK_SHIFT, '!', '1', '&', '6',
MOD_MASK_SHIFT, '!', '2', '&', '7',
MOD_MASK_SHIFT, '!', '3', '&', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
NUL
};
int simplify_key(const int key, int *modifiers)
{
if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT)) {
// TAB is a special case.
if (key == TAB && (*modifiers & MOD_MASK_SHIFT)) {
*modifiers &= ~MOD_MASK_SHIFT;
return K_S_TAB;
}
const int key0 = KEY2TERMCAP0(key);
const int key1 = KEY2TERMCAP1(key);
for (int i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE) {
if (key0 == modifier_keys_table[i + 3]
&& key1 == modifier_keys_table[i + 4]
&& (*modifiers & modifier_keys_table[i])) {
*modifiers &= ~modifier_keys_table[i];
return TERMCAP2KEY(modifier_keys_table[i + 1],
modifier_keys_table[i + 2]);
}
}
}
return key;
}
int handle_x_keys(const int key)
FUNC_ATTR_CONST FUNC_ATTR_WARN_UNUSED_RESULT
{
switch (key) {
case K_XUP: return K_UP;
case K_XDOWN: return K_DOWN;
case K_XLEFT: return K_LEFT;
case K_XRIGHT: return K_RIGHT;
case K_XHOME: return K_HOME;
case K_ZHOME: return K_HOME;
case K_XEND: return K_END;
case K_ZEND: return K_END;
case K_XF1: return K_F1;
case K_XF2: return K_F2;
case K_XF3: return K_F3;
case K_XF4: return K_F4;
case K_S_XF1: return K_S_F1;
case K_S_XF2: return K_S_F2;
case K_S_XF3: return K_S_F3;
case K_S_XF4: return K_S_F4;
}
return key;
}
static const struct key_name_entry {
int key; ///< Special key code or ASCII value.
const char *name; ///< Name of the key
} key_names_table[] = {
{' ', "Space"},
{TAB, "Tab"},
{K_TAB, "Tab"},
{NL, "NL"},
{NL, "NewLine"}, // Alternative name
{NL, "LineFeed"}, // Alternative name
{NL, "LF"}, // Alternative name
{CAR, "CR"},
{CAR, "Return"}, // Alternative name
{CAR, "Enter"}, // Alternative name
{K_BS, "BS"},
{K_BS, "BackSpace"}, // Alternative name
{ESC, "Esc"},
{CSI, "CSI"},
{K_CSI, "xCSI"},
{'|', "Bar"},
{'\\', "Bslash"},
{K_DEL, "Del"},
{K_DEL, "Delete"}, // Alternative name
{K_KDEL, "kDel"},
{K_UP, "Up"},
{K_DOWN, "Down"},
{K_LEFT, "Left"},
{K_RIGHT, "Right"},
{K_XUP, "xUp"},
{K_XDOWN, "xDown"},
{K_XLEFT, "xLeft"},
{K_XRIGHT, "xRight"},
{K_F1, "F1"},
{K_F2, "F2"},
{K_F3, "F3"},
{K_F4, "F4"},
{K_F5, "F5"},
{K_F6, "F6"},
{K_F7, "F7"},
{K_F8, "F8"},
{K_F9, "F9"},
{K_F10, "F10"},
{K_F11, "F11"},
{K_F12, "F12"},
{K_F13, "F13"},
{K_F14, "F14"},
{K_F15, "F15"},
{K_F16, "F16"},
{K_F17, "F17"},
{K_F18, "F18"},
{K_F19, "F19"},
{K_F20, "F20"},
{K_F21, "F21"},
{K_F22, "F22"},
{K_F23, "F23"},
{K_F24, "F24"},
{K_F25, "F25"},
{K_F26, "F26"},
{K_F27, "F27"},
{K_F28, "F28"},
{K_F29, "F29"},
{K_F30, "F30"},
{K_F31, "F31"},
{K_F32, "F32"},
{K_F33, "F33"},
{K_F34, "F34"},
{K_F35, "F35"},
{K_F36, "F36"},
{K_F37, "F37"},
{K_XF1, "xF1"},
{K_XF2, "xF2"},
{K_XF3, "xF3"},
{K_XF4, "xF4"},
{K_HELP, "Help"},
{K_UNDO, "Undo"},
{K_INS, "Insert"},
{K_INS, "Ins"}, // Alternative name
{K_KINS, "kInsert"},
{K_HOME, "Home"},
{K_KHOME, "kHome"},
{K_XHOME, "xHome"},
{K_ZHOME, "zHome"},
{K_END, "End"},
{K_KEND, "kEnd"},
{K_XEND, "xEnd"},
{K_ZEND, "zEnd"},
{K_PAGEUP, "PageUp"},
{K_PAGEDOWN, "PageDown"},
{K_KPAGEUP, "kPageUp"},
{K_KPAGEDOWN, "kPageDown"},
{K_KPLUS, "kPlus"},
{K_KMINUS, "kMinus"},
{K_KDIVIDE, "kDivide"},
{K_KMULTIPLY, "kMultiply"},
{K_KENTER, "kEnter"},
{K_KPOINT, "kPoint"},
{K_K0, "k0"},
{K_K1, "k1"},
{K_K2, "k2"},
{K_K3, "k3"},
{K_K4, "k4"},
{K_K5, "k5"},
{K_K6, "k6"},
{K_K7, "k7"},
{K_K8, "k8"},
{K_K9, "k9"},
{'<', "lt"},
{K_MOUSE, "Mouse"},
{K_LEFTMOUSE, "LeftMouse"},
{K_LEFTMOUSE_NM, "LeftMouseNM"},
{K_LEFTDRAG, "LeftDrag"},
{K_LEFTRELEASE, "LeftRelease"},
{K_LEFTRELEASE_NM, "LeftReleaseNM"},
{K_MIDDLEMOUSE, "MiddleMouse"},
{K_MIDDLEDRAG, "MiddleDrag"},
{K_MIDDLERELEASE, "MiddleRelease"},
{K_RIGHTMOUSE, "RightMouse"},
{K_RIGHTDRAG, "RightDrag"},
{K_RIGHTRELEASE, "RightRelease"},
{K_MOUSEDOWN, "ScrollWheelUp"},
{K_MOUSEUP, "ScrollWheelDown"},
{K_MOUSELEFT, "ScrollWheelRight"},
{K_MOUSERIGHT, "ScrollWheelLeft"},
{K_MOUSEDOWN, "MouseDown"}, // OBSOLETE: Use ScrollWheelXXX instead
{K_MOUSEUP, "MouseUp"}, // Same
{K_X1MOUSE, "X1Mouse"},
{K_X1DRAG, "X1Drag"},
{K_X1RELEASE, "X1Release"},
{K_X2MOUSE, "X2Mouse"},
{K_X2DRAG, "X2Drag"},
{K_X2RELEASE, "X2Release"},
{K_DROP, "Drop"},
{K_ZERO, "Nul"},
{K_SNR, "SNR"},
{K_PLUG, "Plug"},
{K_PASTE, "Paste"},
{K_FOCUSGAINED, "FocusGained"},
{K_FOCUSLOST, "FocusLost"},
{0, NULL}
};
int get_special_key_code(const char_u *name)
{
for (int i = 0; key_names_table[i].name != NULL; i++) {
const char *const table_name = key_names_table[i].name;
int j;
for (j = 0; vim_isIDc(name[j]) && table_name[j] != NUL; j++) {
if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) {
break;
}
}
if (!vim_isIDc(name[j]) && table_name[j] == NUL) {
return key_names_table[i].key;
}
}
return 0;
}
static const struct modmasktable {
short mod_mask; ///< Bit-mask for particular key modifier.
short mod_flag; ///< Bit(s) for particular key modifier.
char_u name; ///< Single letter name of modifier.
} mod_mask_table[] = {
{MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
{MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
{MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
{MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
{MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
{MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
// 'A' must be the last one
{MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
{0, 0, NUL}
};
int name_to_mod_mask(int c)
{
c = TOUPPER_ASC(c);
for (size_t i = 0; mod_mask_table[i].mod_mask != 0; i++) {
if (c == mod_mask_table[i].name) {
return mod_mask_table[i].mod_flag;
}
}
return 0;
}
static int extract_modifiers(int key, int *modp)
{
int modifiers = *modp;
if (!(modifiers & MOD_MASK_CMD)) { // Command-key is special
if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key)) {
key = TOUPPER_ASC(key);
modifiers &= ~MOD_MASK_SHIFT;
}
}
if ((modifiers & MOD_MASK_CTRL)
&& ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))) {
key = Ctrl_chr(key);
modifiers &= ~MOD_MASK_CTRL;
if (key == 0) { // <C-@> is <Nul>
key = K_ZERO;
}
}
*modp = modifiers;
return key;
}
int find_special_key(const char_u **srcp, const size_t src_len, int *const modp,
const bool keycode, const bool keep_x_key,
const bool in_string)
{
const char_u *last_dash;
const char_u *end_of_name;
const char_u *src;
const char_u *bp;
const char_u *const end = *srcp + src_len - 1;
int modifiers;
int bit;
int key;
uvarnumber_T n;
int l;
if (src_len == 0) {
return 0;
}
src = *srcp;
if (src[0] != '<') {
return 0;
}
// Find end of modifier list
last_dash = src;
for (bp = src + 1; bp <= end && (*bp == '-' || vim_isIDc(*bp)); bp++) {
if (*bp == '-') {
last_dash = bp;
if (bp + 1 <= end) {
l = utfc_ptr2len_len(bp + 1, (int)(end - bp) + 1);
// Anything accepted, like <C-?>.
// <C-"> or <M-"> are not special in strings as " is
// the string delimiter. With a backslash it works: <M-\">
if (end - bp > l && !(in_string && bp[1] == '"') && bp[2] == '>') {
bp += l;
} else if (end - bp > 2 && in_string && bp[1] == '\\'
&& bp[2] == '"' && bp[3] == '>') {
bp += 2;
}
}
}
if (end - bp > 3 && bp[0] == 't' && bp[1] == '_') {
bp += 3; // skip t_xx, xx may be '-' or '>'
} else if (end - bp > 4 && STRNICMP(bp, "char-", 5) == 0) {
vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
bp += l + 5;
break;
}
}
if (bp <= end && *bp == '>') { // found matching '>'
end_of_name = bp + 1;
/* Which modifiers are given? */
modifiers = 0x0;
for (bp = src + 1; bp < last_dash; bp++) {
if (*bp != '-') {
bit = name_to_mod_mask(*bp);
if (bit == 0x0) {
break; // Illegal modifier name
}
modifiers |= bit;
}
}
// Legal modifier name.
if (bp >= last_dash) {
if (STRNICMP(last_dash + 1, "char-", 5) == 0
&& ascii_isdigit(last_dash[6])) {
// <Char-123> or <Char-033> or <Char-0x33>
vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
key = (int)n;
} else {
int off = 1;
// Modifier with single letter, or special key name.
if (in_string && last_dash[1] == '\\' && last_dash[2] == '"') {
off = 2;
}
l = mb_ptr2len(last_dash + 1);
if (modifiers != 0 && last_dash[l + 1] == '>') {
key = PTR2CHAR(last_dash + off);
} else {
key = get_special_key_code(last_dash + off);
if (!keep_x_key) {
key = handle_x_keys(key);
}
}
}
// get_special_key_code() may return NUL for invalid
// special key name.
if (key != NUL) {
// Only use a modifier when there is no special key code that
// includes the modifier.
key = simplify_key(key, &modifiers);
if (!keycode) {
// don't want keycode, use single byte code
if (key == K_BS) {
key = BS;
} else if (key == K_DEL || key == K_KDEL) {
key = DEL;
}
}
// Normal Key with modifier:
// Try to make a single byte code (except for Alt/Meta modifiers).
if (!IS_SPECIAL(key)) {
key = extract_modifiers(key, &modifiers);
}
*modp = modifiers;
*srcp = end_of_name;
return key;
}
}
}
return 0;
}
char_u *add_char2buf(int c, char_u *s)
{
char_u temp[MB_MAXBYTES + 1];
const int len = utf_char2bytes(c, temp);
for (int i = 0; i < len; ++i) {
c = temp[i];
// Need to escape K_SPECIAL and CSI like in the typeahead buffer.
if (c == K_SPECIAL) {
*s++ = K_SPECIAL;
*s++ = KS_SPECIAL;
*s++ = KE_FILLER;
} else {
*s++ = c;
}
}
return s;
}
unsigned int trans_special(const char_u **srcp, const size_t src_len,
char_u *const dst, const bool keycode,
const bool in_string)
{
int modifiers = 0;
int key;
unsigned int dlen = 0;
key = find_special_key(srcp, src_len, &modifiers, keycode, false, in_string);
if (key == 0) {
return 0;
}
// Put the appropriate modifier in a string.
if (modifiers != 0) {
dst[dlen++] = K_SPECIAL;
dst[dlen++] = KS_MODIFIER;
dst[dlen++] = (char_u)modifiers;
}
if (IS_SPECIAL(key)) {
dst[dlen++] = K_SPECIAL;
dst[dlen++] = (char_u)KEY2TERMCAP0(key);
dst[dlen++] = KEY2TERMCAP1(key);
} else if (has_mbyte && !keycode) {
dlen += (unsigned int)(*mb_char2bytes)(key, dst + dlen);
} else if (keycode) {
char_u *after = add_char2buf(key, dst + dlen);
assert(after >= dst && (uintmax_t)(after - dst) <= UINT_MAX);
dlen = (unsigned int)(after - dst);
} else {
dst[dlen++] = (char_u)key;
}
return dlen;
}

View File

@ -1,9 +1,89 @@
#include <stddef.h>
#include <inttypes.h>
#include <assert.h>
#include <stdbool.h>
#include "nvim/types.h"
#include "nvim/mbyte.h"
#include "nvim/ascii.h"
const uint8_t utf8len_tab_zero[] = {
//1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 2
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 4
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 6
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 8
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // A
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // C
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,0,0, // E
};
const uint8_t utf8len_tab[] = {
// ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?A ?B ?C ?D ?E ?F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A?
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B?
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C?
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // D?
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // E?
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1, // F?
};
int utf_ptr2char(const char_u *const p)
{
if (p[0] < 0x80) { // Be quick for ASCII.
return p[0];
}
const uint8_t len = utf8len_tab_zero[p[0]];
if (len > 1 && (p[1] & 0xc0) == 0x80) {
if (len == 2) {
return ((p[0] & 0x1f) << 6) + (p[1] & 0x3f);
}
if ((p[2] & 0xc0) == 0x80) {
if (len == 3) {
return (((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6)
+ (p[2] & 0x3f));
}
if ((p[3] & 0xc0) == 0x80) {
if (len == 4) {
return (((p[0] & 0x07) << 18) + ((p[1] & 0x3f) << 12)
+ ((p[2] & 0x3f) << 6) + (p[3] & 0x3f));
}
if ((p[4] & 0xc0) == 0x80) {
if (len == 5) {
return (((p[0] & 0x03) << 24) + ((p[1] & 0x3f) << 18)
+ ((p[2] & 0x3f) << 12) + ((p[3] & 0x3f) << 6)
+ (p[4] & 0x3f));
}
if ((p[5] & 0xc0) == 0x80 && len == 6) {
return (((p[0] & 0x01) << 30) + ((p[1] & 0x3f) << 24)
+ ((p[2] & 0x3f) << 18) + ((p[3] & 0x3f) << 12)
+ ((p[4] & 0x3f) << 6) + (p[5] & 0x3f));
}
}
}
}
}
// Illegal value: just return the first byte.
return p[0];
}
bool utf_composinglike(const char_u *p1, const char_u *p2)
{
return false;
}
char_u *string_convert(const vimconv_T *conv, char_u *data, size_t *size)
{
return NULL;
@ -11,8 +91,117 @@ char_u *string_convert(const vimconv_T *conv, char_u *data, size_t *size)
int utfc_ptr2len_len(const char_u *p, int size)
{
if (size < 1 || *p == NUL) {
assert(false);
return 0;
}
int utf_char2len(const int c)
{
if (c < 0x80) {
return 1;
} else if (c < 0x800) {
return 2;
} else if (c < 0x10000) {
return 3;
} else if (c < 0x200000) {
return 4;
} else if (c < 0x4000000) {
return 5;
} else {
return 6;
}
}
int utf_char2bytes(const int c, char_u *const buf)
{
if (c < 0x80) { // 7 bits
buf[0] = c;
return 1;
} else if (c < 0x800) { // 11 bits
buf[0] = 0xc0 + ((unsigned)c >> 6);
buf[1] = 0x80 + (c & 0x3f);
return 2;
} else if (c < 0x10000) { // 16 bits
buf[0] = 0xe0 + ((unsigned)c >> 12);
buf[1] = 0x80 + (((unsigned)c >> 6) & 0x3f);
buf[2] = 0x80 + (c & 0x3f);
return 3;
} else if (c < 0x200000) { // 21 bits
buf[0] = 0xf0 + ((unsigned)c >> 18);
buf[1] = 0x80 + (((unsigned)c >> 12) & 0x3f);
buf[2] = 0x80 + (((unsigned)c >> 6) & 0x3f);
buf[3] = 0x80 + (c & 0x3f);
return 4;
} else if (c < 0x4000000) { // 26 bits
buf[0] = 0xf8 + ((unsigned)c >> 24);
buf[1] = 0x80 + (((unsigned)c >> 18) & 0x3f);
buf[2] = 0x80 + (((unsigned)c >> 12) & 0x3f);
buf[3] = 0x80 + (((unsigned)c >> 6) & 0x3f);
buf[4] = 0x80 + (c & 0x3f);
return 5;
} else { // 31 bits
buf[0] = 0xfc + ((unsigned)c >> 30);
buf[1] = 0x80 + (((unsigned)c >> 24) & 0x3f);
buf[2] = 0x80 + (((unsigned)c >> 18) & 0x3f);
buf[3] = 0x80 + (((unsigned)c >> 12) & 0x3f);
buf[4] = 0x80 + (((unsigned)c >> 6) & 0x3f);
buf[5] = 0x80 + (c & 0x3f);
return 6;
}
}
int utf_ptr2len(const char_u *const p)
{
if (*p == NUL) {
return 0;
}
return 1;
const int len = utf8len_tab[*p];
for (int i = 1; i < len; i++) {
if ((p[i] & 0xc0) != 0x80) {
return 1;
}
}
return len;
}
int utfc_ptr2len(const char_u *const p)
{
uint8_t b0 = (uint8_t)(*p);
if (b0 == NUL) {
return 0;
}
if (b0 < 0x80 && p[1] < 0x80) { // be quick for ASCII
return 1;
}
// Skip over first UTF-8 char, stopping at a NUL byte.
int len = utf_ptr2len(p);
// Check for illegal byte.
if (len == 1 && b0 >= 0x80) {
return 1;
}
// Check for composing characters. We can handle only the first six, but
// skip all of them (otherwise the cursor would get stuck).
int prevlen = 0;
for (;;) {
if (p[len] < 0x80 || !UTF_COMPOSINGLIKE(p + prevlen, p + len)) {
return len;
}
// Skip over composing char.
prevlen = len;
len += utf_ptr2len(p + len);
}
}
void mb_copy_char(const char_u **fp, char_u **tp)
{
const size_t l = utfc_ptr2len(*fp);
memmove(*tp, *fp, (size_t)l);
*tp += l;
*fp += l;
}

View File

@ -18,6 +18,7 @@
#include "nvim/garray.c"
#include "nvim/gettext.c"
#include "nvim/viml/parser/expressions.c"
#include "nvim/keymap.c"
#define INPUT_SIZE 50

View File

@ -142,6 +142,13 @@ local function eastnode2lua(pstate, eastnode, checked_nodes)
typ = typ .. ('(val=%u)'):format(tonumber(eastnode.data.num.value))
elseif typ == 'Float' then
typ = typ .. ('(val=%e)'):format(tonumber(eastnode.data.flt.value))
elseif typ == 'SingleQuotedString' or typ == 'DoubleQuotedString' then
if eastnode.data.str.value == nil then
typ = typ .. '(val=NULL)'
else
local s = ffi.string(eastnode.data.str.value, eastnode.data.str.size)
typ = format_string('%s(val=%q)', typ, s)
end
end
ret_str = typ .. ':' .. ret_str
local can_simplify = true
@ -4549,4 +4556,822 @@ describe('Expressions parser', function()
hl('InvalidSubscript', ']'),
})
end)
itp('works with strings', function()
check_parsing('\'abc\'', 0, {
-- 01234
ast = {
'SingleQuotedString(val="abc"):0:0:\'abc\'',
},
}, {
hl('SingleQuotedString', '\''),
hl('SingleQuotedBody', 'abc'),
hl('SingleQuotedString', '\''),
})
check_parsing('"abc"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="abc"):0:0:"abc"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedBody', 'abc'),
hl('DoubleQuotedString', '"'),
})
check_parsing('\'\'', 0, {
-- 01
ast = {
'SingleQuotedString(val=NULL):0:0:\'\'',
},
}, {
hl('SingleQuotedString', '\''),
hl('SingleQuotedString', '\''),
})
check_parsing('""', 0, {
-- 01
ast = {
'DoubleQuotedString(val=NULL):0:0:""',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"', 0, {
-- 0
ast = {
'DoubleQuotedString(val=NULL):0:0:"',
},
err = {
arg = '"',
msg = 'E114: Missing double quote: %.*s',
},
}, {
hl('InvalidDoubleQuotedString', '"'),
})
check_parsing('\'', 0, {
-- 0
ast = {
'SingleQuotedString(val=NULL):0:0:\'',
},
err = {
arg = '\'',
msg = 'E115: Missing single quote: %.*s',
},
}, {
hl('InvalidSingleQuotedString', '\''),
})
check_parsing('"a', 0, {
-- 01
ast = {
'DoubleQuotedString(val="a"):0:0:"a',
},
err = {
arg = '"a',
msg = 'E114: Missing double quote: %.*s',
},
}, {
hl('InvalidDoubleQuotedString', '"'),
hl('InvalidDoubleQuotedBody', 'a'),
})
check_parsing('\'a', 0, {
-- 01
ast = {
'SingleQuotedString(val="a"):0:0:\'a',
},
err = {
arg = '\'a',
msg = 'E115: Missing single quote: %.*s',
},
}, {
hl('InvalidSingleQuotedString', '\''),
hl('InvalidSingleQuotedBody', 'a'),
})
check_parsing('\'abc\'\'def\'', 0, {
-- 0123456789
ast = {
'SingleQuotedString(val="abc\'def"):0:0:\'abc\'\'def\'',
},
}, {
hl('SingleQuotedString', '\''),
hl('SingleQuotedBody', 'abc'),
hl('SingleQuotedQuote', '\'\''),
hl('SingleQuotedBody', 'def'),
hl('SingleQuotedString', '\''),
})
check_parsing('\'abc\'\'', 0, {
-- 012345
ast = {
'SingleQuotedString(val="abc\'"):0:0:\'abc\'\'',
},
err = {
arg = '\'abc\'\'',
msg = 'E115: Missing single quote: %.*s',
},
}, {
hl('InvalidSingleQuotedString', '\''),
hl('InvalidSingleQuotedBody', 'abc'),
hl('InvalidSingleQuotedQuote', '\'\''),
})
check_parsing('\'\'\'\'\'\'\'\'', 0, {
-- 01234567
ast = {
'SingleQuotedString(val="\'\'\'"):0:0:\'\'\'\'\'\'\'\'',
},
}, {
hl('SingleQuotedString', '\''),
hl('SingleQuotedQuote', '\'\''),
hl('SingleQuotedQuote', '\'\''),
hl('SingleQuotedQuote', '\'\''),
hl('SingleQuotedString', '\''),
})
check_parsing('\'\'\'a\'\'\'\'bc\'', 0, {
-- 01234567890
-- 0 1
ast = {
'SingleQuotedString(val="\'a\'\'bc"):0:0:\'\'\'a\'\'\'\'bc\'',
},
}, {
hl('SingleQuotedString', '\''),
hl('SingleQuotedQuote', '\'\''),
hl('SingleQuotedBody', 'a'),
hl('SingleQuotedQuote', '\'\''),
hl('SingleQuotedQuote', '\'\''),
hl('SingleQuotedBody', 'bc'),
hl('SingleQuotedString', '\''),
})
check_parsing('"\\"\\"\\"\\""', 0, {
-- 0123456789
ast = {
'DoubleQuotedString(val="\\"\\"\\"\\""):0:0:"\\"\\"\\"\\""',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"abc\\"def\\"ghi\\"jkl\\"mno"', 0, {
-- 0123456789012345678901234
-- 0 1 2
ast = {
'DoubleQuotedString(val="abc\\"def\\"ghi\\"jkl\\"mno"):0:0:"abc\\"def\\"ghi\\"jkl\\"mno"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedBody', 'abc'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedBody', 'def'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedBody', 'ghi'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedBody', 'jkl'),
hl('DoubleQuotedEscape', '\\"'),
hl('DoubleQuotedBody', 'mno'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\b\\e\\f\\r\\t\\\\"', 0, {
-- 0123456789012345
-- 0 1
ast = {
[[DoubleQuotedString(val="\8\27\12\13\9\\"):0:0:"\b\e\f\r\t\\"]],
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\b'),
hl('DoubleQuotedEscape', '\\e'),
hl('DoubleQuotedEscape', '\\f'),
hl('DoubleQuotedEscape', '\\r'),
hl('DoubleQuotedEscape', '\\t'),
hl('DoubleQuotedEscape', '\\\\'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\n\n"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="\\\n\\\n"):0:0:"\\n\n"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\n'),
hl('DoubleQuotedBody', '\n'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\x00"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\0"):0:0:"\\x00"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\x00'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\xFF"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\255"):0:0:"\\xFF"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\xFF'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\xF"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\15"):0:0:"\\xF"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\xF'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\u00AB"', 0, {
-- 01234567
ast = {
'DoubleQuotedString(val="«"):0:0:"\\u00AB"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\u00AB'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U000000AB"', 0, {
-- 01234567
ast = {
'DoubleQuotedString(val="«"):0:0:"\\U000000AB"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U000000AB'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\x"', 0, {
-- 0123
ast = {
'DoubleQuotedString(val="x"):0:0:"\\x"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\x'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\x', 0, {
-- 012
ast = {
'DoubleQuotedString(val="x"):0:0:"\\x',
},
err = {
arg = '"\\x',
msg = 'E114: Missing double quote: %.*s',
},
}, {
hl('InvalidDoubleQuotedString', '"'),
hl('InvalidDoubleQuotedUnknownEscape', '\\x'),
})
check_parsing('"\\xF', 0, {
-- 0123
ast = {
'DoubleQuotedString(val="\\15"):0:0:"\\xF',
},
err = {
arg = '"\\xF',
msg = 'E114: Missing double quote: %.*s',
},
}, {
hl('InvalidDoubleQuotedString', '"'),
hl('InvalidDoubleQuotedEscape', '\\xF'),
})
check_parsing('"\\u"', 0, {
-- 0123
ast = {
'DoubleQuotedString(val="u"):0:0:"\\u"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\u'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\u', 0, {
-- 012
ast = {
'DoubleQuotedString(val="u"):0:0:"\\u',
},
err = {
arg = '"\\u',
msg = 'E114: Missing double quote: %.*s',
},
}, {
hl('InvalidDoubleQuotedString', '"'),
hl('InvalidDoubleQuotedUnknownEscape', '\\u'),
})
check_parsing('"\\U', 0, {
-- 012
ast = {
'DoubleQuotedString(val="U"):0:0:"\\U',
},
err = {
arg = '"\\U',
msg = 'E114: Missing double quote: %.*s',
},
}, {
hl('InvalidDoubleQuotedString', '"'),
hl('InvalidDoubleQuotedUnknownEscape', '\\U'),
})
check_parsing('"\\U"', 0, {
-- 0123
ast = {
'DoubleQuotedString(val="U"):0:0:"\\U"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\U'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\xFX"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\15X"):0:0:"\\xFX"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\xF'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\XFX"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\15X"):0:0:"\\XFX"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\XF'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\xX"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="xX"):0:0:"\\xX"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\x'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\XX"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="XX"):0:0:"\\XX"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\X'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\uX"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="uX"):0:0:"\\uX"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\u'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\UX"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="UX"):0:0:"\\UX"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\U'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\x0X"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\x0X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\x0'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\X0X"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\X0X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\X0'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\u0X"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\u0X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\u0'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U0X"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U0X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U0'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\x00X"', 0, {
-- 0123456
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\x00X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\x00'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\X00X"', 0, {
-- 0123456
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\X00X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\X00'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\u00X"', 0, {
-- 0123456
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\u00X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\u00'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U00X"', 0, {
-- 0123456
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U00X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U00'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\u000X"', 0, {
-- 01234567
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\u000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\u000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U000X"', 0, {
-- 01234567
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\u0000X"', 0, {
-- 012345678
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\u0000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\u0000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U0000X"', 0, {
-- 012345678
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U0000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U0000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U00000X"', 0, {
-- 0123456789
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U00000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U00000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U000000X"', 0, {
-- 01234567890
-- 0 1
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U000000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U000000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U0000000X"', 0, {
-- 012345678901
-- 0 1
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U0000000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U0000000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U00000000X"', 0, {
-- 0123456789012
-- 0 1
ast = {
'DoubleQuotedString(val="\\0X"):0:0:"\\U00000000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U00000000'),
hl('DoubleQuotedBody', 'X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\x000X"', 0, {
-- 01234567
ast = {
'DoubleQuotedString(val="\\0000X"):0:0:"\\x000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\x00'),
hl('DoubleQuotedBody', '0X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\X000X"', 0, {
-- 01234567
ast = {
'DoubleQuotedString(val="\\0000X"):0:0:"\\X000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\X00'),
hl('DoubleQuotedBody', '0X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\u00000X"', 0, {
-- 0123456789
ast = {
'DoubleQuotedString(val="\\0000X"):0:0:"\\u00000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\u0000'),
hl('DoubleQuotedBody', '0X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\U000000000X"', 0, {
-- 01234567890123
-- 0 1
ast = {
'DoubleQuotedString(val="\\0000X"):0:0:"\\U000000000X"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\U00000000'),
hl('DoubleQuotedBody', '0X'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\0"', 0, {
-- 0123
ast = {
'DoubleQuotedString(val="\\0"):0:0:"\\0"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\0'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\00"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="\\0"):0:0:"\\00"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\00'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\000"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\0"):0:0:"\\000"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\000'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\0000"', 0, {
-- 0123456
ast = {
'DoubleQuotedString(val="\\0000"):0:0:"\\0000"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\000'),
hl('DoubleQuotedBody', '0'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\8"', 0, {
-- 0123
ast = {
'DoubleQuotedString(val="8"):0:0:"\\8"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\8'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\08"', 0, {
-- 01234
ast = {
'DoubleQuotedString(val="\\0008"):0:0:"\\08"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\0'),
hl('DoubleQuotedBody', '8'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\008"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\0008"):0:0:"\\008"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\00'),
hl('DoubleQuotedBody', '8'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\0008"', 0, {
-- 0123456
ast = {
'DoubleQuotedString(val="\\0008"):0:0:"\\0008"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\000'),
hl('DoubleQuotedBody', '8'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\777"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\255"):0:0:"\\777"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\777'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\050"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\40"):0:0:"\\050"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\050'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\<C-u>"', 0, {
-- 012345
ast = {
'DoubleQuotedString(val="\\21"):0:0:"\\<C-u>"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedEscape', '\\<C-u>'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\<', 0, {
-- 012
ast = {
'DoubleQuotedString(val="<"):0:0:"\\<',
},
err = {
arg = '"\\<',
msg = 'E114: Missing double quote: %.*s',
},
}, {
hl('InvalidDoubleQuotedString', '"'),
hl('InvalidDoubleQuotedUnknownEscape', '\\<'),
})
check_parsing('"\\<"', 0, {
-- 0123
ast = {
'DoubleQuotedString(val="<"):0:0:"\\<"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\<'),
hl('DoubleQuotedString', '"'),
})
check_parsing('"\\<C-u"', 0, {
-- 0123456
ast = {
'DoubleQuotedString(val="<C-u"):0:0:"\\<C-u"',
},
}, {
hl('DoubleQuotedString', '"'),
hl('DoubleQuotedUnknownEscape', '\\<'),
hl('DoubleQuotedBody', 'C-u'),
hl('DoubleQuotedString', '"'),
})
end)
end)