From 980ac21791b6760ed69ed83cb05a7c66b1fbad67 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Mon, 11 Nov 2019 20:50:12 -0500 Subject: [PATCH] (objectivec) Sync preprocessor highlighting with cpp (#2265) The objectivec highlighting had a few bugs and was missing a few features: - Expressions with < were misidentified as a string like (fixes #1964). - Keywords like if, else, endif, etc. were not highlighted. - The escape sequences in string literals were not parsed. - Line continuations were not parsed. - Comments were not parsed. Fix all these by adapting cpp's preprocessor definition. Pull in the preprocessor test from cpp. (This is actually objectivec's first markup test. It probably could do with tests beyond just this feature.) --- CHANGES.md | 1 + src/languages/cpp.js | 2 +- src/languages/objectivec.js | 22 +++++++++++++------ test/markup/cpp/preprocessor.expect.txt | 6 +++++ test/markup/cpp/preprocessor.txt | 6 +++++ .../markup/objectivec/preprocessor.expect.txt | 19 ++++++++++++++++ test/markup/objectivec/preprocessor.txt | 19 ++++++++++++++++ 7 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 test/markup/objectivec/preprocessor.expect.txt create mode 100644 test/markup/objectivec/preprocessor.txt diff --git a/CHANGES.md b/CHANGES.md index dac7a056dc..0ddce501b4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,7 @@ Improvements: - fix(parser): Look-ahead regex now work for end matches also (#2237) [Josh Goebel][] - fix(parser): Better errors when a language is missing (#2236) [Josh Goebel][] - fix(go): Fix escaped character literals (#2266) [David Benjamin][] +- fix(objectivec): Fix various preprocessor highlighting issues (#2265) [David Benjamin][] - fix(objectivec): Handle multibyte character literals (#2268) [David Benjamin][] [Josh Goebel]: https://github.com/yyyc514 diff --git a/src/languages/cpp.js b/src/languages/cpp.js index 29c33263b6..bec975080f 100644 --- a/src/languages/cpp.js +++ b/src/languages/cpp.js @@ -56,7 +56,7 @@ function(hljs) { hljs.inherit(STRINGS, {className: 'meta-string'}), { className: 'meta-string', - begin: /<[^\n>]*>/, end: /$/, + begin: /<.*?>/, end: /$/, illegal: '\\n', }, hljs.C_LINE_COMMENT_MODE, diff --git a/src/languages/objectivec.js b/src/languages/objectivec.js index c52c4ad659..1a4a9fe1cc 100644 --- a/src/languages/objectivec.js +++ b/src/languages/objectivec.js @@ -66,16 +66,24 @@ function(hljs) { }, { className: 'meta', - begin: '#', - end: '$', + begin: /#\s*[a-z]+\b/, end: /$/, + keywords: { + 'meta-keyword': + 'if else elif endif define undef warning error line ' + + 'pragma ifdef ifndef include' + }, contains: [ + { + begin: /\\\n/, relevance: 0 + }, + hljs.inherit(hljs.QUOTE_STRING_MODE, {className: 'meta-string'}), { className: 'meta-string', - variants: [ - { begin: '\"', end: '\"' }, - { begin: '<', end: '>' } - ] - } + begin: /<.*?>/, end: /$/, + illegal: '\\n', + }, + hljs.C_LINE_COMMENT_MODE, + hljs.C_BLOCK_COMMENT_MODE ] }, { diff --git a/test/markup/cpp/preprocessor.expect.txt b/test/markup/cpp/preprocessor.expect.txt index 62fff51bcc..2a7548ebc8 100644 --- a/test/markup/cpp/preprocessor.expect.txt +++ b/test/markup/cpp/preprocessor.expect.txt @@ -11,3 +11,9 @@ #define x(v) ((v)) # define x(v) ((v)) # define x(v) ((v)) + +#if MACRO_WITH_STRING_ARG("hello \"world\"") +#elif MULTI_LINE /* comment */ < \ + EXPRESSION +int bar; +#endif // comment diff --git a/test/markup/cpp/preprocessor.txt b/test/markup/cpp/preprocessor.txt index b1ad6a67ff..01372cbfcd 100644 --- a/test/markup/cpp/preprocessor.txt +++ b/test/markup/cpp/preprocessor.txt @@ -11,3 +11,9 @@ int foo(void) #define x(v) ((v)) # define x(v) ((v)) # define x(v) ((v)) + +#if MACRO_WITH_STRING_ARG("hello \"world\"") +#elif MULTI_LINE /* comment */ < \ + EXPRESSION +int bar; +#endif // comment diff --git a/test/markup/objectivec/preprocessor.expect.txt b/test/markup/objectivec/preprocessor.expect.txt new file mode 100644 index 0000000000..9002811ef6 --- /dev/null +++ b/test/markup/objectivec/preprocessor.expect.txt @@ -0,0 +1,19 @@ +#include <iostream> +#define foo 1<<16 + +#ifdef DEBUG +TYPE1 foo(void) +#else +int foo(void) +#endif +{ } + +#define x(v) ((v)) +# define x(v) ((v)) +# define x(v) ((v)) + +#if MACRO_WITH_STRING_ARG("hello \"world\"") +#elif MULTI_LINE /* comment */ < \ + EXPRESSION +int bar; +#endif // comment diff --git a/test/markup/objectivec/preprocessor.txt b/test/markup/objectivec/preprocessor.txt new file mode 100644 index 0000000000..01372cbfcd --- /dev/null +++ b/test/markup/objectivec/preprocessor.txt @@ -0,0 +1,19 @@ +#include +#define foo 1<<16 + +#ifdef DEBUG +TYPE1 foo(void) +#else +int foo(void) +#endif +{ } + +#define x(v) ((v)) +# define x(v) ((v)) +# define x(v) ((v)) + +#if MACRO_WITH_STRING_ARG("hello \"world\"") +#elif MULTI_LINE /* comment */ < \ + EXPRESSION +int bar; +#endif // comment