From 2d5eaed745892a5e035caf7ccc02d27044f344fb Mon Sep 17 00:00:00 2001 From: Jak Wings Date: Fri, 8 Apr 2016 18:20:21 +0800 Subject: [PATCH] fix handling of line continuation in keywords This behavior is more consistent with line continuation in strings other than keywords. Fixes #2897 --- src/parse_tree.cpp | 11 ++++++++--- tests/line-continuation.err | 0 tests/line-continuation.in | 25 +++++++++++++++++++++++++ tests/line-continuation.out | 5 +++++ tests/line-continuation.status | 1 + 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/line-continuation.err create mode 100644 tests/line-continuation.in create mode 100644 tests/line-continuation.out create mode 100644 tests/line-continuation.status diff --git a/src/parse_tree.cpp b/src/parse_tree.cpp index 3d2f7b8c7..cdaec9ef0 100644 --- a/src/parse_tree.cpp +++ b/src/parse_tree.cpp @@ -1254,6 +1254,12 @@ static parse_keyword_t keyword_with_name(const wchar_t *name) return result; } +static bool is_keyword_char(wchar_t c) +{ + return (c >= L'a' && c <= L'z') || (c >= L'A' && c <= L'Z') || (c >= L'0' && c <= L'9') + || c == L'\'' || c == L'"' || c == L'\\' || c == '\n'; +} + /* Given a token, returns the keyword it matches, or parse_keyword_none. */ static parse_keyword_t keyword_for_token(token_type tok, const wcstring &token) { @@ -1267,17 +1273,16 @@ static parse_keyword_t keyword_for_token(token_type tok, const wcstring &token) parse_keyword_t result = parse_keyword_none; bool needs_expand = false, all_chars_valid = true; const wchar_t *tok_txt = token.c_str(); - const wchar_t *chars_allowed_in_keywords = L"abcdefghijklmnopqrstuvwxyz'\""; for (size_t i=0; tok_txt[i] != L'\0'; i++) { wchar_t c = tok_txt[i]; - if (! wcschr(chars_allowed_in_keywords, c)) + if (! is_keyword_char(c)) { all_chars_valid = false; break; } // If we encounter a quote, we need expansion - needs_expand = needs_expand || c == L'"' || c == L'\''; + needs_expand = needs_expand || c == L'"' || c == L'\'' || c == L'\\'; } if (all_chars_valid) diff --git a/tests/line-continuation.err b/tests/line-continuation.err new file mode 100644 index 000000000..e69de29bb diff --git a/tests/line-continuation.in b/tests/line-continuation.in new file mode 100644 index 000000000..a1ac92932 --- /dev/null +++ b/tests/line-continuation.in @@ -0,0 +1,25 @@ +ech\ +o echo + +buil\ +tin echo builtin echo + +true; an\ +d echo true + +\i\ +\U00000066\ + true + echo if true +\ +\145n\ +d\ +; + +'if'\ + true + echo if true +"\ +en\ +d\ +"; diff --git a/tests/line-continuation.out b/tests/line-continuation.out new file mode 100644 index 000000000..053209a56 --- /dev/null +++ b/tests/line-continuation.out @@ -0,0 +1,5 @@ +echo +builtin echo +true +if true +if true diff --git a/tests/line-continuation.status b/tests/line-continuation.status new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/tests/line-continuation.status @@ -0,0 +1 @@ +0